Quantum Information Processing: Foundations - Part 3
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.…
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.
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 (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 0 and 1 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.
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 (¬) 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:
in
out
0
1
1
0
Classical NOT gate One component joined by no nets, carrying no wired signals.
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.
in0
in1
out
0
0
0
1
0
0
0
1
0
1
1
1
Classical AND gate One component joined by no nets, carrying no wired signals.
OR (∨) 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:
in0
in1
out
0
0
0
1
0
1
0
1
1
1
1
1
Classical OR Gate One component joined by no nets, carrying no wired signals.
XOR (⊕) 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:
in0
in1
out
0
0
0
1
0
1
0
1
1
1
1
0
XOR gate's symbol is as follows:
Classical XOR gate One component joined by no nets, carrying no wired signals.
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!
in0
in1
out
0
0
1
1
0
1
0
1
1
1
1
0
NAND gate's symbol is as follows:
Classical NAND gate One component joined by no nets, carrying no wired signals.
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.
in0
in1
out
0
0
1
1
0
0
0
1
0
1
1
0
Classical NOR gate One component joined by no nets, carrying no wired signals.
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 (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⟩ is the input, ∣1⟩ will be the output, vice versa. Mathematically, this gate can be expressed, in matrix form, as:
X=[0110]
Now, let's look at the behaviour of this gate on a state, ∣ψ⟩.
Action of Pauli X on an arbitrary qubit Three components joined by two nets, carrying electrical signals.
Therefore, it can be demonstrated that if we feed ∣0⟩ ([10] in matrix form) into an X-gate, we will have:
X∣0⟩=[0110][10]=[0×1+1×01×1+0×0]=[01]=∣1⟩Pauli X flips zero to one Three components joined by two nets, carrying electrical signals.
In the same vein, feeding ∣1⟩ ([01] in matrix form) into the gate gives:
X∣1⟩=[0110][01]=[0×0+1×11×0+0×1]=[10]=∣0⟩
The truth table can easily be built from here:
Input
Output
∣0⟩
∣1⟩
∣1⟩
∣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=[0i−i0]
Let's feed the same arbitrary state, ∣ψ⟩, into it:
Y∣ψ⟩=[0i−i0][αβ]=[−iβiα]=−iβ∣0⟩+iα∣1⟩
The amplitudes have been swapped, but they now carry the phases −i and i. From here, feeding ∣0⟩ into the gate gives:
Y∣0⟩=[0i−i0][10]=[0i]=i∣1⟩
In the same vein, feeding ∣1⟩ into it gives:
Y∣1⟩=[0i−i0][01]=[−i0]=−i∣0⟩
The truth table can be built from here:
Input
Output
∣0⟩
i∣1⟩
∣1⟩
−i∣0⟩
α∣0⟩+β∣1⟩
−iβ∣0⟩+iα∣1⟩
Action of Pauli Y on an arbitrary qubit Three components joined by two nets, carrying electrical signals.
You may notice that i∣1⟩ and −i∣0⟩ still give the same measurement probabilities as ∣1⟩ and ∣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⟩ amplitude. Its matrix is:
Z=[100−1]
Feeding our arbitrary state into it, we have:
Z∣ψ⟩=[100−1][αβ]=[α−β]=α∣0⟩−β∣1⟩
For the basis states:
Z∣0⟩=∣0⟩,Z∣1⟩=−∣1⟩
Input
Output
∣0⟩
∣0⟩
∣1⟩
−∣1⟩
α∣0⟩+β∣1⟩
α∣0⟩−β∣1⟩
You may ask: if ∣1⟩ and −∣1⟩ produce the same measurement probability, what exactly changed? When ∣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(2∣0⟩+∣1⟩)=2∣0⟩−∣1⟩
Recall that 2∣0⟩+∣1⟩=∣+⟩ and 2∣0⟩−∣1⟩=∣−⟩. Therefore, Z∣+⟩=∣−⟩. 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.
Pauli Z changes the relative phase Three components joined by two nets, carrying electrical signals.
H-Gate
The Hadamard gate, commonly written as H, takes a basis state and puts it in an equal superposition. Its matrix is:
H=21[111−1]
Let's first see its action on our arbitrary qubit:
H∣1⟩=21[111−1][01]=21[1−1]=2∣0⟩−∣1⟩=∣−⟩Hadamard creates an equal superposition Three components joined by two nets, carrying electrical signals.
From H∣0⟩=21∣0⟩+21∣1⟩, the probabilities of measuring ∣0⟩ and ∣1⟩ are:
P∣0⟩=212=21
and
P∣1⟩=212=21
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=21[111−1][111−1]=21[2002]=I
Therefore, H2=I. This also means that H∣+⟩=∣0⟩ and H∣−⟩=∣1⟩. The gate takes us from the computational basis, {∣0⟩,∣1⟩}, to the Hadamard basis, {∣+⟩,∣−⟩}, and vice versa.
S-Gate
The S-gate is another phase gate. It leaves ∣0⟩ unchanged but adds a phase of 2π to ∣1⟩. Its matrix is:
S=[100i]
Applying it to our arbitrary state gives:
S∣ψ⟩=[100i][αβ]=[αiβ]=α∣0⟩+iβ∣1⟩
Recall from Euler's formula that ei2π=i. Therefore:
S∣0⟩=∣0⟩,S∣1⟩=i∣1⟩=ei2π∣1⟩
The truth table can be built from here:
Input
Output
∣0⟩
∣0⟩
∣1⟩
i∣1⟩
α∣0⟩+β∣1⟩
α∣0⟩+iβ∣1⟩
S gate adds a quarter-turn phase Three components joined by two nets, carrying electrical signals.
Also, applying S twice produces the Z-gate. From its matrix:
S2=[100i][100i]=[100i2]=[100−1]=Z
T-Gate
The T-gate works in a similar fashion to the S-gate. It is also called the 8π gate, though the relative phase it adds to ∣1⟩ is 4π. Its matrix is:
T=[100eiπ/4]
Recall Euler's formula, eiθ=cosθ+isinθ. Substituting θ=4π, we have:
eiπ/4=cos4π+isin4π=21+i
Now, let's apply T to our arbitrary state:
T∣ψ⟩=[100eiπ/4][αβ]=[αeiπ/4β]=α∣0⟩+eiπ/4β∣1⟩
Input
Output
∣0⟩
∣0⟩
∣1⟩
eiπ/4∣1⟩
α∣0⟩+β∣1⟩
α∣0⟩+eiπ/4β∣1⟩
T gate adds an eighth-turn phase Three components joined by two nets, carrying electrical signals.
From here, applying T twice should give S. Let's confirm this from its matrix:
T2=[100eiπ/4]2=[100eiπ/2]=[100i]=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⟩, the target remains unchanged. However, if the control is ∣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:
∣c,t⟩⟼∣c,t⊕c⟩
where ⊕ is XOR. Recall that XOR returns 1 only when its two inputs differ. In the computational basis ordered as {∣00⟩,∣01⟩,∣10⟩,∣11⟩}, the matrix of the gate is:
CNOT=1000010000010010
Let's work through all four computational basis states:
∣00⟩→∣00⟩: the control is 0, so the target remains 0;
∣01⟩→∣01⟩: the control is 0, so the target remains 1;
∣10⟩→∣11⟩: the control is 1, so the target flips from 0 to 1; and
∣11⟩→∣10⟩: the control is 1, so the target flips from 1 to 0.
Input
Output
∣00⟩
∣00⟩
∣01⟩
∣01⟩
∣10⟩
∣11⟩
∣11⟩
∣10⟩
Controlled NOT gate Five components joined by four nets, carrying quantum signals.
Now, what happens if the control is in a superposition? Let's consider two qubits initialized to ∣00⟩. First, we apply H to the first qubit while leaving the second unchanged:
(H⊗I)∣00⟩=2∣00⟩+∣10⟩
From here, we apply CNOT using the first qubit as the control:
CNOT(2∣00⟩+∣10⟩)=2∣00⟩+∣11⟩
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:
∣ψ⟩∣ϕ⟩∣η⟩⟼∣η⟩∣ψ⟩∣ϕ⟩
Solution
Given:
∣ψ⟩1∣ϕ⟩2∣η⟩3⟶∣η⟩1∣ψ⟩2∣ϕ⟩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
∣ψ⟩
1
2
∣ϕ⟩
2
3
∣η⟩
3
1
From the table:
∣ψ⟩:1→2
∣ϕ⟩:2→3
∣η⟩:3→1
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:
∣ψ⟩1∣ϕ⟩2∣η⟩3SWAP2,3∣ψ⟩1∣η⟩2∣ϕ⟩3
The states are now in the order ∣ψ⟩∣η⟩∣ϕ⟩. We only need to move ∣η⟩ to the first position. So, let's swap the states in positions 1 and 2:
∣ψ⟩1∣η⟩2∣ϕ⟩3SWAP1,2∣η⟩1∣ψ⟩2∣ϕ⟩3
∴
∣ψ⟩∣ϕ⟩∣η⟩SWAP2,3∣ψ⟩∣η⟩∣ϕ⟩SWAP1,2∣η⟩∣ψ⟩∣ϕ⟩
The required circuit is:
Quantum circuit for swapping |ψ⟩ |ϕ⟩ |η⟩ to |η⟩ |ψ⟩ |ϕ⟩ 12 components joined by 15 nets, carrying quantum signals.
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:
CNOT1,2,CNOT2,1,CNOT1,2
The first subscript denotes the control qubit while the second denotes the target. Also, recall these properties of XOR:
a⊕a=0,a⊕0=a
For the first CNOT, a is the control. It remains unchanged while the target, b, becomes a⊕b:
(a,b)CNOT1,2(a,a⊕b)
For the second CNOT, a⊕b is now the control and a is the target. The new target is:
a⊕(a⊕b)=(a⊕a)⊕b=0⊕b=b
Therefore:
(a,a⊕b)CNOT2,1(b,a⊕b)
For the last CNOT, b is the control and a⊕b is the target. The target becomes:
(a⊕b)⊕b=a⊕(b⊕b)=a⊕0=a
So:
(b,a⊕b)CNOT1,2(b,a)
∴
(a,b)⟶(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
code for checking the CNOT construction and the final permutation:
check.py
python
1import cirq
2import numpy as np
3
4# Define three qubits
5q1, q2, q3 = cirq.LineQubit.range(3)
6
7# Build the circuit with two SWAP gates
8swap_circuit = cirq.Circuit(
9 cirq.SWAP(q2, q3),
10 cirq.SWAP(q1, q2),
11)
12
13# Build the same circuit using CNOT gates
14decomposed_circuit = cirq.Circuit(
15# SWAP q2 and q3
16 cirq.CNOT(q2, q3),
17 cirq.CNOT(q3, q2),
18 cirq.CNOT(q2, q3),
19# SWAP q1 and q2
20 cirq.CNOT(q1, q2),
21 cirq.CNOT(q2, q1),
22 cirq.CNOT(q1, q2),
23)
24
25# Confirm that both circuits have the same unitary matrix
26np.testing.assert_allclose(
27 cirq.unitary(swap_circuit),
28 cirq.unitary(decomposed_circuit),
29 atol=1e-8,
30)
31
32# Define arbitrary single-qubit states
33psi = np.array([1 + 1j, 2], dtype=complex)
34phi = np.array([1, -1j], dtype=complex)
35eta = np.array([2 - 1j, 1], dtype=complex)
36
37# Normalize the states
38psi = psi / np.linalg.norm(psi)
39phi = phi / np.linalg.norm(phi)
40eta = eta / np.linalg.norm(eta)
41
42# Form |psi>|phi>|eta> and the expected |eta>|psi>|phi>
61print("The three-qubit permutation is correct!")
Q2:
Find the 4×4 unitary matrix represented by each of the following two-qubit circuits:
(a) Apply the Hadamard gate to x1 while leaving x2 unchanged.
Hadamard on the first of two qubits Six components joined by four nets, carrying electrical signals.
(b) Leave x1 unchanged while applying the Hadamard gate to x2.
Hadamard on the second of two qubits Six components joined by four nets, carrying electrical signals.
Solution
Before going into the two circuits, let's lay out some foundations.
There are 2qubits in each circuit. Recall that an n-qubit gate has a 2n×2n matrix. Therefore, the dimension of each matrix here is:
22×22=4×4
From the circuits, the matrices involved are the Hadamard matrix, H, and the identity matrix, I:
H=21[111−1],I=[1001]
Also, recall that the tensor product of:
A=[acbd]
and another matrix, B, is:
A⊗B=[aBcBbBdB]
(a) Hadamard on x1
In circuit (a), H is applied to x1 while x2 remains unchanged. The identity matrix, I, represents the unchanged qubit. Since x1 comes before x2, the overall operation is:
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.
APA 7 / SOURCE REGISTER
References
4 verified sources
Gopalan College of Engineering & Management. (2022).Module 3: Quantum computing & quantum gates.https://www.gopalancolleges.com/gcem/pdf/syllabus/physics/cse/module-3-quantum-computing-quantum-gates.pdf.