Having laid out some mathematical foundations in the previous part , we will proceed to discussing quantum gates and circuits in depth here. As usual, we will drive home the theories with worked examples from (Rieffel, E. G. & Polak, W. H., 2014) and check their accuracies with qiskit and/or cirq code.

Understanding quantum gates and circuits will be greatly aided by the knowledge of classical gates (AND, OR, NOT, XOR and the universal gates: NAND and NOR). Also, some familiarity with the Python programming language will help you understand qiskit and/or cirq code.

A classical computer, possibly the one you're reading this with, is a sophisticated engineering piece, no doubt. However, the underlying "magic" comprises logic gates. The National Institute of Standards and Technology (NIST) gives this incredible analogical description of classical computers in relation to logic gates (Quantum Logic Gates., 2018):

Traditional computers are like microscopic cities. The roads of these cities are wires with electricity coursing through them. These roads have lots of gates, known as logic gates, which enable computers to do their job. Like physical gates that allow or block cars, logic gates allow or block electricity. Electricity that goes through the gates represents a “1” of digital data, and blocked electricity is a “0.”

When you pick up a motherboard, for instance, you may not see the said gates as they are made of tiny transistors (in modern systems, MOSFETs) in specific combinations. Also, the 00 and 11 referred to in the analogy mean low and high voltage ranges, respectively. Their actual voltage values depend on the hardware.

All schematics were written in and rendered by @schemd/core . Check it out.

Since we will be realizing some classical circuits using quantum gates, it's necessary to get familiar with common logic gates.

NOT (¬\neg) Gate

This gate takes a single classical bit and flips it. For instance, if 00 is the input to this gate, 11 will be seen in the output, vice versa. Just like a flip of a coin. Below, you find both the IEEE symbol of the gate and its truth table:

in out
0 1
1 0
Classical NOT gate1 component and 0 connections.G1NOT
Classical NOT gate

AND (\land) Gate

Borrowing NIST's analogy again (Quantum Logic Gates., 2018):

One kind of logic gate, known as the AND gate, could, for example, quickly determine whether two people agree to a business deal. It takes in two bits of information, and generates a 1 if both incoming bits are 1s. So, if both business people say “yes” (1) to the deal, the AND gate will output 1. If one or both say “no” (0), the AND gate generates a 0 or a no.

From the analogy, the gate usually takes 2 bits (2 people who want to deal). It has the truth table and symbol shown below.

in0in_0 in1in_1 outout
0 0 0
1 0 0
0 1 0
1 1 1
Classical AND gate1 component and 0 connections.G1AND
Classical AND gate

OR (\lor) Gate

This logic gate also takes 2 bits and functions based on the analogy that if two founders are deciding whether to launch a product, OR says “launch if at least one says yes.” Only when both say no does the result become no. Its symbol and truth table are as follows:

in0in_0 in1in_1 outout
0 0 0
1 0 1
0 1 1
1 1 1
Classical OR Gate1 component and 0 connections.G1OR
Classical OR Gate

XOR (\oplus) Gate

Exclusive OR (XOR) gate is a core part of an adder circuit and, in algorithms, we use it for bitwise manipulation in problems including finding a number that does not have a duplicate in an array. For a 2-bit input, its output is 11 if and only if the bits are of opposite values. Its truth table sheds more light on this:

in0in_0 in1in_1 outout
0 0 0
1 0 1
0 1 1
1 1 0

XOR gate's symbol is as follows:

Classical XOR gate1 component and 0 connections.G1XOR
Classical XOR gate

NAND

NAND (Not AND) is one of the universal gates in digital circuits; the other is NOR (Not OR). It is universal because, with only this single gate, all other gates and circuits can be designed. Its behavior is simply as its name sounds: invert the output of an AND gate!

in0in_0 in1in_1 outout
0 0 1
1 0 1
0 1 1
1 1 0

NAND gate's symbol is as follows:

Classical NAND gate1 component and 0 connections.G1NAND
Classical NAND gate

NOR

NOR does the same thing to an OR gate: it inverts its output. Therefore, it only returns 11 when both inputs are 00. Like NAND, it is universal.

in0in_0 in1in_1 outout
0 0 1
1 0 0
0 1 0
1 1 0
Classical NOR gate1 component and 0 connections.G1NOR
Classical NOR gate

