Quantum Information Processing: Foundations – Part 3

작성자

카테고리:

← 피드로
DEV Community · John Owolabi Idogun · 2026-07-24 개발(SW)

Introduction

Having laid out some mathematical foundations in the [previous part][1], we will proceed to discuss quantum gates and circuits in depth here. As usual, we will drive home the theories with worked examples from [@Rieffel2011] and check their accuracy with qiskit and/or cirq code.

Prerequisite

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.

Classical & Quantum gates

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 [@NISTQGATE2026]:

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 $0$ and $1$ referred to in the analogy mean low and high voltage ranges, respectively. Their actual voltage values depend on the hardware.

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

Logic Gates refresher

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 $0$ is the input to this gate, $1$ 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:

“`schemd bounds=”760×340″ title=”Classical NOT gate”
not:G1 “NOT” at (380, 170) #blue [standard=ieee]




#### AND ($\land$) Gate

Borrowing NIST's analogy again [@NISTQGATE2026]:

> 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.

| $in_0$ | $in_1$ | $out$ |
| -- | --  | -- |
|  0 | 0 | 0 |
|  1 | 0 | 0 |
|  0 | 1 | 0 |
|  1 | 1 | 1 |



```schemd bounds="760x340" title="Classical AND gate"
  and:G1 "AND" at (380, 170) #amber

Enter fullscreen mode Exit fullscreen mode

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:

$in_0$ $in_1$ $out$ 0 0 0 1 0 1 0 1 1 1 1 1

“`schemd bounds=”760×340″ title=”Classical OR Gate”
or:G1 “OR” at (380, 170) #cyan




#### 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 $1$ if and only if the bits are of opposite values. Its truth table sheds more light on this:

| $in_0$ | $in_1$ | $out$ |
| -- | --  | -- |
|  0 | 0 | 0 |
|  1 | 0 | 1 |
|  0 | 1 | 1 |
|  1 | 1 | 0 |

XOR gate's symbol is as follows:



```schemd bounds="760x340" title="Classical XOR gate"
xor:G1 "XOR" at (380, 170) #cyan

Enter fullscreen mode Exit fullscreen mode

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!

$in_0$ $in_1$ $out$ 0 0 1 1 0 1 0 1 1 1 1 0

NAND gate’s symbol is as follows:

“`schemd bounds=”760×340″ title=”Classical NAND gate”
nand:G1 “NAND” at (380, 170) #cyan




#### NOR

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

| $in_0$ | $in_1$ | $out$ |
| -- | --  | -- |
|  0 | 0 | 1 |
|  1 | 0 | 0 |
|  0 | 1 | 0 |
|  1 | 1 | 0 |



```schemd bounds="760x340" title="Classical NOR gate"
nor:G1 "NOR" at (380, 170) #cyan

Enter fullscreen mode Exit fullscreen mode

Quantum Gates

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 $0$ or $1$ (and not both at the same time), whereas quantum gates operate on qubits where $0$ and $1$ 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 [@Gopalan2022]. 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 [@MathWorksQGnd]. 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 $\ket{0}$ is the input, $\ket{1}$ will be the output, vice versa. Mathematically, this gate can be expressed, in matrix form, as:

$$
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

$$
\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\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!

“`schemd bounds=”760×340″ title=”Action of Pauli X on an arbitrary qubit”
port:A “∣ψ⟩=α∣0⟩+β∣1⟩” at (164, 170) #cyan
qgate:XG “X” at (350, 170) #amber
port:B “β∣0⟩+α∣1⟩” at (550, 170) #cyan

A.out -> XG.in #blue [line]
XG.out -> B.in #blue [line]




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

$$
X\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}
$$



```schemd bounds="760x340" title="Pauli X flips zero to one"
port:A "∣0⟩" at (164, 170) #cyan
qgate:XG "X" at (350, 170) #amber
port:B "∣1⟩" at (550, 170) #cyan

A.out -> XG.in #blue [line]
XG.out -> B.in #blue [line]

Enter fullscreen mode Exit fullscreen mode

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

$$
X\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 $\ket{0}$ $\ket{1}$ $\ket{1}$ $\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 = \begin{bmatrix}
0 & -i \
i & 0
\end{bmatrix}
$$

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

$$
Y\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$ and $i$. From here, feeding $\ket{0}$ into the gate gives:

