Introduction To Quantum Computing: Wednesday, 17 January 2024 2:24 PM
Introduction To Quantum Computing: Wednesday, 17 January 2024 2:24 PM
Introduction To Quantum Computing: Wednesday, 17 January 2024 2:24 PM
1/45
Vectors & Vector Spaces
Definition of Vector: A collection of complex or real numbers,
generally put in a column v
1 Transpose
v = " = [v1 ! v N ]T
v N
a1 b1 a1 + b1
a = " b = " a + b = "
a N bN a N + bN
2/45
Definition of Scalar: A real or complex number.
a1 αa1
a = " αa = "
a N αa N
(x + y ) + z = y + (x + z )
2. Associativity α(βx) = (αβ)x
α ( x + y ) = αx + α y
3. Distributivity
(α + β)x = αx + βx
1x = x
4. Scalar Unity &
Scalar Zero 0x = 0, where 0 is the zero vector of all zeros
4/45
Definition of a Vector Space: A set V of N-dimensional vectors
(with a corresponding set of scalars) such that the set of vectors
is:
(i) ìclosedî under vector addition
(ii) ìclosedî under scalar multiplication
In other words:
ï addition of vectors ñ gives another vector in the set
ï multiplying a vector by a scalar ñ gives another vector in the set
Examples:
1. The space R2 is a subspace of R3.
2. Any plane in R3 that passes through the origin is a subspace
3. Any line passing through the origin in R2 is a subspace of R2
4. The set R2 is NOT a subspace of C2 because R2 isnít closed
under complex scalars (a subspace must retain the original
spaceís set of scalars)
7/45
Geometric Structure of Vector Space
Length of a Vector (Vector Norm): For any vector v in CN
we define its length (or ìnormî) to be
N N
∑v 2
= ∑ vi
2
2
v 2
= i v
i =1 2
i =1
α v1 + β v 2 2
≤ α v1 2
+ β v2 2 triangle inequality
v 2
< ∞ ∀v ∈ C N
v 2
= 0 iff v = 0
8/45
Distance Between Vectors: the distance between two
vectors in a vector space with the two norm is defined by:
d ( v1 , v 2 ) = v1 − v 2 2
v1 v1 ñ v2
v2
9/45
Angle Between Vectors & Inner Product:
v
Motivate the idea in R :
2
A cos θ 1
A v = A sin θ u=
θ 0
u
2
Note that: ∑u v
i =1
i i = 1 ⋅ A cos θ + 0 ⋅ A sin θ = A cos θ
10/45
Inner Product Between Vectors :
Define the inner product between two complex vectors in CN by:
N
< u, v >= ∑ u i vi*
i =1
2
3. Linking Inner Product to Norm: v 2
=< v, v >
12/45
Building Vectors From Other Vectors
Can we find a set of ìprototypeî vectors {v1, v2, Ö, vM} from
which we can build all other vectors in some given vector space V
by using linear combinations of the vi?
M M
v = ∑α k v k u = ∑ βk vk
k =1 k =1
Same ìIngredientsîÖ just different amounts of them!!!
16/45
Expansion and Transformation
Fact: For a given basis {v1, v2, Ö, vN}, the expansion of a vector v
in V is unique. That is, for each v there is only one, unique set of
N
coefficients {α1, α2, Ö , αN} such that v = α v
∑k =1
k k
18/45
DFT from Basis Viewpoint:
If we have a discrete-time signal x[n] for n = 0, 1, Ö N-1
x = [x[0] x[1] ! x[ N − 1]]
T
Define vector:
Define a orthogonal basis from the exponentials used in the IDFT:
1 1 1 1
1 e j 2 π1⋅1 / N e j 2 π 2⋅1 / N e j 2 π ( N −1)⋅1 / N
d0 =
"
d1 =
"
j 2 π1( N −1) / N
d2 =
"
j 2 π 2( N −1) / N
Ö d N −1 =
"
j 2 π( N −1)( N −1) / N
1 e e e
α i = v, v i = αi
N N
ThenÖ. v = ∑αk vk u = ∑ βk vk
k =1 k =1
21/45
Example: DFT Coefficients as Inner Products:
Recall: N-pt. IDFT is an expansion of the signal vector in terms of
N Orthogonal vectors. Thus
X [k ] = x, d k
N −1
= ∑ x[ n ]d *
k [n ]
n =0
N −1
= ∑ x[n]e − j 2πkn / N
n =0
See ìreading notesî for some details about normalization issues in this case
22/45
Matrices
Matrix: Is an array of (real or complex) numbers organized in
rows and columns. a11 a12 a13 a14
Here is a 3x4 example:
A = a 21 a 22 a 23 a 24
a31 a32 a33 a34
A, A −1
y2
x1 y1
x2
To see this:
< v1 , v1 > < v1 , v 2 > ! < v1 , v N >
< v , v > < v , v > ! < v 2 , v N >
VV H = 2 1 2 2
1 0 ! 0
0 1 ! 0 Inner products are 0 or 1
= =I because this is an ON basis
" ' "
0 0 ! 1
29/45
Unitary and Orthogonal Matrices
A unitary matrix is a complex matrix A whose inverse is A-1 = AH
For the real-valued matrix caseÖ we get a special case of ìunitaryî
the idea of ìunitary matrixî becomes ìorthogonal matrixî
for which A-1 = AT
Two Properties of Unitary Matrices: Let U be a unitary matrix
and let y1 = Ux1 and y2 = Ux2
1. They preserve norms: ||yi|| = ||xi||.
2. They preserve inner products: < y1, y2 > = < x1, x2 >
That is the ìgeometryî of the old space is preserved by the unitary
matrix as it transforms into the new space.
(These are the same as the preservation properties of ON basis.)
30/45
DFT from Unitary Matrix Viewpoint:
Consider a discrete-time signal x[n] for n = 0, 1, Ö N-1.
N −1
Weíve already seen the DFT in a basis viewpoint: 1
x= ∑ N X [k ] d k
k =0 &
#%#
$
αk
Now we can view the DFT as a transform from the Unitary matrix
viewpoint:
1 1 1 ! 1
1 e j 2 π1⋅1 / N e j 2 π 2⋅1 / N ! e j 2 π ( N −1)⋅1 / N
D = [d 0 | d1 | … | d N −1 ] =
" " " "
j 2 π1( N −1) / N j 2 π ( N −1)( N −1) / N
1 e e j 2 π 2 ( N −1) / N ! e
DFT IDFT
~
x = DH x 1 ~
x = Dx
N
(Acutally D is not unitary but N-1/2D is unitaryÖ see reading notes) 31/45
Geometry Preservation of Unitary Matrix Mappings
RecallÖ unitary matrices map in such a way that the sizes of
vectors and the orientation between vectors is not changed.
A, A −1
y2
x1
x2
y1
A, A −1
Unitary mappings just
ìrigidly rotateî the space.
32/45
Effect of Non-Unitary Matrix Mappings
A, A −1
y2
x1
x2
y1
A, A −1
33/45
More on Matrices as Transforms
Weíll limit ourselves here to real-valued vectors and matrices
Rn Rm
A y
x
x A y
OtherwiseÖ.The range(A) ⊂ Rm
Öbecause the columns donít span Rm
35/45
Rank of a Matrix: rank(A) = largest # of linearly independent
columns (or rows) of matrix A
For an m×n matrix we have that rank(A) ≤ min(m,n)
An m×n matrix A has ìfull rankî when rank(A) = min(m,n)
Example: This matrix has rank of 3 because the 4th column cam be
written as a combination of the first 3 columns
1 0 0 1
0 1 0 2
A = 0 0 1 1
0 0 0 0
0 0 0 0
36/45
Characterizing ìTall Matrixî Mappings
We are interested in answering: Given a vector y, what vector x
mapped into it via matrix A?
ìTall Matrixî (m > n) Case
If y does not lie in range(A), then there is No Solution
If y lies in range(A), then there is a solution (but not
necessarily just one unique solution)
y = Ax
y∉range(A) y∈range(A)
39/45
Characterizing ìSquare Matrixî Mappings
One Solution
y∉range(A) y∈range(A)
41/45
Eigenvalues and Eigenvectors of Square Matrices
If matrix A is n×n, then A maps Rn → Rn
Q: For a given n×n matrix A, which vectors get mapped into
being almost themselves???
More preciselyÖ Which vectors get mapped to a scalar multiple
of themselves???
Even more preciselyÖ which vectors v satisfy the following:
Av = λv
Input Output
These vectors are ìspecialî and are called the eigenvectors of A.
The scalar λ is that e-vectorís corresponding eigenvalue.
v Av
42/45
ìEigen-Facts for Symmetric Matricesî
ï If n×n real matrix A is symmetric, then
ñ e-vectors corresponding to distinct e-values are orthonormal
ñ e-values are real valued
ñ can decompose A as A = VΛ V T
V = [v1 v2 ! vn ] VV T = I
Λ = diag{λ1 , λ2 ,…, λn }
ï If, further, A is pos. def. (semi-def.), then
ñ e-values are positive (non-negative)
ñ rank(A) = # of non-zero e-values
ï Pos. Def. ⇒ Full Rank (and therefore invertible)
ï Pos. Semi-Def. ⇒ Not Full Rank (and therefore not invertible)
ñ When A is P. D., then we can write
A −1 = VΛ −1 V T
{ }
For P.D. A, A-1 has
the same e-vectors and Λ −1 = diag 1 λ , 1 λ ,…, 1 λ
1 2 n
has reciprocal e-values 43/45
Other Matrix Issues
Weíll limit our discussion to real-valued matrices and vectors
Quadratic Forms and Positive-(Semi)Definite Matrices
Quadratic Form = Matrix form for a 2nd-order multivariate
polynomial
x1 a11 a12
Example: x = A =
x2 a 21 a 22
variable fixed
The quadratic form of matrix A is:
QA ( x1 , x2 ) = x T Ax (1 × 2) ⋅ ( 2 × 2) ⋅ ( 2 × 1) = (1 × 1) scalar
2 2
scalar = ∑∑ ij i j 11 1 22 2 + (a12 + a21 ) x1 x2
a x x = a x 2
+ a x 2
i =1 j =1 44/45
ï Values of the elements of matrix A determine the characteristics
of the quadratic form QA(x)
ñ If QA(x) ≥ 0 ∀x ≠ 0Ö then say that QA(x) is ìpositive semi-definiteî
ñ If QA(x) > 0 ∀x ≠ 0Ö then say that QA(x) is ìpositive definiteî
ñ Otherwise say that QA(x) is ìnon-definiteî
ï These terms carry over to the matrix that defines the Quad Form
ñ If QA(x) ≥ 0 ∀x ≠ 0Ö then say that A is ìpositive semi-definiteî
ñ If QA(x) > 0 ∀x ≠ 0Ö then say that A is ìpositive definiteî
45/45
Foundations and Trends® in Signal Processing
arXiv:2205.09510v4 [quant-ph] 11 Jun 2022
An Introduction to Quantum
Machine Learning for Engineers
Suggested Citation: Osvaldo Simeone (2022), “An Introduction to Quantum Machine
Learning for Engineers”, Foundations and Trends® in Signal Processing: Vol. xx, No. xx,
pp 1–18. DOI: 10.1561/XXXXXXXXX.
This article may be used only for the purpose of research, teaching,
and/or private study. Commercial use or systematic downloading
(by robots or other automatic processes) is prohibited without ex-
plicit Publisher approval.
Boston — Delft
Contents
Acknowledgements 223
References 224
An Introduction to Quantum
Machine Learning for Engineers
Osvaldo Simeone1
1 King’s College London
ABSTRACT
In the current noisy intermediate-scale quantum (NISQ)
era, quantum machine learning is emerging as a dominant
paradigm to program gate-based quantum computers. In
quantum machine learning, the gates of a quantum circuit
are parametrized, and the parameters are tuned via clas-
sical optimization based on data and on measurements of
the outputs of the circuit. Parametrized quantum circuits
(PQCs) can efficiently address combinatorial optimization
problems, implement probabilistic generative models, and
carry out inference (classification and regression). This mono-
graph provides a self-contained introduction to quantum
machine learning for an audience of engineers with a back-
ground in probability and linear algebra. It first describes
the necessary background, concepts, and tools necessary to
describe quantum operations and measurements. Then, it
covers parametrized quantum circuits, the variational quan-
tum eigensolver, as well as unsupervised and supervised
quantum machine learning formulations.
Motivation
2
3
that appear to define a possible new role for researchers with an engineer-
ing profile similar to mine. First, there are now several software libraries
– such as IBM’s Qiskit, Google’s Cirq, and Xanadu’s PennyLane – that
make programming quantum algorithms more accessible, while also
providing cloud-based access to actual quantum computers. Second, a
new framework is emerging for programming quantum algorithms to be
run on current quantum hardware: quantum machine learning.
classical
optimizer
average
1.1 Introduction
This chapter introduces the qubit as the basic unit of quantum infor-
mation and computing. To this end, we start by reviewing classical bits
(cbits) and random cbits. Then, we present the qubit as an algebraic
“extension” of the two-dimensional probability distribution of a random
cbit to a complex, normalized, two-dimensional vector. The chapter will
also present the two main ways in which a qubit can evolve over time:
unitary operations, also known as quantum gates, and measurements.
While quantum gates describe the evolution of a qubit in a closed system,
measurements convert quantum information to classical information by
coupling the qubit with a measurement instrument.
8
1.2. Random Classical Bit 9
where the last expression is known as Dirac’s bra-ket notation for the
inner product. (The pun is intended.) Note that we have the equality
Note that the subscript in || · ||2 identifies the type of norm. In this
monograph, we will only use the ¸2 norm, and hence we will use the
term norm for the operation || · ||2 .
The kets |0Í and |1Í define an orthonormal basis for the linear
space of two-dimensional vectors. In fact, the two vectors are orthogonal,
i.e.,
È0|1Í = È1|0Í = 0, (1.8)
and they have unitary norm, i.e.,
p0 + p1 = 1. (1.12)
1.3 Qubit
There are two key differences between the state |ÂÍ in (1.15) of a
quantum qubit and the state p of a random cbit:
• The qubit state vector (1.15) has the defining property of having
unitary norm, i.e.,
Figure 1.1: (left) An illustration of the two-dimensional Hilbert space with the
computational basis {|0Í, |1Í} and a given qubit state |ÂÍ in (1.15); (right) An
illustration of computational and diagonal bases.
While being distinct from the state of a random cbit, the qubit
state (1.15) recovers as special cases the two possible states, expressed
as one-hot amplitude vectors, of a deterministic cbit. In fact, setting
the amplitudes as –0 = 1 (and hence –1 = 0), or –1 = 1 (and hence
–0 = 0), recovers the deterministic cbit states |0Í and |1Í, respectively.
Therefore, a qubit that can only assume states |0Í and |1Í is equivalent
to a deterministic cbit.
By (1.15), we say that the qubit is in a superposition of states
|0Í and |1Í, with respective complex amplitudes –0 and –1 . Mathemat-
ically, this implies that the state of a qubit is a vector that lies in a
two-dimensional complex linear vector space, referred to as the Hilbert
space of dimension two. The states |0Í and |1Í form the so-called com-
putational basis of the Hilbert space. A geometric interpretation of a
quantum state, simplified by representing real amplitudes, is provided
by the left part of Fig. 1.1.
Being a two-dimensional vector, the state of the qubit can be equiv-
alently expressed as a superposition of any two orthonormal vectors
forming a basis of the Hilbert space. An important example is given by
the so-called diagonal basis, which consists of the two vectors
C D
1 1 1
|+Í = Ô (|0Í + |1ÍÍ = Ô (1.17)
2 2 1
and C D
1 1 1
|≠Í = Ô (|0Í ≠ |1Í) = Ô . (1.18)
2 2 ≠1
1.3. Qubit 15
x
|ÂÍ = –0 |0Í + –1 |1Í |xÍ w.p. |–x |2
Figure 1.2: A von Neumann measurement in the computational basis, also known
as a standard measurement, for a single qubit. (The abbreviation “w.p.” stands for
“with probability”.)
It can be directly checked that vectors |+Í and |≠Í are orthogonal and
that they have unitary norm. They are illustrated in the right part of
Fig. 1.1. The qubit state (1.15) can be expressed as a superposition of
diagonal states as
C D
–0 1 1
|ÂÍ = = Ô (–0 + –1 )|+Í + Ô (–0 ≠ –1 )|≠Í. (1.19)
–1 2 2
Therefore,
Ô the amplitude of the basis vector |+Í is given by the scaled
sum 1/ 2(–0 + –1 ), while the
Ô amplitude of the basis vector |≠Í is given
by the scaled difference 1/ 2(–0 ≠ –1 ).
a qubit in state |xÍ, for x œ {0, 1}, returns output x with probability 1,
while leaving the qubit state unchanged.
To conclude this section, it should be mentioned that the interpreta-
tion of the “collapse”-of-the-state property is much debated in physics
and philosophy (and in movies, where the “many-world” interpretation
provides an easy excuse for a plot twist).
We will see in Chapter 3, and then again in Chapter 5, that there
are different types of measurements; until then, we will always assume
standard measurements.
By Born’s rule, if the amplitudes {–x }1x=0 are real and non-negative,
we can write the state (1.15) of a qubit as
C Ô D
p0
|ÂÍ = Ô , (1.24)
p1
Figure 1.3: The state of a qubit evolves in a closed system according to the product
of the input state |ÂÍ by a unitary matrix U , also known as a single-qubit quantum
gate.
|ÂÍ X X |ÂÍ
|ÂÍ X |ÂÍ
|Â Õ Í = U |ÂÍ (1.34)
|ÂÍ = U † |Â Õ Í. (1.35)
U = UK · UK≠1 · · · U1 , (1.36)
name operator
C D
1 0
identity I=
0 1
C D
0 1
Pauli X X=
1 0
C D
1 0
Pauli Z Z=
0 ≠1
C D
0 ≠i
Pauli Y Y = iXZ =
i 0
C D
1 1
Hadamard H= Ô1 = Ô1 (X + Z)
2 1 ≠1 2
C D
cos(◊/2) ≠ sin(◊/2)
Pauli Y -rotation RY (◊) =
sin(◊/2) cos(◊/2)
Figure 1.5: Example of quantum circuit describing the evolution of the state of a
qubit in a closed system as a cascade of single-qubit quantum gates implementing
unitary matrices U1 , U2 ,..., and UK .
tions of unitary matrices and hence of quantum gates.
where {|v0 Í, |v1 Í} and {|u0 Í, |u1 Í} are two orthonormal bases of the
two-dimensional Hilbert space. By (1.37), we can interpret a unitary
operator as mapping each vector |ux Í from one orthonormal basis to
a vector |vx Í in another orthonormal basis for x œ {0, 1}. In fact, by
(1.37), we have the mapping
name operator
C D
1 0
identity I= = |v0 ÍÈv0 | + |v1 ÍÈv1 | for any orth. basis {|vx Í}1x=0
0 1
C D
0 1
Pauli X X= = |0ÍÈ1| + |1ÍÈ0|
1 0
C D
1 0
Pauli Z Z= = |+ÍÈ≠| + |≠ÍÈ+|
0 ≠1
C D
1 1
Hadamard H= Ô1 = |0ÍÈ+| + |1ÍÈ≠| = |+ÍÈ0| + |≠ÍÈ1|
2 1 ≠1
of the N -dimensional Hilbert space, and {⁄x }Nx=0 are the corresponding
≠1
That is, function f (A) is evaluated by applying the scalar function f (·)
separately to each eigenvalue of matrix A.
A unitary matrix U is a normal matrix, since it satisfies the condition
(1.32) and hence also the equality (1.45). Therefore, a 2 ◊ 2 unitary
matrix can be expressed in terms of its eigendecomposition (1.46) with
N = 2 eigenvectors and eigenvalues, i.e., as
(x, y) = x⇤ y = x1 y1 + . . . + xn yn .
A⇤ = A.
U ⇤ U = I.
1
For these types of matrices we have the following important theorems.
Spectral theorem for Hermitian matrices. For an Hermitian matrix,
(i) all eigenvalues are real,
(ii) eigenvectors corresponding to distinct eigenvalues are orthogonal,
(iii) there is an orthonormal basis consisting of eigenvectors.
Spectral theorem for unitary matrices. For a unitary matrix,
(i) all eigenvalues have absolute value 1,
(ii) eigenvectors corresponding to distinct eigenvalues are orthogonal,
(iii) there is an orthonormal basis consisting of eigenvectors.
So Hermitian and unitary matrices are always diagonalizable (though
some eigenvalues can be equal). For example, the unit matrix is both Her-
mitian and unitary. I recall that eigenvectors of any matrix corresponding
to distinct eigenvalues are linearly independent. For Hermitian and unitary
matrices we have a stronger property (ii).
Let me prove statements (i) of both theorems.
Suppose A is Hermitian, that is A⇤ = A. Let be an eigenvalue. This
means that there exists a vector v 6= 0 such that
Av = v.
v⇤ Av = v⇤ v = v⇤ v = kvk2 .
⇤
Now apply operation to this equality:
v⇤ A⇤ v = kvk2 .
The left hand sides are equal since A⇤ = A, so the right hand sides have to
be equal. But kvk2 6= 0 (since eigenvector is not 0 by definition), so =
that is is real.
Now suppose that A is unitary, that is A⇤ A = I. Let be an eigenvalue.
This means that there exists v 6= 0 such that
Av = v.
Then also
v ⇤ A⇤ = v ⇤
2
Let us multiply these equalities:
v⇤ A⇤ Av = v⇤ v = | |2 kvk2 .
But since A⇤ A = I, the left hand side is kvk2 . Since kvk2 6= 0, we must have
| | = 1.
So we proved (i) of both theorems. Since statements (ii) and (iii) are
the same, it is reasonable to find a bigger class of matrices for which these
two statements are true, and which contains both Hermitian and unitary
matrices. This class consists of normal matrices.
A matrix is called normal if it satisfies
A⇤ A = AA⇤ .
A = B⇤B 1 ,
3
Hermitian or real symmetric matrices are easy to understand: both classes
are real vector spaces (a linear combination of Hermitian matrices with real
coefficients is Hermitian, and same for real symmetric matrices).
Unitary (or orthogonal) matrices are more difficult.
Example: describe all 2 ⇥ 2 unitary matrices with determinant 1.
Let our matrix be !
a b
A= ,
c d
where a, b, c, d are complex numbers. Let us try to write conditions on a, b, c, d
which ensure that A is unitary. By definition this means that AA⇤ = I. Since
det A = 1, !
d b
A 1= ,
c a
so we must have a = d, c = b, so
!
a b
A= , |a|2 + |b|2 = 1, a, b 2 C.
b a
With matrices of larger size, it is more difficult to describe all unitary (or
orthogonal) matrices.
Let H be a Hermitian matrix, that is H ⇤ = H. I claim that the exponen-
tial of U = exp(iH) is unitary. Indeed,
U = B⇤B 1 , ⇤ = diag( 1 , . . . , n ),
4
where | j | = 1 for all j. Therefore we can write j = exp(i✓j ) with some real
✓j , and thus
1
U = Bei⇥ B 1
= eiB⇥B , where ⇥ = diag(✓1 , . . . , ✓n ).
1
Now the matrix H = B⇥B is unitary, because
H ⇤ = (B⇥B 1 )⇤ = (B 1 )⇤ ⇥B ⇤ = B⇥B 1
= H,
5
Introduction References
February 2024
1 Introduction
2 References
1 Introduction
2 References
• Bra–ket hi notation:
• In quantum mechanics ket notation is used to denote quantum
states, and |v i is pronounced as “ket-v ”
• Mathematically |v i denotes a column vector in an abstract com-
plex vector space V , and physically it represents a quantum
state of some quantum system.
• A hf | is pronounced as “bra-f ”. Mathematically it denotes a
linear form (or linear functional) f : V ! C such that hf |v i 2 C
for v 2 V and f 2 V 0 , where V 0 is a dual space of V
• The linear form hv | 2 V 0 is a covector to |v i 2 V and obtained
by Hermitian conjugation, i.e., hv | = |v i† . This is however not
correct in a technical sense, since the ket represents a vector in
a complex Hilbert-space H, and the bra is a linear functional
on vectors in H. In other words |v i is just a vector, while hv |
is the combination of a vector and an inner product.
P. Singh SOE, JNU Delhi
Introduction to quantum computing 4 / 26
Introduction References
hT v, wi = hv, T † wi
for all vectors v, w 2 C3 , where h·, ·i is the standard inner
product.
In matrix notation, the inner products can be written as:
hT v, wi = vH [T ]H w
hv, T † wi = vH [T † ]w
Here, H denotes the conjugate transpose (Hermitian transpose)
of a matrix.
U:H!H
U| i = ↵| i
Here, | i represents a quantum state, and U| i is obtained by
multiplying the state by a complex scalar ↵.
Now, let’s find the adjoint operator U † . The adjoint satisfies:
hU| i, | ii = h| i, U † | ii
P. Singh SOE, JNU Delhi
Introduction to quantum computing 16 / 26
Introduction References
h| i, U † | ii = h |U † | i
The adjoint U † turns out to be the operator defined by:
U † | i = ↵| i
This means that the adjoint operator U † of the original quan-
tum operator U is obtained by multiplying the quantum state
| i by the complex conjugate of ↵.
In the language of quantum computing, this example illustrates
a quantum operator U and its adjoint U † acting on quantum
P. Singh SOE, JNU Delhi
Introduction to quantum computing 17 / 26
Introduction References
hAf , g i = hf , A† g i
for all f , g 2 L2 ([0, 1]), where h·, ·i is the inner product.
Let’s calculate the inner products:
Z 1
hAf , g i = x · f (x) · g (x) dx
0
Z 1
†
hf , A g i = f (x) · A† g (x) dx
0
1 Introduction
2 References
References I
[1] https://en.wikipedia.org/wiki/Bra%E2%80%93ket_
notation
[2] S. Haykin, M. Moher, Introduction to Analog and Digital Com-
munications, 2nd ed. Hoboken, NJ: John Wiley & Sons, Inc.,
2007.
[3] B.P. Lathi, Signal Processing and Linear Systems, Carmichael,
CA: Berkeley-Cambridge Press, 1998.
[4] P. Singh, S. D. Joshi, R. K. Patney, K. Saha, “The Fourier
decomposition method for nonlinear and non-stationary time
series analysis,” Proc. R. Soc. A, vol. 473 (2199), no. 20160871,
2017.
References II
References III
References IV
name operator
C D
1 0
identity I= = |v0 ÍÈv0 | + |v1 ÍÈv1 | for any orth. basis {|vx Í}1x=0
0 1
C D
0 1
Pauli X X= = |+ÍÈ+| ≠ |≠ÍÈ≠|
1 0
C D
1 0
Pauli Z Z= = |0ÍÈ0| ≠ |1ÍÈ1|
0 ≠1
C D
0 ≠i
Pauli Y Y = = | + iÍÈ+i| ≠ | ≠ iÍÈ≠i|
i 0
By drawing parallels to classical computing from the previous chapter, we can in-
troduce quantum information and computation in a natural way. Since we have not
yet defined “quantum,” just take it to mean a different set of rules, which we will
bring up as they become relevant.
Quantum physics has a reputation for being difficult and confusing, exacerbated by
(incorrect) references to quantum mechanics in movies to justify their sci-fi plot ele-
ments. To help reduce this intimidation factor and ease the introduction of quantum
computing, I made a board game called Qubit Touchdown. It is shown in Fig. 2.1,
and it is available print-on-demand from The Game Crafter.1
Qubit Touchdown is a two player game. It consists of a game board, shown in
Fig. 2.2a, an orange football token, a die with only zeros and ones on it, and fifty-
two action cards, shown in Fig. 2.2b. To start the game, one player “kicks off” by
rolling the binary die, and the football token starts at zero or one, depending on
the outcome of the roll. Beginning with the other player, players take turns playing
action cards, which move the football according to the lines and arrows
p on the game
board. For example, at position 0, H moves the ball to position +; X moves the
ball to i; X and Y move the ball to 1; and Z, I, and S keep the ball at position 0.
For the measurement card, if the football is at 0 or 1, nothing happens. Otherwise,
one kicks off again by rolling the binary die. When a player scores a touchdown by
moving the ball into their opponent’s endzone, they roll the die to kick off again,
and play continues with the other player. Whoever scores the most touchdowns, by
the time all the action cards have been used, wins.
All of these game mechanics come from quantum computing.
1 https://www.thegamecrafter.com/games/qubit-touchdown
73
74 2 One Quantum Bit
It is not necessary to play Qubit Touchdown in order to continue with this text-
book.
Exercise 2.1. In Qubit Touchdown,psay the ball is at position 0. Where would it move if you played
(a) X, (b) Y , (c) Z, (d) H, (e) S, (f) X, (g) I, (h) measurement?
Exercise 2.2. In Qubit Touchdown,psay the ball is at position i. Where would it move if you played
(a) X, (b) Y , (c) Z, (d) H, (e) S, (f) X, (g) I, (h) measurement?
2.2 Superposition
A quantum bit, or qubit, is both similar to and different from a classical bit in some
important ways. First, like a classical bit, a qubit can take two values, 0 or 1. Using
bra-ket notation or Dirac notation from quantum physics, we write 0 and 1 enclosed
between a vertical bar and an angle bracket called a ket:
|0i, |1i.
Although it may seem strange to write a quantum 0 and a quantum 1 like this, it will
be very useful later.
We can visualize these distinct states, |0i and |1i, as the north and south poles of
a sphere of radius 1 called the Bloch sphere:
2.2 Superposition 75
(a) (b)
Fig. 2.2: The Qubit Touchdown (a) game board and (b) action cards.
76 2 One Quantum Bit
z
|0i
y
x
|1i
Following the standard physics convention, the x-axis comes out of the page, the
y-axis points to the side, and z-axis is oriented up. Then, since the Bloch sphere
has radius 1, |0i corresponds to the (x, y, z) point (0, 0, 1), and |1i corresponds to
(0, 0, 1).
2.2.2 Superposition
If we had a classical bit, |0i and |1i would be the only two states. But, the laws of
quantum mechanics allow the state of a qubit to be a combination of |0i and |1i,
called a superposition of |0i and |1i. For example, here is a state that is equal parts
|0i and |1i:
1
p (|0i + |1i) .
2
p p
In this state, the coefficient of |0i is 1/ 2, and the coefficient of |1i is also 1/ 2.
So, it is equal parts |0i and |1i. Given this, it should be on the equator of the Bloch
sphere, which is halfway between the north and south poles:
z
y
x
This state is at the (x, y, z) point (1, 0, 0), where the Bloch sphere intersects the x-
axis. Later, we will learn how to calculate these coordinates, but for now, we will
focus on building geometric intuition.
2.2 Superposition 77
There are many other states on the equator of the Bloch sphere, all of which are
equal parts |0i and |1i. We can reach them by changing the relative phase of |0i and
|1i. For example, if we instead use a negative sign, we get
z
1
p (|0i |1i)
2 y
x
1
p (|0i + i|1i)
2 y
x
We see that imaginary and complex numbers are used in quantum computing. In
case it has been a while since you have used them, in the next section, we will
review complex numbers in detail. Continuing, to reach the y-axis at (0, 1, 0),
we use a phase of i:
z
1
p (|0i i|1i)
2 y
x
78 2 One Quantum Bit
These states appear frequently enough that they have names: “plus,” “minus,” “i,”
and “minus i”:
1
|+i = p (|0i + |1i) ,
2
1
| i = p (|0i |1i) ,
2
(2.1)
1
|ii = p (|0i + i|1i) ,
2
1
| ii = p (|0i i|1i) .
2
Drawing them together with |0i and |1i on the Bloch sphere, we get the following,
which also appears on the back of every action card in Qubit Touchdown:
z
|0i
| i
| ii
|ii
|+i y
x
|1i
In Qubit Touchdown, these six states correspond to the six positions on the game
board in Fig. 2.2a. The football token corresponds to a qubit, so the ball moving
around the game board corresponds to a qubit changing between these states (more
on this later).
Of course, there are many other points on the equator of the Bloch sphere. We
can reach them using other complex phases, such as
2.2 Superposition 79
1 ⇣ ⌘
p |0i + ei⇡/6 |1i
2 y
x
Superpositions are not restricted to the equator, either. They can favor |0i or |1i by
being in the northern or southern hemisphere, such as
p
3 1 2 1 2i
|0i + |1i |0i + |1i
2 2 3 3
z z
y y
x x
In fact,
Again, we will later see how to calculate where a quantum state is on the Bloch
sphere. Before we do that, however, we need to review complex numbers and discuss
measuring qubits.
Exercise 2.3. Draw a Bloch sphere and label the following locations:
(a) Where a qubit is exactly |0i.
(b) Where a qubit is exactly |1i.
(c) Where a qubit is half |0i and half |1i.
(d) Where a qubit is more |0i than |1i.
(e) Where a qubit is more |1i than |0i.
80 2 One Quantum Bit
A complex number z is a number with a real part x plus i times an imaginary part y:
z = x + iy.
p
For example, 1 + i 3 is a complex number. The parts are called components. We
denote the real component of z as ¬(z), and it just equals x:
¬(z) = x.
¡(z) = y.
p p p
For example, ¬(1 + i 3) = 1 and ¡(1 + i 3) = 3. Real numbers are complex
numbers; their imaginary parts are just zero. Similarly, imaginary numbers are com-
plex numbers; their real parts are just zero.
The above form x + iy is called the Cartesian form or rectangular form of a com-
plex number. In quantum computing, it is often useful to write a complex number
as its length r times its complex phase eiq :
z = reiq .
This is called the polar form of a complex number, and any complex number can be
written this way. To convert from the Cartesian form x + iy to the polar form reiq ,
we use the following equations, which we will prove in a moment:
p
r = x2 + y2 , (2.2)
⇣y⌘
q = tan 1 . (2.3)
x
To convert from the polar form reiq to the Cartesian form x+iy, we use the following
equations, which we will also prove in a moment:
x = r cos q (2.4)
y = r sin q . (2.5)
Im
x + iy
r y
✓
x Re
Its length is r, and the angle it makes counter-clockwise from the real axis is
q . Since x and y are the legs of a right triangle, and r is the hypotenuse, the
Pythagorean theorem 2 2 2
p says that x + y = r . Taking the square root of each
2 2
side, we get r = x + y , which proves Eq. (2.2).
Next, from the drawing, q is an angle in a right triangle, and y is opposite of
q , x is adjacent, and r is the hypotenuse. We can relate these quantities using
the trigonometric functions sine, cosine, and tangent.2 First, using sine, we get
sin q = y/r. Multiplying both sides by r, we get y = r sin q , which is Eq. (2.5).
Next using cosine, we get cos q = x/r. Multiplying both sides by r, we get
x = r cos q , which is Eq. (2.4). Finally, using tangent, we get tan q = y/x. Taking
the inverse tangent of both sides, we get q = tan 1 (y/x), which is Eq. (2.3).
• Algebraically, we can write a complex phase in Cartesian form using Euler’s
formula, which says says that
If you have taken more advanced math than is required for this textbook, you
may be familiar with Euler’s formula and its proof. (It is typically seen during
the second semester of calculus.) If not, no worries. You do not need to know
where it comes from. It is simple enough to memorize now, which you should
do, since it will show up again.
Now using Euler’s formula, we can write the polar form of a complex number
as
z = reiq = r (cos q + i sin q ) = r| cos
{z q} +i r| sin
{z q} .
x y
Thus, the real part of z is x = r cos q , which is Eq. (2.4), and the imaginary part
is y = r sin q , which is Eq. (2.5). If we take the sum of the squares of these
components, we get
2 Many people remember the trigonometric functions using the mnemonic SOH-CAH-TOA, where
sine is opposite over hypotenuse (SOH), cosine is adjacent over hypotenuse (CAH), and tangent is
opposite over adjacent (TOA).
82 2 One Quantum Bit
In the last equality, we used the Pythagorean identity sin2 q +p cos2 q = 1 from
trigonometry. Taking the square root of both sides, we get r = x2 + y2 , which
proves Eq. (2.2). Next, if we divide y = r sin q by x = r cos q , we get
y sin q
= = tan q .
x cos q
Taking the inverse tangent of both sides, we get Eq. (2.3).
p
For example, say we have a complex number in Cartesian
q form, z = 1+i 3. To con-
p 2 p p
vert this to polar form, we calculate its length r = 12 + 3 = 1 + 3 = 4 = 2
p
and angle q = tan 1 ( 3/1) = p/3 radians or 60 . The convention is to use radians,
so putting these together, the polar form is z = 2eip/3 .
There are a few more aspects of complex numbers that come up frequently in
quantum computing, so let us review them now:
• The complex conjugate (or just conjugate) of a complex number is the complex
number obtained by negating its imaginary part. That is, we replace i with i.
We denote the complex conjugate of z as z⇤ , so if z = x + iy = reiq , then
z⇤ = x iy = re iq .
p ⇤ p ⇤
From the previous example, 1 + i 3 = 1 i 3 and 2eip/3 = 2e ip/3 .
• The norm of a complex number z, which we denote |z|, is simply its length r:
|z| = r.
p
Then from the previous example, |1 + i 3| = 2, since its length r is 2.
• The norm-square of a complex number z, which we denote |z|2 , is simply the
square of its norm, so it is r2 :
|z|2 = r2 .
p
From the previous example, |1 + i 3|2 = 4. One way to calculate the norm-
square is multiplying a complex number by its conjugate:
We can prove that this works using either the Cartesian or polar form. In Carte-
sian form,
zz⇤ = reiq re iq
= r2 eiq iq
= r2 e0 = r2 = |z|2 .
Finally, Eq. (2.7) gives us another way to find the norm of z, by taking the square
root of z times its conjugate z⇤ :
2.3 Measurement 83
q p p
|z| = |z|2 = z⇤ z = zz⇤ .
2.3 Measurement
In a previous section, we had the following qubit, which was on the equator of the
Bloch sphere:
z
1 ⇣ ⌘
p |0i + ei⇡/6 |1i
2 y
x
Although the laws of quantum mechanics permit this superposition of |0i and |1i,
it also demands that if we measure the qubit, such as at the end of a computation
in order to read the result, we get a single, definite value. That is, we get |0i or
|1i, each with some probability, not a superposition of |0i and |1i. Geometrically,
this particular qubit lies on the equator, halfway between the north and south poles,
so if we measure it, we get |0i with probability 1/2 or |1i with probability 1/2. To
84 2 One Quantum Bit
calculate these probabilities, we take the norm-square of the coefficient of |0i or |1i.
That is, the probability of getting |0i is
2
1 1
p = ,
2 2
2 1 2i
|0i + |1i y
3 3
x
Geometrically, since the qubit is closer to the south pole, we expect that the prob-
ability of getting |1i is greater than the probability of getting |0i. To get the exact
probabilities, we calculate the norm-square of each amplitude:
2
2 4
= ,
3 9
and
2
1 2i 1 2i 1 + 2i 1 + 2i 2i 4i2 5
= = = .
3 3 3 9 9
So, if we measure the qubit, the probability of getting |0i is 4/9, and the probability
of getting |1i is 5/9. As expected, the probability of getting |1i is greater than the
probability of getting |0i, and also note that the total probability is 4/9 + 5/9 = 1,
as it must.
Exercise 2.6. A qubit is in the state
2.3 Measurement 85
p
1+i 3 2 i
|0i + |1i.
3 3
If you measure the qubit, what is the probability of getting
(a) |0i?
(b) |1i?
Say a qubit is superposition of |0i and |1i. Say we measure it, and the outcome
is |0i. Now for something new: The qubit is no longer in a superposition of |0i and
|1i. It is now simply |0i, and we know because we measured it. Measuring the qubit
changed it. It forced it to take a stand. We say the state has collapsed to |0i. If we
measure the qubit again, we get |0i with probability 1. This aspect of measurement
is important enough to box:
In Qubit Touchdown, playing the measurement action card, or kicking off after a
touchdown, corresponds to measuring the qubit. For the measurement action card,
if the ball is at position 0 or 1, then nothing happens, just like measuring a qubit in
state |0i or |1i returns the same state with probability 1. If the ball is at position i or
i, then the binary die is rolled, and the ball is moved to 0 or 1 with 50% probability
each, just like measuring a qubit in the |ii or | ii states collapses the state to |0i or
|1i with 50% probability. Finally, after scoring a touchdown, the ball is either at the
+ or positions in the endzones. Then, the binary die is rolled, moving the ball
to 0 or 1 with 50% probability each. In the same way, when a qubit is in the |+i
or | i state, measuring it yields |0i or |1i with 50% probability each, and the state
collapses to whichever state was measured.
Exercise 2.7. A qubit is in the state
2 1 + 2i
|0i + |1i.
3 3
Say you measure the qubit and get |0i. If you measure the qubit a second time, what is the proba-
bility of getting
(a) |0i?
(b) |1i?
2.3.2 Normalization
We normalize this state by finding the normalization constant A that ensures that the
total probability is 1. So,
p p
1 = (A 2)(A 2)⇤ + (Ai)(Ai)⇤
= 2|A|2 + |A|2
= 3|A|2
1
|A|2 = .
3
As we will prove later, the overall phase does not matter, so we might as well pick
A to be real. Thus,
1
A= p ,
3
and the normalized state is
1 ⇣p ⌘
p 2|0i + i|1i .
3
Even though we introduced |0i and |1i as the north and south poles, respectively, of
the Bloch sphere, the Bloch sphere is not a planet, and it is not spinning. Then, any
two opposite points could be taken as the north and south poles. For example, |+i
and | i could be the north and south poles, or |ii and | ii, or any opposite points.
A set of distinct measurement outcomes is called a basis, and {|0i, |1i} is called the
Z-basis because they lie on the z-axis of the Bloch sphere. Similarly, {|+i, | i} is
called the X-basis because they lie on the x-axis of the Bloch sphere, and {|ii, | ii}
is called the Y -basis because they lie on the y-axis of the Bloch sphere. We can
measure with respect to any of these bases, or with respect to any two states on
opposite sides of the Bloch sphere.
For example, consider a qubit in the state
2.3 Measurement 87
p
3 1
|0i + |1i,
2 2
which appears on the Bloch sphere at an angle of 30 above the x-axis, as shown
below:
z
p
3 1 |0i
|0i + |1i
2 2
| i
| ii •
|ii
|+i y
x
|1i
Let us measure this with respect to four different bases: the Z-basis {|0i, |1i}, the
X-basis {|+i, | i}, the Y -basis {|ii, | ii}, and a fourth basis (below).
1. If we measure the qubit in the Z-basis {|0i, |1i}, then we get |0i with probability
3/4 or |1i with probability 1/4.
2. What if we measure in the X-basis {|+i, | i} instead? Geometrically, the prob-
ability of getting |+i should be much higher than the probability of getting | i
because the state is so much closer to |+i on the Bloch sphere. To calculate the
probabilities precisely, we need to express the state in terms of |+i and | i so
that we can identify the amplitudes and then find their norm-squares. From the
definitions of |+i and | i in Eq. (2.1), we have
1 1
|0i = p (|+i + | i) , |1i = p (|+i | i) .
2 2
Substituting into the state of our qubit,
p p
3 1 3 1 1 1
|0i + |1i = p (|+i + | i) + p (|+i | i)
2 2 2 2 2 2
p p
3+1 3 1
= p |+i + p | i.
2 2 2 2
Now the amplitudes are easy to identify, and we can find the probabilities by
taking their norm-squares. The probability of measuring |+i is
p 2 p
3+1 3+2
p = ⇡ 0.93,
2 2 4
This is consistent with the Bloch sphere, since the state is much closer to |+i
than it is to | i.
3. Now what if we measure in the {|ii, | ii} basis? Geometrically, the Bloch
sphere reveals that the state is halfway between |ii and | ii, so we get one
or the other with probability 1/2 each. We can also calculate this by rewrit-
ing the state in terms of |ii and | ii and then finding the norm-square of the
amplitudes. From the definitions of |ii and | ii in Eq. (2.1),
1 i
|0i = p (|ii + | ii) , |1i = p (|ii | ii) .
2 2
Substituting,
p p
3 1 3 1 1 i
|0i + |1i = p (|ii + | ii) + p (|ii | ii)
2 2 2 2 2 2
p p
3 i 3+i
= p |ii + p | ii.
2 2 2 2
So, the probability of getting |ii is
p 2
3 i 3+1 1
p = = ,
2 2 8 2
as expected.
4. Consider the following two states, which we will call |ai and |bi:
p
3 i
|ai = |0i + |1i,
2 p 2
i 3
|bi = |0i + |1i.
2 2
Here they are on the Bloch sphere:
2.3 Measurement 89
z
p p
3 1 3 i
|0i + |1i |0i + |1i
2 2 2 2
•
y
x
p
i 3
|0i + |1i
2 2
|ai is located 30 above the y-axis, and |bi is located 30 below the y-axis.
(You will prove this in Exercise 2.16.) Since they are located on opposite points
of the Bloch sphere, they are a basis. Let us measure in this {|ai, |bi} basis.
Geometrically, the qubit is closer to |ai, so we expect a higher probability of
getting |ai than |bi. To calculate the precise numbers, we first write |0i and |1i
in terms of |ai and |bi:
p p
3 i i 3
|0i = |ai |bi, |1i = |ai + |bi.
2 2 2 2
Substituting, the state of our qubit is
p p p ! p !
3 1 3 3 i 1 i 3
|0i + |1i = |ai |bi + |ai + |bi
2 2 2 2 2 2 2 2
p
3 i 3(1 i)
= |ai + |bi.
4 4
Taking the norm-square of each amplitude, the probability of getting |ai is
2
3 i 9+1 5
= = ,
4 16 8
Later, when we describe qubits in the mathematics of linear algebra, we will see
another way to convert between bases.
Exercise 2.10. A qubit is in the state
p
1 3
|0i |1i.
2 2
90 2 One Quantum Bit
(a) If you measure it in the Z-basis {|0i, |1i}, what states can you get and with what probabilities?
(b) Write the qubit’s state in terms of |+i and | i.
(c) If you measure it in the basis {|+i, | i}, what states can you get and with what probabilities?
Exercise 2.11. The following two states are opposite points on the Bloch sphere:
p
3 i
|ai = |0i + |1i,
2 2
p
i 3
|bi = |0i + |1i.
2 2
So, we can measure relative to them. Now consider a qubit in the state
p
1 3
|0i |1i.
2 2
(a) Write the qubit’s state in terms of |ai and |bi.
(b) If you measure the qubit in the basis {|ai, |bi}, what states can you get and with what proba-
bilities?
We have already seen that measuring a qubit collapses the state to whatever was
measured. This can lead to interesting statistics, even more so if we change the
measurement basis. For example, consider the following three measurements:
1. Say we first measure the qubit in the Z-basis {|0i, |1i}. Then, the qubit collapses
to |0i or |1i.
2. Next, if we measure in the X-basis {|+i, | i}, then since both |0i and |1i are
halfway between |+i and | i, the qubit collapses to |+i or | i, each with
probability 1/2.
3. If we then measure in the Z-basis {|0i, |1i}, then since |+i and | i are halfway
between |0i and |1i, the probability of each is 1/2. We can continue alternating
between these two measurement bases, each time having a 50:50 chance of
getting each outcome.
Exercise 2.12. A qubit is in the state |0i. If you measure it in the X-basis {|+i, | i} and then
measure it again in the Z-basis {|0i, |1i}, what is the probability of getting
(a) |0i?
(b) |1i?
We know that a qubit can be visualized as a point on the Bloch sphere. Now, let us
explain how to determine where the point should be.
2.4 Bloch Sphere Mapping 91
Say the qubit from the last section is multiplied by an overall, global phase:
p !
iq 3 1
e |0i + |1i ,
2 2
for some angle q . If we measure this in the Z-basis, {|0i, |1i} the probability of
getting |0i is
p 2
iq 3 3
e = ,
2 4
as they were without the global phase. So, the phase does not change anything.
If we instead measure in the X-basis {|+i, | i}, then we can rewrite the state as
p p !
iq 3+1 3 1
e p |+i + p | i .
2 2 2 2
as they were before without the global phase. So again, the phase does not change
anything.
This is true no matter what measurement basis we use, and it leads to the follow-
ing result:
As such, global phases can be dropped/ignored. States that differ by a global phase
are actually the same state; they correspond to the same point on the Bloch sphere.
Note that a relative phase is physically significant, such as
92 2 One Quantum Bit
1
|+i = p (|0i + |1i)
2
vs
1 1 ⇣ ⌘
|ii = p (|0i + i|1i) = p |0i + eip/2 |1i .
2 2
These correspond to different points on the Bloch sphere, and they can be distin-
guished by measurements in appropriate bases. Although measuring |+i and |ii in
the Z-basis yields the same statistics, i.e., |0i with probability 1/2 or |1i with prob-
ability 1/2, measuring in the X-basis {|+i, | i}yields different results. Measuring
|+i in the X-basis always yields |+i, but measuring |ii in the X-basis yields |+i or
| i with a 50:50 probability.
Exercise 2.13. Is there a measurement that can distinguish the following pairs of states? If yes,
give a measurement. If no, explain your reasoning.
1 eip/8
(a) |+i = p (|0i + |1i) and eip/8 |+i = p (|0i + |1i).
2 2
1 1
(b) |+i = p (|0i + |1i) and | i = p (|0i |1i).
2 2
(c) |0i and eip/4 |0i.
A generic quantum state is typically called y (the Greek letter “psi,” which is pro-
nounced “sigh”), and since it is quantum, we write it as a ket |yi. Now say we have
a generic qubit |yi with some amplitudes a and b :
where |a|2 + |b |2 = 1 for normalization. Since the global phase does not matter, we
can assume that a is real and positive, and b may be complex. To determine the
location of this qubit on the Bloch sphere, we first parameterize, or write in terms
of other parameters, a and b in terms of two angles q and f :
✓ ◆ ✓ ◆
q q
a = cos , b = eif sin .
2 2
With 0 q p and 0 f < 2p, this captures all the properties we need: a is
real and positive, b is complex, and the state is normalized. Substituting, we have
rewritten the qubit’s state as
✓ ◆ ✓ ◆
q q
|yi = cos |0i + eif sin |1i. (2.8)
2 2
where ⌘ denotes “equivalent to,” and the states are equivalent because the global
phase does not matter and can be dropped. Comparing this to the Bloch sphere
parameterization in Eq. (2.8), we still need to change the minus sign to a plus sign.
We can do this using eip = 1. Then, the state is
p p
3 ip ip/6 1 3 1
|0i + e e |1i = |0i + ei5p/6 |1i.
2 2 2 2
Now it takes the form of Eq. (2.8), and we identify
✓ ◆ p ✓ ◆
q 3 q 1
cos = , eif = ei5p/6 , sin = .
2 2 2 2
Solving the first or last equation for q using the inverse cosine or inverse sine, and
solving the second equation for f , we get
p 5p
q= , f= .
3 6
Thus, plugging into Eq. (2.8), the state of the qubit is equivalent to
✓ ◆ ✓ ◆
p/3 p/3
cos |0i + ei5p/6 sin |1i.
2 2
Next, to map this qubit to a location on the Bloch sphere, we identify q and f as
the following angles:
94 2 One Quantum Bit
z
✓
y
So, q measures the angle down from the north pole, called the polar angle, and
f measures the angle across from the x-axis in the xy plane, called the azimuthal
angle. If you have taken more advanced mathematics than is required for this book,
these are precisely spherical coordinates with a radius of 1. Spherical coordinates
are typically covered in the third semester of calculus, but everything you need for
this textbook will be explained here.
Returning to our example, we had q = p/3 and f = 5p/6, so the state is located
on the Bloch sphere at an angle of p/3 or 60 from the north pole, and 5p/6 or 150
rotated to the side:
z
p
3+i 3 1
|0i |1i y
4 2
x
1
|ii = p (|0i + i|1i) .
2
(a) Where on the Bloch sphere is this state? Give your answer in (q , f ) coordinates.
(b) Sketch the point on the Bloch sphere.
(a) Where on the Bloch sphere is this state? Give your answer in (q , f ) coordinates.
2.4 Bloch Sphere Mapping 95
Exercise 2.16. Consider the following two states from Exercise 2.11:
p
3 i
|ai = |0i + |1i,
2 2
p
i 3
|bi = |0i + |1i.
2 2
Prove these are opposite points of the Bloch sphere by finding their points in spherical coordinates
(qa , fa ) and (qb , fb ). Verify that qb = p qa and fb = fa + p, which means they lie on opposite
points of the Bloch sphere.
sin
✓
cos ✓
✓ zoom
! 1
y
✓
y
x
In this picture, we drew a dashed line from the point to the z-axis, perpendicular to
it. This creates a right triangle, which we shaded gray. Since the radius of the Bloch
sphere is 1, the hypotenuse of the gray triangle is 1, which we labeled in the picture.
Then, since sin q is opposite over hypotenuse, and the hypotenuse is 1, we get that
sin q is the opposite side, which we labeled in the picture. Similarly, since cos q is
adjacent over hypotenuse, and the hypotenuse is 1, we get that cos q is the adjacent
side, which we also labeled in the picture. This adjacent side gives the z-coordinate
(height) of the point, i.e., z = cos q .
To get the x and y coordinates, we look at the projection onto the xy-plane:
96 2 One Quantum Bit
z
sin
✓
cos ✓
1
os y
✓c
sin
✓
sin
sin ✓ sin
x
First, we copied sin q from the top dashed line to below, since they are opposite sides
of a rectangle. This is the hypotenuse of the bottom gray triangle. Then, since sin f
is opposite over hypotenuse, we get that the opposite side is equal to the hypotenuse
times sin f , or sin q sin f , which we labeled in the drawing. This corresponds to
the y-coordinate (distance to the right) of the point, i.e., y = sin q sin f . Similarly,
since cos f is adjacent over hypotenuse, we get that the adjacent side is equal to
the hypotenuse times cos f , or sin q cos f . This is labeled as the left side of the
gray triangle in the drawing, and it corresponds to the x-coordinate (distance out of
the page), i.e., x = sin q cos f . Combining these results with the z-coordinate, we
can convert from spherical coordinates to Cartesian coordinates using the following
formulas:
x = sin q cos f ,
y = sin q sin f ,
z = cos q .
Continuing the previous example, we had q = p/3 and f = 5p/6, so the (x, y, z)
coordinates are of the qubit are
⇣p ⌘ ✓ ◆ p p
5p 3 3 3
x = sin cos = · = ,
3 6 2 2 4
⇣ p ⌘ ✓ 5p ◆ p p
3 1 3
y = sin sin = · = ,
3 6 2 2 4
⇣p ⌘ 1
z = cos = .
3 2
p
So, the qubit corresponds to point ( 3/4, 3/4, 1/2) on the Bloch sphere.
Exercise 2.17. In Cartesian (x, y, z), coordinates, where on the Bloch sphere is the state from
(a) Exercise 2.14?
(b) Exercise 2.15?
2.5 Physical Qubits 97
Physically, any quantum system with two distinct states can be used as a qubit.
While one is not expected to be familiar with quantum physics, some examples
include:
• Photons, or quantum particles of light, have a property called polarization. A
photon’s polarization can be vertical or horizontal, or a superposition of both,
and we can use this as a qubit.
• Trapped ions. An ion is an atom that has an overall charge (instead of being
neutral) because it has gained or lost one or more electrons. Individual ions can
be trapped in space using electric fields. Two energy levels of an ion can be used
as a qubit.
• Cold atoms. Neutral atoms can be trapped at low temperatures using a magneto-
optical trap, which uses magnetic fields and lasers to cool and trap the atoms.
Another approach is using an optical lattice constructed by laser beams. Once
trapped, two energy levels of an atom can be used as a qubit.
• Nuclear magnetic resonance. The nuclei of atoms and molecules have a quan-
tum property called spin, which can be used as a qubit. Spins can be identified
by their frequency of precession when subject to a strong magnetic field.
• Quantum dots. An electron can be bound to a small semiconductor device, sim-
ilar to an electron bound to the nucleus of an atom. In these “artificial atoms,”
the spin of an electron, which can be “spin up” or “spin down,” can be used as
a qubit.
• Defect qubits. A diamond crystal may have a missing carbon atom, and if we
replace a carbon atom next to this vacancy with a nitrogen atom, we get a
“spin triplet” that can be used for quantum computing. This is called a nitrogen-
vacancy center in diamond.
• Superconductors. In a superconducting circuit, charge flows with zero resis-
tance. The magnetic flux across an inductor and the charge on a capacitor
cause a harmonic potential energy with equally spaced, discrete energy levels.
A Josephson junction is a thin insulating layer that is added, and it changes the
potential energy so that the energy levels become unequally spaced. Then, the
energy levels can be distinguished, and two of them can be used as a qubit.
Informationally, these systems contain the same amount of information: two distinct
quantum states. So we simply use |0i and |1i for the rest of the discussion.
Exercise 2.18. A review of various ways to build a quantum computer is the article “Quantum
Computers” by Ladd et al. (2010). The published version in Nature 464, 45–53 is available at
https://dx.doi.org/10.1038/nature08812, but it may require a subscription. Search
through the text and fill in the blanks:
(a) Photons: “Realizing a qubit as the state of a photon is appealing because
photons are relatively free of the that plagues other quantum systems.”
(b) Trapped ions: “Individual atomic ions can be confined in free space with nanometre precision
using appropriate from nearby electrodes.”
(c) Cold atoms: “An array of cold neutral atoms may be confined in free space by a pattern of
crossed , forming an optical lattice.”
98 2 One Quantum Bit
Quantum gates act on qubits, like logic gates act on bits. In this section, we will
explore what quantum gates are.
A quantum gate transforms the state of a qubit into other states. As we will
see later, we often use the capital letter U to denote a quantum gate. For example,
consider a quantum gate that performs the following map:
2.6 Quantum Gates 99
p
2 i 1
U|0i = |0i |1i,
2 p 2
1 2+i
U|1i = |0i + |1i.
2 2
A quantum gate must be linear, meaning we can distribute it across superpositions:
For this to be a valid quantum gate, the total probability must remain 1. Assuming
the original state was normalized, i.e., |a|2 + |b |2 = 1, we can calculate the total
probability by summing the norm-square of each amplitude to see if it is still 1:
p 2 p 2
2 i 1 1 2+i
a +b + a +b
2 2 2 2
p ! p !
2 i 1 ⇤ 2+i ⇤1
= a +b a +b
2 2 2 2
p ! p !
1 2+i 1 2 i
+ a +b a⇤ + b ⇤
2 2 2 2
p p p p
2 ( 2 i)( 2 + i) ⇤ 2 i ⇤ 2+i 1
= |a| + ab +ba + |b |2
4 p p4 p4 p 4
21 ⇤ 2 i ⇤ 2+i 2 ( 2 + i)( 2 i)
+ |a| ab ba + |b |
4 4 4 4
3 1 1 3
= |a|2 + |b |2 + |a|2 + |b |2
4 4 4 4
= |a|2 + |b |2
= 1.
Quantum gates are linear maps that keep the total probability equal to 1.
In the next chapter, when we introduce linear algebra, we will learn that operators
correspond to tables of numbers called matrices, and a valid quantum gate is a type
of matrix called a unitary matrix. That is why quantum gates are often labeled U,
for unitary.
100 2 One Quantum Bit
Exercise 2.22. Consider a map U that transforms the Z-basis states as follows:
Exercise 2.23. Consider a map U that transforms the Z-basis states as follows:
p p
3 3+i
U|0i = |0i + |1i,
2 4
p p
3+i 3 + 3i
U|1i = |0i |1i.
4 4
Say |yi = a|0i + b |1i is a normalized quantum state, i.e., |a|2 + |b |2 = 1.
(a) Calculate U|yi.
(b) From your answer to (a), is U a valid quantum gate? Explain your reasoning.
Recall from Section 1.5 that a classical logic gate is reversible if its outputs are
unique. For example, a gate with input A and output B with the following truth table
is reversible, since it is always possible to determine the input from the output:
AB
0 1
1 0
Thus, the gate does the following to a bit:
0 ! 1,
1 ! 0.
How would this gate act on a qubit? It would map the following:
Gate|0i = |1i,
Gate|1i = |0i.
|b |2 + |a|2 = |a|2 + |b |2 = 1.
2.6 Quantum Gates 101
Thus, this classical reversible logic gate is also a valid quantum gate.
This is true in general. Any classical reversible logic gate simply permutes (shuf-
fles) the amplitudes around. This chapter is just on a single qubit, but jumping ahead
to Chapter 4 on multiple qubits, if there were more than two amplitudes, a classical
reversible logic gate would just permute them, so the state stays normalized. Thus,
In contrast, irreversible gates are not valid quantum gates. For example, consider
the irreversible gate with the following truth table:
AB
0 0
1 0
This would act on the basis states of a qubit as:
Gate|0i = |0i,
Gate|1i = |0i.
Now, the amplitudes are not permuted. Instead, they are combined. The total prob-
ability of this is
|a + b |2 = |a|2 + |b |2 + a ⇤ b + ab ⇤ = 1 + a ⇤ b + ab ⇤ 6= 1,
Exercise 2.25. Consider each of the following classical logic gates with inputs A and B, outputs C
and D, and truth table shown below. Is each gate a valid quantum gate? Why?
(a) (b)
ABCD ABCD
0 0 0 1 0 0 0 0
0 1 1 1 0 1 0 0
1 0 0 0 1 0 1 0
1 1 1 0 1 1 1 1
102 2 One Quantum Bit
Although any probability-preserving linear map is a valid quantum gate, let us list
some important one-qubit gates that frequently appear in quantum computing:
• The identity gate turns |0i into |0i and |1i into |1i, hence doing nothing:
I|0i = |0i,
I|1i = |1i.
This is a classical reversible gate (the identity gate), so it keeps states normal-
ized and is a valid quantum gate.
In Qubit Touchdown, this corresponds to the Identity Gate action card, which
does nothing to the football’s position.
• The Pauli X gate, or NOT gate, turns |0i into |1i, and |1i into |0i:
X|0i = |1i,
X|1i = |0i.
This is a classical reversible gate (the NOT gate), so it keeps states normalized
and is a valid quantum gate.
On the Bloch sphere, it can be shown that X is a rotation about the x-axis by
180 :
z
y
x
With this rotation in mind, we geometrically see that X causes |0i (the north
pole) to rotate to |1i (the south pole), and vice versa. We also see that |ii and
| ii rotate to each other, whereas |+i and | i are unchanged. Note, however,
that mathematically X| i = | i ⌘ | i since the global phase does not matter.
If we apply the X gate twice, we rotate around the x-axis of the Bloch sphere
by 360 , which does nothing. Then, X 2 = I. We can use this fact to simplify
consecutive applications of X. For example,
500
X 1001 = X 1000 X = X 2 X = I 500 X = X.
In Qubit Touchdown, the Pauli X Gate action card corresponds to the X gate.
2.6 Quantum Gates 103
• The Pauli Y gate turns |0i into i|1i, and |1i into i|0i:
Y |0i = i|1i,
Y |1i = i|0i.
This is not a classical gate at all because of the i and i. Let us prove that it is
a valid quantum gate by acting on a general superposition:
y
x
So, if we apply the Y gate twice, we rotate around the y-axis of the Bloch sphere
by 360 , which does nothing. Then, Y 2 = I. In Qubit Touchdown, the Pauli Y
Gate action card corresponds to the Y gate.
• The Pauli Z gate keeps |0i as |0i and turns |1i into |1i:
Z|0i = |0i,
Z|1i = |1i.
This is not a classical gate at all. In Exercise 2.28, you will show that this is a
valid quantum gate.
On the Bloch sphere, it can be shown that Z is a rotation about the z-axis by
180 :
104 2 One Quantum Bit
y
x
• Phase gate, which is the square root of the Z gate (i.e., S2 = Z):
S|0i = |0i,
S|1i = i|1i.
In Exercise 2.28, you will show that this is a valid quantum gate.
On the Bloch sphere, it can be shown that S is a rotation about the z-axis by 90 :
z
90
y
x
• T gate (also called p/8 gate), which is the square root the S gate (i.e., T 2 = S),
or fourth root of the Z gate:
T |0i = |0i,
T |1i = eip/4 |1i.
In Exercise 2.28, you will show that this is a valid quantum gate.
On the Bloch sphere, T is a rotation about the z-axis by 45 :
z
45
y
x
Exercise 2.28. Consider the gate Rz (q ), which rotates about the z-axis by angle q :
Rz (q )|0i = |0i,
Rz (q )|1i = eiq |1i.
The Z gate, S gate, and T gate are all specific instances of the Rz gate, with Z = Rz (p), S = Rz (p/2),
and T = Rz (p/4). Say |yi = a|0i + b |1i is a normalized quantum state, i.e., |a|2 + |b |2 = 1.
(a) Calculate Rz (q )|yi.
(b) Show that the total probability of Rz (q )|yi is 1, so Rz (q ) is a valid quantum gate, and hence,
Z, S, and T are all valid quantum gates.
• The Hadamard gate turns |0i into |+i, and |1i into | i:
1
H|0i = p (|0i + |1i) = |+i,
2
1
H|1i = p (|0i |1i) = | i.
2
In Exercise 2.29, you will show that this is a valid quantum gate.
On the Bloch sphere, it can be shown that H is a rotation about the x + z-axis by
180 :
106 2 One Quantum Bit
x+z
y
x
Then, H maps between |0i and |+i, between |1i and | i, and between |ii and
| ii. Let us also prove these algebraically. From the definition of the Hadamard
gate, we already have H|0i = |+i and H|1i = | i. Going in the other direction,
1
H|+i = H p (|0i + |1i)
2
1
= p (H|0i + H|1i)
2
1 1 1
= p p (|0i + |1i) + p (|0i |1i)
2 2 2
= |0i,
H| i = |1i.
We also have
1
H|ii = H p (|0i + i|1i)
2
1
= p (H|0i + iH|1i)
2
1 1 i
= p p (|0i + |1i) + p (|0i |1i)
2 2 2
✓ ◆
1 1+i 1 i
=p p |0i + p |1i
2 2 2
1 ⇣ ip/4 ⌘
= p e |0i + e ip/4 |1i
2
1 ⇣ ⌘
= eip/4 p |0i + e ip/2 |1i
2
ip/4 1
=e p (|0i i|1i)
2
2.6 Quantum Gates 107
= eip/4 | ii
⌘ | ii.
If we apply the H gate twice, we rotates by 360 , which does nothing. So,
H 2 = I.
In Qubit Touchdown, the Hadamard Gate action card is the Hadamard gate,
which is why it moves the football between 0 and +, between 1 and , and
between i and i.
Exercise 2.29. Say |yi = a|0i + b |1i is a normalized quantum state, i.e., |a|2 + |b |2 = 1.
(a) Calculate H|yi.
(b) Show that the total probability of H|yi is 1, so H is a valid quantum gate.
Exercise 2.30. Work out the math to show that
(a) H| i = |1i.
(b) H| ii = e ip/4 |ii ⌘ |ii.
We can combine these quantum gates to create all sorts of states. For example,
1
HST H|0i = HST p (|0i + |1i)
2
1 ⇣ ⌘
= HS p |0i + eip/4 |1i
2
1 ⇣ ⌘
= H p |0i + ei3p/4 |1i
2
1 1 1
= p p (|0i + |1i) + ei3p/4 p (|0i |1i)
2 2 2
1 h⇣ ⌘ ⇣ ⌘ i
= 1 + ei3p/4 |0i + 1 ei3p/4 |1i , (2.9)
2
where in the third line, we used ieip/4 = eip/2 eip/4 = ei3p/4 . On the Bloch sphere,
this state is in the southern hemisphere:
z
y
x
108 2 One Quantum Bit
If we measure this qubit in the Z-basis {|0i, |1i}, the probability of getting |0i is
1⇣ ⌘ 2 1⇣ ⌘1⇣ ⌘
1 + ei3p/4 = 1 + ei3p/4 1 + e i3p/4
2 2 2
1⇣ ⌘
= 1 + e i3p/4 + ei3p/4 + e0
4✓ ◆
1 3p
= 2 + 2 cos
4 4
p !
1 2
= 1
2 2
⇡ 0.146,
where to go from the second to the third line, we used Euler’s formula Eq. (2.6) so
that eiq + e iq = 2 cos q . Similarly, the probability of getting |1i is
⌘ 2 1✓ ◆ p !
1⇣ i3p/4 3p 1 2
1 e = 2 2 cos = 1+ ⇡ 0.854.
2 4 4 2 2
As expected, since the state is in the southern hemisphere, there is a greater proba-
bility of getting |1i when measuring the qubit.
Exercise 2.31. Calculate Y 51 H 99 T 36 Z 25 |0i.
Exercise 2.32. Prove that HXH = Z by showing that HXH|0i and Z|0i result in the same state,
and HXH|1i and Z|1i result in the same state. Such an equation is called a circuit identity.
You may have noticed that all the quantum gates from the last section were rotations
by some angle around some axis. This is true in general:
This is because rotations on the Bloch sphere satisfy the two properties that we
require of quantum gates: they are linear maps, and they keep the total probability
equal to 1. Proving that rotations are linear requires some math, which we sketch
below. Rotations keep the total probability equal to 1 because a qubit is a point on
the Bloch sphere, so if we rotate it, it remains a point on the Bloch sphere.
2.6 Quantum Gates 109
n̂ = nx x̂ + ny ŷ + nz ẑ.
Note n̂ is a unit vector, meaning it must has length 1, i.e., n2x + n2y + n2z = 1.
For example, the Hadamard gate is a rotation by q = 180 = p radians about the
axis halfway between the x- and z-axes, and we can express this axis by
1 1
n̂ = p x̂ + p ẑ,
2 2
where g is a global phase that we can set to anything (or drop), since it does not have
any physical relevance. p
pReturning to our example of the Hadamard gate, with q = p and n̂ = (1/ 2, 0,
1/ 2), we have
⇣ ⌘ ⇣p ⌘✓ 1 ◆
p 1
U = eig cos I i sin p X + 0Y + p Z
2 2 2 2
1
= ieig p (X + Z) .
2
To show that U is the Hadamard gate, let us see how it acts on |0i and |1i:
1 1
U|0i = ieig p (X + Z) |0i = ieig p (X|0i + Z|0i)
2 2
1
= ieig p (|1i + |0i) = ieig |+i,
2
1 1
U|1i = ieig p (X + Z) |1i = ieig p (X|1i + Z|1i)
2 2
ig 1
= ie p (|0i |1i) = ieig | i.
2
We can drop the global phase of ieig , so U|0i = |+i and U|1i = | i, which is the
Hadamard gate. Alternatively, we can choose g = p/2, and then since i = ei3p/2 ,
the global phase is ieig = ei3p/2 eip/2 = ei2p = 1.
110 2 One Quantum Bit
We can also use Eq. (2.10) to prove that rotations are linear. Note U is a sum of I,
X, Y , and Z with numbers as coefficients; we call this a linear combination of I, X,
Y , and Z. Since each of these gates distribute over superpositions, U also distributes
over superpositions, and so it is linear:
✓ ◆ ✓ ◆
q q
U(a|0i + b |1i) = eig cos I i sin (nx X + nyY + nz Z) (a|0i + b |1i)
2 2
✓ ◆ ✓ ◆
q q
= aeig cos I i sin (nx X + nyY + nz Z) |0i
2 2
✓ ◆ ✓ ◆
q q
+ b eig cos I i sin (nx X + nyY + nz Z) |1i
2 2
= aU|0i + bU|1i.
Exercise 2.35. Consider a rotation by 180 around the axis equidistant from the x-, y-, and z-axes.
Below, the first picture shows the axis on the Bloch sphere. It may be a little difficult to visualize,
however, so another description of it using a cube is shown below in the second picture. The cube
has a corner at the origin and edges of length s along the x-, y-, and z-axes. Then, the axis of rotation
goes through the origin and the point (s, s, s), and the axis of rotation is drawn as a thicker line.
z
z
x+y+z
s
(s, s, s)
y
s
x y
s
x
(a) Draw the Bloch sphere and show where |0i goes after applying the rotation. Do this without
any calculations by visualizing the rotation on the Bloch sphere.
(b) Draw the Bloch sphere and show where |1i goes after applying the rotation. Do this without
any calculations by visualizing the rotation on the Bloch sphere.
(c) What is n̂? Hint: It should have equal components in the x-, y-, and z-axes, and it should be a
unit vector.
(d) Use Eq. (2.10) to express the rotation U is terms of I, X, Y , and Z.
(e) Find U|0i.
(f) Find U|1i.
2.7 Quantum Circuits 111
Just like we can draw a classical circuit diagram consisting of bits and logic gates,
we can draw quantum circuit diagrams consisting of qubits and quantum gates. For
example, HST H|0i is
|0i H T S H
The circuit is read left-to-right, just like a classical circuit diagram. So, we start with
a single qubit in the |0i state and apply a Hadamard gate H to it, followed by a T
gate, then a phase gate S, and finally another H gate. It is implied that we measure
the qubit at the end of the circuit. We can also explicitly draw the measurement as a
meter:
|0i H T S H
2.7.2 Quirk
There are a variety of quantum gates that users can drag and drop onto the circuit,
and users can also make custom gates. Each qubit is initially a |0i, as shown below:
112 2 One Quantum Bit
The state of each qubit is visualized a couple ways. First, the probability that a
measurement of the qubit yields |1i is zero, and this is labeled as “Off.” Second,
the Bloch sphere representation of the qubit is shown, and the state is a point at the
north pole, as expected.
Now if we apply the X gate, Quirk shows the following:
Note Quirk uses to denote a single X gate, whereas we use X (later, we will
also use when the X gate is controlled by another qubit). Since X|0i = |1i, the
probability of measuring |1i is 1. This is labeled as “On,” and the Bloch sphere now
shows the state at the south pole, as expected.
Now consider H|0i = |+i:
In Quirk, it shows a 50% chance of being |1i when measured in the Z-basis, and it
lies on the x-axis of the Bloch sphere, as expected.
Finally, let us simulate HST H|0i, which we earlier computed by hand to have a
0.146 probability of being |0i and a 0.854 probability of being |1i. Simulating it in
Quirk,
Quirk displays that the probability of measuring |1i is 85.4% = 0.854, in agreement
with our previous calculations from Eq. (2.9).
Exercise 2.36. Answer the following about HY T HX|0i.
(a) Draw HY T HX|0i as a quantum circuit.
(b) Using Quirk, sketch where HY T HX|0i appears on the Bloch sphere.
(c) Using Quirk, if you measure HY T HX|0i in the Z-basis, what is the probability that you get
|0i and the probability that you get |1i?
2.8 Summary
The smallest unit of quantum information is the qubit. Besides having two orthog-
onal states |0i and |1i, a qubit can be a superposition of them with complex am-
plitudes. The norm-square of the amplitudes gives the probability of measuring the
qubit as a 0 or 1, and depending on the outcome, the qubit collapses to |0i or |1i.
Qubits can also be measured in other bases. A qubit can be visualized on the Bloch
2.8 Summary 113
sphere. Qubits are operated on by quantum gates, which are linear maps that keep
the total probability equal to 1. A single-qubit gate is a rotation on the Bloch sphere.
A quantum circuit is a drawing to depict what quantum gates act on a qubit, and an
online simulator for quantum circuits is Quirk.
Chapter 3
Linear Algebra
So far, we have done quantum computing using elementary algebra and using the
fact that quantum gates are linear, so they distribute across superpositions. This can
be tedious, however, such as when calculating HY T HX(a|0i + b |1i). Fortunately,
there is an easier way to do the math of quantum computing using linear algebra,
where numbers are arranged in columns, rows, and tables (called matrices). Ulti-
mately, linear algebra is a tool. Learning how to use a new tool may be difficult at
first, but ultimately, it makes the job easier. We will show how linear algebra can be
used for many of the calculations from the previous chapter.
We had a similar progression with classical computing. In Chapter 1, we could
do all of classical computing using truth tables, including to show that ABC +ABC +
ABC + ABC = A + B + AC. Boolean algebra makes such calculations easier, but it
requires becoming proficient enough with the tool. Linear algebra is to quantum
computing as boolean algebra is to classical computing.
We write |0i and |1i as column vectors, which are vertical lists of numbers:
✓ ◆ ✓ ◆
1 0
|0i = , |1i = .
0 1
Our notation is to write vectors in large parenthesis, but some people use square
brackets instead. These are called column vectors because they have a single col-
umn, and they have length 2 because they have two entries. Writing |0i and |1i, this
way, it is easy to write superpositions of them. A generic qubit with amplitudes a
and b would be
115
116 3 Linear Algebra
✓ ◆ ✓ ◆ ✓ ◆ ✓ ◆
1 0 a 0
|yi = a|0i + b |1i = a +b = +
0 1 0 b
✓ ◆
a
= .
b
If you measure this qubit in the Z-basis {|0i, |1i}, what states can you get and with what probabil-
ities?
hy| = a ⇤ b ⇤ .
3.1 Quantum States 117
Then, a bra is the conjugate transpose of a ket, and conversely, a ket is the conjugate
transpose of a bra. Flipping between kets and bras is called “taking the dual”, and the
dual of a ket is its✓brapversion,
◆ and the dual of a bra is its ket version. For example,
1/p 2
the dual of |ii = is
i/ 2
p p
hi| = 1/ 2 i/ 2 .
✓ ◆ ✓ ◆
1 0
We can also take the conjugate transpose of |0i = and |1i = to get the
0 1
Z-basis vectors as bras:
h0| = 1 0 , h1| = 0 1 .
hy| = a ⇤ b ⇤ = a ⇤ 0 + 0 b ⇤ = a ⇤ 1 0 + b ⇤ 0 1
= a ⇤ h0| + b ⇤ h1|.
Notice this has amplitudes a ⇤ and b ⇤ , so when we go from |yi to hy|, we need
to take the complex conjugate of the amplitudes. For example, taking the dual of
|ii = p12 |0i + pi2 |1i, we get
1 i
hi| = p h0| p h1|.
2 2
To summarize, to go between kets and bras,
✓ ◆
a
|yi = () hy| = a ⇤ b ⇤ ,
b
|yi = a|0i + b |1i () hy| = a ⇤ h0| + b ⇤ h1|.
Exercise 3.3. Consider the following two states |ai and |bi:
p
3 1 2 1 2i
|ai = |0i + |1i, |bi = |0i + |1i.
2 2 3 3
Answer the following questions:
(a) What is ha| in terms of h0| and h1|?
(b) What is ha| as a row vector?
(c) What is hb| in terms of h0| and h1|?
(d) What is hb| as a row vector?
118 3 Linear Algebra
One way to multiply |yi and |f i is by taking their inner product, and it is defined
as hy| times |f i: ✓ ◆
g
hy||f i = a ⇤ b ⇤ .
d
Typically, we combine the two vertical bars into one and write the inner product as
✓ ◆
g
hy|f i = a ⇤ b ⇤ .
d
We call hy|f i a bra-ket or bracket, and the word bracket is the origin of the terms
bra and ket. To evaluate the inner product, from linear algebra, we multiply a row
vector and a column vector by multiplying the first entry of each vector together,
multiplying the second entry of each vector together, and adding them (i.e., taking
their dot product):
hy|f i = a ⇤ g + b ⇤ d .
Note the result is just a number, or scalar. So, an inner product is also called a scalar
product.
The inner product of |f i and |yi is just the complex conjugate of the inner prod-
uct of |yi and |f i:
hf |yi = hy|f i⇤ .
To prove this, we can just do a simple calculation:
✓ ◆
a
hf |yi = g d⇤ ⇤
= g ⇤ a + d ⇤ b = (ga ⇤ + d b ⇤ )⇤ = (a ⇤ g + b ⇤ d )⇤
b
= hy|f i⇤ .
Inner products have several uses, which we will see next and throughout this
chapter.
Exercise 3.4. Consider
p
3+i 3 1
|ai = |0i + |1i,
4 2
p
1 15
|bi = |0i + |1i.
4 4
(a) Find ha|bi.
3.2 Inner Products 119
3.2.2 Orthonormality
So, hy|yi is just the total probability, and if hy|yi = 1, the state |yi is normal-
ized.
• Consider the inner product of the Z-basis states |0i and |1i:
✓ ◆
0
h0|1i = 1 0 = 1 · 0 + 0 · 1 = 0 + 0 = 0.
1
Next, consider the inner product of the X-basis states |+i and | i:
✓ ◆ ✓ ◆
1 1 1 1 1 1
h+| i = p 1 1 p = 11 = (1 1) = 0.
2 2 1 2 1 2
Finally, consider the inner product of the Y -basis states |ii and | ii:
✓ ◆ ✓ ◆
1 1 1 1 1 1
hi| ii = p 1 i p = 1 i = 1 + i2 = 0,
2 2 i 2 i 2
p
where we used i2 = ( 1)2 = 1.
In all of these, the outcome was zero. In fact, any two states on opposite sides of
the Bloch sphere have zero inner product (Exercise 3.8). We say that states with
zero inner product are orthogonal. Thus, orthogonal states are distinct measure-
ment outcomes.
• These two properties, normalized and orthogonal, can be combined into a sin-
gle word, orthonormal. So |0i and |1i are orthonormal because each state is
individually normalized, and they are orthogonal to each other. Similarly, |+i
and | i are orthonormal, and |ii and | ii are orthonormal.
Exercise 3.8. Say we have two qubits |ai and |bi. We can parameterize them in spherical coordi-
nates (q , f ) on the Bloch sphere:
✓ ◆ ✓ ◆
qa qa
|ai = cos |0i + eifa sin |1i,
2 2
✓ ◆ ✓ ◆
qb qb
|bi = cos |0i + eifb sin |1i.
2 2
Now say |ai and |bi lie on opposite points of the Bloch sphere. This means qb = p qa and
fb = fa + p. Show that ha|bi = 0, i.e., they are orthogonal. Possibly useful trigonometric identities:
Next, inner products can be used to find the amplitudes of quantum states, which
can be norm-squared to yield measurement probabilities. The amplitudes can also
be used to change the basis. As an example, consider a qubit in the following state:
p
3 1
|yi = |0i + |1i.
2 2
It appears on the Bloch sphere as shown below:
3.2 Inner Products 121
z
p
3 1 |0i
|0i + |1i
2 2
| i
| ii •
|ii
|+i y
x
|1i
In Section 2.3.3, we measured this qubit in the Z-basis, X-basis, Y -basis, and a
fourth basis. Let us see how to reproduce the results using inner products.
First, we want to find the possible measurement outcomes and their probabili-
ties if we measure |yi in the {|0i, |1i} basis. Although we can just “read off” the
amplitudes of |yi with respect to |0i and |1i and take the norm-square of each to
find the probabilities, we can also find the amplitudes using inner products and the
orthonormality of {|0i, |1i}. For example, the amplitude of |0i is
p ! p p
3 1 3 1 3
h0|yi = h0| |0i + |1i = h0|0i + h0|1i = .
2 2 2 | {z } 2 | {z } 2
1 0
When calculating this, the amplitude from |1i vanishes because |0i and |1i are or-
thogonal (i.e., h0|1i = 0). We get just the amplitude from |0i because |0i is normal-
ized (i.e., h0|0i = 1). Similarly, the amplitude of |1i is
p ! p
3 1 3 1 1
h1|yi = h1| |0i + |1i = h1|0i + h1|1i = .
2 2 2 | {z } 2 | {z } 2
0 1
Taking the norm-square of each of these, the possible measurement outcomes are
|0i with probability 3/4 and |1i with probability 1/4. Since h0|yi and h1|yi are the
amplitudes of |0i and |1i, respectively, we can write the state of the qubit as
This technique is far more useful when we cannot just read off the amplitudes,
such as when measuring in other bases. Let us measure in the X-basis {|+i, | i}
now. The amplitude of |+i is
p !
1 3 1
h+|yi = p (h0| + h1|) |0i + |1i
2 2 2
122 3 Linear Algebra
p p !
1 3 1 3 1
=p h0|0i + h0|1i + h1|0i + h1|1i
2 2 | {z } 2 | {z } 2 | {z } 2 | {z }
1 0 0 1
p !
1 3 1
=p +
2 2 2
p
3+1
= p .
2 2
This agrees with our calculation in Section 2.3.3. Similarly, the amplitude of | i is
h |yi, but now let us calculate it using linear algebra:
✓p ◆ p ! p
1 3/2 1 3 1 3 1
h |yi = p 1 1 =p = p .
2 1/2 2 2 2 2 2
Again, this agrees with Section 2.3.3. This approach is especially convenient be-
cause we can do the calculations using a computer algebra system that supports
linear algebra, like Mathematica or SageMath:
• In Mathematica,
psi={{Sqrt[3]/2},{1/2}};
minus={{1/Sqrt[2]},{-1/Sqrt[2]}};
ConjugateTranspose[minus].psi
The first line defines a column vector named psi (for |yi), and the second line
defines a column vector named minus (for | i). To take their inner product,
we take the conjugate transpose of minus (which is h |) and perform a vector
multiplication (denoted by the period) with psi. The result of this is
p
3 1
p ,
2 2
as expected.
• In SageMath,
sage: psi = vector([sqrt(3)/2,1/2])
sage: minus = vector([1/sqrt(2),-1/sqrt(2)])
sage: minus.conjugate()*psi
1/4*sqrt(3)*sqrt(2) - 1/4*sqrt(2)
The first line defines a vector psi (for |yi), and the second line defines a vector
minus (for | i). Note we do not need to specify whether it is a column vector
or row vector in SageMath. It will automatically transpose the vector to what-
ever shape is needed. In the third line, we calculate minus.conjugate() (for
h |) and multiply it onto psi using an asterisks, yielding the fourth line as the
answer. Let us simplify it:
p p p p
p p p 3 2 2 3 1
(1/4) 3 2 (1/4) 2 = = p .
4 4 2 2
3.2 Inner Products 123
In Section 2.3.3, we also measured |yi in the Y -basis and in a fourth basis. These
will be left as Exercise 3.9.
In general,
For an orthonormal basis {|ai, |bi}, the state of a qubit can be written as
Finally, we end with some terminology. We have been saying that ha|yi is the
amplitude of |yi in |ai. We can also say that ha|yi is the amount of |yi that is in
|ai. Or, ha|yi is the amount that |yi and |ai overlap. In mathematical language,
ha|yi is the projection of |yi onto |ai.
Exercise 3.9. Consider a qubit in the following state
p
3 1
|yi = |0i + |1i.
2 2
Consider measuring this qubit in the Y -basis {|ii, | ii} and the orthonormal basis {|ai, |bi}, where
p
3 i
|ai = |0i + |1i,
2 2
p
i 3
|bi = |0i + |1i.
2 2
(a) Calculate hi|yi.
(b) Calculate h i|yi.
(c) If you measure the qubit in the Y -basis, what states can you get and with what probabilities?
(d) Calculate ha|yi.
(e) Calculate hb|yi.
(f)If you measure the qubit in the {|ai, |bi} basis, what states can you get and with what proba-
bilities?
Hint: Your answers should agree with Section 2.3.3.
(b) If you measure it in the X-basis {|+i, | i}, what states can you get and with what probabili-
ties?
(c) If you measure it in the Y -basis {|ii, | ii}, what states can you get and with what probabili-
ties?
Recall a quantum gate U generally turns |0i and |1i into superpositions of |0i and
|1i:
✓ ◆
a
U|0i = a|0i + b|1i = ,
b
✓ ◆
c
U|1i = c|0i + d|1i = .
d
This is a 2⇥2 matrix because it has two rows and two columns. Plugging this matrix
into U|0i and U|1i, we get
✓ ◆✓ ◆ ✓ ◆
ac 1 a
U|0i = = ,
bd 0 b
✓ ◆✓ ◆ ✓ ◆
ac 0 c
U|1i = = .
bd 1 d
These correctly suggest that we can multiply a matrix and a vector in the following
manner: To get the first (second) entry, we take the first (second) row of the matrix
and multiply it component-by-component with the vector, then add the results:
3.3 Quantum Gates 125
✓ ◆✓ ◆ ✓ ◆ ✓ ◆
ac 1 a·1+c·0 a
= = ,
bd 0 b·1+d ·0 b
✓ ◆✓ ◆ ✓ ◆ ✓ ◆
ac 0 a·0+c·1 c
= = .
bd 1 b·0+d ·1 d
This is exactly the way matrices and vectors multiply in linear algebra.
Of course, U can also act on superpositions. If a qubit is in the state
✓ ◆
a
|yi = a|0i + b |1i = ,
b
Let us show that the matrix representation of this yields the expected result:
✓ ◆✓ ◆ ✓ ◆
ac a aa + cb
U|yi = = .
bd b ba + db
Quantum gates are matrices that keep the total probability equal to 1.
For example, we previously showed in Section 2.6.1 that the following is a valid
quantum gate because it keeps the total probability equal to 1:
p p !
2 i 1 2 i
U|0i = |0i |1i = 2
1 ,
2 2 2
p !
1
1 2+i 2
U|1i = |0i + |1i = p2+i .
2 2 2
Then, as a matrix, p !
2 i 1
U= 2 p 2 .
1 2+i
2 2
126 3 Linear Algebra
In Quirk, we can create this quantum gate by clicking the “Make Gate” button at
the top of the page. A dialog box will pop up with different options, and we want to
create a gate from a matrix:
We enter the matrix, give it a name, and then click “Create Matrix Gate.” Then, it
will appear in the bottom-right toolbar under “Custom Gates:”
We can drag this onto the main circuit like any other gate. Here it is, along with a T
and H gate:
From this, we see that HTU|0i has a 57.3% chance of collapsing to |1i, and hence
a 42.7% chance of collapsing to a |0i.
Exercise 3.12. Consider an operator U performs the following mapping on the Z-basis states:
✓ ◆ ✓ ◆
1 1 1 i
U|0i = p , U|1i = p .
2 i 2 1
(a) What is U ✓as a◆matrix?
a
(b) What is U ?
b
(c) From your answer to (b), is U a valid quantum gate? Explain your reasoning.
3.3 Quantum Gates 127
Exercise 3.13. A quantum gate U performs the following mapping on the Z-basis states:
1
U|0i = p [(3 + i)|0i (1 + i)|1i] ,
2 3
1
U|1i = p [(1 i)|0i + (3 i)|1i] .
2 3
(a) What is U as a matrix?
(b) Create U as a custom gate in Quirk. Using Quirk, if you measure HUH|0i, what are the
possible outcomes, and with what probabilities?
Of particular note is ✓ ◆
10
I= .
01
This is called the 2 ⇥ 2 identity matrix. When it acts on a vector, it does nothing. For
example, ✓ ◆✓ ◆ ✓ ◆ ✓ ◆
10 a 1a + 0b a
= = .
01 b 0a + 1b b
128 3 Linear Algebra
The same is true of large matrices. An N ⇥ N matrix with 1’s on the diagonal and
0’s everywhere else is called the N ⇥ N identity matrix.
Exercise 3.14. Recall from Eq. (2.10) than a single-qubit gate is a rotation by some angle q about
some axis n̂ = (nx , ny , nz ). Consider a quantum gate that rotates by 90 about the y-axis. Using
Eq. (2.10) with g = 0, what is this gate as a matrix?
Using linear algebra, we can compute the effect of a sequence of quantum gates. For
example, we had previously shown in Eq. (2.9) that
1 h⇣ ⌘ ⇣ ⌘ i
HST H|0i = 1 + ei3p/4 |0i + 1 ei3p/4 |1i .
2
Now, we can perform this same calculation using linear algebra by multiply each
matrix onto the vector:
✓ ◆✓ ◆✓ ◆ ✓ ◆✓ ◆
1 1 1 10 1 0 1 1 1 1
HST H|0i = p ip/4 p
2 1 1 0 i 0 e 2 1 1 0
✓ ◆✓ ◆✓ ◆✓ ◆
1 1 1 10 1 0 1
= ip/4
2 1 1 0 i 0 e 1
✓ ◆✓ ◆✓ ◆
1 1 1 10 1
= ip/4
2 1 1 0 i e
✓ ◆✓ ◆
1 1 1 1
=
2 1 1 ei3p/4
✓ ◆
1 1 + ei3p/4
= .
2 1 ei3p/4
We can also compute this using any computing system that supports linear algebra,
such as Mathematica or SageMath:
• In Mathematica,
zero = {{1}, {0}};
H = 1/Sqrt[2] {{1, 1}, {1, -1}};
S = {{1, 0}, {0, I}};
T = {{1, 0}, {0, Eˆ(I Pi/4)}};
H.S.T.H.zero
The first line defines a column vector named zero, and the second, third, and
fourth lines define the quantum gates as matrices. The fifth line multiplies them
together. Note a period (.) must be used for matrix multiplication; an asterisk
(*) denotes element-by-element multiplication. The output of this code is
⇢⇢ ⇢
1 1 ip 1 1 ip
+ ie 4 , ie 4 ,
2 2 2 2
3.3 Quantum Gates 129
The first line defines a column vector named zero, and the next three lines define
the quantum gates as matrices. The fifth line multiplies them together, and the
final two lines are the output, which is a column vector:
✓ i 1 p ◆
4 4 p2 + 12
i 1 .
4 4 2 + 12
p p p
Since (i/4 1/4) 2 = (i 1)/(2 2) and (i 1)/ 2 = ei3p/4 , this becomes
✓ ◆
1 1 + ei3p/4
,
2 1 ei3p/4
where p !
2 i 1
U= 2 p2 .
1 2+i
2 2
Calculate HTU|0i.
In Exercise 2.32, we proved the circuit identity HXH = Z by showing that HXH|0i
and Z|0i result in the same state, and HXH|1i and Z|1i result in the same state. We
can prove HXH = Z another way using linear algebra:
✓ ◆✓ ◆ ✓ ◆
1 1 1 01 1 1 1
HXH = p p
2 1 1 10 2 1 1
✓ ◆✓ ◆✓ ◆
1 1 1 01 1 1
= .
2 1 1 10 1 1
130 3 Linear Algebra
To continue this calculation, let us multiply the two matrices on the right (X and H).
The procedure is very similar to multiplying a matrix onto a column vector, except
we now have two column vectors. So, we distribute the middle matrix across the
two column vectors of the rightmost matrix.
✓ ◆ ✓ ◆✓ ◆ ✓ ◆✓ ◆!
1 1 1 01 1 01 1
HXH =
2 1 1 10 1 10 1
✓ ◆ ✓ ◆ ✓ ◆!
1 1 1 1 1
=
2 1 1 1 1
✓ ◆✓ ◆
1 1 1 1 1
= .
2 1 1 1 1
Now, we can multiply these two matrices by again distributing the left matrix so that
it multiples both columns of the right vector:
✓ ◆✓ ◆ ✓ ◆✓ ◆!
1 1 1 1 1 1 1
HXH =
2 1 1 1 1 1 1
✓ ◆ ✓ ◆! ✓ ◆ ✓ ◆
1 2 0 1 2 0 1 0
= = =
2 0 2 2 0 2 0 1
= Z.
So, we have proved HXH = Z. We can also perform these calculations using a
computer algebra system that supports linear algebra, such as Mathematica or Sage-
Math:
• In Mathematica,
H = 1/Sqrt[2] {{1, 1}, {1, -1}};
X = {{0, 1}, {1, 0}};
H.X.H
This defines the H and X gates as matrices and then multiplies them together,
and the output is
{{1,0},{0,-1}}.
This defines the H and X gates as matrices and then multiplies them together,
and the output is a 2 ⇥ 2 matrix, which is precisely Z.
3.3 Quantum Gates 131
3.3.5 Unitarity
Recall from Section 3.3.1 that if a quantum gate U transforms |0i and |1i as follows
✓ ◆
a
U|0i = a|0i + b|1i = ,
b
✓ ◆
c
U|1i = c|0i + d|1i = ,
d
We see that U|yi is a column vector, so we can also write it as a ket |Uyi:
U|yi = |Uyi.
where the second equality comes from the convention for multiplying a row vector
and a matrix, where the first column of the matrix is multiplied by the row vector
according to the usual rule to yield the first entry, and the second column of the
matrix is multiplied by the row vector according to the usual rule to yield the second
entry. As another proof, a property of the (conjugate) transpose is that (AB)† = B† A† ,
and since |yi† = hy|, we have
To summarize,
132 3 Linear Algebra
|Uyi = U|yi,
(3.1)
hUy| = hy|U † .
Using this, we can come up with an easy way to determine whether a matrix
keeps the total probability equal to 1. Consider a quantum gate (matrix) U. If it acts
on |yi, we have
U|yi = |Uyi.
For U to be a quantum gate, this must be normalized. That is, the inner product of
|Uyi with itself must equal 1:
hUy|Uyi = 1
hy|U †U|yi = hy|yi
U †U = I.
Quantum gates are unitary matrices, and unitary matrices are quantum gates.
This is why we typically use U to denote a quantum gate. It stands for unitary.
As an example application of this, is the following matrix a quantum gate?
✓ ◆
1 1 i
U=p
2 i1
Exercise 3.18. Is ✓ ◆
1 1 1
U=p
2 i i
a quantum gate? If so, what is U|0i, and what is U|1i?
3.3.6 Reversibility
1 1
M M = MM = I.
So, if we multiply a vector by both a matrix and its inverse, nothing happens to the
vector because this is equivalent to multiplying it by the identity matrix.
Now, since a quantum gate U must be unitary, it satisfies
U †U = UU † = I.
If we have a qubit and we applied a quantum gate U, we can undo the gate by
applying U † :
U †U|yi = I|yi = |yi.
Instead of multiplying |yi and |f i as an inner product hy|f i, where the bra is on
the left and the ket is on the right, another way to multiply them is by having the ket
on the left and the bra on the right, which is called an outer product:
✓ ◆
a
|yihf | = g⇤ d ⇤ .
b
134 3 Linear Algebra
To multiply these vectors according to the rules of linear algebra, we multiply each
row of |yi by each column of |f i, resulting in
✓✓ ◆ ✓ ◆ ◆ ✓✓ ⇤ ◆ ✓ ⇤ ◆◆ ✓ ⇤ ◆
a ⇤ a ag ad ag ad ⇤
|yihf | = g d ⇤ = = .
b b b g⇤ bd⇤ b g⇤ b d ⇤
The result is a 2 ⇥ 2 matrix. So, whereas inner products result in scalars, outer prod-
ucts result in matrices, and we can add outer products together to construct various
quantum gates.
For example, consider
U = |1ih0| + |0ih1|.
Let us find how this acts on |yi = a|0i + b |1i and show that it is a valid quantum
gate.
This is precisely the matrix for the X gate. To confirm that it is a valid quantum gate,
we simply show that it is unitary, i.e., if U †U = I:
✓ ◆✓ ◆ ✓ ◆
† 01 01 10
U U= = = I.
10 10 01
The outer product of |f i and |yi is just the conjugate transpose of the outer
product of |yi and |f i:
|f ihy| = |yihf |† .
We can prove this through a simple calculation:
✓ ◆ ✓ ⇤ ◆ ✓ ⇤ ◆†
g ga gb ⇤ ag ad ⇤
|f ihy| = a⇤ b ⇤ = = = |yihf |† .
d d a⇤ d b ⇤ b g⇤ b d ⇤
In the above equtaion, one may use parenthesis to clarify that the entire outer product
is conjugated and transposed, not just the bra, i.e., |f ihy| = (|yihf |)† .
Exercise 3.20. Consider the following outer product
3.4 Outer Products 135
|iih |.
1 1 1 1
p |0ih0| + p |0ih1| + p |1ih0| p |1ih1|.
2 2 2 2
(a) What is it as a matrix?
(b) Is this a valid quantum gate?
Recall from Section 3.2.3 that for any orthonormal basis {|ai, |bi}, the state of a
qubit can be written as
|yi = a|ai + b |bi,
where a = ha|yi and b = hb|yi. Substituting these values,
As indicated above, the inner products are just scalars/numbers, so instead of multi-
ply them onto the vectors |ai and |bi on the left, we can equivalently multiply them
on the right:
|yi = |ai ha|yi +|bi hb|yi .
| {z } | {z }
scalar scalar
Both of these terms are a ket times a bra times a ket. To make this more clear, we
can write them as
|yi = |aiha||yi + |bihb||yi.
Now, notice we have two outer products, |aiha| and |bihb|. Since they are both
multiplying |yi, we can factor to get
|aiha| + |bihb| = I.
This is called the completeness relation, and it indicates the state of any qubit can be
expressed in terms of |ai and |bi, a property we call completeness. We say {|ai, |bi}
forms a complete orthonormal basis. All the bases we have discussed (any two states
on opposite sides on the Bloch sphere) are complete.
Let us box this:
136 3 Linear Algebra
|aiha| + |bihb| = I.
Exercise 3.22. Verify that {|+i, | i} is a complete orthonormal basis by showing that
|+ih+| + | ih | = I.
Exercise 3.23. Verify that {|0i, |+i} is a not a complete orthonormal basis by showing that
|0ih0| + |+ih+| 6= I.
3.5 Summary
In Chapter 2, we explored the qubit, what happens when it is measured, and how
quantum gates act on it. In Chapter 3, we upgraded our tools for working with a
qubit by introducing linear algebra. Now, we are positioned to explore systems con-
sisting of multiple qubits. Sometimes, these qubits are disjoint, but other times, the
qubits are intertwined together. We will learn how quantum gates acting on multi-
qubit systems can be used to perform computations, namely adding binary numbers.
This same addition problem was explored in Chapter 1 using classical computers,
providing a comparison. Then, we will explore sets of universal quantum gates and
how to correct for errors in quantum computers.
4.1.1 Mechanics
IBM Research released an open-source board game called Entanglion to teach the
fundamental ideas and mechanics of quantum computing. It is available at https:
//entanglion.github.io, and anyone can download and print the game board
and pieces. The complete rules are available on the website, but let us summarize
the most important parts here, since they reflect the rules of quantum computing.
Entanglion is a two-player collaborative game, and the goal is to collect, as
a team, eight components to build a quantum computer that are scattered across
different planets, while avoiding detection by the planetary defenses. There are
three galaxies in the Entanglion universe: Centarious, Superious, and Entanglion,
as shown in Fig. 4.1. Centarious has two planets, Zero and One, and Superious also
has two planets, Plus and Minus. On the other hand, Entanglion has eight planets,
each holding one of the components to build a quantum computer.
Each player has one spaceship, and one is red while the other is blue. Players
determine the starting locations of their spaceships by rolling a die that only has 0
137
138 4 Multiple Quantum Bits
Fig. 4.1: The universe for the Entanglion board game. It consists of three galaxies:
Centarious, Superious, and Entanglion. The two players’ spaceships (red and blue)
move across the board from planet to planet according to the labeled paths. Dashed
paths correspond to the red player, and solid paths correspond to the blue player.
Inside the Entanglion galaxy, both spaceships move together.
and 1 as the outcomes. This is called the Centarious die because the outcomes of 0
and 1 correspond to the planets Zero and One, both in the galaxy Centarious.
The players take turns moving their spaceships to different planets by playing en-
gine cards H, X, CNOT, and SWAP. As shown in Fig. 4.1, different engine cards are
used for transitions between different planets. In Centarious and Superious, play-
ers’ spaceships can be on different planets. To move into the Entanglion galaxy, one
player must be in Centarious, and the other player must be in Superious. Then the
player in Centarious uses a CNOT engine card, and both spaceships move to the
same planet in the Entanglion galaxy. This planet is where a horizontal line from
4.1 Entanglion: A Quantum Computing Board Game 139
the Centarious planet intersects with the vertical line from the Superious planet, as
shown by the lines . pppppppppp / in Fig. 4.1. For example, if the red player is at One,
and the blue player is at Plus, and the red player uses a CNOT engine card, then
the red player moves horizontally from One to Psi Plus, and the blue player moves
vertically from Plus to Psi Plus. Inside the Entanglion galaxy, the spaceships always
move together as a pair, so they are always at the same planet.
Anytime the spaceships move to a planet in the Entanglion galaxy, or when a
player attempts to retrieve a component to build a quantum computer, there is a
chance they will be detected by the planetary defenses. The roll of an eight-sized die
determines this, and if the spaceships are detected, both of them move to a random
planet in Centarious determining by rolling the Centarious die.
The game also contains a shuffled deck of event cards, which are played when-
ever the spaceships are detected by planetary defenses, or after six engine cards
have been played. The cards are named after important scientists who contributed to
quantum physics and quantum computing, or after quantum effects. The mechanics
of these cards do not precisely correlate with actual quantum computing, so we limit
our discussion of them here.
Exercise 4.1. Refer to the Entanglion game board in Fig. 4.1.
(a) When can a player use CNOT to move between planets Zero and One?
(b) If the red player is at planet Zero and the blue player is at planet Minus, and the red player
uses a CNOT engine card, where do the players move?
(c) How can the players move between planets Psi Plus and Omega Three?
The rules of Entanglion reflect how quantum computers work. We will explore these
connections in detail throughout this chapter, but here is a quick summary:
• The red and blue spaceships are qubits.
• The planets are various states that qubits can be in. Centarious contains the
classical states |0i and |1i, Superious contains the two superposition states |+i
and | i, and Entanglion contains eight entangled states, where the states of the
two qubits are intertwined, so the spaceships move together.
• The engine cards H, X, CNOT, and SWAP are quantum gates that are applied to
the qubits. This transforms the qubits to different states, or moves the spaceships
to different planets.
• Detection by planetary defenses corresponds to a measurement. Measuring a
qubit yields a classical 0 or 1 with some probability, so the spaceships move to
planet Zero or One according to a roll of the Centarious die.
(c) What win rates for AI players corresponded to an adequate level of challenge for human
players?
(d) Entanglion is a play on what word?
When we have multiple qubits, we write their states as a tensor product ⌦. For
example, two qubits, both in the |0i state, are written
|0i ⌦ |0i,
and this is pronounced “zero tensor zero.” Often, we compress the notation and leave
out the tensor product in both writing and speech:
|0i|0i.
|00i.
With two qubits, the Z-basis is {|00i, |01i, |10i, |11i}. A general state is a super-
position of these basis states:
If we measure these two qubits in the Z-basis, we get |00i with probability |c0 |2 ,
|01i with probability |c1 |2 , |10i with probability |c2 |2 , or |11i with probability |c3 |2 .
Thus, the total probability is |c0 |2 + |c1 |2 + |c2 |2 + |c3 |2 , and it should equal 1.
With three qubits, there are eight Z-basis states |000i, |001i, |010i, |011i, |100i,
|101i, |110i, and |111i. Sometimes, these binary strings are written as decimal num-
bers |0i, |1i, . . . , |7i. Inspired by this, let us call the right qubit the zeroth qubit, the
middle qubit the first qubit, and the left qubit the second qubit, so a Z-basis state
takes the form
|b2 b1 b0 i.
Then, the decimal representation of this is
22 b2 + 21 b1 + 20 b0 .
In other words, we label qubits right-to-left, starting with zero. This convention,
where the rightmost qubit is the zeroth qubit, is called little endian. Quirk and many
quantum programming languages, including those in Chapter 5, also use little en-
dian. In contrast, the opposite convention, where the leftmost qubit is the zeroth
4.2 States and Measurement 141
qubit, is called big endian. Of note, Nielsen and Chuang’s standard advanced text-
book uses the big endian convention. Disputes over which convention is “better”
has raged classical computing for decades, and the same debates carry into quantum
computing. The reality is that you should be able to use both, but for consistency,
we use little endian throughout this textbook. Next, the general state of three qubits
is a superposition of these basis vectors:
7
 c j | ji = c0 |0i + c1 |1i + · · · + c7 |7i,
j=0
This has N amplitudes c0 through cN 1 . Thus, if we have just n = 300 qubits, then
we must keep track of N = 2300 ⇡ 2.04 ⇥ 1090 amplitudes, which is more than the
number of atoms in the visible universe (1078 to 1082 ). This is evidence, but not a
proof, that it is difficult for classical computers to simulate quantum computers. It
is evidence because classical computers cannot keep track of this many amplitudes,
but it is not a proof because it is unknown whether quantum computers need all these
amplitudes. That is, if quantum computers can function with much fewer amplitudes
(a polynomial number instead of an exponential number in n), a classical computer
would be able to keep track of all of them. In terms of complexity classes, the
exponential number of amplitudes in a general entangled state is evidence that P 6=
BQP.
We can also use powers to simplify the notation. If we have n qubits, each in the
state |0i, we can write the state as
q q
cos |0i + eif sin |1i,
2 2
with the coordinates (q , f ) interpreted as a point on the Bloch sphere. With two
qubits, however, we have four complex amplitudes c0 , c1 , c2 , c3 (although one can
142 4 Multiple Quantum Bits
be made real by factoring out an global phase), and unfortunately, this is too many
parameters to represent in three-dimensions. There is no Bloch sphere representa-
tion for a general multi-qubit state.
The tensor product also works for bras, so
Then, the inner product of, say h01| and |00i, is obtained by matching up qubits. For
example,
h01|00i = h0|0i · h1|0i = 0.
| {z } | {z }
1 0
In linear algebra, the tensor product is simply the Kronecker product, which is ob-
tained by multiplying each term of the first matrix/vector by the entire second ma-
trix/vector. For example, with two qubits,
0 ✓ ◆1 0 1
✓ ◆ ✓ ◆ 1 1
1 1 B1 0 C B0C
|00i = |0i|0i = |0i ⌦ |0i = ⌦ =B ✓ ◆C B C
@ 1 A = @0A .
0 0
0
0 0
0 ✓ ◆1 0 1
✓ ◆ ✓ ◆ 0 0
1 0 B1 1 C B1C
|01i = |0i|1i = |0i ⌦ |1i = ⌦ =B ✓ ◆C B C
@ 0 A = @0A .
0 1
0
1 0
0 ✓ ◆1 0 1
✓ ◆ ✓ ◆ 1 0
0 1 B0 0 C B0C
|10i = |1i|0i = |1i ⌦ |0i = ⌦ =B ✓ ◆C B C
@ 1 A = @1A .
1 0
1
0 0
0 ✓ ◆1 0 1
✓ ◆ ✓ ◆ 0 0
0 0 B0 1 C B0C
|11i = |1i|1i = |1i ⌦ |1i = ⌦ =B ✓ ◆C B C
@ 0 A = @0A .
1 1
1
1 1
Then,
4.2 States and Measurement 143
0 1
c0
Bc1 C
c0 |00i + c1 |01i + c2 |10i + c3 |11i = B C
@c2 A .
c3
Similarly, with three qubits, its state can be written as a column vector with eight
elements: 0 1
c0
7 Bc1 C
B C
 c j | ji = c0 |0i + c1 |1i + · · · + c7 |7i = B . C.
@ .. A
j=0
c7
With n qubits, the vector has N = 2n elements:
0 1
c0
N 1 B c1 C
B C
|yi = Â c j | ji = c0 |0i + c1 |1i + · · · + cN 1 |N 1i = B
@
..
.
C.
A
j=0
cN 1
With bras, the Kronecker product is still the tensor product. For example,
Exercise 4.6. Show that {|00i, |01i, |10i, |11i} is a complete orthonormal basis for the state of two
qubits by showing that it satisfies the completeness relation
If we measure both qubits, we would get |00i with probability 1/2, |01i with prob-
ability 1/4, |10i with probability 3/16, or |11i with probability 1/16.
Now, instead of measuring both qubits, let us only measure the left qubit. This
yields |0i or |1i with some probabilities, and the state collapses to some state, so the
outcomes are
The probability of getting |0i when measuring the left qubit is given by the sum of
the norm-squares of the amplitudes of |00i and |01i, since those both have the left
qubit as |0i. That is, the probability of getting |0i is
2 2
1 1 3
p + = .
2 2 4
Similarly, if the outcome is |1i, then from the |10i and |11i states, the probability is
p 2 2
3 1 1
+ = .
4 4 4
where A is a normalization constant. Similarly, if the outcome is |1i, then the state
collapses to the terms where the left qubit is |1i, so it becomes
p !
3 1
B |10i + |11i .
4 4
p
where B is a normalization constant. Normalizing these, we get A = 2/ 3 and B =
2, so measuring the left qubit yields
r
3 2 1
|0i with probability , and the state collapses to |00i + p |01i,
4 3 3
p
1 3 1
|1i with probability , and the state collapses to |10i + |11i.
4 2 2
We can apply these ideas to any number of qubits. For example, if we have three
qubits in the state
and we measure the left and middle qubits, the possible outcomes are
c0 |000i + c1 |001i
|00i with probability |c0 |2 + |c1 |2 , collapses to p ,
|c0 |2 + |c1 |2
c2 |010i + c3 |011i
|01i with probability |c2 |2 + |c3 |2 , collapses to p ,
|c2 |2 + |c3 |2
c4 |100i + c5 |101i
|10i with probability |c4 |2 + |c5 |2 , collapses to p ,
|c4 |2 + |c5 |2
c6 |110i + c7 |111i
|11i with probability |c6 |2 + |c7 |2 , collapses to p .
|c6 |2 + |c7 |2
We have answered the question of what happens when we measure just a single qubit
or a subset of qubits. Now, let us take this a step further and consider what happens
if we measure the qubits, one after another. For example, in the last section, we
started with two qubits in the state
p
1 1 3 1
p |00i + |01i + |10i + |11i.
2 2 4 4
32 1
Prob(|00i) = Prob(first left |0i) Prob(then right |0i) = = ,
43 2
31 1
Prob(|01i) = Prob(first left |0i) Prob(then right |1i) = = ,
43 4
13 3
Prob(|10i) = Prob(first left |1i) Prob(then right |0i) = = ,
4 4 16
11 1
Prob(|11i) = Prob(first left |1i) Prob(then right |1i) = = .
4 4 16
Notice these outcomes and probabilities are exactly the same as if we had measured
both qubits at the same time, as they should be. Measuring both qubits is the same
as measuring one after another, assuming the state was not modified between the
two measurements.
Exercise 4.9. Consider the two-qubit state
p
1 1 1 3
|00i + |01i + p |10i + |11i.
4 2 2 4
If you measure only the left qubit, what are the resulting states, and with what probabilities?
1 1 1 1 1 1 1 1
|000i + p |001i + p |010i + |011i + |100i + |101i + |110i + p |111i.
6 3 2 6 2 6 3 6 3
4.3 Entanglement 147
If you measure only the left and right qubits, but not the middle qubit, what are the resulting states,
and with what probabilities?
4.3 Entanglement
Some quantum states can be factored into (the tensor product of) individual qubit
states. For example,
1 1 1
(|00i |01i + |10i |11i) = p (|0i + |1i) ⌦ p (|0i |1i)
2 2 2
| {z } | {z }
|+i | i
= |+i ⌦ | i
= |+i| i.
To confirm this to yourself, work it out in reverse order by multiplying out the states
and showing that you get the original state. Such factorizable states are called prod-
uct states or simply separable states. Each single-qubit state can be visualized on
the Bloch sphere, so |+i| i would be two Bloch spheres, with the first at the x-axis,
and the other at the x-axis:
z z
y y
x x
Let us work through an example of how to factor a state. Say two qubits are in
the state
1 ⇣p p ⌘
p 3|00i 3|01i + |10i |11i .
2 2
We want to write this as the product of two single-qubit states,
|y1 i|y0 i,
where
|y1 i = a1 |0i + b1 |1i, |y0 i = a0 |0i + b0 |1i.
Then,
148 4 Multiple Quantum Bits
Plugging this into the second equation, we can solve for b0 in terms of a0 :
b0 = a0 .
1 1
p = p ,
2 2 2 2
which is a true statement, so it is satisfied, although it does not tell us anything new.
So, we have solved for a1 , b1 , and b0 in terms of a0 , and this is actually sufficient.
Plugging into the product state,
This only has 2n amplitudes, so a classical computer can efficiently store the ampli-
tudes of product states. If quantum computers only used product states, they would
be efficiently simulated by classical computers.
There exist quantum states that cannot be factored into product states. These are
called entangled states. For example, with two qubits,
↵ 1
F + = p (|00i + |11i)
2
cannot be written as |y1 i|y0 i. As a proof, let us try writing it as a product state
using the procedure from the last section:
In the Entanglion board game, the planets within the Entanglion galaxy corre-
spond to two-qubit states that are entangled. Planet Phi Plus is precisely |F + i.
We will discuss entanglement in more detail in Chapter 6.
150 4 Multiple Quantum Bits
Exercise 4.11. Are each of the following states a product state or entangled state? If it is a product
state, give the factorization.
1
(a) p (|01i + |10i).
2
1
(b) p (|10i + i|11i).
2
Exercise 4.12. Are each of the following states a product state or entangled state? If it is a product
state, give the factorization.
1⇣ p p ⌘
(a) 3|00i 3|01i + 3|10i |11i .
4 r
1 2
(b) p |0i|+i + |1i| i.
3 3
Say we have multiple qubits, and we want to apply a single-qubit gate (like I, X,
Y , Z, S, T , or H) to just a single qubit. For example, say we have two qubits in the
|00i = |0i ⌦ |0i state, and we want to apply the Hadamard gate to the left qubit,
but leave the right qubit alone (i.e., apply the identity gate to it). We write the gates
using a tensor product, so we write
To draw as a quantum circuit, we use the convention that the rightmost qubit corre-
sponds to the top row of the quantum circuit, and the leftmost qubit corresponds to
the bottom row of the quantum circuit:
|0i I |0i
or
|0i H |0i H
4.4 Quantum Gates 151
We follow this convention so that it matches Quirk, and in Chapter 5 the IBM Quan-
tum Composer. Nielsen and Chuang follows the opposite convention, where the
leftmost qubit corresponds to the top row of the quantum circuit.
We can find H ⌦ I as a matrix a couple different ways. First, we can find how
H ⌦ I acts on each of the basis states |00i, |01i, |10i, |11i. We already found how it
acts on |00i above. Continuing with the rest,
0 1
0
1 1 B 1C
(H ⌦ I)|01i = p (|01i + |11i) = p B C,
2 2 0A
@
1
0 1
1
1 1 B 0C
(H ⌦ I)|10i = p (|00i |10i) = p B @
C,
2 2 1A
0
0 1
0
1 1 B 1C
(H ⌦ I)|11i = p (|01i |11i) = p B C.
2 2 0A
@
1
The second way to find this matrix is by taking the Kronecker product of H and I:
0 ✓ ◆ ✓ ◆1
✓ ◆ ✓ ◆ 10 10
1· 1·
1 1 1 10 1 B 0 1 C
✓0 1 ◆C
H ⌦I = p ⌦ =p B @
✓ ◆
A
2 1 1 0 1 2 1· 1 0 1 0
1·
01 01
0 1
10 1 0
1 B0 1 0 1 C
=p B C.
2 @1 0 1 0 A
01 0 1
This matches what we previously obtained. We can also find the Kronecker product
using Mathematica or SageMath:
• In Mathematica,
H=1/Sqrt[2]*{{1,1},{1,-1}};
eye={{1,0},{0,1}};
KroneckerProduct[H,eye]
152 4 Multiple Quantum Bits
• In SageMath,
sage: H = 1/sqrt(2) * Matrix([[1,1],[1,-1]])
sage: eye = Matrix([[1,0],[0,1]])
sage: H.tensor_product(eye)
As another example, to act on the left qubit with H and the right qubit with X,
we would write H ⌦ X, so
1
(H ⌦ X)|0i|0i = |+i|1i = p (|01i + |11i) .
2
As a quantum circuit, we would draw this as
|0i X
|0i H
This is consistent with the state |+i|1i. Since the right/top qubit is |1i, Quirk cor-
rectly shows that the probability of getting |1i when measuring it is 100% (On),
and it correctly draws the state at the south pole of the Bloch sphere. Similarly, the
left/bottom qubit is |+i, and Quirk correctly shows that the probability of measuring
it to be |1i is 50%, and it correctly draws the state at the x-axis of the Bloch sphere.
In additional, Quirk also depicts the amplitudes on the real-imaginary plane, labeled
“Final amplitudes.” There are four boxes, and the top-left box depicts the amplitude
of p|00i, which is zero, and the top-right box depicts the amplitude of |01i, which is
1/ 2. Since this is real, it corresponds to a vector pointing along the real axis of the
real-imaginary
p plane. The background is also half filled, indicating a probability of
|1/ 2|2 = 1/2. Mousing over, we get
p
and the amplitude is also explicitly given as 0.70711 = 1/ 2, which has a phase
or angle of 0 on the real-imaginary plane since it is purely real, and a norm-square
magnitude of 50%. The bottom-left box depicts the amplitude of |10i, which pis zero,
and finally the bottom-right box depicts the amplitude of |11i, which is 1/ 2.
4.4 Quantum Gates 153
H ⌦n |0i⌦n = |+i⌦n .
Note one-qubit gates are unable to create entangled states because each qubit
evolves independently of the others. To create entanglement, we need quantum gates
that operate on multiple qubits at a time.
Exercise 4.13. In this problem, you will prove some of the game mechanics of Entanglion. Please
refer to Fig. 4.1 for the game board. If the players are on planet Psi Plus, and either player uses an
X engine card, they both move to planet Phi Plus, and vice versa. Similarly, if the players are on
planet Psi Minus, and either player uses an X engine card, they both move to planet Phi Minus,
and vice versa. These planets correspond to the following states:
↵ 1
F + = p (|00i + |11i) ,
2
↵ 1
F = p (|00i |11i) ,
2
↵ 1
Y + = p (|01i + |10i) ,
2
↵ 1
Y = p (|01i |10i) .
2
(a) Show that when the X gate is applied to either qubit of |Y + i, the result is |F + i, up to a global
phase.
(b) Show that when the X gate is applied to either qubit of |F + i, the result is |Y + i, up to a global
phase.
(c) Show that when the X gate is applied to either qubit of |Y i, the result is |F i, up to a global
phase.
(d) Show that when the X gate is applied to either qubit of |F i, the result is |Y i, up to a global
phase.
Quantum gates can also operate on two qubits at the same time. Some important
examples include:
• The CNOT gate or controlled-NOT gate inverts the right qubit if the left qubit
is 1:
154 4 Multiple Quantum Bits
CNOT|00i = |00i,
CNOT|01i = |01i,
CNOT|10i = |11i,
CNOT|11i = |10i.
The left qubit is called the control qubit, and the right qubit is called the target
qubit. Note the control qubit is unchanged by CNOT, whereas the target qubit
becomes the XOR (exclusive OR) of the inputs:
Thus, CNOT is a quantum XOR gate. Also, since the X gate is the NOT gate,
the CNOT gate is also called the CX gate or controlled-X gate.
In Entanglion (see Fig. 4.1), the player who uses the CNOT engine card is the
target qubit, and the other player is the control qubit. So, you can move between
planets Zero and One by playing a CNOT engine card when the other player is
at One.
Acting on a superposition,
•
4.4 Quantum Gates 155
The solid dot indicates control, and the denotes the target, which is the XOR
of the control and the target. Simulating this in Quirk, we drag an X gate onto the
top line and a “Control” solid dot, which is in the top Toolbox under “Probes,”
onto the bottom line:
We also clicked on the initial state of the control qubit to change it to |1i (alter-
natively, we could leave the initial state as |0i and apply X to it, resulting in |1i).
This triggers the CNOT, changing the target from |0i to |1i. The result is that
both qubits are “On” with 100% probability. They are both at the south poles of
their Bloch spheres, and the amplitude of |11i is 1.
To further clarify the control and target qubits, we may write CNOT with sub-
scripts:
CNOTi j = CNOT with qubit i as the control and qubit j as the target.
Since we label the qubits from right-to-left starting with 0, we have been using
CNOT = CNOT10 .
If we instead want the control and target to be flipped, it would be CNOT01 , and
we would draw the circuit as
•
To simulate this in Quirk, we just put the control on the zeroth qubit and the X
gate on the first qubit:
We set the control qubit to |1i, and so the CNOT gate flipped the target to |1i.
Another way to flip the control and target qubits is to apply Hadamard gates to
both sides of the CNOT:
H H •
=
H • H
In other words,
(H ⌦ H)CNOT(H ⌦ H) = CNOT01
156 4 Multiple Quantum Bits
We can prove this circuit identity using either elementary algebra or linear alge-
bra. First, using elementary algebra, the right-hand-side of equation yields the
following when applied to a superposition of the Z-basis states:
This is the same state, and so we have proved the circuit identity. It was rather
tedious, however. Proving the circuit identity using linear algebra is easier. First,
4.4 Quantum Gates 157
note that 0 1
1 0 0 0
B0 0 0 1C
CNOT01 = B
@0
C,
0 1 0A
0 1 0 0
since its columns show that |00i stays |00i, |01i becomes |11i, |10i stays |10i,
and |11i becomes |01i. Now, let us show that (H ⌦ H)CNOT(H ⌦ H) corre-
sponds to the same matrix. First,
0 1
✓ ◆ ✓ ◆ 1 1 1 1
1 1 1 1 1 1 1 B1 1 1 1C
H ⌦H = p ⌦p = B C.
2 1 1 2 1 1 2 @1 1 1 1A
1 1 1 1
Then,
(H ⌦ H)CNOT(H ⌦ H)
0 10 1 0 1
1 1 1 1 1 0 0 0 1 1 1 1
1BB1 1 1 1C B
C B0 1 0 0C B
C 1 B1 1 1 1C
C
= @
2 1 1 1 1A @0 0 0 1 2 1 1 1 1A
A @
1 1 1 1 0 0 1 0 1 1 1 1
0 10 1
1 1 1 1 1 1 1 1
1B 1 1 1 1C B1 1 1 1C
= B @
CB C
4 1 1 1 1A @1 1 1 1A
1 1 1 1 1 1 1 1
0 1
4000
1 B0 0 0 4C
= B C
4 @0 0 4 0A
0400
0 1
1000
B0 0 0 1C
=B C
@0 0 1 0A .
0100
This is precisely CNOT01 , and so we have proved the circuit identity using
linear algebra. We also could have computed it using Mathematica or SageMath.
Simulating the identity in Quirk,
158 4 Multiple Quantum Bits
Since the top qubit is initially |1i, and it is now the control qubit, the bottom
qubit gets flipped to |1i. So, both qubits are “On.”
The CNOT gate is important because it can produce entanglement. For example,
1 1 ↵
CNOT|+i|0i = CNOT p (|00i + |10i) = p (|00i + |11i) = F+ ,
2 2
1 1 ↵
CNOT| i|0i = CNOT p (|00i |10i) = p (|00i |11i) = F ,
2 2
1 1 ↵
CNOT|+i|1i = CNOT p (|01i + |11i) = p (|01i + |10i) = Y+ ,
2 2
1 1 ↵
CNOT| i|1i = CNOT p (|01i |11i) = p (|01i |10i) = Y .
2 2
Exercise 4.15. Prove the following circuit identities, such as by finding the matrix representation
of each circuit.
(a) CNOT(X ⌦ I) = (X ⌦ X)CNOT.
X
=
X • • X
(b) CNOT(I ⌦ X) = (I ⌦ X)CNOT.
X X
=
• •
(c) CNOT(Z ⌦ I) = (Z ⌦ I)CNOT.
=
Z • • Z
(d) CNOT(I ⌦ Z) = (Z ⌦ Z)CNOT.
4.4 Quantum Gates 159
Z Z
=
• • Z
Exercise 4.16. Consider the following circuit, which consists of two CNOTs.
A • A0
B • B0
C C0
(a) What is the truth table for this circuit?
(b) How does it compare to the reversible circuit for XOR in Exercise 1.43?
Exercise 4.17. Recall CNOT flips the right qubit if the left qubit is 1. The anti-controlled-NOT
gate flips the right qubit if the left qubit is 0. As a quantum circuit, the anti-control is drawn as an
open dot instead of a solid dot. Prove that it can be obtained from an ordinary CNOT by applying
an X gate to each side of the control:
=
X • X
Exercise 4.18. If we apply CNOT in the Z-basis {|00i, |01i, |10i, |11i}, the left qubit acts as the
control and the right qubit acts as the target. In this problem, we will prove that in the X-basis
{|++i, |+ i, | +i, | i, if the right qubit is | i, the left qubit gets flipped between |+i and | i,
so the control and target are reversed. That is,
CNOT|+i|+i = |+i|+i,
CNOT|+i| i = | i| i,
CNOT| i|+i = | i|+i,
CNOT| i| i = |+i| i.
To prove these four equations, we start with the circuit identity from the main text:
(H ⌦ H)CNOT(H ⌦ H) = CNOT01 .
Now it is straightforward to prove how CNOT acts in the X-basis. Beginning with |++i,
Work out how CNOT acts on the remaining three basis states |+ i, | +i, and | i.
160 4 Multiple Quantum Bits
Exercise 4.19. Prove that the Bell basis satisfies the completeness relation:
↵⌦ ↵⌦ ↵⌦ ↵⌦
F+ F+ + F F + Y+ Y+ + Y Y = I,
• Just like CNOT, the controlled-U gate applies some quantum gate U to the right
qubit if the left qubit is 1:
CU|00i = |00i,
CU|01i = |01i,
CU|10i = |1i ⌦U|0i,
CU|11i = |1i ⌦U|1i.
To get the matrix representation of CU, first say U acts on a single qubit as
So, U as a 2 ⇥ 2 matrix is ✓ ◆
ac
U= .
bd
Then,
CU|00i = |00i,
CU|01i = |01i,
CU|10i = |1i ⌦ (a|0i + b|1i) = a|10i + b|11i,
CU|11i = |1i ⌦ (c|0i + d|1i) = c|10i + d|11i.
SWAP|00i = |00i,
SWAP|01i = |10i,
SWAP|10i = |01i,
SWAP|11i = |11i.
In other words,
SWAP|ai|bi = |bi|ai.
This gate cannot produce entanglement because, if the qubits are in a product
state, swapping the factors results in a product state. Acting on a superposition,
We also included an X gate so that the top qubit is a |1i. This swaps with the
bottom qubit, which then swaps with the middle qubit, so the result is that the
middle qubit is |1i.
A SWAP gate can also be created using three CNOT gates:
⇥ •
=
⇥ • •
Or as an equation,
As a proof, we can work through what each CNOT does and show that the result
is a SWAP:
CNOT CNOT01
|ai|bi ! |ai|a bi ! |a a bi|a bi = |(a a) bi|a bi
CNOT
= |0 bi|a bi = |bi|a bi ! |bi|a b bi = |bi|ai.
As another proof, we can multiply the three CNOTs as matrices and show that
we get the matrix of a SWAP:
0 10 10 1 0 1
1000 1000 1000 1000
B0 1 0 0C B0 0 0 1C B0 1 0 0C B0 0 1 0C
B CB CB C B C
@0 0 0 1A @0 0 1 0A @0 0 0 1A = @0 1 0 0A = SWAP.
0010 0100 0010 0001
Exercise 4.21. Entanglion contains four yellow planets besides the Bell States. Please see the game
board at Fig. 4.1. They are labeled Omega Zero through Omega Three. These are not standard
names, but they correspond to the quantum states
1
|w0 i = (|00i |01i + |10i + |11i) ,
2
1
|w1 i = ( |00i + |01i + |10i + |11i) ,
2
1
|w2 i = (|00i + |01i + |10i |11i) ,
2
1
|w3 i = (|00i + |01i |10i + |11i) .
2
The blue player corresponds to the left qubit, and the red player corresponds to the right qubit.
(a) Show that when the SWAP gate is applied to |w0 i, we get |w3 i.
4.4 Quantum Gates 163
(b) Show that when X is applied to the left qubit of |w1 i, we get |w3 i.
(c) Show that when CNOT01 is applied to |w2 i, we get |w0 i.
(d) Show that when CNOT = CNOT10 is applied to |w3 i, we get |w2 i.
Exercise 4.22. The Mølmer-Sørensen (MS) gate is a two-qubit gate that can be naturally imple-
mented on trapped ion quantum computers. It transforms Z-basis states by
1
|00i ! p (|00i + i|11i) ,
2
1
|01i ! p (|01i i|10i) ,
2
1
|10i ! p (|10i i|01i) ,
2
1
|11i ! p (|11i + i|00i) .
2
(a) What is the MS gate as a matrix?
(b) Show that MS8 = I. (You may use a computer.)
A three-qubit gate that often appears in quantum computing is the Toffoli gate, or
controlled-controlled-NOT gate, that we discussed in Section 1.5.3. Since it is re-
versible, it is a quantum gate, and it flips the right qubit if the left and middle qubits
are 1:
Toffoli|000i = |000i,
Toffoli|001i = |001i,
Toffoli|010i = |010i,
Toffoli|011i = |011i,
Toffoli|100i = |100i,
Toffoli|101i = |101i,
Toffoli|110i = |111i,
Toffoli|111i = |110i.
Or
Toffoli|ai|bi|ci = |ai|bi|ab ci.
Recall from Section 1.5.3 that the Toffoli gate is universal for classical comput-
ing, and any efficient classical algorithm can be converted into an efficient algorithm
only utilizing Toffoli gates. Since the Toffoli gate is a quantum gate, quantum com-
puters can efficiently do everything a classical computer can efficiently do. In terms
of complexity classes, P is contained within BQP.
As a matrix, the columns correspond to Toffoli acting on |000i, |001i, . . . , |111i:
164 4 Multiple Quantum Bits
0 1
1 0 0 0 0 0 0 0
B0 1 0 0 0 0 0 0C
B C
B0 0 1 0 0 0 0 0C
B C
B0 0 0 1 0 0 0 0C
Toffoli = B
B0
C.
B 0 0 0 1 0 0 0CC
B0 0 0 0 0 1 0 0C
B C
@0 0 0 0 0 0 0 1A
0 0 0 0 0 0 1 0
•
•
In Quirk, we simply drag two control dots onto the circuit, along with the X gate:
We made the bottom two qubits both in the |1i state, so the Toffoli gate flips the top
qubit to |1i.
Exercise 4.23. Show that the Toffoli gate can be constructed from the one-qubit gates Hadamard
H, phase S, T , and T † , plus the two-qubit CNOT gate:
H T† T T† T H
• = • • T† T† S
• • • • • T
Just do the matrix multiplications on the computer.
Exercise 4.24. Consider the anti-Toffoli gate, which was introduced in Exercise 1.41. In quan-
tum computing, it is typically drawn like the anti-CNOT gate from Exercise 4.17, with open dots
indicating the anti-controls:
(a) How does the anti-Toffoli gate act on each basis state?
(b) What is the anti-Toffoli gate as a matrix?
4.4 Quantum Gates 165
Classically, it is easy to copy or clone information by reading each bit and writing
it somewhere. In quantum computing, cloning qubits is more complicated. Say we
have a qubit in some superposition state. If we measure it in the Z-basis, we get |0i
or |1i with some probability. So, we do not learn the original superposition state.
Furthermore, the measurement collapses the state to |0i or |1i, meaning we lost
whatever superposition state we originally had.
To investigate this in greater detail, say we have a qubit in a known quantum
state, such as |+i. Since we know its state, we can produce additional copies of it:
I⌦H
|+i|0i ! |+i|+i.
We went from having one copy to two. So, copying a known quantum state is no
problem.
The issue is copying an unknown quantum state. Say we have a qubit in an un-
known quantum state |yi = a|0i + b |1i, and we would like to make a copy of it:
|yi|0i ! |yi|yi.
Is there a quantum gate U that allows us to copy or clone a general unknown qubit?
U would need to satisfy
U|yi|0i = |yi|yi.
Expressing this using linear algebra, we require
0 1
U11 U12 U13 U14 ✓ ◆ ✓ ◆ ✓ ◆ ✓ ◆
BU21 U22 U23 U24 C a 1 a a
B C
@U31 U32 U33 U34 A b ⌦ 0 = b ⌦ b
U41 U42 U43 U44
0 10 1 0 2 1
U11 U12 U13 U14 a a
BU21 U22 U23 U24 C B 0 C Bab C
B CB C B C
@U31 U32 U33 U34 A @b A = @ab A
U41 U42 U43 U44 0 b2
0 1 0 21
U11 a +U13 b a
BU21 a +U23 b C Bab C
B C B C
@U31 a +U33 b A = @ab A
U41 a +U43 b b2
but this requires knowing a and b , which we do not know. Any general solution
requires knowing a and b , so there is no operator U that allows us to copy a general,
unknown quantum state.
As another “proof,” U|yi|0i = |yi|yi is akin to going from y to y 2 , and this is
quadratic, not linear. The mathematics we are using is called linear algebra because
matrices are linear. Vectors are transformed by linear transformations.
This result is called the no-cloning theorem. While classical information can be
cloned, quantum information can not generally be cloned.
Using this theorem, some scientists have proposed quantum software that cannot
be copied or pirated, and quantum money that cannot be copied or counterfeited,
but that is beyond the scope of this textbook.
Exercise 4.25. Say there is a unitary U that is able to clone qubits in two known states |yi and
|f i. That is,
U|yi|0i = |yi|yi,
U|f i|0i = |f i|f i.
For example, an operator that can clone both |0i and |1i is CNOT, since CNOT|00i = |00i and
CNOT|10i = |11i. Taking the inner product of the previous two equations,
For hy|f i to be equal to its square, it must equal 0 or 1. Thus, |yi = |f i, or |yi and |f i are
orthogonal. Thus, an operator can only clone states that are orthogonal.
Does there exist a quantum operator U that can clone both
(a) |+i and | i?
(b) |ii and | ii?
(c) |0i and |+i?
In Section 1.3, after defining classical bits and logic gates, we demonstrated how
to compute something: the sum of two binary numbers, each of length n. Now that
we have defined qubits and quantum gates, let us also construct quantum circuits
that add binary numbers. Before we do that, however, let us review the classical
ripple-carry adder.
(carry) 1 1 1 0 0
1011
“+” 1110
(sum) 11001
(carry) c4 c3 c2 c1 c0
a3 a2 a1 a0
“+” b3 b2 b1 b0
(sum) s4 s3 s2 s1 s0
where the initial carry in is c0 = 0. In general, if the binary numbers have length n,
then the output has length n + 1.
Classically, we can add binary numbers using the ripple-carry adder from Sec-
tion 1.3.5:
C0 = 0 S0
A0 FA
B0 S1
A1 FA
B1 S2
A2 FA
B2 S3
A3 FA
B3 C 4 = S4
Each full adder has three inputs: a carry in Cin and two bits A and B. From these, it
computes the sum S = A B Cin and the carry out is Cout = AB +Cin (A B).
168 4 Multiple Quantum Bits
This full adder is not reversible, since it does not have enough outputs to uniquely
determine the inputs. So, it is not a quantum gate. There are several ways, however,
to convert it to a quantum gate.
1. From Exercise 1.45, we can turn the full adder into a reversible circuit by taking
the XOR of each of its outputs with and extra bit:
Cin Cin
A A
B B
S
FA
Cout
S D
D
Cout E
E
This entire circuit can be drawn as a single gate with five inputs and five outputs:
Cin Cin
A A
B FA B
Rev
D S D
E Cout E
Cin
S
A
B Cout
One approach is to replace all five of these logic gates with (more than five)
NAND gates. Then, we can implement each NAND gate using a Toffoli gate,
which is reversible and a quantum gate. While this works in principle, the pro-
cedure can be wasteful, leading to extra gates and qubits.
3. Adapting the previous method, instead of converting all the logic gates into
NAND gates and then Toffoli gates, we convert each logic gate into a re-
versible/quantum gate more directly. The basic logic gates are NOT, AND, OR,
XOR, NAND, and NOR. From Section 2.6.3, the X gate is simply the NOT
gate. From Section 1.5.3, the Toffoli gate can implement AND and NAND by
setting its third bit to 0 or 1, respectively. From Exercise 1.41, the anti-Toffoli
gate can implement NOR and OR by setting its third bit to 0 or 1, respectively.
From Exercise 4.16, two CNOT gates can be used to implement XOR. These
results are summarized in the following table:
170 4 Multiple Quantum Bits
Classical Reversible/Quantum
NOT A A X-Gate A X A
A A • A
AND AB Toffoli B • B
B 0 AB
A A
A
OR A+B anti-Toffoli B B
B
1 A+B
A • A
A
XOR A B CNOTs B • B
B
0 A B
A A • A
NAND AB Toffoli B • B
B 1 AB
A A
A
NOR A+B anti-Toffoli B B
B
0 A+B
Replacing each gate in the full adder, we get the following circuit:
Cin • • Cin
A • • A
B • • B
A B
0 • • A B
A B Cin
0 S
AB
0 AB
Cin (A B)
0 Cin (A B)
0 X Cout
The first two CNOTs implement an XOR gate (A B), and the next two CNOTs
also implement an XOR gate, computing A B Cin = S. Next, the Toffoli gate
implements an AND gate (AB), and another Toffoli gate implements another
AND gate [Cin (A B)]. Finally, an X gate turns an extra bit from a 0 to a 1,
and together with an anti-Toffoli gate, they implement an OR gate, yielding
AB + Cin (A B) = Cout . Since these are all reversible gates, it is a quantum
circuit.
4.5 Quantum Adders 171
Notice this method uses several extra bits. Besides the inputs (Cin , A, and B) and
the outputs (S and Cout ), three extra bits were used for intermediate calculations:
A B, AB, and Cin (A B). These extra bits are called ancilla bits or ancillary
bits, and in quantum circuits, they should be cleaned up by turning them back
into zeros. This is so they can be reused in later parts of a circuit and so that they
do not cause unintended entanglement. One method for cleaning up ancillary
bits is called uncomputation, where we apply in reverse order the inverses of
the gates that were used to calculate the ancillas. Since the Toffoli and CNOT
gates are their own inverses, the full adder becomes
Cin • • • Cin
A • • • • A
B • • • • B
0 • • • 0
0 S
0 0
0 0
0 X Cout
We see that with all three inputs Cin , A, and B set to 1, both the sum S and carry-
out Cout are 1, as expected, and each of the three ancilla qubits correctly start
and end in |0i.
4. We can come up with a more clever implementation that uses fewer gates and
bits. Let us do this over the next several sections.
172 4 Multiple Quantum Bits
Quantumly, we can encode the binary numbers in two quantum registers |ai and |bi.
One way to add them reversibly is to replace |bi with the sum:
|ai|bi ! |ai|si,
where s = a + b. For example, using the quantum adder, 1011 + 1110 = 11001,
would be
|1011i|01110i ! |1011i|11001i.
By adding this way, it is always possible to determine the inputs: a is the left register,
and b can be obtained by subtracting a from s. Since the sum can have length n + 1,
this means our second register needs an extra qubit |bn i that is initially |0i:
where s = a + b.
In the intermediate steps of the computation, the quantum adder also needs to
keep track of carry bits, so we also have a quantum register of length n for the carry
bits:
|ci = |cn 1 i . . . |c1 i|c0 i.
Initially, this ancillary carry register contains all zeros, and at the end of our com-
putation, we should restore them to all zeros. Putting these together, we want our
quantum adder to map
|ai|bi|ci ! |ai|si|ci.
Since CNOT|ai|bi = |ai|a bi, we can implement the sum using two CNOTs:
|ci i
|ci i • |ci i
|ai i
|ai i • |ai i
|ai bi i
|bi i |ai bi ci i = |si i
In the above circuit, the first CNOT turns |bi i into |ai bi i, and the second turns it
into |ai bi ci i, which is |si i: We can combine this into a single quantum gate:
4.5 Quantum Adders 173
|ci i |ci i
|ai i S |ai i
|bi i |ai bi ci i = |si i
There are also several ways to create custom gates in Quirk. One way is to first
create the sum by dragging and dropping controls and X gates:
Next, we can click on the “Make Gate” button at the top of the page. A dialog box
will pop up with different options, and we want to create a gate from the circuit we
just drew:
We can either turn the whole circuit into a gate, or we can just select the first two
columns (1:2). Let us also name the gate “S.” If we click “Create Circuit Gate,” we
return to the main screen, and now our gate appears at the bottom right toolbar under
“Custom Gates:”
174 4 Multiple Quantum Bits
We can drag this onto the main circuit like any other gate:
By changing the initial state of the qubits to |1i, we can also test the circuit to verify
that it adds correctly.
Recall from Exercise 1.30) that the OR gate that is used to calculate Cout can be
replaced by an XOR gate. That is,
This allows us to create the AND of A and B, XORed with C. Then, a quantum carry
circuit is
|ci i |ci i
|ci i • |ci i
|ai i |ai i
|ai i • • |ai i
|bi i |ai bi i
|bi i • • |ai bi i
|ai bi ci+1 i |ai bi ci+1 i
|ci+1 i |ai bi ci (ai bi ) ci+1 i
In the above circuit, when ci+1 = 0, this carry gate transforms it to ai bi ci (ai bi ),
which is precisely Cout . Combining all this into a single quantum gate,
4.5 Quantum Adders 175
|ci i |ci i
|ai i |ai i
C
|bi i |ai bi i
|ci+1 i |ai bi ci (ai bi ) ci+1 i
Note in the third row that bi ! ai bi , so we will need to uncompute this later.
Now, let us construct a quantum adder that was proposed by Vedral, Barenco, and
Ekert in 1996. We order the wires to alternate between ci , ai , and bi :
c0 = 0
a0
b0
c1 = 0
a1
b1
c2 = 0
a2
b2
c3 = 0
a3
b3
b4 = 0
For the first operation, we can either calculate the sum s0 using our sum circuit S,
or we can calculate the carry c1 using our carry circuit C. If we begin by calculating
s0 , then we no longer have b0 , but we need b0 to calculate the carry c1 . So, let us
calculate the carry first:
176 4 Multiple Quantum Bits
c0 = 0 c0 = 0
a0 a0
C
b0 a0 b0
c1 = 0 c01
a1 a1
b1 b1
c2 = 0 c2 = 0
a2 a2
b2 b2
c3 = 0 c3 = 0
a3 a3
b3 b3
b4 = 0 b4 = 0
Now that we have c01 , we can either add it to a1 and b1 , or we can calculate the carry
c02 . Again, if we add first, then we no longer have b1 to calculate the carry. So, let us
calculate the next carry, and repeating this argument, we calculate all the carries:
c0 = 0 c0 = 0
a0 a0
C
b0 a0 b0
c1 = 0 c01
a1 a1
C
b1 a1 b1
c2 = 0 c02
a2 a2
C
b2 a2 b2
c3 = 0 c03
a3 a3
C
b3 a3 b3
b4 = 0 s4
Note the last carry corresponds to the leftmost bit of the sum s4 . Now, to calculate
s3 using our sum circuit S, we need the inputs to be c03 , a3 , and b3 , but currently
the third input is a3 b3 . To make this third input simply b3 , we CNOT a3 with it,
resulting in a3 (a3 b3 ) = (a3 a3 ) b3 = 0 b3 = b3 :
4.5 Quantum Adders 177
c0 = 0 c0 = 0
a0 a0
C
b0 a0 b0
c1 = 0 c01
a1 a1
C
b1 a1 b1
c2 = 0 c02
a2 a2
C
b2 a2 b2
c3 = 0 c03
a3 • a3
C
b3 b3
b4 = 0 s4
Next, we need to undo c03 so that we just have c3 = 0. We can do this by inverting
the carry gate:
178 4 Multiple Quantum Bits
•
• •
C† =
• •
Note since C is a quantum gate, it is unitary, so its inverse is equal to its conjugate
transpose (i.e., C 1 = C† ). Applying this,
c0 = 0 c0 = 0
a0 a0
C
b0 a0 b0
c1 = 0 c01
a1 a1
C
b1 a1 b1
c2 = 0 c02
a2 a2
C C†
b2 b2
c3 = 0 c3 = 0
a3 • S a3
C
b3 s3
b4 = 0 s4
This also converted a2 b2 back to b2 , so we can again use the sum circuit to find
s2 = a2 b2 c02 :
4.5 Quantum Adders 179
c0 = 0 c0 = 0
a0 a0
C
b0 a0 b0
c1 = 0 c01
a1 a1
C
b1 a1 b1
c2 = 0 c02
a2 S a2
C C†
b2 s2
c3 = 0 c3 = 0
a3 • S a3
C
b3 s3
b4 = 0 s4
This is our quantum ripple-carry adder, and |bi has been replaced by |si (while
keeping |ai and |ci unchanged), as we wanted.
Note the qubits in this circuit have a different order. Rather than taking
|ai|bi|ci ! |ai|si|ci,
180 4 Multiple Quantum Bits
|b4 i|b3 i|a3 i|c3 i|b2 i|a2 i|c2 i|b1 i|a1 i|c1 i|b0 i|a0 i|c0 i
to
|s4 i|s3 i|a3 i|c3 i|s2 i|a2 i|c2 i|s1 i|a1 i|c1 i|s0 i|a0 i|c0 i.
Let us verify our quantum circuit in Quirk by adding |ai = |1011i and |bi =
|01110i, which should result in |si = |11001i. With the qubit ordering from above,
where all the carry qubits are |0i at the start and end of the computation, the quantum
ripple-carry adder should take
|0110100110010i ! |1110000010110i.
Exercise 4.27. We can use an adder to subtract binary numbers by using the fact that
a b = a + b.
4.5 Quantum Adders 181
In Quirk, modify your circuit from Exercise 4.26 to subtract 1111 1011. Do this by adding X
gates to each bit of the input a (this gives a). Then, the adder computes a + b. Then, add X gates to
each bit of s, except for the extra bit s4 , since it is not needed and should stay 0. This gives a + b.
Exercise 4.28. While teaching a course on quantum computing in Fall 2018, one of my Creighton
University students, Lorenzo Riva, proposed the following change to the quantum ripple-carry
adder:
c0 = 0 c0 = 0
a0 S a0
C C†
b0 s0
c1 = 0 c1 = 0
a1 S a1
C C†
b1 s1
c2 = 0 c2 = 0
a2 S a2
C C†
b2 s2
c3 = 0 • c3 = 0
a3 a3
C
b3 s3
b4 = 0 s4
p
That is, the CNOT between a3 and b3 , and the bottommost S (sum, not the S = Z gate), can be
replaced by a single CNOT between c3 and b3 .
(a) Explain why this simplification is correct.
(b) If each binary number has length n, how many Toffoli gates and how many CNOT gates does
this circuit use?
Exercise 4.29. In this exercise, we will learn about another quantum adder that does not need
carry bits. It is called Draper’s adder, and it uses the “quantum Fourier transform,” which will be
discussed later in the textbook in Section 7.7.3.
First, let us define a single-qubit gate Rr that rotates about the z-axis of the Bloch sphere by
angle 360 /2r . For example, R1 = Z is a rotation by 180 , R2 = S is a rotation by 90 , R3 = T is a
rotation by 45 , and R4 is a rotation by 22.5 . Rotations about axes can be created in Quirk using
the “Make Gate” feature, e.g., R4 is
182 4 Multiple Quantum Bits
We also have the conjugate transpose (or inverse) of the rotation, which we denote R†r , and it rotates
about the z-axis by 360 /2r . For example, R†4 rotates by 22.5 , and since it is a negative angle,
it rotates the “other way.”
Draper’s quantum adder transforms
|ai|bi ! |a + bi|bi,
and it does not use any carry qubits. Instead of using the ripple-carry adder, The circuit for the
adder is a little long, so we break it up over three parts:
Part 1
b0 ...
b1 ...
b2 ...
b3 ...
a0 • • • H ...
a1 • • H R2 ...
a2 • H R2 R3 ...
a3 H R2 R3 R4 ...
... Part 2 ...
• • • •
... • • • ...
... • • ...
... • ...
... R1 ...
... R1 R2 ...
... R1 R2 R3 ...
... R1 R2 R3 R4 ...
4.5 Quantum Adders 183
Part 3
... b0
... b1
... b2
... b3
... H • • • s0
... R2† H • • s1
Implement Draper’s adder in Quirk and use it to add |ai = |0111i and |bi = |0011i.
Generalizing this, adding two n-bit strings uses n carry gates C, n 1 inverses of the
carry gate C† , n sum gates S, and an extra CNOT gate. Each C and C† gate uses two
Toffoli gates and one CNOT gate, and each S gate uses two CNOT gates. The total
number of quantum gates to add two n-bit strings is summarized in the following
table:
Gate No. of Gates Total No. of Toffolis Total No. of CNOTs
C n 2n n
C† n 1 2(n 1) n 1
S n 0 2n
Extra CNOT 1 0 1
4n 2 4n
Altogether, the quantum ripple-carry adder uses 4n 2 Toffoli gates and 4n CNOT
gates, which is linear in n, i.e., Q (n), so the algorithm is efficient.
Exercise 4.30. How many Toffoli gates and how many CNOT gates does the quantum ripple-carry
adder need to add two (a) 4-bit strings, (b) 8-bit strings.
Exercise 4.31. To add two binary numbers of length 4, our quantum ripple-carry adder used 13
qubits. How many qubits does the quantum ripple-carry adder need to add two binary numbers of
length n?
184 4 Multiple Quantum Bits
Note our quantum ripple-carry adder is a quantum circuit, so it can also act on
superpositions. For example, if |ai is an equal superposition of 6 and 3, i.e.,
1
|ai = p (|0110i + |0011i) ,
2
and if |bi is 11, then
|bi = |01011i,
then the quantum ripple-carry adder turns |ai|bi into
1
p (|0110i|10001i + |0011i|01110i) .
2
It may appear as though we solved two addition problems at once, i.e., in “paral-
lel,” since both 6 + 11 = 17 and 3 + 11 = 14 appear in the answers as |10001i and
|01110i, respectively, but this is not the case. When we measure the result, we get
one sum with probability 1/2 or the other sum with probability 1/2. In contrast, in
parallel computing, two computers calculate both answers at the same time, so we
get both sums at the end.
It is incorrect to think of a quantum computer as a massively parallel classical
computer because we must measure and only get one result. In fact, this misunder-
standing is so common that it might be best to avoid the term “parallel” altogether
when describing quantum computing.
We have seen our first quantum algorithm: the ripple-carry adder. We will get to
many more quantum algorithms in Chapter 7, but there are several other topics to
cover first.
Exercise 4.32. Read “Quantum Computing: A Soccer Analogy” at
https://medium.com/@thomaswong_8663/quantum-computing-a-soccer-a
nalogy-1335644a1472
4.6.1 Definition
A set of quantum gates that allows us to approximate any quantum gate to any
desired precision is called a universal gate set. Recall in Section 1.2.5 that we used
the same term to describe a set of logic gates that can reproduce all classical gates.
It is usually clear from the context. For example, if the Hadamard gate is involved,
then we must be discussing quantum gates because there is no classical Hadamard
gate. Or, we might describe a set as “universal for classical computing” or “universal
for quantum computing.”
Proving that a set of gates is universal is a more advanced topic, which we do
not discuss in this textbook. Nielsen and Chuang is a good resource for additional
details. Instead, we provide some intuition below.
There are several components that we need for a set of quantum gates to be universal.
1 Mathematically, the Clifford group is the normalizer of the Pauli group, which is generated by
the Pauli matrices.
186 4 Multiple Quantum Bits
Although the Clifford group generator {CNOT, H, S} is not universal for quan-
tum computing, replacing H with Rp/8 does yield a universal gate set.
• {Toffoli, H, S} is universal for quantum computing.4 Although {CNOT, H, S} is
not universal for quantum computing, replacing CNOT with Toffoli does yield
a universal gate set.
• H plus almost any two-qubit unitary.
Exercise 4.34. The Clifford group generator {CNOT, H, S} is not universal for quantum comput-
ing. Give three ways to modify the it so that it is universal for quantum computing.
The Solovay-Kitaev theorem says that with any universal gate set, we can approx-
imate a quantum gate on n qubits to precision e using Q (2n logc (1/e)) gates for
some constant c. The dependence on the number of qubits 2n is what we might ex-
pect since an operator on n qubits is a matrix of 2n ⇥ 2n entries. The dependence on
the precision logc (1/e) is great! The precision e is the “distance” (in some measure-
ment or metric) that the approximate quantum gate is to the actual quantum gate,
and we want it to be small. So 1/e is big, but taking the logarithm of it makes it
small. A logarithm to a constant power, such as logc , is a polynomial of a logarithm,
so is also called polylog. This is also considered small. Thus, this dependence means
our approximation quickly converges on the actual quantum gate.
Recall any complex number z has a real part x and an imaginary part y, i.e., z = x+iy.
Since x and y are real numbers, this means we can express any complex number as
two real numbers (x, y) and keep track of the fact that they play different roles. So
in theory, we can formulate all of quantum computing just in terms of real numbers.
Then, a universal set of quantum gates technically does not need to produce states
with complex amplitudes. For example, the following sets are also universal for
quantum computing:
5 Y. Shi, Both Toffoli and controlled-NOT need little help to do universal quantum computation,
arXiv:quant-ph/0205115 (2002).
6 D. Aharonov, A Simple Proof that Toffoli and Hadamard are Quantum Universal, arXiv:quant-
ph/0301040 (2003).
7 D. J. Shepherd, T. Franz, and R. F. Werner, Universally Programmable Quantum Cellular Au-
CH|00i = |00i,
CH|01i = |01i,
1
CH|10i = |1+i = p (|10i + |11i) ,
2
1
CH|11i = |1 i = p (|10i |11i) .
2
This can simulate both the Toffoli gate and the Hadamard gate, so from the
previous bullet, it is universal.
• {CNOT, any single-qubit gate whose square is basis-changing} is universal for
quantum computing.8 An example of a single-qubit gate whose square is basis-
changing is the following gate U:
p
3 1
U|0i = |0i + |1i,
2 2
p
1 3
U|1i = |0i + |1i.
2 2
Next, if we apply U again, meaning twice to the Z-basis states, we get
p ! p
2 3 1 1 3
U |0i = U |0i + |1i = |0i + |1i,
2 2 2 2
p ! p
2 1 3 3 1
U |1i = U |0i + |1i = |0i + |1i.
2 2 2 2
H 2 |0i = |0i,
H 2 |1i = |1i.
So, {CNOT, H} is not universal, whereas from the first bullet point, {Toffoli, H}
is universal.
8 Y. Shi (2002).
4.7 Quantum Error Correction 189
4.7.1 Decoherence
y
x
|1i
The north pole corresponds to |0i and the south pole corresponds to |1i. For a clas-
sical bit, these would be the only possible states, and the only error is for the bit to
completely flip between the north and south poles. For a qubit, however, every loca-
tion on the Bloch sphere is a different state. For example, beginning at |0i, instead
of completely flipping to |1i, a qubit could experience a partial bit flip error, where
it only rotates a little toward |1i:
z
|0i
y
x
|1i
Since a full bit flip corresponds to the X gate, and the X gate is a rotation about
the x-axis by p = 180 , a partial bit flip corresponds to rotating about the x-axis by
some angle. So, in the above figure, the state is moving leftward, down the Bloch
sphere, in the yz-plane. This small change is an error.
To further complicate matters, a qubit’s state is not just its latitude up and down
the Bloch sphere, but also its longitude around the Bloch sphere. For example, if a
qubit initially in the |+i state gets bumped to the side, we get a different state:
190 4 Multiple Quantum Bits
|+i y
x
This is called a phase flip error, because rotations around the p z-axis correspond
changes
p in the relative phase. For example, |+i = (|0i + |1i)/ 2 and | i = (|0i
|1i)/ 2 lie on opposite sides of the equator.
Since qubits are more sensitive to errors than classical bits, small interactions
with the environment can move the qubit to a different location on the Bloch sphere.
This process is called decoherence. In practice, decoherence is the biggest obstacle
to building large-scale quantum computers, since it is very difficult to isolate a qubit
from its environment while making it accessible for quantum gates and measure-
ments.
Next, we will see how to correct for bit-flip errors and then phase-flip errors.
Then, we will combine both types of error correction into what is known as the
Shor code.
To make it possible to correct bit-flip errors, we use three physical qubits to encode
each logical qubit:
|0L i = |000i, |1L i = |111i,
where subscript L denotes a logical qubit. A logical qubit is, in general, a superpo-
sition of |0L i and |1L i:
We would like to detect this error and correct it. Classically, we could just measure
the bits, see which one disagrees with the others, and then flip it back to correct
it. Quantumly, however, if we measure the bits (or even just a single bit), the state
collapses to |100i or |011i, and we lose the superposition. So, instead of measuring
the bits, we follow Section 1.6.3 and measure the parity of adjacent qubits. Recall
that the parity of two bits, a and b, can be calculated using Exclusive OR. That is,
parity(a, b) = a b. Also recall that CNOT|ai|bi = |ai|a bi. Then, we can use
two CNOTs to calculate the parity of two qubits, putting the answer in an ancilla
qubit:
|ai • |ai
|bi • |bi
|0 ai = |ai
|0i |a bi
With three qubits, we can calculate the parities of adjacent qubits by doing this
twice:
|ai • |ai
|bi • • |bi
|ci • |ci
|0i |a bi
|0i |b ci
In this example, the parity of the left two qubits is 1, and the parity of the right two
qubits is 0. This tells us that the left two qubits differ, and the right two qubits are the
same. Then, we know the left qubit has flipped, and we inferred this without directly
measuring and collapsing the state. This is called an error syndrome. To correct
the error, we can simply apply (X ⌦ I ⌦ I), which results in b |111i + a|000i =
a|000i + b |111i, thus correcting the error.
Now, say there is a partial flip. In Section 2.6.4, it was stated that on the Bloch
sphere, a rotation by angle q about the axis n̂ = (nx , ny , nz ) is given by Eq. (2.10):
✓ ◆ ✓ ◆
ia q q
e cos I i sin (nx X + nyY + nz Z) , (2.10 revisited)
2 2
where eia is a global phase, so a can be chosen as we please. Now, a partial bit flip
corresponds to a rotation about the x-axis by some angle q , so we have n̂ = (1, 0, 0).
We also choose a = p/2. Then, the rotation corresponds to
✓ ◆ ✓ ◆
q q
i cos I + sin X.
2 2
q p
Letting e = sin(q /2), we get cos(q /2) = 1 sin2 (q /2) = 1 e 2 , so the rota-
tion is
192 4 Multiple Quantum Bits
p p ◆ ✓ ◆ ✓
10 01
i 1 e I + eX = i 1 e
2 2 +e
01 10
✓p ◆
i 1 e2 p e
= .
e i 1 e2
When q = p, e = 1, and we get |0i ! |1i and |1i ! |0i, which is a complete bit
flip, or the X gate.
For example, if the left qubit partially flips,
⇣p ⌘ ⇣p ⌘
a|000i + b |111i ! a i 1 e 2 |000i + e|100i + b i 1 e 2 |111i + e|011i
p p
= ai 1 e 2 |000i + ae|100i + b i 1 e 2 |111i + b e|011i
p p
= ai 1 e 2 |000i + b e|011i + ae|100i + b i 1 e 2 |111i.
Now, we measure the parity of adjacent qubits. Labeling the qubits |q2 q1 q0 i, we get
the following possible outcomes with corresponding probabilities:
• parity(q2 , q1 ) = 0 and parity(q1 , q0 ) = 0 with probability
p 2 p 2
ai 1 e2 + b i 1 e2 = |a|2 1 e 2 + |b |2 1 e2
= |a|2 + |b |2 1 e2
=1 e 2,
|q1 i • • X
|q2 i • X
|0i • • |0i
|0i • • |0i
The first four columns are the CNOTs that calculate the parities of adjacent qubits.
Then, we measure these parities, as shown by the meter symbols, which results
in classical bits. We denote these classical bits/wires using double lines. We end
with three X gates conditioned on these classical bits/parities. If both parities are 1,
then q1 flipped, so we apply an X gate to it to correct it. If parity(q2 , q1 ) = 0 and
parity(q1 , q0 ) = 1, then q0 flipped, so we apply an X gate to it to correct it. Finally,
if parity(q2 , q1 ) = 1 and parity(q1 , q0 ) = 0, then q2 flipped, so we apply an X gate
to it to correct it. We end by resetting the ancillas to |0i, indicated by the boxes with
|0i in them. Simulating this in Quirk (see https://bit.ly/3jZ4zKQ),
194 4 Multiple Quantum Bits
In this simulation, the logical qubit starts as |0L i = |000i. In the first column of the
circuit, we can visualize the states on Bloch spheres and confirm that we have |000i.
In the second column, we introduce a bit-flip error by applying a bit-flip X t , with
t varying from 0 to 360 , to q2 . In the third column, we again visualize the states
on the Bloch sphere, confirming that |q2 i 6= |0i. Then, we have our error correction
circuit, and at the very end of the circuit, the top three qubits are restored to |000i,
as expected. Note Quirk does not have a “reset” tool, so the ancillas have not yet
been restored to |0i. This would need to be done to repeat the circuit.
We can modify the above circuit using the principle of deferred measurement,
which says,
|q1 i • • X
|q2 i • X
|0i • • • |0i
|0i • • |0i
Phrased another way, we can collapse and then do the controlled operations, or we
can do the controlled operations in superposition, and then collapse. Let us prove
this for our previous example, where the qubits started in the state a|000i + b |111i,
but then the left qubit partially flips with amplitude e. From earlier, if we include
the ancilla qubits, the state after the first four CNOTs is
p p
ai 1 e 2 |00000i + b e|10011i + ae|10100i + b i 1 e 2 |00111i.
Recall the qubits are ordered as |parity(q2 , q1 )i|parity(q1 , q0 )i|q2 i|q1 i|q0 i. Now, if
we apply the controlled- and anti-controlled-X gates to correct the answers, the state
becomes
4.7 Quantum Error Correction 195
p p
ai 1 e 2 |00000i + b e|10111i + ae|10000i + b i 1 e 2 |00111i
p
= i 1 e 2 |00i (a|000i + b |111i) + e|10i (a|000i + b |111i)
⇣p ⌘
= i 1 e 2 |00i + e|10i (a|000i + b |111i) .
The top three qubits have been successfully corrected to |000i. Again, we have not
reset the ancilla qubits at the end of this circuit, which would be necessary to repeat
the error correction scheme.
Exercise 4.35. In this exercise, we will work through encoding a qubit in the bit-flip code. Say we
have a single qubit in the state
|yi = a|0i + b |0i.
We want to encode this using the bit-flip code. we add two more qubits to our system, all initially
in |0i, so our three qubits are in the state
|0i
|0i =
| i • • •
196 4 Multiple Quantum Bits
We can similarly correct phase-flip errors by using three physical qubits to encode
each logical qubit, but instead of using three |0i’s and |1i’s, we use three |+i’s and
| i’s, i.e.,
|0L i = |+ + +i, |1L i = | i,
so a general superposition is
A way to create this encoding is given in Exercise 4.37. The reason why we use |+i
and | i is because a complete phase flip (the Z gate) switches between these states:
1 Z 1
|+i = (|0i + |1i) ! (|0i |1i) = | i,
2 2
1 Z 1
| i = (|0i |1i) ! (|0i + |1i) = |+i.
2 2
Say the left qubit experiences a complete phase flip:
a|+ + +i + b | i ! a| + +i + b |+ i.
Then, we detect and correct this just like we did for the bit-flip error, except working
in the X-basis. So, we measure the parity of consecutive qubits in the X-basis, which
4.7 Quantum Error Correction 197
In our example where the left qubit experienced a phase flip, we get parity 1 for the
left two qubits and parity 0 for the right two qubits, implying that the first qubit is
flipped. So we apply (Z ⌦ I ⌦ I), restoring a|+ + +i + b | i.
Now for partial phase flips, a partial phase flip corresponds to a rotation about the
z-axis by some angle q , so again using Eq. (2.10) with a = p/2 and e = sin(q /2),
but now with n̂ = (0, 0, 1), we get that the rotation is
p p ✓ ◆ ✓ ◆
10 1 0
i 1 e 2 I + eZ = i 1 e 2 +e
01 0 1
✓p ◆
i 1 e +e p 0
2
= .
0 i 1 e2 e
Note when q = p, e = 1, and we get |0i ! |0i and |1i ! |1i, which is a complete
phase flip, or the Z gate. Let us see how a partial phase flip transforms |+i and | i:
1
|+i = p (|0i + |1i)
2
1 h⇣ p ⌘ ⇣p ⌘ i
!p i 1 e 2 + e |0i + i 1 e 2 e |1i
2
p 1 1
= i 1 e 2 p (|0i + |1i) + e p (|0i |1i)
2 2
p
= i 1 e 2 |+i + e| i,
1
| i = p (|0i |1i)
2
1 h⇣ p ⌘ ⇣p ⌘ i
!p i 1 e 2 + e |0i i 1 e 2 e |1i
2
198 4 Multiple Quantum Bits
1 p 1
= e p (|0i + |1i) + i 1 e 2 p (|0i |1i)
2 2
p
= e|+i + i 1 e 2 | i.
Now, we measure the parity of adjacent qubits in the X-basis (i.e., whether the num-
ber of | i’s are even or odd). We get:
• parity(q2 , q1 ) = 0 and parity(q1 , q0 ) = 0 with probability
p 2 p 2
ai 1 e2 + b i 1 e2 = |a|2 1 e 2 + |b |2 1 e2
= |a|2 + |b |2 1 e2
=1 e 2,
b| i + a|+ + +i = a|+ + +i + b | i,
|q1 i H • • H Z
|q2 i H • H Z
|0i • • |0i
|0i • • |0i
The top three qubits are |0L i = |+ + +i, and the bottom two qubits will be used to
calculate the parities. In the first column of the circuit, we can visualize the states
on Bloch spheres and confirm that we have |+ + +i. Next, we simulate an error by
applying a phase-flip Z t , with t varying from 0 to 360 , to the middle qubit. Now,
another set of Bloch spheres confirms that the middle qubit has changed. We want
to correct this so that we end up with |+ + +i again. The rest of our circuit is the
same as the bit-flip circuit, except we apply Hadamard gates before and after it so
that we work in the X-basis. In the output, we see that we have restored |+ + +i.
We can move the phase flip to any of the top three qubits, and our error-correcting
circuit will restore the state to |+ + +i. Note we also need to reset the ancilla qubits.
Exercise 4.37. In this exercise, we will work through an exercise for encoding a qubit in the phase-
flip code. Say we have a single qubit in the state
We want to encode this using the phase-flip code. we add two more qubits to our system, all initially
in |0i, so our three qubits are in the state
a|0L i + b |1L i,
where |0L i and |1L i are encoded using the phase-flip code:
a|+ + +i + b | i.
Now, the left physical qubit suffers a slight phase flip, causing the state to become
⇣p ⌘ ⇣p ⌘
a i 1 e 2 |+ + +i + e| + +i + b i 1 e 2 | i + e|+ i .
To detect/correct this, you measure the parity of the left two qubits and the parity of the right two
qubits, both in the X-basis.
4.7 Quantum Error Correction 201
(a) What is the probability that both parities are even? If this probability is nonzero, say you
get this outcome. What is the state after the measurement? What gate(s) should you apply to
correct the error, if any?
(b) What is the probability that the parity of the left two qubits is odd and the parity of the right
two qubits is even? If this probability is nonzero, say you get this outcome. What is the state
after the measurement? What gate(s) should you apply to correct the error, if any?
(c) What is the probability that the parity of the left two qubits is even and the parity of the right
two qubits is odd? If this probability is nonzero, say you get this outcome. What is the state
after the measurement? What gate(s) should you apply to correct the error, if any?
(d) What is the probability that both parities are odd? If this probability is nonzero, say you get
this outcome. What is the state after the measurement? What gate(s) should you apply to
correct the error, if any?
We can combine the phase-flip code and bit-flip code to correct both kinds of errors.
We begin with the phase-flip code, so we can correct phase-flip errors. That is,
|0L i = |+ + +i
1 1 1
= p (|0i + |1i) p (|0i + |1i) p (|0i + |1i)
2 2 2
1
= 3/2 (|0i + |1i) (|0i + |1i) (|0i + |1i) .
2
Then, so we can correct bit-flip errors, we replace each of the three qubits with three
qubits using the bit-flip encoding, i.e., |0i ! |000i and |1i ! |111i, so that each
logical qubit is encoded using nine physical qubits:
1
|0L i = (|000i + |111i) (|000i + |111i) (|000i + |111i) .
23/2
Similarly, we begin with |1L i = | i and replace |0i ! |000i and |1i ! |111i:
1
|1L i = (|000i |111i) (|000i |111i) (|000i |111i) .
23/2
Then, the state of a general logical qubit is
a
a|0L i + b |1L i = (|000i + |111i) (|000i + |111i) (|000i + |111i)
23/2
b
+ 3/2 (|000i |111i) (|000i |111i) (|000i |111i) .
2
This encoding is called the Shor code, and it is named after its inventor, Peter Shor,
who proposed it in 1995 and, by doing so, invented quantum error correction. It uses
nine physical qubits to encode one logical qubit. A way to create this encoding is
given in Exercise 4.41.
202 4 Multiple Quantum Bits
Exercise 4.41. A qubit can be encoded using the Shor code by first encoding it in the three-qubit
phase-flip code (Exercise 4.37) followed by encoding each of the three qubits using the three-qubit
bit-flip code (Exercise 4.35), which results in nine qubits total. Applying these encodings one after
another, a method called concatenation, yields the following circuit:
|0i
|0i
|0i H • H •
|0i
|0i =
|0i H • H •
|0i
|0i
| i • H • • H •
In the above circuit, the large dashed box to the left is the phase-flip encoding, which turns
|0i ! |+ + +i and |1i ! | i. Then, the three dashed boxes in the middle are each the bit-flip
encoding, which turns |0i ! |000i and |1i ! |111i.
If the initial state of the circuit is |y00000000i, where |yi = a|0i + b |1i, show that:
(a) The state of the circuit after the first column (after the CNOT with two targets) is
a|000i|000i|000i + b |100i|100i|100i.
(b) The state of the circuit after the second column (after the Hadamard gates) is
a
p (|000i + |100i) (|000i + |100i) (|000i + |100i)
2
b
+ p (|000i |100i) (|000i |100i) (|000i |100i) .
2
(c) The final state of the circuit is
a
(|000i + |111i) (|000i + |111i) (|000i + |111i)
23/2
b
+ 3/2 (|000i |111i) (|000i |111i) (|000i |111i) .
2
This is precisely a|0L i + b |1L i.
Let us see how to correct bit flips and phase flips using the Shor code, beginning
with bit flips. First, remember that the qubits are ordered q8 q7 . . . q0 . Say q8 and q3
both experience complete bit flips. Then, the state of the system is
a
(|100i + |011i) (|001i + |110i) (|000i + |111i)
23/2
b
+ 3/2 (|100i |011i) (|001i |110i) (|000i |111i) .
2
To detect this, we measure the parities of adjacent qubits within each triplet. In this
example, we would get:
4.7 Quantum Error Correction 203
This tells us that the eighth qubit and third qubit have flipped, so we can apply
X gates to those two qubits to correct them. This also works with partial bit flips.
Measuring the parities of adjacent qubits might collapse the state and correct the
errors, or it might collapse the state into one with full bit flips, which we correct by
applying X gates to the appropriate qubits.
Exercise 4.42. A logical qubit is encoded using nine physical qubits in the Shor code. In each
triplet, you measure the parity of adjacent qubits and get the following results:
Are there any bit flip errors? If so, which bits flipped, and what can you do to correct them?
Exercise 4.43. Bit flips can be corrected in the Shor code using the following quantum circuit:
|q0 i • X
|q1 i • • X
|q2 i • X
|q3 i • X
|q4 i • • X
|q5 i • X
|q6 i • X
|q7 i • • X
|q8 i • X
|0i • • |0i • • |0i • • |0i
|0i • • |0i • • |0i • • |0i
The first third of the circuit measures the parities of adjacent qubits in the top three qubits, correct
any errors, and reset the ancillas. The middle third of the circuit calculates the parities of adjacent
qubits in the next triplet, correcting any errors. Finally, it does the same for the last triplet of qubits.
Using Quirk, simulate this circuit by inserting it into the following circuit (see https://bi
t.ly/3D1kRKI):
204 4 Multiple Quantum Bits
The first part of this circuit applies the Hadamard gate to |q8 i, turning it into |+i. Then, it uses
the circuit in Exercise 4.41 to encode this in the Shor code. Then, the X t gates applies a partial bit
flip to one qubit in each triplet. In the middle section, you should add the previously given circuit.
Although there is no reset feature in Quirk, you can use the postselection tool as a workaround. In
Quirk, it is drawn as the outer product |0ih0|). It measures a qubit in the {|0i, |1i} basis, and if the
result is |0i, the calculation continues. Otherwise, the simulation starts over. For our purposes, it
has the effect of guaranteeing that the ancilla qubit is |0i before proceeding. A true “reset” feature
would allow us to continue with the ancilla as |0i without the risk of restarting the simulation. In
the last section of the circuit, we undo the Shor encoding and Hadamard gate so that all the qubits
are |0i again. Verify that your circuit does this.
Next, let us see how the Shor code also allows us to correct phase flips. Say q3
experiences a complete phase flip. Then, the state of the system is
a
(|000i + |111i) (|000i |111i) (|000i + |111i)
23/2
b
+ 3/2 (|000i |111i) (|000i + |111i) (|000i |111i) .
2
Then, we can measure the
p “phase parity” of adjacent triplets, i.e., whether the num-
ber of (|000i |111i)/ 2 triplets is even or odd. This is similar to the phase flip
code, where we measured the parity in the X basis to determine if the number of
| i’s was even or odd. How to measure this parity is shown in Exercise 4.44). In
our example, we would get
This indicates that the middle triplet needs to be flipped, so we apply the Z gate
to any one of the three qubits in that triplet. That is, we can apply the Z gate to
either q5 , q4 , or q3 , correcting the error. Similarly, when there is a partial phase flip,
if we measure all the phase parities and get zero, the state collapsed and corrected
the error, and if there was a discrepancy in phase parities, we apply a Z gate to the
appropriate triplet to correct it.
4.7 Quantum Error Correction 205
By alternating between correcting bit-flip errors and phase-flip errors, the Shor
code corrects all quantum errors, assuming each triplet experiences at most one bit-
flip error per correction cycle, and at most one triplet experiences a phase-flip error
per correction cycle.
A quantum computer that accumulates errors slowly enough that errors can be
corrected is called fault tolerant. Depending on the error correcting code that is used,
the maximum correctable error rate can vary, and this is an area of active research.
At the time of this writing, a fault tolerant quantum computer does not yet exist, and
one could argue that building one is the “holy grail” of the field.
Exercise 4.44. Consider the following circuit, which computes the phase parity of adjacent triplets
in the Shor code:
|q0 i
|q1 i
|q2 i • H • H •
|q3 i
|q4 i
|q5 i • H • • H •
|q6 i
|q7 i
|q8 i • H • H •
|0i
|0i
Comparing this circuit with Exercise 4.41, the first two layers of this circuit partially unencode
the qubit. Then the middle four layers (the CNOTs) calculate the parity of adjacent triplets in two
ancilla qubits. Then, the final two layers reencode the qubit. In this exercise, we will work through
this for an example.
(a) A qubit a|0L i + b |1L i is encoded using the nine-qubit Shor code, but a physical qubit in the
middle triplet experienced a phase flip, so the state of the nine qubits is
a
(|000i + |111i) (|000i |111i) (|000i + |111i)
23/2
b
+ 3/2 (|000i |111i) (|000i + |111i) (|000i |111i) .
2
Show that after the first two columns of the circuit (the CNOTs with two targets and the
Hadamards), the state of the nine qubits is
a|000100000i + b |100000100i.
(b) Show that after the middle layers of the circuit (CNOTs), the ancilla qubits now store the
phase parities of adjacent triplets.
(c) Show that at the end of the circuit, the state of the nine qubits is again
206 4 Multiple Quantum Bits
a
(|000i + |111i) (|000i |111i) (|000i + |111i)
23/2
b
+ 3/2 (|000i |111i) (|000i + |111i) (|000i |111i) .
2
(d) From the parities in (b), what gate should be applied to fix the phase flip, and to which qubit?
Exercise 4.45. Construct a quantum circuit that corrects when a triplet experiences a phase flip
error in the Shor code. To check your answer, simulate your circuit in Quirk by inserting it into the
following circuit (see https://bit.ly/3kmbTAm):
The first part of this circuit applies the Hadamard gate to |q8 i, turning it into |+i. Then, it uses the
circuit in Exercise 4.41 to encode this in the Shor code. Then, the Z t gates applies a partial bit flip
to one triplet. In the middle section, you should add the previously given circuit. As described in
Exercise 4.43, postselect on |0i as a workaround for resetting the ancilla qubits. In the last section
of the circuit, we undo the Shor encoding and Hadamard gate so that all the qubits are |0i again.
Verify that your circuit does this. Try moving the Z t gate around to different triplets to ensure that
the phase flip is corrected in all instances, as long as at most one triplet experiences a phase flip.
Exercise 4.46. You have a logical qubit encoded in nine physical qubits using the Shor code. Let
us label the qubits q8 q7 . . . q0 . They are grouped into three triplets (triplet2 , triplet1 , triplet0 ).
(a) You begin by detecting bit flip errors. Within each triplet, you measure the parity of adjacent
qubits in the Z-basis. Here are the results:
Are there any bit flip errors? If so, which bits flipped, and what can you do to correct them?
(b) Next, you measure the parities of adjacent triplets in the H3 -basis. Here are the results:
Was there a phase flip error? If so, which triplet flipped, and what can you do to correct it?
4.8 Summary 207
4.8 Summary
The state of multiple qubits is written as as a tensor product. With n qubits, there
are 2n orthonormal basis states, and a general state is a superposition of these basis
states. In a product state, measuring one qubit cannot affect the others, while in an
entangled state, measuring one qubit can affect the other qubits. A quantum gate on
n qubits is a 2n ⇥ 2n unitary matrix. There are various ways to add binary numbers
on a quantum computer. A universal set of quantum gates can approximate any
quantum gate to any desired precision. Quantum bits can suffer from both bit-flip
and phase-flip errors, but they can be corrected, so building a quantum computer
“only” requires really good qubits, not perfect qubits.