Just as classical gates are the foundations of classical computers, quantum gates are those of quantum computers. They make it possible for quantum algorithms to run. The fundamental difference between them is that classical gates work on bits whose state is either 00 or 11 (and not both at the same time), whereas quantum gates operate on qubits where 00 and 11 can be superposed and even entangled (this will be discussed in the next part).

Quantum gates can either be single-qubit or multiple-qubit gates (Gopalan College of Engineering & Management., 2022). As with almost anything in quantum computing, these gates can be mathematically expressed as matrices, and we will explore this heavily to understand how they work. One thing to keep in mind is that their matrices must be unitary. As discussed in the first part of this series, it means their inverses are the same as their Hermitian (conjugate transpose). It also means that the gate operations are reversible.

We will explore the X-, Y-, Z-, H-, S- and T- gates as examples of single-qubit gates. Though there are many multiple-qubit gates, we will only discuss the CNOT gate here. The reason is that the set of H-, CNOT, and T- gates is universal and can approximate any quantum computation to arbitrary precision (MathWorks., n.d.). In simple terms, they can get as close as needed to any quantum operation.

X-Gate

This is the quantum equivalent of the classical NOT gate. In the quantum world, it is called the Pauli X-gate. Just like its classical counterpart, this gate flips its incoming qubit. If 0\ket{0} is the input, 1\ket{1} will be the output, vice versa. Mathematically, this gate can be expressed, in matrix form, as:

X=[0110]X= \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}

Now, let's look at the behaviour of this gate on a state, ψ\ket{\psi}.

If

ψ=α0+β1\ket{\psi} = \alpha\ket{0} + \beta\ket{1}

Then, in matrix form,

ψ=[αβ]\ket{\psi} = \begin{bmatrix}\alpha \\ \beta\end{bmatrix}

Feeding this into the Pauli X-gate, we have:

Xψ=[0110][αβ]=[0×α+1×β1×α+0×β]=[βα]=β0+α1X\ket{\psi} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \begin{bmatrix} 0\times\alpha + 1\times\beta \\ 1\times\alpha + 0\times\beta \end{bmatrix} = \begin{bmatrix}\beta \\ \alpha\end{bmatrix} = \beta\ket{0} + \alpha\ket{1}

It got flipped!

Action of Pauli X on an arbitrary qubit3 components and 2 connections.A∣ψ⟩=α∣0⟩+β∣1⟩XXGXBβ∣0⟩+α∣1⟩
Action of Pauli X on an arbitrary qubit

Therefore, it can be demonstrated that if we feed 0\ket{0} ([10]\begin{bmatrix}1 \\ 0\end{bmatrix} in matrix form) into an X-gate, we will have:

X0=[0110][10]=[0×1+1×01×1+0×0]=[01]=1X\ket{0} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix}1 \\ 0\end{bmatrix} = \begin{bmatrix} 0\times1 + 1\times0 \\ 1\times1 + 0\times0 \end{bmatrix} = \begin{bmatrix}0 \\ 1\end{bmatrix} = \ket{1}
Pauli X flips zero to one3 components and 2 connections.A∣0⟩XXGXB∣1⟩
Pauli X flips zero to one

In the same vein, feeding 1\ket{1} ([01]\begin{bmatrix}0 \\ 1\end{bmatrix} in matrix form) into the gate gives:

X1=[0110][01]=[0×0+1×11×0+0×1]=[10]=0X\ket{1} = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix}0 \\ 1\end{bmatrix} = \begin{bmatrix} 0\times0 + 1\times1 \\ 1\times0 + 0\times1 \end{bmatrix} = \begin{bmatrix}1 \\ 0\end{bmatrix} = \ket{0}

The truth table can easily be built from here:

Input Output
0\ket{0} 1\ket{1}
1\ket{1} 0\ket{0}

Y-Gate

The Pauli Y-gate also flips a qubit. However, unlike the X-gate, it adds a phase to the result. Its matrix is:

Y=[0ii0]Y = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix}

Let's feed the same arbitrary state, ψ\ket{\psi}, into it:

Yψ=[0ii0][αβ]=[iβiα]=iβ0+iα1Y\ket{\psi} = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \begin{bmatrix}-i\beta \\ i\alpha\end{bmatrix} = -i\beta\ket{0} + i\alpha\ket{1}

