In this section we will go into the different operations that are available in Qiskit Terra. These are:
We will also show you how to use the three different simulators:
In [1]:
# Useful additional packages
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from math import pi
In [2]:
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import available_backends, execute, register, get_backend
from qiskit.tools.visualization import circuit_drawer
from qiskit.tools.qi.qi import state_fidelity
from qiskit import Aer
backend = Aer.get_backend('unitary_simulator')
A single qubit quantum state can be written as
$$|\psi\rangle = \alpha|0\rangle + \beta |1\rangle$$where $\alpha$ and $\beta$ are complex numbers. In a measurement the probability of the bit being in $|0\rangle$ is $|\alpha|^2$ and $|1\rangle$ is $|\beta|^2$. As a vector this is
$$ |\psi\rangle = \begin{pmatrix} \alpha \\ \beta \end{pmatrix}. $$Note due to conservation probability $|\alpha|^2+ |\beta|^2 = 1$ and since global phase is undetectable $|\psi\rangle := e^{i\delta} |\psi\rangle$ we only requires two real numbers to describe a single qubit quantum state.
A convenient representation is
$$|\psi\rangle = \cos(\theta/2)|0\rangle + \sin(\theta/2)e^{i\phi}|1\rangle$$where $0\leq \phi < 2\pi$, and $0\leq \theta \leq \pi$. From this it is clear that there is a one-to-one correspondence between qubit states ($\mathbb{C}^2$) and the points on the surface of a unit sphere ($\mathbb{R}^3$). This is called the Bloch sphere representation of a qubit state.
Quantum gates/operations are usually represented as matrices. A gate which acts on a qubit is represented by a $2\times 2$ unitary matrix $U$. The action of the quantum gate is found by multiplying the matrix representing the gate with the vector which represents the quantum state.
$$|\psi'\rangle = U|\psi\rangle$$A general unitary must be able to take the $|0\rangle$ to the above state. That is
$$ U = \begin{pmatrix} \cos(\theta/2) & a \\ e^{i\phi}\sin(\theta/2) & b \end{pmatrix} $$where $a$ and $b$ are complex numbers constrained such that $U^\dagger U = I$ for all $0\leq\theta\leq\pi$ and $0\leq \phi<2\pi$. This gives 3 constraints and as such $a\rightarrow -e^{i\lambda}\sin(\theta/2)$ and $b\rightarrow e^{i\lambda+i\phi}\cos(\theta/2)$ where $0\leq \lambda<2\pi$ giving
$$ U = \begin{pmatrix} \cos(\theta/2) & -e^{i\lambda}\sin(\theta/2) \\ e^{i\phi}\sin(\theta/2) & e^{i\lambda+i\phi}\cos(\theta/2) \end{pmatrix}. $$This is the most general form of a single qubit unitary.
In [3]:
q = QuantumRegister(1)
In [4]:
qc = QuantumCircuit(q)
qc.u3(pi/2,pi/2,pi/2,q)
circuit_drawer(qc)
Out[4]:
In [5]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[5]:
The $u2(\phi, \lambda) =u3(\pi/2, \phi, \lambda)$ has the matrix form
$$ u2(\phi, \lambda) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi + \lambda)} \end{pmatrix}. $$This is a useful gate as it allows us to create superpositions
In [6]:
qc = QuantumCircuit(q)
qc.u2(pi/2,pi/2,q)
circuit_drawer(qc)
Out[6]:
In [7]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[7]:
The $u1(\lambda)= u3(0, 0, \lambda)$ gate has the matrix form
$$ u1(\lambda) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i \lambda} \end{pmatrix}, $$which is a useful as it allows us to apply a quantum phase.
In [8]:
qc = QuantumCircuit(q)
qc.u1(pi/2,q)
circuit_drawer(qc)
Out[8]:
In [9]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[9]:
The $u0(\delta)= u3(0, 0, 0)$ gate is the identity matrix. It has the matrix form
$$ u0(\delta) = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}. $$The identity gate does nothing (but can add noise in the real device for a period of time equal to fractions of the single qubit gate time)
In [10]:
qc = QuantumCircuit(q)
qc.u0(pi/2,q)
circuit_drawer(qc)
Out[10]:
In [11]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[11]:
In [12]:
qc = QuantumCircuit(q)
qc.iden(q)
circuit_drawer(qc)
Out[12]:
In [13]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[13]:
In [14]:
qc = QuantumCircuit(q)
qc.x(q)
circuit_drawer(qc)
Out[14]:
In [15]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[15]:
In [16]:
qc = QuantumCircuit(q)
qc.y(q)
circuit_drawer(qc)
Out[16]:
In [17]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[17]:
In [18]:
qc = QuantumCircuit(q)
qc.z(q)
circuit_drawer(qc)
Out[18]:
In [19]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[19]:
In [20]:
qc = QuantumCircuit(q)
qc.h(q)
circuit_drawer(qc)
Out[20]:
In [21]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[21]:
In [22]:
qc = QuantumCircuit(q)
qc.s(q)
circuit_drawer(qc)
Out[22]:
In [23]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[23]:
In [24]:
qc = QuantumCircuit(q)
qc.sdg(q)
circuit_drawer(qc)
Out[24]:
In [25]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[25]:
In [26]:
qc = QuantumCircuit(q)
qc.t(q)
circuit_drawer(qc)
Out[26]:
In [27]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[27]:
In [28]:
qc = QuantumCircuit(q)
qc.tdg(q)
circuit_drawer(qc)
Out[28]:
In [29]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[29]:
The standard rotation gates are those that define rotations around the Paulis $P=\{X,Y,Z\}$. They are defined as
$$ R_P(\theta) = \exp(-i \theta P/2) = \cos(\theta/2)I -i \sin(\theta/2)P$$
In [30]:
qc = QuantumCircuit(q)
qc.rx(pi/2,q)
circuit_drawer(qc)
Out[30]:
In [31]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[31]:
In [32]:
qc = QuantumCircuit(q)
qc.ry(pi/2,q)
circuit_drawer(qc)
Out[32]:
In [33]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[33]:
In [34]:
qc = QuantumCircuit(q)
qc.rz(pi/2,q)
circuit_drawer(qc)
Out[34]:
In [35]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[35]:
Note this is different due only to a global phase
The space of quantum computer grows exponential with the number of qubits. For $n$ qubits the complex vector space has dimensions $d=2^n$. To describe states of a multi-qubit system, the tensor product is used to "glue together" operators and basis vectors.
Let's start by considering a 2-qubit system. Given two operators $A$ and $B$ that each act on one qubit, the joint operator $A \otimes B$ acting on two qubits is
$$\begin{equation} A\otimes B = \begin{pmatrix} A_{00} \begin{pmatrix} B_{00} & B_{01} \\ B_{10} & B_{11} \end{pmatrix} & A_{01} \begin{pmatrix} B_{00} & B_{01} \\ B_{10} & B_{11} \end{pmatrix} \\ A_{10} \begin{pmatrix} B_{00} & B_{01} \\ B_{10} & B_{11} \end{pmatrix} & A_{11} \begin{pmatrix} B_{00} & B_{01} \\ B_{10} & B_{11} \end{pmatrix} \end{pmatrix}, \end{equation}$$where $A_{jk}$ and $B_{lm}$ are the matrix elements of $A$ and $B$, respectively.
Analogously, the basis vectors for the 2-qubit system are formed using the tensor product of basis vectors for a single qubit: $$\begin{equation}\begin{split} |{00}\rangle &= \begin{pmatrix} 1 \begin{pmatrix} 1 \\ 0 \end{pmatrix} \\ 0 \begin{pmatrix} 1 \\ 0 \end{pmatrix} \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \\ 0 \\0 \end{pmatrix}~~~|{01}\rangle = \begin{pmatrix} 1 \begin{pmatrix} 0 \\ 1 \end{pmatrix} \\ 0 \begin{pmatrix} 0 \\ 1 \end{pmatrix} \end{pmatrix} = \begin{pmatrix}0 \\ 1 \\ 0 \\ 0 \end{pmatrix}\end{split} \end{equation}$$
$$\begin{equation}\begin{split}|{10}\rangle = \begin{pmatrix} 0\begin{pmatrix} 1 \\ 0 \end{pmatrix} \\ 1\begin{pmatrix} 1 \\ 0 \end{pmatrix} \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ 1 \\ 0 \end{pmatrix}~~~ |{11}\rangle = \begin{pmatrix} 0 \begin{pmatrix} 0 \\ 1 \end{pmatrix} \\ 1\begin{pmatrix} 0 \\ 1 \end{pmatrix} \end{pmatrix} = \begin{pmatrix} 0 \\ 0 \\ 0 \\1 \end{pmatrix}\end{split} \end{equation}.$$Note we've introduced a shorthand for the tensor product of basis vectors, wherein $|0\rangle \otimes |0\rangle$ is written as $|00\rangle$. The state of an $n$-qubit system can described using the $n$-fold tensor product of single-qubit basis vectors. Notice that the basis vectors for a 2-qubit system are 4-dimensional; in general, the basis vectors of an $n$-qubit sytsem are $2^{n}$-dimensional, as noted earlier.
Within the physics community, the qubits of a multi-qubit systems are typically ordered with the first qubit on the left-most side of the tensor product and the last qubit on the right-most side. For instance, if the first qubit is in state $|0\rangle$ and second is in state $|1\rangle$, their joint state would be $|01\rangle$. Qiskit uses a slightly different ordering of the qubits, in which the qubits are represented from the most significant bit (MSB) on the left to the least significant bit (LSB) on the right (big-endian). This is similar to bitstring representation on classical computers, and enables easy conversion from bitstrings to integers after measurements are performed. For the example just given, the joint state would be represented as $|10\rangle$. Importantly, this change in the representation of multi-qubit states affects the way multi-qubit gates are represented in Qiskit, as discussed below.
The representation used in Qiskit enumerates the basis vectors in increasing order of the integers they represent. For instance, the basis vectors for a 2-qubit system would be ordered as $|00\rangle$, $|01\rangle$, $|10\rangle$, and $|11\rangle$. Thinking of the basis vectors as bit strings, they encode the integers 0,1,2 and 3, respectively.
A common multi-qubit gate involves the application of a gate to one qubit, conditioned on the state of another qubit. For instance, we might want to flip the state of the second qubit when the first qubit is in $|0\rangle$. Such gates are known as controlled gates. The standard multi-qubit gates consist of two-qubit gates and three-qubit gates. The two-qubit gates are:
The three-qubit gates are:
Most of the two-gates are of the controlled type (the SWAP gate being the exception). In general, a controlled two-qubit gate $C_{U}$ acts to apply the single-qubit unitary $U$ to the second qubit when the state of the first qubit is in $|1\rangle$. Suppose $U$ has a matrix representation
$$U = \begin{pmatrix} u_{00} & u_{01} \\ u_{10} & u_{11}\end{pmatrix}.$$We can work out the action of $C_{U}$ as follows. Recall that the basis vectors for a two-qubit system are ordered as $|00\rangle, |01\rangle, |10\rangle, |11\rangle$. Suppose the control qubit is qubit 0 (which, according to Qiskit's convention, is one the right-hand side of the tensor product). If the control qubit is in $|1\rangle$, $U$ should be applied to the target (qubit 1, on the left-hand side of the tensor product). Therefore, under the action of $C_{U}$, the basis vectors are transformed according to
$$\begin{align*} C_{U}: \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle} &\rightarrow \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle} &\rightarrow \underset{\text{qubit}~1}{U|0\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle} &\rightarrow \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle} &\rightarrow \underset{\text{qubit}~1}{U|1\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle}\\ \end{align*}.$$In matrix form, the action of $C_{U}$ is
$$\begin{equation} C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & u_{00} & 0 & u_{01} \\ 0 & 0 & 1 & 0 \\ 0 & u_{10} &0 & u_{11} \end{pmatrix}. \end{equation}$$To work out these matrix elements, let
$$C_{(jk), (lm)} = \left(\underset{\text{qubit}~1}{\langle j |} \otimes \underset{\text{qubit}~0}{\langle k |}\right) C_{U} \left(\underset{\text{qubit}~1}{| l \rangle} \otimes \underset{\text{qubit}~0}{| k \rangle}\right),$$compute the action of $C_{U}$ (given above), and compute the inner products.
As shown in the examples below, this operation is implemented in Qiskit as cU(q[0],q[1])
.
If qubit 1 is the control and qubit 0 is the target, then the basis vectors are transformed according to $$\begin{align*} C_{U}: \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle} &\rightarrow \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle} &\rightarrow \underset{\text{qubit}~1}{|0\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{|0\rangle} &\rightarrow \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{U|0\rangle}\\ C_{U}: \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{|1\rangle} &\rightarrow \underset{\text{qubit}~1}{|1\rangle}\otimes \underset{\text{qubit}~0}{U|1\rangle}\\ \end{align*},$$
which implies the matrix form of $C_{U}$ is $$\begin{equation} C_U = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & u_{00} & u_{01} \\ 0 & 0 & u_{10} & u_{11} \end{pmatrix}. \end{equation}$$
In [73]:
q = QuantumRegister(2)
The controlled-not gate flips the target
qubit when the control qubit is in the state $|1\rangle$. If we take the MSB as the control qubit (e.g. cx(q[1],q[0])
), then the matrix would look like
However, when the LSB is the control qubit, (e.g. cx(q[0],q[1])
), this gate is equivalent to the following matrix:
In [37]:
qc = QuantumCircuit(q)
qc.cx(q[0],q[1])
circuit_drawer(qc)
Out[37]:
In [38]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[38]:
Apply the $Y$ gate to the target qubit if the control qubit is the MSB
$$ C_Y = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & -i\\ 0 & 0 & i & 0 \end{pmatrix}, $$or when the LSB is the control
$$ C_Y = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 0 & -i\\ 0 & 0 & 1 & 0\\ 0 & i & 0 & 0 \end{pmatrix}. $$
In [39]:
qc = QuantumCircuit(q)
qc.cy(q[0],q[1])
circuit_drawer(qc)
Out[39]:
In [40]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[40]:
Similarly, the controlled Z gate flips the phase of the target qubit if the control qubit is $1$. The matrix looks the same regardless of whether the MSB or LSB is the control qubit:
$$ C_Z = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & -1 \end{pmatrix} $$
In [41]:
qc = QuantumCircuit(q)
qc.cz(q[0],q[1])
circuit_drawer(qc)
Out[41]:
In [42]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[42]:
Apply $H$ gate to the target qubit if the control qubit is $|1\rangle$. Below is the case where the control is the LSB qubit.
$$ C_H = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & \frac{1}{\sqrt{2}} & 0 & \frac{1}{\sqrt{2}}\\ 0 & 0 & 1 & 0\\ 0 & \frac{1}{\sqrt{2}} & 0& -\frac{1}{\sqrt{2}} \end{pmatrix} $$
In [43]:
qc = QuantumCircuit(q)
qc.ch(q[0],q[1])
circuit_drawer(qc)
Out[43]:
In [44]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary']/(0.707+0.707j), 3)
Out[44]:
In [45]:
qc = QuantumCircuit(q)
qc.crz(pi/2,q[0],q[1])
circuit_drawer(qc)
Out[45]:
In [46]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[46]:
Perform a phase rotation if both qubits are in the $|11\rangle$ state. The matrix looks the same regardless of whether the MSB or LSB is the control qubit.
$$ C_{u1}(\lambda) = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & e^{i\lambda} \end{pmatrix} $$
In [47]:
qc = QuantumCircuit(q)
qc.cu1(pi/2,q[0], q[1])
circuit_drawer(qc)
Out[47]:
I THINK SHOULD BE CALLED $C_\mathrm{PHASE}(\lambda)$
In [48]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[48]:
Perform controlled-$u3$ rotation on the target qubit if the control qubit (here LSB) is $|1\rangle$.
$$ C_{u3}(\theta, \phi, \lambda) \equiv \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & e^{-i(\phi+\lambda)/2}\cos(\theta/2) & 0 & -e^{-i(\phi-\lambda)/2}\sin(\theta/2)\\ 0 & 0 & 1 & 0\\ 0 & e^{i(\phi-\lambda)/2}\sin(\theta/2) & 0 & e^{i(\phi+\lambda)/2}\cos(\theta/2) \end{pmatrix}. $$
In [49]:
qc = QuantumCircuit(q)
qc.cu3(pi/2, pi/2, pi/2, q[0], q[1])
circuit_drawer(qc)
Out[49]:
In [50]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[50]:
NOTE I NEED TO FIX THIS AND DECIDE ON CONVENTION - I ACTUALLY THINK WE WANT A FOUR PARAMETER GATE AND JUST CALL IT CU AND TO REMOVE THIS GATE.
The SWAP gate exchanges the two qubits. It transforms the basis vectors as
$$|00\rangle \rightarrow |00\rangle~,~|01\rangle \rightarrow |10\rangle~,~|10\rangle \rightarrow |01\rangle~,~|11\rangle \rightarrow |11\rangle,$$which gives a matrix representation of the form
$$ \mathrm{SWAP} = \begin{pmatrix} 1 & 0 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 1 \end{pmatrix}. $$
In [51]:
qc = QuantumCircuit(q)
qc.swap(q[0], q[1])
circuit_drawer(qc)
Out[51]:
In [52]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[52]:
There are two commonly-used three-qubit gates. For three qubits, the basis vectors are ordered as
$$|000\rangle, |001\rangle, |010\rangle, |011\rangle, |100\rangle, |101\rangle, |110\rangle, |111\rangle,$$which, as bitstrings, represent the integers $0,1,2,\cdots, 7$. Again, Qiskit uses a representation in which the first qubit is on the right-most side of the tensor product and the third qubit is on the left-most side:
$$|abc\rangle : \underset{\text{qubit 2}}{|a\rangle}\otimes \underset{\text{qubit 1}}{|b\rangle}\otimes \underset{\text{qubit 0}}{|c\rangle}.$$The Toffoli gate_gate) flips the third qubit if the first two qubits (LSB) are both $|1\rangle$:
$$|abc\rangle \rightarrow |bc\oplus a\rangle \otimes |b\rangle \otimes c \rangle.$$In matrix form, the Toffoli gate is $$ C_{CX} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \end{pmatrix}. $$
In [53]:
q = QuantumRegister(3)
In [54]:
qc = QuantumCircuit(q)
qc.ccx(q[0], q[1], q[2])
circuit_drawer(qc)
Out[54]:
In [55]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[55]:
The Fredkin gate_gate), or the controlled swap gate, exchanges the second and third qubits if the first qubit (LSB) is $|1\rangle$:
$$ |abc\rangle \rightarrow \begin{cases} |bac\rangle~~\text{if}~c=1 \cr |abc\rangle~~\text{if}~c=0 \end{cases}.$$In matrix form, the Fredkin gate is
$$ C_{\mathrm{SWAP}} = \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pmatrix}. $$
In [56]:
qc = QuantumCircuit(q)
qc.cswap(q[0], q[1], q[2])
circuit_drawer(qc)
Out[56]:
In [57]:
job = execute(qc, backend)
np.round(job.result().get_data(qc)['unitary'], 3)
Out[57]:
In [58]:
q = QuantumRegister(1)
c = ClassicalRegister(1)
In [59]:
qc = QuantumCircuit(q, c)
qc.measure(q, c)
circuit_drawer(qc)
Out[59]:
In [60]:
backend = Aer.get_backend('qasm_simulator')
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[60]:
The simulator predicts that 100 percent of the time the classical register returns 0.
In [61]:
qc = QuantumCircuit(q, c)
qc.h(q)
qc.measure(q, c)
circuit_drawer(qc)
Out[61]:
In [62]:
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[62]:
The simulator predicts that 50 percent of the time the classical register returns 0 or 1.
In [63]:
qc = QuantumCircuit(q, c)
qc.reset(q[0])
qc.measure(q, c)
circuit_drawer(qc)
Out[63]:
In [64]:
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[64]:
In [65]:
qc = QuantumCircuit(q, c)
qc.h(q)
qc.reset(q[0])
qc.measure(q, c)
circuit_drawer(qc)
Out[65]:
In [66]:
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[66]:
Here we see that for both of these circuits the simulator always predicts that the output is 100 percent in the 0 state.
In [67]:
qc = QuantumCircuit(q, c)
qc.x(q[0]).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[67]:
Here the classical bit always takes the value 0 so the qubit state is always flipped.
In [68]:
qc = QuantumCircuit(q, c)
qc.h(q)
qc.measure(q,c)
qc.x(q[0]).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)
Out[68]:
In [69]:
job = execute(qc, backend, shots=1024)
job.result().get_counts(qc)
Out[69]:
Here the classical bit by the first measurement is random but the conditional operation results in the qubit being deterministically put into $|1\rangle$.
What if we want to initialize a qubit register to an arbitrary state? An arbitrary state for $n$ qubits may be specified by a vector of $2^n$ amplitudes, where the sum of amplitude-norms-squared equals 1. For example, the following three-qubit state can be prepared:
$$|\psi\rangle = \frac{i}{4}|000\rangle + \frac{1}{\sqrt{8}}|001\rangle + \frac{1+i}{4}|010\rangle + \frac{1+2i}{\sqrt{8}}|101\rangle + \frac{1}{4}|110\rangle$$
In [70]:
# Initializing a three-qubit quantum state
import math
desired_vector = [
1 / math.sqrt(16) * complex(0, 1),
1 / math.sqrt(8) * complex(1, 0),
1 / math.sqrt(16) * complex(1, 1),
0,
0,
1 / math.sqrt(8) * complex(1, 2),
1 / math.sqrt(16) * complex(1, 0),
0]
q = QuantumRegister(3)
qc = QuantumCircuit(q)
qc.initialize(desired_vector, [q[0],q[1],q[2]])
Out[70]:
In [71]:
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
qc_state = job.result().get_statevector(qc)
qc_state
Out[71]:
Fidelity is useful to check whether two states are same or not. For quantum (pure) states $\left|\psi_1\right\rangle$ and $\left|\psi_2\right\rangle$, the fidelity is
$$ F\left(\left|\psi_1\right\rangle,\left|\psi_2\right\rangle\right) = \left|\left\langle\psi_1\middle|\psi_2\right\rangle\right|^2. $$The fidelity is equal to $1$ if and only if two states are same.
In [72]:
state_fidelity(desired_vector,qc_state)
Out[72]:
How does the desired state get generated behind the scenes? There are multiple methods for doing this. Qiskit uses a method proposed by Shende et al. Here, the idea is to assume the quantum register to have started from our desired state, and construct a circuit that takes it to the $|00..0\rangle$ state. The initialization circuit is then the reverse of such circuit.
To take an arbitrary quantum state to the zero state in the computational basis, we perform an iterative procedure that disentangles qubits from the register one-by-one. We know that any arbitrary single-qubit state $|\rho\rangle$ can be taken to the $|0\rangle$ state using a $\phi$-degree rotation about the Z axis followed by a $\theta$-degree rotation about the Y axis:
$$R_y(-\theta)R_z(-\phi)|\rho\rangle = re^{it}|0\rangle$$Since now we are dealing with $n$ qubits instead of just 1, we must factorize the state vector to separate the Least Significant Bit (LSB):
$$\begin{align*} |\psi\rangle =& \alpha_{0_0}|00..00\rangle + \alpha_{0_1}|00..01\rangle + \alpha_{1_0}|00..10\rangle + \alpha_{1_1}|00..11\rangle + ... \\&+ \alpha_{(2^{n-1}-1)_0}|11..10\rangle + \alpha_{(2^{n-1}-1)_1}|11..11\rangle \\ =& |00..0\rangle (\alpha_{0_0}|0\rangle + \alpha_{0_1}|1\rangle) + |00..1\rangle (\alpha_{1_0}|0\rangle + \alpha_{1_1}|1\rangle) + ... \\&+ |11..1\rangle (\alpha_{(2^{n-1}-1)_0}(|0\rangle + \alpha_{(2^{n-1}-1)_1}|1\rangle) \\ =& |00..0\rangle|\rho_0\rangle + |00..1\rangle|\rho_1\rangle + ... + |11..1\rangle|\rho_{2^{n-1}-1}\rangle \end{align*}$$Now each of the single-qubit states $|\rho_0\rangle, ..., |\rho_{2^{n-1}-1}\rangle$ can be taken to $|0\rangle$ by finding appropriate $\phi$ and $\theta$ angles per the equation above. Doing this simultaneously on all states amounts to the following unitary, which disentangles the LSB:
$$U = \begin{pmatrix} R_{y}(-\theta_0)R_{z}(-\phi_0) & & & &\\ & R_{y}(-\theta_1)R_{z}(-\phi_1) & & &\\ & . & & &\\ & & . & &\\ & & & & R_y(-\theta_{2^{n-1}-1})R_z(-\phi_{2^{n-1}-1}) \end{pmatrix} $$Hence,
$$U|\psi\rangle = \begin{pmatrix} r_0e^{it_0}\\ r_1e^{it_1}\\ . \\ . \\ r_{2^{n-1}-1}e^{it_{2^{n-1}-1}} \end{pmatrix}\otimes|0\rangle$$U can be implemented as a "quantum multiplexor" gate, since it is a block diagonal matrix. In the quantum multiplexor formalism, a block diagonal matrix of size $2^n \times 2^n$, and consisting of $2^s$ blocks, is equivalent to a multiplexor with $s$ select qubits and $n-s$ data qubits. Depending on the state of the select qubits, the corresponding blocks are applied to the data qubits. A multiplexor of this kind can be implemented after recursive decomposition to primitive gates of cx, rz and ry.
In [ ]: