\section{Logic Gates} Now that we know how to write vectored bits, let's look at the ways we can change them. \generic{Remark:} A few weeks ago, we talked about matrices. Recall that every linear map may be written as a matrix, and that every matrix represents a linear map. For example, if $f: \mathbb{R}^2 \to \mathbb{R}^2$ is a linear map, we can write it as follows: \begin{equation*} f\left( \ket{x} \right) = \begin{bmatrix} m_1 & m_2 \\ m_3 & m_4 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \left[ \begin{matrix} m_1x_1 + m_2x_2 \\ m_3x_1 + m_4x_2 \end{matrix} \right] \end{equation*} \problem{} The \say{not} gate is a map from $\mathbb{B}$ to $\mathbb{B}$ defined by the following table: \begin{itemize} \item $\text{not}(\texttt{1}) = \texttt{0}$ \item $\text{not}(\texttt{0}) = \texttt{1}$ \end{itemize} Write the not gate as a matrix that operates on single-bit vector states. \par That is, find a matrix $N$ so that $N\ket{0} = \ket{1}$ and $N\ket{1} = \ket{0}$. \begin{solution} \begin{equation*} N = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \end{equation*} \end{solution} \vfill \problem{} The \say{and} gate is a map $\mathbb{B}^2 \to \mathbb{B}$ defined by the following table: \begin{center} \begin{tabular}{ c | c | c } \hline \texttt{a} & \texttt{b} & \texttt{a} and \texttt{b} \\ \hline 0 & 0 & 0 \\ 0 & 1 & 0 \\ 1 & 0 & 0 \\ 1 & 1 & 1 \end{tabular} \end{center} Find a matrix $A$ so that $A\ket{\texttt{ab}}$ works as expected. \par \hint{ What is the dimension of the input? What is the dimension of the desired output? \\ What do these two values tell us about the dimension of the matrix $A$? } \begin{solution} \begin{equation*} A = \begin{bmatrix} 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ \end{bmatrix} \end{equation*} \end{solution} \vfill \pagebreak \generic{Remark:} The way a quantum computer handles information is a bit different than the way a classical computer does. For spooky physics reasons, all quantum gates must be invertible. Naturally, this implies that the usual logic gates we use (and, or, xor) aren't valid quantum gates: each of these takes two inputs (four states) and produces one output (two states), losing information by mapping many inputs to the same output. \par \note[Note]{The \say{not} gate is fine. It is its own inverse, after all!} \vspace{2mm} Although we are still using classical bits, we'll design our gates to be reversible. \par One consequence of the \say{reversibility} rule is that all quantum gate matrices must be square. \par (i.e, they must take the same number of inputs and outputs.) \par \note[Note]{All invertible matrices are square, but not all square matrices are invertible.} \vspace{2mm} This is fairly intuitive: if we have more inputs than we have outputs, we inevitably lose information. \vspace{2mm} There's also a better way to think about this: rather than seeing quantum gates as \textit{functions} that consume one set of bits and produce another, it's better to think of them as \textit{transformations} we apply to an existing set of bits. \problem{} For example, take 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: \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 X'd 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 are \textit{transformations,} modifying a set of bits. \par Thus, quantum circuits are drawn with a fixed set of bits, whose states transform 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