The amplitudes have been swapped, but they now carry the phases i-i and ii. From here, feeding 0\ket{0} into the gate gives:

Y0=[0ii0][10]=[0i]=i1Y\ket{0} = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \begin{bmatrix}1 \\ 0\end{bmatrix} = \begin{bmatrix}0 \\ i\end{bmatrix} = i\ket{1}

In the same vein, feeding 1\ket{1} into it gives:

Y1=[0ii0][01]=[i0]=i0Y\ket{1} = \begin{bmatrix} 0 & -i \\ i & 0 \end{bmatrix} \begin{bmatrix}0 \\ 1\end{bmatrix} = \begin{bmatrix}-i \\ 0\end{bmatrix} = -i\ket{0}

The truth table can be built from here:

Input Output
0\ket{0} i1i\ket{1}
1\ket{1} i0-i\ket{0}
α0+β1\alpha\ket{0}+\beta\ket{1} iβ0+iα1-i\beta\ket{0}+i\alpha\ket{1}
Action of Pauli Y on an arbitrary qubit3 components and 2 connections.A∣ψ⟩=α∣0⟩+β∣1⟩YYGYB-iβ∣0⟩+iα∣1⟩
Action of Pauli Y on an arbitrary qubit

You may notice that i1i\ket{1} and i0-i\ket{0} still give the same measurement probabilities as 1\ket{1} and 0\ket{0}, respectively, in the computational basis. This is because an overall phase does not affect the measurement probabilities. However, when the qubit is in a superposition, a phase on one part of the state matters. We will see this with the next gate.

Z-Gate

The Pauli Z-gate is also called the phase-flip gate. Unlike the X- and Y- gates, it does not swap the amplitudes. Instead, it changes the sign of the 1\ket{1} amplitude. Its matrix is:

Z=[1001]Z = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}

Feeding our arbitrary state into it, we have:

Zψ=[1001][αβ]=[αβ]=α0β1Z\ket{\psi} = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \begin{bmatrix}\alpha \\ -\beta\end{bmatrix} = \alpha\ket{0} - \beta\ket{1}

For the basis states:

Z0=0,Z1=1Z\ket{0}=\ket{0}, \qquad Z\ket{1}=-\ket{1}
Input Output
0\ket{0} 0\ket{0}
1\ket{1} 1-\ket{1}
α0+β1\alpha\ket{0}+\beta\ket{1} α0β1\alpha\ket{0}-\beta\ket{1}

You may ask: if 1\ket{1} and 1-\ket{1} produce the same measurement probability, what exactly changed? When 1\ket{1} is by itself, the minus sign is a global phase and cannot be observed. However, in a superposition, it changes the relative phase between the amplitudes. For example:

Z(0+12)=012Z\left(\frac{\ket{0}+\ket{1}}{\sqrt{2}}\right) = \frac{\ket{0}-\ket{1}}{\sqrt{2}}

Recall that 0+12=+\frac{\ket{0}+\ket{1}}{\sqrt{2}}=\ket{+} and 012=\frac{\ket{0}-\ket{1}}{\sqrt{2}}=\ket{-}. Therefore, Z+=Z\ket{+}=\ket{-}. Both states have a 50%50\% probability of returning 00 or 11 when measured in the computational basis. They are, however, different states and behave differently when another gate, such as H, acts on them.

Pauli Z changes the relative phase3 components and 2 connections.A∣+⟩ZZGZB∣-⟩
Pauli Z changes the relative phase

H-Gate

The Hadamard gate, commonly written as H, takes a basis state and puts it in an equal superposition. Its matrix is:

H=12[1111]H = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}

Let's first see its action on our arbitrary qubit:

Hψ=12[1111][αβ]=12[α+βαβ]H\ket{\psi} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix} \alpha+\beta \\ \alpha-\beta \end{bmatrix} Hψ=α+β20+αβ21H\ket{\psi} = \frac{\alpha+\beta}{\sqrt{2}}\ket{0} + \frac{\alpha-\beta}{\sqrt{2}}\ket{1}

Now, if 0\ket{0} is fed into the gate:

H0=12[1111][10]=12[11]=0+12=+H\ket{0} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix}1 \\ 0\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\ 1\end{bmatrix} = \frac{\ket{0}+\ket{1}}{\sqrt{2}} = \ket{+}

In the same vein:

H1=12[1111][01]=12[11]=012=H\ket{1} = \frac{1}{\sqrt{2}}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix}0 \\ 1\end{bmatrix} = \frac{1}{\sqrt{2}}\begin{bmatrix}1 \\ -1\end{bmatrix} = \frac{\ket{0}-\ket{1}}{\sqrt{2}} = \ket{-}
Hadamard creates an equal superposition3 components and 2 connections.A∣0⟩HHGHB∣+⟩
Hadamard creates an equal superposition

From H0=120+121H\ket{0}=\frac{1}{\sqrt{2}}\ket{0}+\frac{1}{\sqrt{2}}\ket{1}, the probabilities of measuring 0\ket{0} and 1\ket{1} are:

P0=122=12\mathcal{P}_{\ket{0}}=\left|\frac{1}{\sqrt{2}}\right|^2=\frac{1}{2}

and

P1=122=12\mathcal{P}_{\ket{1}}=\left|\frac{1}{\sqrt{2}}\right|^2=\frac{1}{2}

Before moving on, there is another interesting property of this gate. Applying H twice returns the original state. Let's confirm that from its matrix:

H2=12[1111][1111]=12[2002]=IH^2 = \frac{1}{2}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} = \frac{1}{2}\begin{bmatrix} 2 & 0 \\ 0 & 2 \end{bmatrix} = I

Therefore, H2=IH^2=I. This also means that H+=0H\ket{+}=\ket{0} and H=1H\ket{-}=\ket{1}. The gate takes us from the computational basis, {0,1}\{\ket{0},\ket{1}\}, to the Hadamard basis, {+,}\{\ket{+},\ket{-}\}, and vice versa.

S-Gate

The S-gate is another phase gate. It leaves 0\ket{0} unchanged but adds a phase of π2\frac{\pi}{2} to 1\ket{1}. Its matrix is:

S=[100i]S = \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix}

Applying it to our arbitrary state gives:

Sψ=[100i][αβ]=[αiβ]=α0+iβ1S\ket{\psi} = \begin{bmatrix} 1 & 0 \\ 0 & i \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \begin{bmatrix}\alpha \\ i\beta\end{bmatrix} = \alpha\ket{0}+i\beta\ket{1}

Recall from Euler's formula that eiπ2=ie^{i\frac{\pi}{2}}=i. Therefore:

S0=0,S1=i1=eiπ21S\ket{0}=\ket{0}, \qquad S\ket{1}=i\ket{1}=e^{i\frac{\pi}{2}}\ket{1}

The truth table can be built from here:

Input Output
0\ket{0} 0\ket{0}
1\ket{1} i1i\ket{1}
α0+β1\alpha\ket{0}+\beta\ket{1} α0+iβ1\alpha\ket{0}+i\beta\ket{1}
S gate adds a quarter-turn phase3 components and 2 connections.Aα∣0⟩+β∣1⟩Sπ/2SGSBα∣0⟩+iβ∣1⟩
S gate adds a quarter-turn phase

Also, applying S twice produces the Z-gate. From its matrix:

S2=[100i][100i]=[100i2]=[1001]=ZS^2 = \begin{bmatrix}1&0\\0&i\end{bmatrix} \begin{bmatrix}1&0\\0&i\end{bmatrix} = \begin{bmatrix}1&0\\0&i^2\end{bmatrix} = \begin{bmatrix}1&0\\0&-1\end{bmatrix} = Z

T-Gate

The T-gate works in a similar fashion to the S-gate. It is also called the π8\frac{\pi}{8} gate, though the relative phase it adds to 1\ket{1} is π4\frac{\pi}{4}. Its matrix is:

T=[100eiπ/4]T = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix}

Recall Euler's formula, eiθ=cosθ+isinθe^{i\theta}=\cos\theta+i\sin\theta. Substituting θ=π4\theta=\frac{\pi}{4}, we have:

eiπ/4=cosπ4+isinπ4=1+i2e^{i\pi/4} = \cos\frac{\pi}{4}+i\sin\frac{\pi}{4} = \frac{1+i}{\sqrt{2}}

Now, let's apply T to our arbitrary state:

Tψ=[100eiπ/4][αβ]=[αeiπ/4β]=α0+eiπ/4β1T\ket{\psi} = \begin{bmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{bmatrix} \begin{bmatrix}\alpha \\ \beta\end{bmatrix} = \begin{bmatrix}\alpha \\ e^{i\pi/4}\beta\end{bmatrix} = \alpha\ket{0}+e^{i\pi/4}\beta\ket{1}
Input Output
0\ket{0} 0\ket{0}
1\ket{1} eiπ/41e^{i\pi/4}\ket{1}
α0+β1\alpha\ket{0}+\beta\ket{1} α0+eiπ/4β1\alpha\ket{0}+e^{i\pi/4}\beta\ket{1}
T gate adds an eighth-turn phase3 components and 2 connections.Aα∣0⟩+β∣1⟩Tπ/4TGTBα∣0⟩+e(iπ/4)β∣1⟩
T gate adds an eighth-turn phase

From here, applying T twice should give S. Let's confirm this from its matrix:

T2=[100eiπ/4]2=[100eiπ/2]=[100i]=ST^2 = \begin{bmatrix}1&0\\0&e^{i\pi/4}\end{bmatrix}^2 = \begin{bmatrix}1&0\\0&e^{i\pi/2}\end{bmatrix} = \begin{bmatrix}1&0\\0&i\end{bmatrix} = S

CNOT Gate

All the gates discussed thus far take a single qubit. The Controlled NOT (CNOT or CX) gate takes two: a control and a target. If the control is 0\ket{0}, the target remains unchanged. However, if the control is 1\ket{1}, an X-gate is applied to the target. The control itself does not change.

If we represent the control with cc and the target with tt, the operation can be written as:

c,tc,tc\ket{c,t}\longmapsto\ket{c,t\oplus c}

where \oplus is XOR. Recall that XOR returns 11 only when its two inputs differ. In the computational basis ordered as {00,01,10,11}\{\ket{00},\ket{01},\ket{10},\ket{11}\}, the matrix of the gate is:

CNOT=[1000010000010010]\operatorname{CNOT} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{bmatrix}

Let's work through all four computational basis states:

  • 0000\ket{00}\rightarrow\ket{00}: the control is 00, so the target remains 00;
  • 0101\ket{01}\rightarrow\ket{01}: the control is 00, so the target remains 11;
  • 1011\ket{10}\rightarrow\ket{11}: the control is 11, so the target flips from 00 to 11; and
  • 1110\ket{11}\rightarrow\ket{10}: the control is 11, so the target flips from 11 to 00.
Input Output
00\ket{00} 00\ket{00}
01\ket{01} 01\ket{01}
10\ket{10} 11\ket{11}
11\ket{11} 10\ket{10}
Controlled NOT gate5 components and 4 connections.C0∣c⟩T0∣t⟩CXCNOTC1∣c⟩T1∣t⊕c⟩
Controlled NOT gate

Now, what happens if the control is in a superposition? Let's consider two qubits initialized to 00\ket{00}. First, we apply H to the first qubit while leaving the second unchanged:

(HI)00=00+102(H\otimes I)\ket{00} = \frac{\ket{00}+\ket{10}}{\sqrt{2}}

From here, we apply CNOT using the first qubit as the control:

CNOT(00+102)=00+112\operatorname{CNOT}\left(\frac{\ket{00}+\ket{10}}{\sqrt{2}}\right) = \frac{\ket{00}+\ket{11}}{\sqrt{2}}

From here, both qubits are in one of the Bell states. Notice that we can no longer separate it into an independent state for the first qubit and another for the second. This is regarded as entanglement. We will properly unpack it in the next part.

Design a circuit that rearranges three arbitrary single-qubit states in this manner:

ψϕηηψϕ\ket{\psi}\ket{\phi}\ket{\eta} \longmapsto \ket{\eta}\ket{\psi}\ket{\phi}

Solution

Given:

ψ1ϕ2η3η1ψ2ϕ3\ket{\psi}_1\ket{\phi}_2\ket{\eta}_3 \longrightarrow \ket{\eta}_1\ket{\psi}_2\ket{\phi}_3

where the subscripts represent the wire positions.

To solve this, let's compare the initial and final positions of the states:

State Initial wire Final wire
ψ\ket{\psi} 11 22
ϕ\ket{\phi} 22 33
η\ket{\eta} 33 11

From the table:

  • ψ:12\ket{\psi}: 1\rightarrow2
  • ϕ:23\ket{\phi}: 2\rightarrow3
  • η:31\ket{\eta}: 3\rightarrow1

Looking at the movements together, we see that they form a cycle. A SWAP gate exchanges the states in two positions, so we can complete this cycle with two of them.

First, swap the states in positions 22 and 33:

ψ1ϕ2η3SWAP2,3ψ1η2ϕ3\ket{\psi}_1\ket{\phi}_2\ket{\eta}_3 \xrightarrow{\operatorname{SWAP}_{2,3}} \ket{\psi}_1\ket{\eta}_2\ket{\phi}_3

The states are now in the order ψηϕ\ket{\psi}\ket{\eta}\ket{\phi}. We only need to move η\ket{\eta} to the first position. So, let's swap the states in positions 11 and 22:

ψ1η2ϕ3SWAP1,2η1ψ2ϕ3\ket{\psi}_1\ket{\eta}_2\ket{\phi}_3 \xrightarrow{\operatorname{SWAP}_{1,2}} \ket{\eta}_1\ket{\psi}_2\ket{\phi}_3

\therefore

ψϕηSWAP2,3ψηϕSWAP1,2ηψϕ\ket{\psi}\ket{\phi}\ket{\eta} \xrightarrow{\operatorname{SWAP}_{2,3}} \ket{\psi}\ket{\eta}\ket{\phi} \xrightarrow{\operatorname{SWAP}_{1,2}} \ket{\eta}\ket{\psi}\ket{\phi}

The required circuit is:

Quantum circuit for swapping |ψ⟩ |ϕ⟩ |η⟩ to |η⟩ |ψ⟩ |ϕ⟩12 components and 15 connections.S1ACXS1BCXS1CCXS2ACXS2BCXS2CCX
Quantum circuit for swapping |ψ⟩ |ϕ⟩ |η⟩ to |η⟩ |ψ⟩ |ϕ⟩

Before going to the check, let's show how a SWAP gate can be built with three CNOT gates. Suppose the first and second qubits contain the basis values aa and bb, respectively. The gates are applied in this order:

CNOT1,2,CNOT2,1,CNOT1,2\operatorname{CNOT}_{1,2},\qquad \operatorname{CNOT}_{2,1},\qquad \operatorname{CNOT}_{1,2}

The first subscript denotes the control qubit while the second denotes the target. Also, recall these properties of XOR:

aa=0,a0=aa\oplus a=0,\qquad a\oplus0=a

For the first CNOT, aa is the control. It remains unchanged while the target, bb, becomes aba\oplus b:

(a,b)CNOT1,2(a,ab)(a,b) \xrightarrow{\operatorname{CNOT}_{1,2}} (a,a\oplus b)

For the second CNOT, aba\oplus b is now the control and aa is the target. The new target is:

a(ab)=(aa)b=0b=b\begin{align*} a\oplus(a\oplus b) &=(a\oplus a)\oplus b\\ &=0\oplus b\\ &=b \end{align*}

Therefore:

(a,ab)CNOT2,1(b,ab)(a,a\oplus b) \xrightarrow{\operatorname{CNOT}_{2,1}} (b,a\oplus b)

For the last CNOT, bb is the control and aba\oplus b is the target. The target becomes:

(ab)b=a(bb)=a0=a\begin{align*} (a\oplus b)\oplus b &=a\oplus(b\oplus b)\\ &=a\oplus0\\ &=a \end{align*}

So:

(b,ab)CNOT1,2(b,a)(b,a\oplus b) \xrightarrow{\operatorname{CNOT}_{1,2}} (b,a)

\therefore

(a,b)(b,a)(a,b)\longrightarrow(b,a)

We started with (a,b)(a,b) and ended with (b,a)(b,a). The two values have been swapped! We used the basis values aa and bb to make the calculation easier. Since quantum gates are linear, the same result holds for arbitrary single-qubit states, including states in superposition.

Check

This is a Google Cirq code for checking the CNOT construction and the final permutation:

check.py
python

Find the 4×44\times4 unitary matrix represented by each of the following two-qubit circuits:

(a) Apply the Hadamard gate to x1x_1 while leaving x2x_2 unchanged.

Hadamard on the first of two qubits6 components and 4 connections.X1x₁HH1HY1H(x₁)X2x₂II2IY2x₂
Hadamard on the first of two qubits

(b) Leave x1x_1 unchanged while applying the Hadamard gate to x2x_2.

Hadamard on the second of two qubits6 components and 4 connections.X1x₁II1IY1x₁X2x₂HH2HY2H(x₂)
Hadamard on the second of two qubits

Solution

Before going into the two circuits, let's lay out some foundations.

There are 22 qubits in each circuit. Recall that an nn-qubit gate has a 2n×2n2^n\times2^n matrix. Therefore, the dimension of each matrix here is:

22×22=4×42^2\times2^2=4\times4

From the circuits, the matrices involved are the Hadamard matrix, HH, and the identity matrix, II:

H=12[1111],I=[1001]H=\frac{1}{\sqrt{2}} \begin{bmatrix} 1&1\\ 1&-1 \end{bmatrix},\qquad I=\begin{bmatrix} 1&0\\ 0&1 \end{bmatrix}

Also, recall that the tensor product of:

A=[abcd]A= \begin{bmatrix} a&b\\ c&d \end{bmatrix}

and another matrix, BB, is:

AB=[aBbBcBdB]A\otimes B= \begin{bmatrix} aB&bB\\ cB&dB \end{bmatrix}

(a) Hadamard on x1x_1

In circuit (a), HH is applied to x1x_1 while x2x_2 remains unchanged. The identity matrix, II, represents the unchanged qubit. Since x1x_1 comes before x2x_2, the overall operation is:

HIH\otimes I

Now, substitute the matrices:

HI=12[1111][1001]=12[1I1I1I1I]=12[1[1001]1[1001]1[1001]1[1001]]=12[1001100110011001]=12[1010010110100101]\begin{align*} H\otimes I &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1&1\\ 1&-1 \end{bmatrix} \otimes \begin{bmatrix} 1&0\\ 0&1 \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1I&1I\\ 1I&-1I \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1\begin{bmatrix}1&0\\0&1\end{bmatrix} & 1\begin{bmatrix}1&0\\0&1\end{bmatrix}\\[6pt] 1\begin{bmatrix}1&0\\0&1\end{bmatrix} & -1\begin{bmatrix}1&0\\0&1\end{bmatrix} \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} \begin{matrix}1&0\\0&1\end{matrix} & \begin{matrix}1&0\\0&1\end{matrix}\\[6pt] \begin{matrix}1&0\\0&1\end{matrix} & \begin{matrix}-1&0\\0&-1\end{matrix} \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1&0&1&0\\ 0&1&0&1\\ 1&0&-1&0\\ 0&1&0&-1 \end{bmatrix} \end{align*}