$$
Y\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 $\ket{1}$ into it gives:

$$
Y\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 $\ket{0}$ $i\ket{1}$ $\ket{1}$ $-i\ket{0}$ $\alpha\ket{0}+\beta\ket{1}$ $-i\beta\ket{0}+i\alpha\ket{1}$

“`schemd bounds=”760×340″ title=”Action of Pauli Y on an arbitrary qubit”
port:A “∣ψ⟩=α∣0⟩+β∣1⟩” at (164, 170) #cyan
qgate:YG “Y” at (350, 170) #amber
port:B “-iβ∣0⟩+iα∣1⟩” at (550, 170) #cyan

A.out -> YG.in #blue [line]
YG.out -> B.in #blue [line]




You may notice that $i\ket{1}$ and $-i\ket{0}$ still give the same measurement probabilities as $\ket{1}$ and $\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 $\ket{1}$ amplitude. Its matrix is:

$$
Z = \begin{bmatrix}
1 & 0 \\
0 & -1
\end{bmatrix}
$$

Feeding our arbitrary state into it, we have:

$$
Z\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:

$$
Z\ket{0}=\ket{0}, \qquad Z\ket{1}=-\ket{1}
$$

| Input | Output |
| -- | -- |
| $\ket{0}$ | $\ket{0}$ |
| $\ket{1}$ | $-\ket{1}$ |
| $\alpha\ket{0}+\beta\ket{1}$ | $\alpha\ket{0}-\beta\ket{1}$ |

You may ask: if $\ket{1}$ and $-\ket{1}$ produce the same measurement probability, what exactly changed? When $\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\left(\frac{\ket{0}+\ket{1}}{\sqrt{2}}\right)
= \frac{\ket{0}-\ket{1}}{\sqrt{2}}
$$

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



```schemd bounds="760x340" title="Pauli Z changes the relative phase"
port:A "∣+⟩" at (164, 170) #cyan
qgate:ZG "Z" at (350, 170) #amber
port:B "∣-⟩" at (550, 170) #cyan

A.out -> ZG.in #blue [line]
ZG.out -> B.in #blue [line]

Enter fullscreen mode Exit fullscreen mode

H-Gate

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

$$
H = \frac{1}{\sqrt{2}}\begin{bmatrix}
1 & 1 \
1 & -1
\end{bmatrix}
$$

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

$$
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\ket{\psi}
= \frac{\alpha+\beta}{\sqrt{2}}\ket{0}

  • \frac{\alpha-\beta}{\sqrt{2}}\ket{1} $$

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

$$
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:

$$
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{-}
$$

“`schemd bounds=”760×340″ title=”Hadamard creates an equal superposition”
port:A “∣0⟩” at (164, 170) #cyan
hadamard:HG “H” at (350, 170) #amber
port:B “∣+⟩” at (550, 170) #cyan

A.out -> HG.in #blue [line]
HG.out -> B.in #blue [line]




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

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

and

$$
\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:

$$
H^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, $H^2=I$. This also means that $H\ket{+}=\ket{0}$ and $H\ket{-}=\ket{1}$. The gate takes us from the computational basis, $\{\ket{0},\ket{1}\}$, to the Hadamard basis, $\{\ket{+},\ket{-}\}$, and vice versa.

#### S-Gate

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

$$
S = \begin{bmatrix}
1 & 0 \\
0 & i
\end{bmatrix}
$$

Applying it to our arbitrary state gives:

$$
S\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 $e^{i\frac{\pi}{2}}=i$. Therefore:

$$
S\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 |
| -- | -- |
| $\ket{0}$ | $\ket{0}$ |
| $\ket{1}$ | $i\ket{1}$ |
| $\alpha\ket{0}+\beta\ket{1}$ | $\alpha\ket{0}+i\beta\ket{1}$ |



```schemd bounds="760x340" title="S gate adds a quarter-turn phase"
port:A "α∣0⟩+β∣1⟩" at (164, 170) #cyan
qgate:SG "S" at (350, 170) #amber [phase="π/2"]
port:B "α∣0⟩+iβ∣1⟩" at (550, 170) #cyan

A.out -> SG.in #blue [line]
SG.out -> B.in #blue [line]

Enter fullscreen mode Exit fullscreen mode

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

