Showing posts with label controlled gates. Show all posts
Showing posts with label controlled gates. Show all posts

Constructing a Toffoli gate out of two-qubit gates

The Toffoli gate (controlled-controlled-NOT) is a 3-qubit gate, and here it will be shown how to implement it with 2-qubit gates. The construction is given by the following quantum circuit.


Here, the unitary gate $V$ is given by
\[
V=\frac{1}{\sqrt{2}}\begin{pmatrix}
\omega & \overline{\omega} \\
\overline{\omega} & \omega
\end{pmatrix}
\]
where $\omega=e^{i\pi/4}$ and $\overline{\omega}=e^{-i\pi/4}$ is the complex conjugate of $\omega$, and thus
\[
V^{\dagger}=V^{-1}=\frac{1}{\sqrt{2}}\begin{pmatrix}
\overline{\omega} & \omega \\
\omega & \overline{\omega}
\end{pmatrix}
\]

 Consider the product of $V$ with itself:
\[
V^2=\frac{1}{2}\begin{pmatrix}
\omega & \overline{\omega} \\
\overline{\omega} & \omega
\end{pmatrix}
\begin{pmatrix}
\omega & \overline{\omega} \\
\overline{\omega} & \omega
\end{pmatrix}=
\frac{1}{2}\begin{pmatrix}
\omega^2+\overline{\omega}^2 & \omega\overline{\omega}+\overline{\omega}\omega \\
\overline{\omega}\omega+\omega\overline{\omega} & \overline{\omega}^2+\omega^2
\end{pmatrix}.
\]

Now
\[\begin{align*}
 \omega^2+\overline{\omega}^2=& e^{i\pi/2}+e^{-i\pi/2}=i-i=0 \\
\overline{\omega}^2+\omega^2=& e^{-i\pi/2}+e^{i\pi/2}=-i+i=0 \\
\omega\overline{\omega}+\overline{\omega}\omega=&e^{i\pi/4}e^{-i\pi/4}+e^{-i\pi/4}e^{i\pi/4}=1+1=2 \\
\overline{\omega}\omega+\omega\overline{\omega}=&e^{-i\pi/4}e^{i\pi/4}+e^{i\pi/4}e^{i\pi/4}=1+1=2,
\end{align*}\]
and therefore
\[
V^2=
\frac{1}{2}\begin{pmatrix}
\omega^2+\overline{\omega}^2 & \omega\overline{\omega}+\overline{\omega}\omega \\
\overline{\omega}\omega+\omega\overline{\omega} & \overline{\omega}^2+\omega^2
\end{pmatrix}=
\frac{1}{2}\begin{pmatrix}
0&2\\
2&0
\end{pmatrix}
=\begin{pmatrix}
0&1\\
1&0
\end{pmatrix}
=X.
\]


 Let $\ket{\psi}=\alpha\ket{0}+\beta\ket{0}$ be an arbitrary 1-qubit state. Note that $VV^{\dagger}=V^{\dagger}V=I$, and that $VV=X$ as shown in (a). Consider how the circuit maps the following states:
\[\begin{align*}
\ket{00}\ket{\psi}&\overset{c_2V_3}\mapsto\ket{00}\ket{\psi}\overset{c_1X_2}\mapsto\ket{00}\ket{\psi}\overset{c_2V^{\dagger}_2}\mapsto\ket{00}\ket{\psi}\overset{c_1X_2}\mapsto\ket{00}\ket{\psi}\overset{c_2V_3}\mapsto\ket{00}\ket{\psi}=\ket{00}\ket{\psi} \\
\ket{01}\ket{\psi}&\overset{c_2V_3}\mapsto\ket{01}V\ket{\psi}\overset{c_1X_2}\mapsto\ket{01}V\ket{\psi}\overset{c_2V^{\dagger}_2}\mapsto\ket{01}V^{\dagger}V\ket{\psi}\overset{c_1X_2}\mapsto\ket{01}V^{\dagger}V\ket{\psi}\overset{c_2V_3}\mapsto\ket{01}V^{\dagger}V\ket{\psi}=\ket{01}\ket{\psi}\\
\ket{10}\ket{\psi}&\overset{c_2V_3}\mapsto\ket{10}\ket{\psi}\overset{c_1X_2}\mapsto\ket{11}\ket{\psi}\overset{c_2V^{\dagger}_2}\mapsto\ket{11}V^{\dagger}\ket{\psi}\overset{c_1X_2}\mapsto\ket{10}V^{\dagger}\ket{\psi}\overset{c_2V_3}\mapsto\ket{10}VV^{\dagger}\ket{\psi}=\ket{10}\ket{\psi} \\
\ket{11}\ket{\psi}&\overset{c_2V_3}\mapsto\ket{11}V\ket{\psi}\overset{c_1X_2}\mapsto\ket{10}V\ket{\psi}\overset{c_2V^{\dagger}_2}\mapsto\ket{10}V\ket{\psi}\overset{c_1X_2}\mapsto\ket{11}\ket{\psi}\overset{c_2V_3}\mapsto\ket{11}VV\ket{\psi}=\ket{11}X\ket{\psi}
\end{align*}
\]

