\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Dinesh1102} \pdfinfo{ /Title (qiskit-programming-qml.pdf) /Creator (Cheatography) /Author (Dinesh1102) /Subject (Qiskit Programming - QML Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Qiskit Programming - QML Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Dinesh1102} via \textcolor{DarkBackground}{\uline{cheatography.com/172447/cs/36251/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Dinesh1102 \\ \uline{cheatography.com/dinesh1102} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 24th December, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Import Libraries}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{from qiskit import QuantumRegister , ClassicalRegister, QuantumCircuit , Aer , execute \newline from qiskit.visualization import visualize\_transition \newline import numpy as np \newline import math as m} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Quantum Circuits}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\#Define Simulator \newline S\_simulator = Aer.backends(name='statevector\_simulator'){[}0{]} \newline M\_simulator = Aer.backends(name='qasm\_simulator'){[}0{]} \newline \newline \#Define Register (1 qubit) \newline qreg\_q = QuantumRegister(1,'q') \newline creg\_c = ClassicalRegister(1,'c') \newline \newline \#Define quantum circuit \newline qc = QuantumCircuit(qreg\_q,creg\_c) \newline \newline \#Add Quantum gates to Circuit \newline qc.h(qreg\_q{[}0{]}) \newline \# Add Measure \newline qc.measure(qreg\_q,creg\_c) \newline \newline \#RUn circuit on Simulator \newline job = execute(qc,M\_simulator) \newline result = job.result() \newline result.get\_counts(qc)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Quantum Gates}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Names Example Notes \newline I, Identity qc.id(0) or qc.i(o) Applies I gate to qubit 0. \newline H, Hadamard qc.h(0) Applies H gate to qubit 0. \newline X qc.x(0) Applies X gate to qubit 0. \newline Y qc.y({[}0,1,2{]}) Applies Y gates to qubits 0, 1, and 2. \newline Z qc.z(0) Applies Z gate to qubit 0. Equivalent to P gate with π phase rotation. \newline P, Phase qc.p(math.pi/2,0) Applies P gate with π/2 phase rotation to qubit 0. \newline S qc.s(0) Applies S gate to qubit 0. Equivalent to P gate with π/2 phase rotation. \newline S\textdagger{} qc.sdg(0) Applies S\textdagger{} gate to qubit 0. Equivalent to P gate with 3π/2 phase rotation. \newline SX qc.sx(0) Applies SX (square root of X) gate to qubit 0. Equivalent to RX gate with π/2 rotation. \newline T qc.t(0) Applies T gate to qubit 0. Equivalent to P gate with π/4 phase rotation. \newline T\textdagger{} qc.tdg(0) Applies T\textdagger{} gate to qubit 0. Equivalent to P gate with 7π/4 phase rotation. \newline RX qc.rx(math.pi/4,0) Applies RX gate with π/4 rotation to qubit 0. \newline RY qc.ry(math.pi/8,0) Applies RY gate with π/8 rotation to qubit 0. \newline RZ qc.rz(math.pi/2,0) Applies RZ gate with π/2 rotation to qubit 0. \newline U qc.u(math.pi/2,0,math.pi,5) Applies rotation with 3 Euler angles to qubit 5.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Quantum Teleportation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{from qiskit import {\emph{ \newline from qiskit.tools.jupyter import }} \newline from qiskit.visualization import * \newline import matplotlib.pyplot as plotter \newline import numpy as np \newline from IPython.display import display, Math, Latex \newline \%matplotlib inline \newline \newline sim = \seqsplit{Aer.get\_backend('aer\_simulator')} \newline \newline qr = QuantumRegister(3) \newline crz = ClassicalRegister(1) \newline crx = ClassicalRegister(2) \# we will need seperates registers for using 'c\_if' later. \newline qc = QuantumCircuit(qr,crz,crx) \newline \newline qc.x(0) \newline qc.h(0) \# 'psi' can't be unknown to us as we are creating it here. Let us take '-' state as our 'psi'. This is done by operating X and H gate on the q0 i.e., H.X|0\textgreater{} \newline \# We will verify later if the '-' is been teleported. \newline qc.draw('mpl') \newline \newline \newline qc.h(1) \newline qc.cx(1,2) \# creating a bell state \newline qc.barrier() \# Use barrier to separate steps, everything till this barrier is just intialisation. \newline qc.draw('mpl') \newline \newline qc.cx(0,1) \# '0' and '1' are with Alice and '2' is with Bob. \newline \# psi\_1 prepared. \newline qc.barrier() \# Use barrier to separate steps \newline qc.draw('mpl') \newline \newline qc.h(0) \newline \# psi\_2 prepared. \newline qc.barrier() \newline qc.draw('mpl') \newline \newline qc.measure(0,0) \newline qc.measure(1,1) \newline qc.barrier() \newline qc.draw('mpl') \newline \newline qc.x(2).c\_if(crx,1) \# 'c\_if' compares a classical register with a value (either 0 or 1) and performs the \newline qc.z(2).c\_if(crz,1) \# operation if they are equal. \newline qc.draw('mpl') \newline \newline qc.h(2) \newline qc.measure(2,crx{[}1{]}) \newline qc.draw('mpl') \newline \newline qobj = assemble(qc) \newline result = sim.run(qobj).result() \newline counts = result.get\_counts() \newline plot\_histogram(counts)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{QKD with BB84 protocol}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{from qiskit import {\emph{ \newline from qiskit.compiler import transpile, assemble \newline from qiskit.tools.jupyter import }} \newline from qiskit.visualization import {\emph{ \newline import matplotlib.pyplot as plotter \newline import numpy as np \newline from IPython.display import display, Math, Latex \newline import math as m \newline \newline \%matplotlib inline \newline \newline from qiskit import }} \newline from qiskit.visualization import plot\_histogram \newline \%config \seqsplit{InlineBackend.figure\_format} = 'svg' \newline \newline qc\_ab = QuantumCircuit(6,6) \#Create a quantum circuit with 6 qubits and 6 classical bits \newline \newline \newline \#\#ENCODE BIT STRING \newline \#The random bit sequence Alice needs to encode is: 100100, so the first and fourth qubits are flipped from |0\textgreater{} -\textgreater{} |1\textgreater{} \newline qc\_ab.x(0) \#The first qubit is indexed at 0, following Python being zero-indexed. From now on it'll be referred to as qubit 0 and so on. \newline qc\_ab.x(3) \newline qc\_ab.barrier() \newline \newline \newline \#\#ALICE CHOOSES \newline \#Alice randomly chooses to apply an X or an H. \newline \#Note that since the state is already either a |0\textgreater{} or |1\textgreater{}, a Z essentially leaves the qubit state unchanged. But let's write it anyway, shall we? \newline \newline qc\_ab.h(0) \# or qc.z(0) \# switch these based on your own choice \newline qc\_ab.z(1) \# or qc.h(1) \newline qc\_ab.z(2) \# or qc.h(2) \newline qc\_ab.h(3) \# or qc.z(3) \newline qc\_ab.z(4) \# or qc.h(4) \newline qc\_ab.h(5) \# or qc.z(5) \newline qc\_ab.barrier() \newline \newline \newline \#\#BOB CHOOSES \newline \#Alice sends the qubit sequence to Bob, and Bob randomly chooses measurements \newline qc\_ab.h(0) \# or qc.z(0) \# switch these based on your own choice \newline qc\_ab.z(1) \# or qc.h(1) \newline qc\_ab.h(2) \# or qc.z(2) \newline qc\_ab.h(3) \# or qc.z(3) \newline qc\_ab.z(4) \# or qc.h(4) \newline qc\_ab.z(5) \# or qc.h(5) \newline qc\_ab.barrier() \newline \newline \newline \#\#PUBLICIZE CHOICES \newline \#Alice and Bob publicize their choices and only retain those for which their choices match. In this case: qubits 0,1,3,4. \newline \#Note: technically Bob performs the measurement BEFORE publicizing, but we're combining the two here since no one is actually communicating. \newline \newline qc\_ab.measure(0,0) \newline qc\_ab.measure(1,1) \newline qc\_ab.measure(3,3) \newline qc\_ab.measure(4,4) \newline \newline \newline \#qc\_ab.measure(2,2) \#come back to uncomment these to see what happens to the results after you've run this once \newline \#qc\_ab.measure(5,5) \newline \newline \newline qc\_ab.draw(output='mpl') \#let's see what this circuit looks like! \newline \newline \#\#EXECUTE \newline result = execute(qc\_ab, \seqsplit{Aer.get\_backend('qasm\_simulator')).result().get\_counts()} \#We're only making use of the simulator. Refer to {[}2{]} to see how you can run this on a real quantum computer. \newline plot\_histogram(result) \newline \newline \#Same situation but now with an eavesdropper (Eve) \newline \newline qc\_aeb = QuantumCircuit(6,6) \#Create a quantum circuit with 6 qubits and 6 classical bits \newline \newline \#\#ENCODE BIT STRING \newline qc\_aeb.x(0) \newline qc\_aeb.x(3) \newline qc\_aeb.barrier() \newline \newline \#\#ALICE CHOOSES \newline qc\_aeb.h(0) \newline qc\_aeb.z(1) \newline qc\_aeb.z(2) \newline qc\_aeb.h(3) \newline qc\_aeb.z(4) \newline qc\_aeb.h(5) \newline qc\_aeb.barrier() \newline \newline \#\#EVE CHOOSES \newline qc\_aeb.h(0) \#play around with these to see how many states with non-zero probabilities show up at the end for a fixed set of Alice's and Bob's choices \newline qc\_aeb.z(1) \newline qc\_aeb.h(2) \newline qc\_aeb.h(3) \newline qc\_aeb.z(4) \newline qc\_aeb.z(5) \newline qc\_aeb.barrier() \newline \newline \#\#BOB CHOOSES \newline qc\_aeb.h(0) \newline qc\_aeb.z(1) \newline qc\_aeb.h(2) \newline qc\_aeb.h(3) \newline qc\_aeb.z(4) \newline qc\_aeb.z(5) \newline qc\_aeb.barrier() \newline \newline \newline \#\#PUBLICIZE CHOICES \newline \newline qc\_aeb.measure(0,0) \newline qc\_aeb.measure(1,1) \newline qc\_aeb.measure(3,3) \newline qc\_aeb.measure(4,4) \newline \newline \#qc\_aeb.measure(2,2) \#come back to uncomment these to see what happens to the results after you've run this once \newline \#qc\_aeb.measure(5,5) \newline \newline qc\_aeb.draw(output='mpl') \#let's see what this circuit looks like! \newline \newline \#\#EXECUTE \newline result = execute(qc\_aeb, \seqsplit{Aer.get\_backend('qasm\_simulator')).result().get\_counts()} \newline plot\_histogram(result)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}