$$
S^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 $\frac{\pi}{8}$ gate, though the relative phase it adds to $\ket{1}$ is $\frac{\pi}{4}$. Its matrix is:

$$
T = \begin{bmatrix}
1 & 0 \
0 & e^{i\pi/4}
\end{bmatrix}
$$

Recall Euler’s formula, $e^{i\theta}=\cos\theta+i\sin\theta$. Substituting $\theta=\frac{\pi}{4}$, we have:

$$
e^{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\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 $\ket{0}$ $\ket{0}$ $\ket{1}$ $e^{i\pi/4}\ket{1}$ $\alpha\ket{0}+\beta\ket{1}$ $\alpha\ket{0}+e^{i\pi/4}\beta\ket{1}$

“`schemd bounds=”760×340″ title=”T gate adds an eighth-turn phase”
port:A “α∣0⟩+β∣1⟩” at (164, 170) #cyan
qgate:TG “T” at (350, 170) #amber [phase=”π/4″]
port:B “α∣0⟩+e^(iπ/4)β∣1⟩” at (550, 170) #cyan

A.out -> TG.in #blue [line]
TG.out -> B.in #blue [line]




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

$$
T^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 $\ket{0}$, the target remains unchanged. However, if the control is $\ket{1}$, an *X-gate* is applied to the target. The control itself does not change.

If we represent the control with $c$ and the target with $t$, the operation can be written as:

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

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

$$
\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:

- $\ket{00}\rightarrow\ket{00}$: the control is $0$, so the target remains $0$;
- $\ket{01}\rightarrow\ket{01}$: the control is $0$, so the target remains $1$;
- $\ket{10}\rightarrow\ket{11}$: the control is $1$, so the target flips from $0$ to $1$; and
- $\ket{11}\rightarrow\ket{10}$: the control is $1$, so the target flips from $1$ to $0$.

| Input | Output |
| -- | -- |
| $\ket{00}$ | $\ket{00}$ |
| $\ket{01}$ | $\ket{01}$ |
| $\ket{10}$ | $\ket{11}$ |
| $\ket{11}$ | $\ket{10}$ |



```schemd bounds="760x340" title="Controlled NOT gate"
port:C0 "∣c⟩" at (164, 152) #cyan
port:T0 "∣t⟩" at (164, 188) #cyan

cnot:CX "CNOT" at (350, 170) #amber

port:C1 "∣c⟩" at (550, 152) #cyan
port:T1 "∣t⊕c⟩" at (550, 188) #cyan

C0.out -> CX.in1 #blue [quantum line]
T0.out -> CX.in2 #blue [quantum line]
CX.out1 -> C1.in #blue [quantum line]
CX.out2 -> T1.in #blue [quantum line]

Enter fullscreen mode Exit fullscreen mode

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

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

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

$$
\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.

Worked examples

Q1:

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:

$$
\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}$ $1$ $2$ $\ket{\phi}$ $2$ $3$ $\ket{\eta}$ $3$ $1$

From the table:

  • $\ket{\psi}: 1\rightarrow2$
  • $\ket{\phi}: 2\rightarrow3$
  • $\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 $2$ and $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 $1$ and $2$:

$$
\ket{\psi}1\ket{\eta}_2\ket{\phi}_3
\xrightarrow{\operatorname{SWAP}
{1,2}}
\ket{\eta}_1\ket{\psi}_2\ket{\phi}_3
$$

$\therefore$

$$
\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:

“`schemd bounds=”660×200″ title=” Quantum circuit for swapping |ψ⟩ |ϕ⟩ |η⟩ to |η⟩ |ψ⟩ |ϕ⟩”
initial:PSI “|\psi\rangle” at (30, 50) #blue
initial:PHI “|\phi\rangle” at (30, 110) #purple
initial:ETA “|\eta\rangle” at (30, 170) #cyan

// SWAP(|phi>, |eta>)
cnot:S1A “CX” at (100, 132) #purple
cnot:S1B “CX” at (186, 132) #purple [orientation=left]
cnot:S1C “CX” at (272, 132) #purple

// SWAP(|psi>, |eta>)
cnot:S2A “CX” at (380, 114) #cyan
cnot:S2B “CX” at (466, 114) #cyan [orientation=left]
cnot:S2C “CX” at (552, 114) #cyan

initial:OETA “|\eta\rangle” at (640, 50) #cyan
initial:OPSI “|\psi\rangle” at (640, 110) #blue
initial:OPHI “|\phi\rangle” at (640, 170) #purple

PSI.out -> S2A.in1 #blue [quantum ortho]
PHI.out -> S1A.in1 #purple [quantum ortho]
ETA.out -> S1A.in2 #cyan [quantum ortho]

S1A.out1 -> S1B.out2 #purple [quantum line]
S1A.out2 -> S1B.out1 #cyan [quantum line]
S1B.in2 -> S1C.in1 #purple [quantum line]
S1B.in1 -> S1C.in2 #cyan [quantum line]

S1C.out1 -> S2A.in2 #cyan [quantum line]
S1C.out2 -> OPHI.in #purple [quantum ortho]

S2A.out1 -> S2B.out2 #blue [quantum line]
S2A.out2 -> S2B.out1 #cyan [quantum line]
S2B.in2 -> S2C.in1 #blue [quantum line]
S2B.in1 -> S2C.in2 #cyan [quantum line]

S2C.out1 -> OETA.in #cyan [quantum ortho]
S2C.out2 -> OPSI.in #blue [quantum ortho]




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 $a$ and $b$, respectively. The gates are applied in this order:

$$
\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:

$$
a\oplus a=0,\qquad a\oplus0=a
$$

For the first CNOT, $a$ is the control. It remains unchanged while the target, $b$, becomes $a\oplus b$:

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

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

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

Therefore:

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

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

$$
\begin{align*}
(a\oplus b)\oplus b
&=a\oplus(b\oplus b)\\
&=a\oplus0\\
&=a
\end{align*}
$$

So:

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

$\therefore$

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

We started with $(a,b)$ and ended with $(b,a)$. The two values have been swapped! We used the basis values $a$ and $b$ 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][2] code for checking the CNOT construction and the final permutation:



```python :check.py:
import cirq
import numpy as np

# Define three qubits
q1, q2, q3 = cirq.LineQubit.range(3)

# Build the circuit with two SWAP gates
swap_circuit = cirq.Circuit(
    cirq.SWAP(q2, q3),
    cirq.SWAP(q1, q2),
)

# Build the same circuit using CNOT gates
decomposed_circuit = cirq.Circuit(
    # SWAP q2 and q3
    cirq.CNOT(q2, q3),
    cirq.CNOT(q3, q2),
    cirq.CNOT(q2, q3),
    # SWAP q1 and q2
    cirq.CNOT(q1, q2),
    cirq.CNOT(q2, q1),
    cirq.CNOT(q1, q2),
)

# Confirm that both circuits have the same unitary matrix
np.testing.assert_allclose(
    cirq.unitary(swap_circuit),
    cirq.unitary(decomposed_circuit),
    atol=1e-8,
)

# Define arbitrary single-qubit states
psi = np.array([1 + 1j, 2], dtype=complex)
phi = np.array([1, -1j], dtype=complex)
eta = np.array([2 - 1j, 1], dtype=complex)

# Normalize the states
psi = psi / np.linalg.norm(psi)
phi = phi / np.linalg.norm(phi)
eta = eta / np.linalg.norm(eta)

# Form |psi>|phi>|eta> and the expected |eta>|psi>|phi>
initial_state = np.kron(np.kron(psi, phi), eta)
expected_state = np.kron(np.kron(eta, psi), phi)

# Simulate the SWAP circuit
result = cirq.Simulator().simulate(
    swap_circuit,
    qubit_order=[q1, q2, q3],
    initial_state=initial_state,
)

# Confirm the final state
np.testing.assert_allclose(
    result.final_state_vector,
    expected_state,
    atol=1e-7,
)

print(swap_circuit)
print("The three-qubit permutation is correct!")

Enter fullscreen mode Exit fullscreen mode

Q2:

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

(a) Apply the Hadamard gate to $x_1$ while leaving $x_2$ unchanged.

