\section{Logic Gates}

\definition{Matrices}
Throughout this handout, we've been using matrices. Again, 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*}


\definition{}
Before we discussing multi-qubit quantum gates, we need to review to classical logic. \par
Of course, a classical logic gate is a linear map from $\{0,1\}^m$ to $\{0,1\}^n$


\problem{}<notgatex>
The \texttt{not} gate is a map defined by the following table: \par

\begin{itemize}
	\item $X\ket{0} = \ket{1}$
	\item $X\ket{1} = \ket{0}$
\end{itemize}

Write the \texttt{not} gate as a matrix that operates on single-bit vector states. \par
That is, find a matrix $X$ so that
$
	X\left[\begin{smallmatrix} 1 \\ 0 \end{smallmatrix}\right]
	= \left[\begin{smallmatrix} 0 \\ 1 \end{smallmatrix}\right]
$
and
$
	X\left[\begin{smallmatrix} 0 \\ 1 \end{smallmatrix}\right]
	= \left[\begin{smallmatrix} 1 \\ 0 \end{smallmatrix}\right]
$ \par

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


\vfill


\problem{}
The \texttt{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{Remember, we write bits as vectors.}


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

	\begin{instructornote}
		Because of the way we represent bits here, we also have the following property: \par
		The columns of $A$ correspond to the output for each input---i.e, $A$ is just a table of outputs. \par

		\vspace{2mm}
		For example, if we look at the first column of $A$ (which is $[1, 0]$), we see: \par
		$A\ket{00} = A[1,0,0,0] = [1,0] = \ket{0}$

		\vspace{2mm}
		Also with the last column (which is $[0,1]$): \par
		$A\ket{00} = A[0,0,0,1] = [0,1] = \ket{1}$
	\end{instructornote}
\end{solution}


\vfill
\pagebreak

\generic{Remark:}
The way a quantum circuit handles information is a bit different than the way a classical circuit does.
We usually think of logic gates as \textit{functions}: they consume one set of bits, and return another:


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


This model, however, won't work for quantum logic. If we want to understand quantum gates, we need to see them
not as \textit{functions}, but as \textit{transformations}. This distinction is subtle, but significant:
\begin{itemize}
	\item functions \textit{consume} a set of inputs and \textit{produce} a set of outputs
	\item transformations \textit{change} a set of objects, without adding or removing any elements
\end{itemize}

\vspace{2mm}

Our usual logic circuit notation models logic gates as functions---we thus can't use it. \par
We'll need a different diagram to draw quantum circuits. \par

\vfill


First, we'll need a set of bits. For this example, we'll use two, drawn in a vertical array. \par
We'll also add a horizontal time axis, moving from left to right:

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

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

	\draw[
		color = oblue,
		->>,
		line width = 0.5mm
	] (-1,-1.5) -- (5, -1.5);

	\node[fill=white, text=oblue] at (2, -1.5) {\texttt{time axis}};


	\node[left, gray] at (-1, -0.5) {State of each bit at start};
	\node[right, gray] at (5, -0.5) {State of each bit at end};

	\draw[
		->,
		color = gray,
		line width = 0.2mm,
		rounded corners = 2mm
	]
		(-1, -0.5) -- (-0.8, -0.5) -- (-0.8, 0) --(a)
	;
	\draw[
		->,
		color = gray,
		line width = 0.2mm,
		rounded corners = 2mm
	]
		(-1, -0.5) -- (-0.8, -0.5) -- (-0.8, -1) -- (b)
	;

	\draw[
		<-,
		color = gray,
		line width = 0.2mm,
		rounded corners = 2mm
	]
		(4.2, 0) -- (4.8, 0) -- (4.8, -0.5) -- (5, -0.5)
	;
	\draw[
		<-,
		color = gray,
		line width = 0.2mm,
		rounded corners = 2mm
	]
		(4.2, -1) -- (4.8, -1) -- (4.8, -0.5) -- (5, -0.5)
	;

	\end{tikzpicture}
\end{center}

In the diagram above, we didn't change our bits---so the labels at the start match those at the end.

\vfill

Thus, our circuit forms a grid, with bits ordered vertically and time horizontally. \par
If we want to change our state, we draw transformations as vertical boxes. \par
Every column represents a single transformation on the entire state:

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

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

	\ghostqubox{a}{1}{b}{2}{$T_1$}
	\ghostqubox{a}{2}{b}{3}{$T_2$}
	\ghostqubox{a}{3}{b}{4}{$T_3$}

\end{tikzpicture}
\end{center}

Note that the transformations above span the whole state. This is important: \par
we cannot apply transformations to individual bits---we always transform the \textit{entire} state.


\vfill
\pagebreak

\generic{Setup:}
Say we want to invert the first bit of a two-bit state. That is, we want a transformation $T$ so that \par

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

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

	\qubox{a}{1.5}{b}{2.5}{$T$}
\end{tikzpicture}
\end{center}

In other words, we want a matrix $T$ satisfying the following equalities:
\begin{itemize}
	\item $T\ket{00} = \ket{10}$
	\item $T\ket{01} = \ket{11}$
	\item $T\ket{10} = \ket{00}$
	\item $T\ket{11} = \ket{01}$
\end{itemize}



\problem{}
Find the matrix that corresponds to the above transformation. \par
\hint{
	Remember that
	$\ket{0} = \left[\begin{smallmatrix} 1 \\ 0 \end{smallmatrix}\right]$ and
	$\ket{1} = \left[\begin{smallmatrix} 0 \\ 1 \end{smallmatrix}\right]$ \\
	Also, we found earlier that $X = \left[\begin{smallmatrix} 0 && 1 \\ 1 && 0 \end{smallmatrix}\right]$,
	and of course $I = \left[\begin{smallmatrix} 1 && 0 \\ 0 && 1 \end{smallmatrix}\right]$.
}

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

\vfill

\generic{Remark:}
We could draw the above transformation as a combination $X$ and $I$ (identity) gate:
\begin{center}
\begin{tikzpicture}[scale=0.8]
	\node[qubit] (a) at (0, 0) {$\ket{0}$};
	\node[qubit] (b) at (0, -1) {$\ket{0}$};

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

	\qubox{a}{1}{a}{2}{$X$}
	\qubox{b}{1}{b}{2}{$I$}
\end{tikzpicture}
\end{center}

We can even omit the $I$ gate, since we now know that transformations affect the whole state: \par
\begin{center}
\begin{tikzpicture}[scale=0.8]
	\node[qubit] (a) at (0, 0) {$\ket{0}$};
	\node[qubit] (b) at (0, -1) {$\ket{0}$};

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

	\qubox{a}{1}{a}{2}{$X$}
\end{tikzpicture}
\end{center}
We're now done: this is how we draw quantum circuits.
Don't forget that transformations \textit{always} affect the whole state---even if our diagram doesn't explicitly state this.

\pagebreak

% TODO:
% distributive property of tensor product
% quantum gate algebra