For each of the four mappings calculated in part (b), observe how $\ket{ab}\ket{\psi}$ is mapped in the two cases where $\ket{\psi}$ is either of the basis states $\ket{0}$ or $\ket{1}$. Since the action is trivially $\ket{ab}\ket{\psi}\mapsto\ket{ab}\ket{\psi}$ in all cases where both $ab$ is $11$, it follows that $\ket{ab}\ket{c}\mapsto\ket{ab}\ket{c}$ for all $ab\in\{00,01,10\}$ and $c\in\{0,1\}$. However, in the case where $ab=11$, and $c\in\{0,1\}$ it is seen that
\[\begin{align*}
\ket{11}\ket{0}\mapsto&\ket{11}X\ket{0}=\ket{11}\ket{1},\\
\ket{11}\ket{1}\mapsto&\ket{11}X\ket{1}=\ket{11}\ket{0}.
\end{align*}\]
Therefore, the circuit  leaves the states unchanged unless the first two qubits in the register are in the state $\ket{11}$, in which case the third qubit in the register is flipped to the opposite bit value. By thinking of each of these eight resulting states as column vectors, the matrix representation of the circuit, $T$ can be constructed by making each column of the matrix correspond to the vector that represents the resulting state under the circuit mapping as follows:
\[
T=\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&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&1 \\
0&0&0&0&0&0&1&0 \\
\end{pmatrix}.
\]


Constructing simple two qubit states

Here, we will describe how to construct some simple two-qubit states using some primitive gates.

In what follows, a two-qubit quantum circuit consisting of one $CNOT$ gate and two Hadamard $H$ gates that computes the following unitary transformation will be described:
\[U=\begin{pmatrix}
1&0&0&0 \\
0&1&0&0\\
0&0&1&0 \\
0&0&0&-1
\end{pmatrix}\]

Consider the for computational basis states and their corresponding representation as column vectors:
\[\ket{00}=\begin{pmatrix}
1\\0\\0\\0
\end{pmatrix},
\ket{01}=\begin{pmatrix}
0\\1\\0\\0
\end{pmatrix},
\ket{10}=\begin{pmatrix}
0\\0\\1\\0
\end{pmatrix},
\ket{11}=\begin{pmatrix}
0\\0\\0\\1
\end{pmatrix}.\]

Observe that $U$ leaves the first three basis states unchanged and only multiplies the last by a factor of $-1$:
\[U\ket{00}=\ket{00},
U\ket{01}=\ket{01},
U\ket{10}=\ket{10},
U\ket{11}=-\ket{11}.\]

Consider the following circuit consisting of a Hadamard gate acting on the second qubit, followed by a controlled-NOT gate, and then with a second Hadamard gate acting on the second qubit again.


 The action of this circuit on each of the four basis states is described below:
 \[\ket{00} \overset{I\otimes H}\mapsto\super{\ket{00}+\ket{01}}\overset{CNOT}\mapsto\super{\ket{00}+\ket{01}}\overset{I\otimes H}\mapsto\frac{1}{2}(\ket{00}+\ket{01}+\ket{00}-\ket{01})=\ket{00},\]

\[\ket{01} \overset{I\otimes H}\mapsto\super{\ket{00}-\ket{01}}\overset{CNOT}\mapsto\super{\ket{00}-\ket{01}}\overset{I\otimes H}\mapsto\frac{1}{2}(\ket{00}+\ket{01}-\ket{00}+\ket{01})=\ket{01},\]

\[\ket{10} \overset{I\otimes H}\mapsto\super{\ket{10}+\ket{11}}\overset{CNOT}\mapsto\super{\ket{11}+\ket{10}}\overset{I\otimes H}\mapsto\frac{1}{2}(\ket{10}-\ket{11}+\ket{10}+\ket{11})=\ket{10}, \]

