\section{Quantum Gates}

\definition{}
The quantum analog of a logic gate is a \textit{quantum gate,} \par
which we'll define as a linear map from $\mathbb{B}^n$ to $\mathbb{B}^n$. \par
\note[Note]{This definition is temporary, we'll extend it soon.}




\problem{}
Consider the CNOT (controlled not) gate. \par
When applied to a two-bit state $\ket{ab}$, CNOT inverts $b$ iff $a$ is $\ket{1}$. \par
Find the matrix that represents the CNOT gate. \par
\hint{what are the dimensions of this matrix?}

\begin{solution}
	\begin{equation*}
		\text{CNOT} = \begin{bmatrix}
			1 & 0 & 0 & 0 \\
			0 & 1 & 0 & 0 \\
			0 & 0 & 0 & 1 \\
			0 & 0 & 1 & 0 \\
		\end{bmatrix}
	\end{equation*}

	\vspace{4mm}

	If $\ket{a}$ is $\ket{0}$, $\ket{a} \otimes \ket{b}$ is
	$
	\begin{bmatrix}
		\begin{bmatrix}
			b_1 \\ b_2
		\end{bmatrix} \\ 0 \\ 0
	\end{bmatrix}
	$, and the \say{not} portion of the matrix is ignored.


	\vspace{4mm}

	If $\ket{a}$ is $\ket{1}$, $\ket{a} \otimes \ket{b}$ is
	$
	\begin{bmatrix}
		0 \\ 0 \\
		\begin{bmatrix}
			b_1 \\ b_2
		\end{bmatrix}
	\end{bmatrix}
	$, and the \say{identity} portion of the matrix is ignored.


	The state of $\ket{a}$ is always preserved, since it's determined by the position of
	$\left[\begin{smallmatrix}b_1 \\ b_2\end{smallmatrix}\right]$ in the tensor product.
	If $\left[\begin{smallmatrix}b_1 \\ b_2\end{smallmatrix}\right]$ is on top, $\ket{a}$ is $\ket{0}$,
	and if $\left[\begin{smallmatrix}b_1 \\ b_2\end{smallmatrix}\right]$ is on the bottom, $\ket{a}$ is $\ket{1}$.
\end{solution}

\vfill









\problem{}
Now, modify the CNOT gate so that it inverts $\ket{a}$ whenever it is applied.

\begin{solution}
	\begin{equation*}
		\text{CNOT}_{\text{mod}} = \begin{bmatrix}
			0 & 1 & 0 & 0 \\
			1 & 0 & 0 & 0 \\
			0 & 0 & 1 & 0 \\
			0 & 0 & 0 & 1
		\end{bmatrix}
	\end{equation*}
\end{solution}

\vfill
\pagebreak

\iffalse
\problem{}
Finally, modify the original CNOT gate so that the roles of its bits are reversed: \par
$\text{CNOT}_{\text{flip}} \ket{ab}$ should invert $\ket{a}$ iff $\ket{b}$ is $\ket{1}$.


\begin{solution}
	\begin{equation*}
		\text{CNOT}_{\text{flip}} = \begin{bmatrix}
			1 & 0 & 0 & 0 \\
			0 & 0 & 0 & 1 \\
			0 & 0 & 1 & 0 \\
			0 & 1 & 0 & 0 \\
		\end{bmatrix}
	\end{equation*}
\end{solution}

\vfill
\fi









\problem{}
The SWAP gate swaps two bits: $\text{SWAP}\ket{ab} = \ket{ba}$. \par
Find its matrix.

\begin{solution}
	\begin{equation*}
		\text{SWAP} = \begin{bmatrix}
			1 & 0 & 0 & 0 \\
			0 & 0 & 1 & 0 \\
			0 & 1 & 0 & 0 \\
			0 & 0 & 0 & 1 \\
		\end{bmatrix}
	\end{equation*}
\end{solution}

\vfill









\problem{}
The $T$ gate is a three-bit gate that inverts its right bit iff its left and middle inputs are both $\ket{1}$. \par
In other words, $T\ket{11x} = \ket{11}\ket{\text{not } x}$, and $T\ket{abx} = \ket{abx}$ for all other inputs. \par
Find the $T$ gate's matrix. \par
\note{
	This gate is particularly interesting because it's a \textit{universal quantum gate}: \\
	like NOR and NAND in classical logic, any quantum gate may emulated by only applying $T$ gates.
}

\begin{solution}
	\begin{equation*}
		\text{T} = \begin{bmatrix}
			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{bmatrix}
	\end{equation*}
\end{solution}

\vfill
\pagebreak














The last thing we need is a way to draw complex sequences of gates. \par
We already know how to do this with classical gates, \'a la logic circuit:

\begin{center}
\begin{tikzpicture}[circuit logic US, scale=1.5]
	\node[and gate] (and) at (0,-0.8) {\tiny\texttt{and}};
	\draw ([shift={(-0.5, 0)}] and.input 1) node[left] {\texttt{1}} -- (and.input 1);
	\draw ([shift={(-0.5, 0)}] and.input 2) node[left] {\texttt{0}} -- (and.input 2);
	\draw (and.output) -- ([shift={(0.5, 0)}] and.output) node[right] {\texttt{0}};
\end{tikzpicture}
\end{center}

We draw quantum circuits in a very similar way. \par
For example, here a simple three-bit circuit consisting of a CNOT gate on the first bit, \par
controlled by the third. The first bit is inverted iff the third bit is $\ket{1}$:

\begin{center}
\begin{tikzpicture}[scale=1]
	\node[qubit] (a) at (0, 0) {$\ket{0}$};
	\node[qubit] (b) at (0, -1) {$\ket{0}$};
	\node[qubit] (c) at (0, -2) {$\ket{1}$};

	\draw[wire] (a) -- ([shift={(4, 0)}] a.center) node[qubit] {$\ket{1}$};
	\draw[wire] (b) -- ([shift={(4, 0)}] b.center) node[qubit] {$\ket{0}$};
	\draw[wire] (c) -- ([shift={(4, 0)}] c.center) node[qubit] {$\ket{1}$};

	\draw[wire]
		($([shift={(1,0)}] a)!0.5!([shift={(3,0)}] a)$) --
		($([shift={(1,0)}] c)!0.5!([shift={(3,0)}] c)$)
	;

	\draw[wirejoin]
		($([shift={(1,0)}] c)!0.5!([shift={(3,0)}] c)$)
		circle[radius=0.1] coordinate(dot)
	;

	\qubox{a}{1}{a}{3}{CNOT}
\end{tikzpicture}
\end{center}













\problem{}
Draw the CNOT gate as a classical logic circuit. \par
\hint{This can be done with one gate.}

\begin{solution}
	\begin{center}
	\begin{tikzpicture}[circuit logic US, scale=2]
		\node[xor gate] (xor) at (0, 0) {\tiny\texttt{xor}};
		\draw (xor.input 1) ++(-0.5, 0) coordinate (start);
		\draw (xor.input 1) ++(-0.25, 0) coordinate (startjoin);
		\draw (xor.input 1) -- (xor.input 1 -| start) node[left] {$a$};
		\draw (xor.input 2) -| (0,-0.25 -| startjoin) |- (0,-0.25, -| start) node[left] {$b$};
		\filldraw (0,-0.25, -| startjoin) circle[radius=0.3mm] coordinate(dot);
		\draw (dot) -- (dot -| 1,0) node[right] {$b_\text{out}$};
		\draw (xor.output) -- (xor.output -| 1,0) node[right] {$a_\text{out}$};
	\end{tikzpicture}
	\end{center}
\end{solution}


\vfill

Gate controls may be marked with a filled circle or an empty circle. \par
Empty circles denote \textit{inverse controls,} which (of course) have an inverse effect. \par
For example, the two circuits below are identical:

\null\hfill
\begin{minipage}{0.48\textwidth}
	\begin{center}
	\begin{tikzpicture}[scale=1]
		\node[qubit] (a) at (0, 0) {$\ket{0}$};
		\node[qubit] (b) at (0, -1) {$\ket{0}$};

		\draw[wire] (a) -- ([shift={(4, 0)}] a.center) node[
qubit] {$\ket{a}$};
		\draw[wire] (b) -- ([shift={(4, 0)}] b.center) node[
qubit] {$\ket{b}$};

		\draw[wire]
			($([shift={(1,0)}] a)!0.5!([shift={(3,0)}] a)$) --
			($([shift={(1,0)}] b)!0.5!([shift={(3,0)}] b)$)
		;

		\draw[wireijoin]
			($([shift={(1,0)}] b)!0.5!([shift={(3,0)}] b)$)
			circle[radius=0.1] coordinate(dot)
		;

		\qubox{a}{1}{a}{3}{CNOT}
	\end{tikzpicture}
	\end{center}
\end{minipage}
\hfill
\begin{minipage}{0.48\textwidth}
	\begin{center}
	\begin{tikzpicture}[scale=1]
		\node[qubit] (a) at (0, 0) {$\ket{0}$};
		\node[qubit] (b) at (0, -1) {$\ket{0}$};

		\draw[wire] (a) -- ([shift={(4, 0)}] a.center) node[
qubit] {$\ket{a}$};
		\draw[wire] (b) -- ([shift={(4, 0)}] b.center) node[
qubit] {$\ket{b}$};

		\draw[wire]
			($([shift={(1.5,0)}] a)!0.5!([shift={(2.5,0)}] a)$) --
			($([shift={(1.5,0)}] b)!0.5!([shift={(2.5,0)}] b)$)
		;

		\draw[wirejoin]
			($([shift={(1.5,0)}] b)!0.5!([shift={(2.5,0)}] b)$)
			circle[radius=0.1] coordinate(dot)
		;
		\qubox{a}{1}{a}{3}{CNOT}
		\qubox{b}{0.5}{b}{1.5}{X}
		\qubox{b}{2.5}{b}{3.5}{X}
	\end{tikzpicture}
	\end{center}
\end{minipage}
\hfill\null









\problem{}
What are $\ket{a}$ and $\ket{b}$ in the diagrams above?

\begin{solution}
	\begin{center}
	\begin{tikzpicture}[scale=1]
		\node[qubit] (a) at (0, 0) {$\ket{0}$};
		\node[qubit] (b) at (0, -1) {$\ket{0}$};

		\draw[wire] (a) -- ([shift={(4, 0)}] a.center) node[
qubit] {$\ket{1}$};
		\draw[wire] (b) -- ([shift={(4, 0)}] b.center) node[
qubit] {$\ket{0}$};

		\draw[wire]
			($([shift={(1,0)}] a)!0.5!([shift={(3,0)}] a)$) --
			($([shift={(1,0)}] b)!0.5!([shift={(3,0)}] b)$)
		;

		\draw[wireijoin]
			($([shift={(1,0)}] b)!0.5!([shift={(3,0)}] b)$)
			circle[radius=0.1] coordinate(dot)
		;

		\qubox{a}{1}{a}{3}{CNOT}
	\end{tikzpicture}
	\end{center}
\end{solution}

\vfill
\pagebreak











As we noted before, quantum gates don't \textit{consume} bits, they \textit{transform} them. \par
Thus, quantum circuits are drawn with a fixed set of bits, whose states change with time: \par
\note[Note]{
	In this diagram, CNOT and SWAP are drawn as $\oplus$ and \rotatebox[origin=c]{90}{$\leftrightarrows$} to save space.
}

\begin{center}
\begin{tikzpicture}[scale=1]
	\node[qubit] (a) at (0, 0) {$\ket{a}$};
	\node[qubit] (b) at (0, -1) {$\ket{1}$};
	\node[qubit] (c) at (0, -2) {$\ket{c}$};
	\node[qubit] (d) at (0, -3) {$\ket{d}$};
	\node[qubit] (e) at (0, -4) {$\ket{1}$};

	\draw[wire] (a) -- ([shift={(7, 0)}] a.center) node[qubit] {$\ket{0}$};
	\draw[wire] (b) -- ([shift={(7, 0)}] b.center) node[qubit] {$\ket{b}$};
	\draw[wire] (c) -- ([shift={(7, 0)}] c.center) node[qubit] {$\ket{1}$};
	\draw[wire] (d) -- ([shift={(7, 0)}] d.center) node[qubit] {$\ket{0}$};
	\draw[wire] (e) -- ([shift={(7, 0)}] e.center) node[qubit] {$\ket{e}$};


	\draw[wire]
		($([shift={(1,0)}] a)!0.5!([shift={(2,0)}] a)$) --
		($([shift={(1,0)}] c)!0.5!([shift={(2,0)}] c)$)
	;
	\draw[wirejoin]
		($([shift={(1,0)}] b)!0.5!([shift={(2,0)}] b)$)
		circle[radius=0.1] coordinate(dot)
	;
	\draw[wireijoin]
		($([shift={(1,0)}] c)!0.5!([shift={(2,0)}] c)$)
		circle[radius=0.1] coordinate(dot)
	;
	\qubox{a}{1}{a}{2}{T}

	\qubox{a}{4}{a}{5}{X}

	\draw[wire]
		($([shift={(2,0)}] e)!0.5!([shift={(3,0)}] e)$) --
		($([shift={(2,0)}] d)!0.5!([shift={(3,0)}] d)$)
	;
	\draw[wireijoin]
		($([shift={(2,0)}] d)!0.5!([shift={(3,0)}] d)$)
		circle[radius=0.1] coordinate(dot)
	;
	\qubox{e}{2}{e}{3}{$\oplus$}

	\qubox{b}{3}{c}{4}{\rotatebox[origin=c]{90}{$\leftrightarrows$}}


	\draw[wire]
		($([shift={(5,0)}] a)!0.5!([shift={(6,0)}] a)$) --
		($([shift={(5,0)}] c)!0.5!([shift={(6,0)}] c)$)
	;
	\draw[wirejoin]
		($([shift={(5,0)}] a)!0.5!([shift={(6,0)}] a)$)
		circle[radius=0.1] coordinate(dot)
	;
	\draw[wirejoin]
		($([shift={(5,0)}] c)!0.5!([shift={(6,0)}] c)$)
		circle[radius=0.1] coordinate(dot)
	;
	\qubox{b}{5}{b}{6}{T}
\end{tikzpicture}
\end{center}

In a quantum circuit, the ONLY way two bits can interact is through a gate. \par
We cannot add \say{branches} in quantum circuits like we do in classical circuits:

\begin{center}
\begin{tikzpicture}[circuit logic US, scale=1.2]
	\node[xor gate] (xor) at (0, 0) {\tiny\texttt{xor}};
	\draw (xor.input 1) ++(-0.5, 0) coordinate (start);
	\draw (xor.input 1) ++(-0.25, 0) coordinate (startjoin);
	\draw (xor.input 1) -- (xor.input 1 -| start) node[left] {$x$};
	\draw (xor.input 2) -| (0,-0.25 -| startjoin) |- (0,-0.25, -| start) node[left] {$y$};
	\filldraw (0,-0.25, -| startjoin) circle[radius=0.3mm] coordinate(dot);
	\draw (dot) -- (dot -| 1,0) node[right] {$y_\text{out}$};
	\draw (xor.output) -- (xor.output -| 1,0) node[right] {$x_\text{out}$};

	\draw[->, line width = 1, ogrape]
		([shift={(0.3,-0.5)}] dot) node[right] {This is a branch}
		-| ([shift={(0,-0.2)}] dot)
	;
\end{tikzpicture}
\end{center}














\problem{}
Find the values of $\ket{a}$ through $\ket{e}$ in the above circuit.

\begin{solution}
	\begin{center}
	\begin{tikzpicture}[scale=1]
		\node[qubit] (a) at (0, 0) {$\ket{1}$};
		\node[qubit] (b) at (0, -1) {$\ket{1}$};
		\node[qubit] (c) at (0, -2) {$\ket{1}$};
		\node[qubit] (d) at (0, -3) {$\ket{0}$};
		\node[qubit] (e) at (0, -4) {$\ket{1}$};

		\draw[wire] (a) -- ([shift={(7, 0)}] a.center) node[
qubit] {$\ket{0}$};
		\draw[wire] (b) -- ([shift={(7, 0)}] b.center) node[
qubit] {$\ket{1}$};
		\draw[wire] (c) -- ([shift={(7, 0)}] c.center) node[
qubit] {$\ket{1}$};
		\draw[wire] (d) -- ([shift={(7, 0)}] d.center) node[
qubit] {$\ket{0}$};
		\draw[wire] (e) -- ([shift={(7, 0)}] e.center) node[
qubit] {$\ket{0}$};


		\draw[wire]
			($([shift={(1,0)}] a)!0.5!([shift={(2,0)}] a)$) --
			($([shift={(1,0)}] c)!0.5!([shift={(2,0)}] c)$)
		;
		\draw[wirejoin]
			($([shift={(1,0)}] b)!0.5!([shift={(2,0)}] b)$)
			circle[radius=0.1] coordinate(dot)
		;
		\draw[wireijoin]
			($([shift={(1,0)}] c)!0.5!([shift={(2,0)}] c)$)
			circle[radius=0.1] coordinate(dot)
		;
		\qubox{a}{1}{a}{2}{T}

		\qubox{a}{4}{a}{5}{X}

		\draw[wire]
			($([shift={(2,0)}] e)!0.5!([shift={(3,0)}] e)$) --
			($([shift={(2,0)}] d)!0.5!([shift={(3,0)}] d)$)
		;
		\draw[wireijoin]
			($([shift={(2,0)}] d)!0.5!([shift={(3,0)}] d)$)
			circle[radius=0.1] coordinate(dot)
		;
		\qubox{e}{2}{e}{3}{$\oplus$}

		\qubox{b}{3}{c}{4}{\rotatebox[origin=c]{90}{$\leftrightarrows$}}


		\draw[wire]
			($([shift={(5,0)}] a)!0.5!([shift={(6,0)}] a)$) --
			($([shift={(5,0)}] c)!0.5!([shift={(6,0)}] c)$)
		;
		\draw[wirejoin]
			($([shift={(5,0)}] a)!0.5!([shift={(6,0)}] a)$)
			circle[radius=0.1] coordinate(dot)
		;
		\draw[wirejoin]
			($([shift={(5,0)}] c)!0.5!([shift={(6,0)}] c)$)
			circle[radius=0.1] coordinate(dot)
		;
		\qubox{b}{5}{b}{6}{T}
	\end{tikzpicture}
	\end{center}
\end{solution}


\vfill
\pagebreak