---
title: "Quantum Gates"
format:
  html:
    toc: true
    number-sections: true

execute:
  echo: false
---

::: {.hidden}
$$
\newcommand{\braket}[2]{\langle{#1}|{#2}\rangle}
$$
:::
[Pre-class notes](HandWrittenNotes/GatesPre.pdf)

[In-class notes](HandWrittenNotes/Gates2.pdf)



## Learning Goals

- Describe quantum gate using ket notation
- Describe sufficient and necessary properties of quantum gates
- Apply ket formalism for gates to analyze novel situations

## Quantum Gates

We have previously seen examples of quantum *gates* (which are also sometimes called *operations* or *unitaries,*):

Beamsplitter:
$$
\begin{align}
\ket{0}_P\ket{H}_D&\rightarrow\ket{0}_P\ket{H}_D\\
\ket{0}_P\ket{V}_D&\rightarrow\ket{0}_P\ket{V}_D\\
\ket{1}_P\ket{H}_D&\rightarrow\ket{1}_P\ket{V}_D\\
\ket{1}_P\ket{V}_D&\rightarrow\ket{1}_P\ket{H}_D
\end{align}
$$

$\theta$-Waveplate:
$$
\begin{align}
\ket{0}_P&\rightarrow\cos(\theta)\ket{0}_P+\sin(\theta)\ket{1}_P\\
\ket{1}_P&\rightarrow\sin(\theta)\ket{0}_P-\cos(\theta)\ket{1}_P
\end{align}
$$

To describe what the gate does, we have listed how it transforms each standard basis state. 

### Properties of Gates

:::{#def-gate1}
A quantum transformation is a **gate** if and only if

- It takes any quantum state to another quantum state
- It is reversible. (This means that for every gate there is a reversing gate that can undo the action of the original gate.)
:::


::: {.callout-tip appearance="simple"}
### Group Exercises

Are the following gates? Why or why not (based on the definition of gates)?

- A vertically polarized filter

- The transformation
$$
\begin{align}
\ket{0}&\rightarrow\ket{0}+\ket{1}\\
\ket{1}&\rightarrow\ket{0}-\ket{1}
\end{align}
$$

:::

Alternative definitions:



:::{#def-gate3}
A quantum transformation is a **gate** if and only if it it takes an orthonormal set of states to an orthonormal set of states
:::

:::{#def-gate2}
A quantum transformation is a **gate** if and only if it is a unitary operation.
:::

- Using  @def-gate3, the following is a gate:

$$
\begin{align}
\ket{00}&\rightarrow\ket{\psi_1}\\
\ket{01}&\rightarrow\ket{\psi_2}\\
\ket{10}&\rightarrow\ket{\psi_3}\\
\ket{11}&\rightarrow\ket{\psi_4}\\
\end{align}
$$
as long as $\braket{\psi_i}{\psi_j}=\delta_{ij}$ where $\delta_{ij}$ is the Kronecker delta:
$$
\delta_{ij}=
\begin{cases}
1 & \textrm{if }i=j\\
0 & \textrm{else }
\end{cases}
$$

- From @def-gate2, a quantum gate is a multidimensional generalization of reflection and rotation transformations. Note that a rotation and reflection are reversible because you can rotate back in the opposite direction, or reflect again to recover the original state.

::: {.callout-note}

## Vector Note

Gates are represented with vector notation as matrices. We apply matrix multiplication between the operation matrix and the state vector and the result is another vector state. Any unitary matrix is a valid quantum gate. A unitary matrix $U$ has the property that $UU^\dagger=I$.
::: 

### Famous Gates

There are 4 single qubit gates called the Paulis:

$I$:
$$
\begin{align}
\ket{0}&\rightarrow\ket{0}\\
\ket{1}&\rightarrow\ket{1}
\end{align}
$$

$X$:
$$
\begin{align}
\ket{0}&\rightarrow\ket{1}\\
\ket{1}&\rightarrow\ket{0}
\end{align}
$$

$Y$:
$$
\begin{align}
\ket{0}&\rightarrow i\ket{1}\\
\ket{1}&\rightarrow-i\ket{0}
\end{align}
$$

$Z$:
$$
\begin{align}
\ket{0}&\rightarrow\ket{0}\\
\ket{1}&\rightarrow -\ket{1}
\end{align}
$$

The Hadamard gate transforms from the standard to Hadamard basis:
$$
\begin{align}
\ket{0}&\rightarrow\ket{+}\\
\ket{1}&\rightarrow \ket{-}
\end{align}
$$

The primary two-qubit gate you will need to know for this class is CNOT:
$$
\begin{align}
\ket{00}&\rightarrow\ket{00}\\
\ket{01}&\rightarrow\ket{01}\\
\ket{10}&\rightarrow\ket{11}\\
\ket{11}&\rightarrow\ket{10}
\end{align}
$$
In fact, CNOT is the same as the beamsplitter, if we label $\ket{H}$ as $\ket{0}$ and $\ket{V}$ as $\ket{1}.$ It is called CNOT, which is short for "controlled-not" because it applies a NOT gate to the second qubit, based on or "controlled by" the value of the first qubit. In particular, if the first qubit is $\ket{0}$, then the value of the second qubit doesn't change. But if the first qubit is $\ket{1}$, then the $X$ gate is applied. The $X$ gate exchanges $\ket{0}$ and $\ket{1}$, just like the Boolean not gate.

::: {.callout-important}

Gates apply left to right (like matrix multiplication).

In other words, if we want to apply the gate $Y$ to $\ket{0}$ followed by $H$, we would right this as $HY\ket{0}.$ We would evaluate it as

$$
HY\ket{0}=H(Y\ket{0})=H(i\ket{1})=iH\ket{1}=i\ket{-}
$$

::: 



::: {.callout-tip}

## Example

What is $H\ket{+}$?

I have only told you how $H$ acts on $\ket{0}$ and $\ket{-}$, so how do we figure out how $H$ acts on a superposition state?

First, write $\ket{+}$ in terms of the states that we know how $H$ applies:

$$
H\ket{+}=H\left(\frac{1}{\sqrt{2}}\ket{0}+\frac{1}{\sqrt{2}}\ket{1}\right).
$$
Then, just like matrix multiplication, we can distribute the $H$, and pull any scalars out front:
$$
\frac{1}{\sqrt{2}}H\ket{0}+\frac{1}{\sqrt{2}}H\ket{1}.
$$
And finally, we have $H$ acting on standard basis states, so we can use our expressions for $H$:
$$
\frac{1}{\sqrt{2}}\ket{+}+\frac{1}{\sqrt{2}}\ket{-}.
$$
Next we can write $\ket{+}$ and $\ket{-}$ in terms of standard basis states:
$$
\frac{1}{\sqrt{2}}\left(\frac{1}{\sqrt{2}}\ket{0}+\frac{1}{\sqrt{2}}\ket{1}\right)+\frac{1}{\sqrt{2}}\left(\frac{1}{\sqrt{2}}\ket{0}-\frac{1}{\sqrt{2}}\ket{1}\right)=\frac{1}{2}\ket{0}+\frac{1}{2}\ket{1}+\frac{1}{2}\ket{0}-\frac{1}{2}\ket{1}=\ket{0}
$$

:::

## Single Qubit Gates on 2-Qubit States

If we have a two-qubit state $\ket{\psi}_{AB}$ and we want to apply a single-qubit gate $U_A$ to $A$, and a single qubit gate $U_B$ to $B$, 
we write the effective gate on the entire system as 
$$ U_A\otimes U_B.$$

There is one important rule for this situation. Because the gate $U_B$ only acts on the $B$ system, when it encounters an $A$ ket, it doesn't effect it, and can commute through it. This is perhaps easiest seen with an example:


::: {.callout-tip}

## Example

As in the CHSH game, suppose Amir and Bei have the state
$$\ket{\psi}_{AB}=\frac{1}{\sqrt{2}}\ket{00}+\frac{1}{\sqrt{2}}\ket{11},
$$
and that Bei applies the $Z$ gate to his qubit, while Amir doesn't do anything to her qubit. What happens to their state?

First we need to write the effective 2-qubit gate. Even though Amir doesn't do anything, we can still describe his action as a unitary gate - the identity $I$. So the effective 2-qubit gate on their state is $$I_A\otimes Z_B.$$ 

So the state transforms as 
$$
(I_A\otimes Z_B)\left(\frac{1}{\sqrt{2}}\ket{00}_{AB}+\frac{1}{\sqrt{2}}\ket{11}_{AB}\right)=\frac{1}{\sqrt{2}}I_A\otimes Z_B\ket{00}_{AB}+\frac{1}{\sqrt{2}}I_A\otimes Z_B\ket{11}_{AB},
$$
where we have first distributed the gate to the two superposition states. Next, we'll expand the $AB$ kets into an explicit tensor product of kets:
$$
\frac{1}{\sqrt{2}}I_A\otimes Z_B\ket{0}_{A}\otimes\ket{0}_{B}+\frac{1}{\sqrt{2}}I_A\otimes Z_B\ket{1}_{A}\otimes\ket{1}_{B}.
$$
Now as described above, because the $B$ unitary doesn't act on the $A$ part of the system, we can commute it through the $A$ ket, so the expression becomes:
$$
\frac{1}{\sqrt{2}}(I_A\ket{0}_{A})\otimes (Z_B\ket{0}_{B})+\frac{1}{\sqrt{2}}(I_A\ket{1}_{A})\otimes(Z_B\ket{1}_{B}).
$$
Now in the above expression we have a single qubit unitary $I_A$ acting on a single qubit state ($A$ ket), and a single qubit unitary $Z_B$ acting on a single qubit state ($B$ ket). We can use our usual rules for how these gates transform single qubit states to get:
$$
\frac{1}{\sqrt{2}}(\ket{0}_{A})\otimes (\ket{0}_{B})+\frac{1}{\sqrt{2}}(\ket{1}_{A})\otimes(-\ket{1}_{B})=\frac{1}{\sqrt{2}}\ket{00}_{AB}-\frac{1}{\sqrt{2}}\ket{11}_{AB}
$$

:::


::: {.callout-tip appearance="simple"}
### Group Exercise
As in the CHSH game, suppose Amir and Bei have the state
$$\ket{\psi}_{AB}=\frac{1}{\sqrt{2}}\ket{00}+\frac{1}{\sqrt{2}}\ket{11}.
$$ What Pauli gates should they each apply to create the state
$$\ket{\psi}_{AB}=\frac{1}{\sqrt{2}}\ket{01}-\frac{1}{\sqrt{2}}\ket{10}?
$$

Write this effective 2-qubit gate as a transformation of standard basis states:
$$
\begin{align}
\ket{00}&\rightarrow ??\\
\ket{01}&\rightarrow ??\\
\ket{10}&\rightarrow ??\\
\ket{11}&\rightarrow ??
\end{align}
$$



:::