“`schemd bounds=”760×340″ title=”Hadamard on the first of two qubits”
port:X1 “x₁” at (140, 130) #cyan
hadamard:H1 “H” at (350, 130) #amber
port:Y1 “H(x₁)” at (570, 130) #cyan

port:X2 “x₂” at (140, 210) #cyan
qgate:I2 “I” at (350, 210) #slate
port:Y2 “x₂” at (570, 210) #cyan

X1.out -> H1.in #blue [line]
H1.out -> Y1.in #blue [line]
X2.out -> I2.in #blue [line]
I2.out -> Y2.in #blue [line]




**(b)** Leave $x_1$ unchanged while applying the Hadamard gate to $x_2$.



```schemd bounds="760x340" title="Hadamard on the second of two qubits"
port:X1 "x₁" at (140, 130) #cyan
qgate:I1 "I" at (350, 130) #slate
port:Y1 "x₁" at (570, 130) #cyan

port:X2 "x₂" at (140, 210) #cyan
hadamard:H2 "H" at (350, 210) #amber
port:Y2 "H(x₂)" at (570, 210) #cyan

X1.out -> I1.in #blue [line]
I1.out -> Y1.in #blue [line]
X2.out -> H2.in #blue [line]
H2.out -> Y2.in #blue [line]

Enter fullscreen mode Exit fullscreen mode

Solution

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

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

$$
2^2\times2^2=4\times4
$$

From the circuits, the matrices involved are the Hadamard matrix, $H$, and the identity matrix, $I$:

$$
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=
\begin{bmatrix}
a&b\
c&d
\end{bmatrix}
$$

and another matrix, $B$, is:

$$
A\otimes B=
\begin{bmatrix}
aB&bB\
cB&dB
\end{bmatrix}
$$

(a) Hadamard on $x_1$

In circuit (a), $H$ is applied to $x_1$ while $x_2$ remains unchanged. The identity matrix, $I$, represents the unchanged qubit. Since $x_1$ comes before $x_2$, the overall operation is:

$$
H\otimes I
$$

Now, substitute the matrices:

$$
\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 $x_2$

In circuit (b), $x_1$ remains unchanged while $H$ is applied to $x_2$. This time, $I$ comes before $H$. Therefore, the operation is:

$$
I\otimes H
$$

Substitute both matrices:

$$
\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:

“`python :check.py:
import cirq
import numpy as np

Define the two qubits

x1, x2 = cirq.LineQubit.range(2)

Circuit (a): H on x1 and I on x2

circuit_a = cirq.Circuit(
cirq.H(x1),
cirq.I(x2),
)

Circuit (b): I on x1 and H on x2

circuit_b = cirq.Circuit(
cirq.I(x1),
cirq.H(x2),
)

Define the matrix obtained for circuit (a)

expected_a = np.array(
[
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, -1, 0],
[0, 1, 0, -1],
],
dtype=complex,
) / np.sqrt(2)

Define the matrix obtained for circuit (b)

expected_b = np.array(
[
[1, 1, 0, 0],
[1, -1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, -1],
],
dtype=complex,
) / np.sqrt(2)

Let Cirq obtain the matrices directly from the circuits

unitary_a = cirq.unitary(circuit_a)
unitary_b = cirq.unitary(circuit_b)

Compare Cirq’s matrices with our answers

np.testing.assert_allclose(unitary_a, expected_a, atol=1e-8)
np.testing.assert_allclose(unitary_b, expected_b, atol=1e-8)

print(“H tensor I:”)
print(np.round(unitary_a, 3))

print(“\nI tensor H:”)
print(np.round(unitary_b, 3))




## Outro

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](https://www.linkedin.com/in/john-owolabi-idogun/) and [X](https://x.com/Sirneij). I am also an [email](mailto:[email protected]) away.




References
[@Rieffel2011]: Rieffel, E. G., & Polak, W. H. (2014). Quantum Computing: A Gentle Introduction. MIT Press. https://books.google.com/books?id=CQ3YoAEACAAJ
[@NISTQGATE2026]: Quantum Logic Gates. (2018). NIST. https://www.nist.gov/physics/introduction-new-quantum-revolution/quantum-logic-gates
[@Gopalan2022]: Gopalan College of Engineering and Management. (2022). Module 3: Quantum computing & quantum gates. https://www.gopalancolleges.com/gcem/pdf/syllabus/physics/cse/module-3-quantum-computing-quantum-gates.pdf
[@MathWorksQGnd]: MathWorks. (n.d.). What are quantum gates?. https://www.mathworks.com/discovery/quantum-gates.html

Enter fullscreen mode Exit fullscreen mode

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다