(b) Hadamard on x2x_2

In circuit (b), x1x_1 remains unchanged while HH is applied to x2x_2. This time, II comes before HH. Therefore, the operation is:

IHI\otimes H

Substitute both matrices:

IH=[1001]12[1111]=12[1[1111]0[1111]0[1111]1[1111]]=12[1111000000001111]=12[1100110000110011]\begin{align*} I\otimes H &= \begin{bmatrix} 1&0\\ 0&1 \end{bmatrix} \otimes \frac{1}{\sqrt{2}} \begin{bmatrix} 1&1\\ 1&-1 \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1\begin{bmatrix}1&1\\1&-1\end{bmatrix} & 0\begin{bmatrix}1&1\\1&-1\end{bmatrix}\\[6pt] 0\begin{bmatrix}1&1\\1&-1\end{bmatrix} & 1\begin{bmatrix}1&1\\1&-1\end{bmatrix} \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} \begin{matrix}1&1\\1&-1\end{matrix} & \begin{matrix}0&0\\0&0\end{matrix}\\[6pt] \begin{matrix}0&0\\0&0\end{matrix} & \begin{matrix}1&1\\1&-1\end{matrix} \end{bmatrix}\\ &= \frac{1}{\sqrt{2}} \begin{bmatrix} 1&1&0&0\\ 1&-1&0&0\\ 0&0&1&1\\ 0&0&1&-1 \end{bmatrix} \end{align*}

Check

This is another cirq code for checking both matrices:

check.py
python

Enjoyed this article? I'm a Software Engineer and Technical Writer actively seeking new opportunities to impact and learn, particularly in areas related to web security, finance, healthcare, and education. If you think my expertise aligns with your team's needs, let's chat! You can find me on LinkedIn and X . I am also an email away.