\[\ket{11} \overset{I\otimes H}\mapsto\super{\ket{10}-\ket{11}}\overset{CNOT}\mapsto\super{\ket{11}-\ket{10}}\overset{I\otimes H}\mapsto\frac{1}{2}(\ket{10}-\ket{11}-\ket{10}-\ket{11})=-\ket{11}.\]

Thus, the action of the circuit is identical to the unitary transformation $U$ expressed as the matrix since the four basis  states are mapped similarly.


Now, define the one-qubit gates $H$ and $S$ as
\[H=\frac{1}{\sqrt{2}}\begin{pmatrix}
1&1\\
1&-1
\end{pmatrix}\]
and
\[S=\begin{pmatrix}
1&0\\
0&i
\end{pmatrix}\]

In each case, the $4\times4$ matrix corresponding to the two-qubit controlled gate is given.

Denote the following gate by $c_1H_2$:

Observe how the $c_1H_2$ gate acts on the four basis states:
\[\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}\overset{c_1H_2}\mapsto\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}, \ \ \ \ \ \ \
\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix}\overset{c_1H_2}\mapsto\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix},\]

\[\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}\overset{c_1H_2}\mapsto\super{\ket{10}+\ket{11}}=\frac{1}{\sqrt{2}}\begin{pmatrix} 0\\0\\1\\1\end{pmatrix},\]

\[\ket{11}=\begin{pmatrix} 0\\0\\0\\1\end{pmatrix}\overset{c_1H_2}\mapsto\super{\ket{10}-\ket{11}}=\frac{1}{\sqrt{2}}\begin{pmatrix} 0\\0\\1\\-1\end{pmatrix}.\]

The matrix representation of the gate $c_1H_2$ can be constructed by placing each of these output vectors as the columns of the matrix:
\[c_1H_2=\begin{pmatrix}
1&0&0&0\\
0&1&0&0\\
0&0&\frac{1}{\sqrt{2}}&\frac{1}{\sqrt{2}}\\
0&0&\frac{1}{\sqrt{2}}&\frac{-1}{\sqrt{2}}
\end{pmatrix}.\]


Denote the following gate by $c_2H_1$:

Observe how the $c_2H_1$ gate acts on the four basis states:
\[\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}\overset{c_2H_1}\mapsto\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}, \ \ \ \
\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix}\overset{c_2H_1}\mapsto\super{\ket{01}+\ket{11}}=\frac{1}{\sqrt{2}}\begin{pmatrix} 0\\1\\0\\1\end{pmatrix},\]

\[\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}\overset{c_2H_1}\mapsto\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}, \ \ \ \ \
\ket{11}=\begin{pmatrix} 0\\0\\0\\1\end{pmatrix}\overset{c_2H_1}\mapsto\super{\ket{01}-\ket{11}}=\frac{1}{\sqrt{2}}\begin{pmatrix} 0\\1\\0\\-1\end{pmatrix}.\]

Then, matrix representation of the gate $c_1H_2$ can be constructed by placing each of these output vectors as the columns of the matrix:
\[c_1H_2=\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}.\]


Denote the following gate by $c_1S_2$:

 First note that $S\ket{0}=\ket{0}$ and $S\ket{1}=i\ket{1}$. Now observe how the $c_1S_2$ gate acts on the four basis states:
\[\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}\overset{c_1S_2}\mapsto\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}, \ \ \ \ \ \ \
\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix}\overset{c_1S_2}\mapsto\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix},\]

\[\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}\overset{c_1S_2}\mapsto\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}, \ \ \ \ \ \ \
\ket{11}=\begin{pmatrix} 0\\0\\0\\1\end{pmatrix}\overset{c_1S_2}\mapsto\ket{11}=\begin{pmatrix} 0\\0\\0\\i\end{pmatrix}.\]

The matrix representation of the gate $c_1S_2$ can be constructed by placing each of these output vectors as the columns of the matrix:
\[c_1S_2=\begin{pmatrix}
1&0&0&0\\
0&1&0&0\\
0&0&1&0\\
0&0&0&i
\end{pmatrix}.\]


Denote the following gate by $c_2S_1$:

Observe how the $c_2S_1$ gate acts on the four basis states:
\[\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}\overset{c_2S_1}\mapsto\ket{00}=\begin{pmatrix} 1\\0\\0\\0\end{pmatrix}, \ \ \ \ \ \ \
\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix}\overset{c_2S_1}\mapsto\ket{01}=\begin{pmatrix} 0\\1\\0\\0\end{pmatrix},\]

\[\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}\overset{c_2S_1}\mapsto\ket{10}=\begin{pmatrix} 0\\0\\1\\0\end{pmatrix}, \ \ \ \ \ \ \
\ket{11}=\begin{pmatrix} 0\\0\\0\\1\end{pmatrix}\overset{c_2S_1}\mapsto\ket{11}=\begin{pmatrix} 0\\0\\0\\i\end{pmatrix}.\]

Note that the action of $c_2S_1$ is identical to that of $c_1S_2$ from case (iii), and so the matrix representation of $c_2S_1$ is also the same:
\[c_2S_1=\begin{pmatrix}
1&0&0&0\\
0&1&0&0\\
0&0&1&0\\
0&0&0&i
\end{pmatrix}.\]

Here, the $4\times4$ matrix corresponding to the following quantum circuit will be given:

where $S$ is defined as it was above, and the last two-qubit gate denoted by $SWAP$ transposes the two qubits (more precisely, the $SWAP$ gate maps $\ket{00}\mapsto\ket{00}, \ket{01}\mapsto\ket{10}, \ket{10}\mapsto\ket{01},\ \text{and}\ \ket{11}\mapsto\ket{11}$).


Denote the overall action of this circuit by the unitary operator $U$. In order to express the action of $U$ as a matrix, first observe how $U$ acts on the four computational basis states:

\[\begin{array}{rcl}
\ket{00} &\overset{H\otimes I}\mapsto&\super{\ket{00}+\ket{10}} \\
&\overset{c_1S_2}\mapsto&\super{\ket{00}+\ket{10}} \\
&\overset{I\otimes H}\mapsto&\frac{1}{2}(\ket{00}+\ket{01}+\ket{10}+\ket{11}) \\
&\overset{SWAP}\mapsto&\frac{1}{2}(\ket{00}+\ket{10}+\ket{01}+\ket{11})
\end{array}
\implies U\begin{pmatrix}1\\0\\0\\0\end{pmatrix}=\frac{1}{2}\begin{pmatrix}1\\1\\1\\1\end{pmatrix},\]

\[\begin{array}{rcl}
\ket{01} &\overset{H\otimes I}\mapsto&\super{\ket{01}+\ket{11}} \\
&\overset{c_1S_2}\mapsto&\super{\ket{01}+i\ket{11}} \\
&\overset{I\otimes H}\mapsto&\frac{1}{2}(\ket{00}-\ket{01}+i\ket{10}-i\ket{11}) \\
&\overset{SWAP}\mapsto&\frac{1}{2}(\ket{00}-\ket{10}+i\ket{01}-i\ket{11})
\end{array}
\implies U\begin{pmatrix}0\\1\\0\\0\end{pmatrix}=\frac{1}{2}\begin{pmatrix}1\\i\\-1\\-i\end{pmatrix},\]

\[\begin{array}{rcl}
\ket{10} &\overset{H\otimes I}\mapsto&\super{\ket{00}-\ket{10}} \\
&\overset{c_1S_2}\mapsto&\super{\ket{00}-\ket{10}} \\
&\overset{I\otimes H}\mapsto&\frac{1}{2}(\ket{00}+\ket{01}-\ket{10}-\ket{11}) \\
&\overset{SWAP}\mapsto&\frac{1}{2}(\ket{00}+\ket{10}-\ket{01}-\ket{11})
\end{array}
\implies U\begin{pmatrix}0\\0\\1\\0\end{pmatrix}=\frac{1}{2}\begin{pmatrix}1\\-1\\1\\-1\end{pmatrix},\]

\[\begin{array}{rcl}
\ket{11} &\overset{H\otimes I}\mapsto&\super{\ket{01}-\ket{11}} \\
&\overset{c_1S_2}\mapsto&\super{\ket{01}-i\ket{11}} \\
&\overset{I\otimes H}\mapsto&\frac{1}{2}(\ket{00}-\ket{01}-i\ket{10}+i\ket{11}) \\
&\overset{SWAP}\mapsto&\frac{1}{2}(\ket{00}-\ket{10}-i\ket{01}+i\ket{11})
\end{array}
\implies U\begin{pmatrix}1\\0\\0\\0\end{pmatrix}=\frac{1}{2}\begin{pmatrix}1\\-i\\-1\\i\end{pmatrix}.\]

Then the matrix constructed that contains the four output vectors as its columns gives the matrix representation of the unitary operation $U$ given by the circuit:
\[U=\frac{1}{2}\begin{pmatrix}
1&1&1&1\\
1&i&-1&-i\\
1&-1&1&-1\\
1&-i&-1&i
\end{pmatrix}.\]