Fundamentals of Quantum Programming in I
Fundamentals of Quantum Programming in I
Weng-Long Chang
Athanasios V. Vasilakos
Fundamentals
of Quantum
Programming in
IBM’s Quantum
Computers
Studies in Big Data
Volume 81
Series Editor
Janusz Kacprzyk, Polish Academy of Sciences, Warsaw, Poland
The series “Studies in Big Data” (SBD) publishes new developments and advances
in the various areas of Big Data- quickly and with a high quality. The intent is to
cover the theory, research, development, and applications of Big Data, as embedded
in the fields of engineering, computer science, physics, economics and life sciences.
The books of the series refer to the analysis and understanding of large, complex,
and/or distributed data sets generated from recent digital sources coming from
sensors or other physical instruments as well as simulations, crowd sourcing, social
networks or other internet transactions, such as emails or video click streams and
other. The series contains monographs, lecture notes and edited volumes in Big
Data spanning the areas of computational intelligence including neural networks,
evolutionary computation, soft computing, fuzzy systems, as well as artificial
intelligence, data mining, modern statistics and Operations research, as well as
self-organizing systems. Of particular value to both the contributors and the
readership are the short publication timeframe and the world-wide distribution,
which enable both wide and rapid dissemination of research output.
Indexed by zbMATH.
All books published in the series are submitted for consideration in Web of Science.
Fundamentals of Quantum
Programming in IBM’s
Quantum Computers
123
Weng-Long Chang Athanasios V. Vasilakos
Department of Computer Science School of Electrical and Data Engineering
and Information Engineering University of Technology Sydney
National Kaohsiung University Sydney, Australia
of Science and Technology
Kaohsiung, Taiwan, Republic of China
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
Preface
v
vi Preface
algorithm. Two examples that solve an instance of the two famous NP-complete
problems are provided, which can be easily run in the backend simulator or ibmqx4
in IBM’s quantum computers.
Chapter 4 illustrates core concepts of quantum Fourier transform and inverse
quantum Fourier transform. It gives the reason of why quantum Fourier transform
and inverse quantum Fourier transform are able to give exponential speed-up for
fast Fourier transform. Two examples that compute the period and the frequency of
two given oracular functions are provided, which can be easily run in the backend
simulator or ibmqx4 in IBM’s quantum computers.
Chapter 5 describes core concepts of Shor’s order-finding algorithm. One
example that completes the prime factorization to 15 is provided, which can be
easily run in the backend simulator or ibmqx4 in IBM’s quantum computers.
Chapter 6 explains core concepts of phase estimation and quantum counting. One
example that computes eigenvalue of a (22 22) unitary matrix U with a (22 1)
eigenvector |u> is provided. Another example that computes the number of solution
(s) in the independent-set problem in a graph with two vertices and one edge is also
provided. The two examples can be easily run in the backend simulator or ibmqx4 in
IBM’s quantum computers.
This book contains extensive exercises at the end of each chapter. Solutions of
exercises from Chaps. 1 through 6 can easily be completed by readers if they fully
understand the contents of each chapter. Each reader can obtain solutions of all
exercises when he (she) buys this book because the publisher copies answers of
each exercise to a CD.
Power Point presentations have been developed for this book as an invaluable
tool for learning. Each reader can obtain Power Point presentations because the
publisher copies Power Point presentations to the same CD.
Finally, the authors would like to thank Renata who help us to do solution of
exercise and to make Power Point presentations. Although the authors are
responsible for all errors and omissions, this book benefited immensely from the
invaluable feedback of a number of technical reviewers, consisting of Ju-Chin
Chen, Kawuu Wei-Ching Li, Chih-Chiang Wang, Wen-Yu Chung, Chun-Yuan
Hsiao, Mang Feng and Renata Wong.
vii
viii Contents
A classical bit has a state that is either zero (0) or one (1). A quantum bit (or a
qubit for short) also has a state. Two possible states for a quantum bit are the states
|0 and |1. Notation like ‘ | ’ is called the Dirac notation. The states |0 and |1
for a quantum bit correspond to the states 0 and 1 for a classical bit and are known
as ‘computational basis state vectors’ of thetwo-dimensional Hilbert space. The
computational basis state vector
to the state |0 of a quantum bit is represented as a
1
(2 × 1) column vector and the computational basis state vector to the state |1
0
0
of a quantum bit is also represented as a (2 × 1) column vector . They form an
1
orthonormal basis for the two-dimensional Hilbert space.
The main difference between bits and quantum bits is that a quantum bit can be
in a state other than |0 or |1. A quantum bit has two ‘computational basis state
vectors’ |0 and |1of the two-dimensional Hilbert space. Its arbitrary state | is
nothing else than a linearly weighted combination of the following computational
basis state vectors, often called superposition:
1 0 l
| > = l0 |0 > +l1 |1 > = l0 + l1 = 0 . (1.1)
0 1 l1
The weighted factors l0 and l 1 that are complex numbers are the so-called prob-
ability amplitudes. Thus they must satisfy | l0 |2 + | l 1 |2 = 1. Put another way, the
state of a quantum bit is a unit vector in the two-dimensional Hilbert space.
All the time, classical computers do examination of a bit to decide whether it is
in the state 0 or 1 when they retrieve the content of the memory. However, quantum
computers cannot check a quantum bit to decide its quantum state, that is, the values
of l 0 and l 1 . Instead, when after measuring a quantum bit from quantum computers,
we obtain the result 0 with the probability |l0 |2 or the result 1 with the probability
|l 1 |2 . This is to say that reading quantum bits is to measure, and the readout is in
classical bits.
In a system of two classical bit, there are four possible states 00, 01, 10, and 11. Simi-
larly, a system of two quantum bits has four states |00, |01, |10 and |11 that corre-
spond to the classical four states 00, 01, 10, and 11 and are known as “computational
basis state vectors” of the four-dimensional Hilbert space. The computational basis
state vector to the state |00 of two quantum bits is represented as a (4 × 1) column
⎛ ⎞
1
⎜0⎟
vector ⎜⎝ 0 ⎠. The computational basis state vector to the state |01 of two quantum
⎟
0
1.1 Quantum Bits 3
⎛
⎞
0
⎜1⎟
bits is represented as a (4 × 1) column vector ⎜ ⎝ 0 ⎠. The computational basis state
⎟
0
vector to the state |10 of two quantum bits is represented as a (4 × 1) column vector
⎛ ⎞
0
⎜0⎟
⎜ ⎟ and the computational basis state vector to the state |11 of two quantum bits
⎝1⎠
0
⎛ ⎞
0
⎜0⎟
is represented as a (4 × 1) column vector ⎜ ⎝ 0 ⎠. They form an orthonormal basis
⎟
1
for the four-dimensional Hilbert space. The arbitrary state of two quantum bits is
nothing else than a linearly weighted combination of the following computational
basis state vectors, often called superposition:
⎛ ⎞
1
⎜0⎟
| > = l0 |00 > +l1 |01 > +l2 |10 > +l3 |11 > = l0 ⎜
⎝0⎠
⎟
0
⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞
0 0 0 l0
⎜1⎟ ⎜0⎟ ⎜ 0 ⎟ ⎜ l1 ⎟
+ l1 ⎜
⎝ 0 ⎠ + l 2 ⎝ 1 ⎠ + l 3 ⎝ 0 ⎠ = ⎝ l 2 ⎠.
⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ (1.2)
0 0 1 l3
The weighted factors l0 , l 1 , l 2 and l 3 that are complex numbers are the so-called
probability amplitudes. Therefore they must satisfy|l0 |2 + |l1 |2 + |l2 |2 + |l3 |2 =
2 2 2
k ∈ {0, 1} |l k | = 1, where the notation “{0, 1} ” means “the set of strings of
length two with each letter being either zero or one”. Put another way, the state of
two quantum bits is a unit vector in the four-dimensional Hilbert space.
Similarly, all the time, classical computers do examination of two bits to judge
whether it is in the state 00, 01, 10 or 11 when they retrieve the content of the memory.
However, quantum computers cannot do examination of two quantum bits to judge its
quantum state, that is, the values of l0 , l 1 , l 2 and l 3 . Instead, when after measuring two
quantum bits from quantum computers, we obtain the result 00 with the probability
|l 0 |2 , the result 01 with the probability |l1 |2 , the result 10 with the probability |l2 |2 or
the result 11 with the probability |l3 |2 . This is to say that reading quantum bits is to
measure, and the readout is in classical bits.
More generally, in a system of n classical bit, there are 2n possible states 0, 1,
2, … and (2n − 1) that are the decimal representation of n classical bits. Similarly,
a system of n quantum bits has 2n states |0, |1, |2, … and |2n − 1 that are the
decimal representation of n quantum bits and correspond to the classical 2n states 0,
4 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
1, 2, … and (2n − 1). The 2n states are to ‘computational basis state vectors’ of the
2n -dimensional Hilbert space. The first computational basis state vector to the state
⎛ ⎞
1
⎜0⎟
|0 of n quantum bits is represented as a (2n × 1) column vector ⎜ . ⎟. The second
⎜ ⎟
⎝ .. ⎠
0
computational basis state vector to the state |1 of n quantum bits is represented as a
⎛ ⎞
0
⎜1⎟
(2n × 1) column vector ⎜ . ⎟ and so on with that the last computational basis state
⎜ ⎟
⎝ .. ⎠
0
vector to the state |2n − 1 of n quantum bits is represented as a (2n × 1) column vector
⎛ ⎞
0
⎜ .. ⎟
⎜ . ⎟
⎜ ⎟. They form an orthonormal basis for the 2n -dimensional Hilbert space. The
⎝0⎠
1
arbitrary state of n quantum bits is nothing else than a linearly weighted combination
of the following computational basis state vectors, often called superposition:
2n −1
| > = lk | k (1.3)
k=0
Fig. 1.2 The quantum circuit of declaring and measuring five quantum bits
In Listing 1.1, the program in the backend ibmqx4 with five quantum bits in
IBM’s.
Listing. 1.1 The program of declared and measured statements of five quantum bits.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. measure q[0] → c[0];
6. measure q[1] → c[1];
7. measure q[2] → c[2];
8. measure q[3] → c[3];
9. measure q[4] → c[4];
Fig. 1.3 After the measurement to the program in Listing 1.1 is completed, we obtain the answer
00000 with the probability 1.000
outcome by overwriting the first classical bit c[0]. Next, the statement “measure q[1]
→ c[1];” on line six of Listing 1.1 is to measure the second quantum bit q[1] and to
record the measurement outcome by overwriting the second classical bit c[1]. The
statement “measure q[2] → c[2];” on line seven of Listing 1.1 is to measure the
third quantum bit q[2] and to record the measurement outcome by overwriting the
third classical bit c[2]. Next, the statement “measure q[3] → c[3];” on line eight of
Listing 1.1 is to measure the fourth quantum b-it q[3] and to record the measurement
outcome by overwriting the fourth classical bit c[3]. The statement “measure q[4]
→ c[4];” on line nine of Listing 1.1 is to measure the fifth quantum bit q[4] and to
record the measurement outcome by overwriting the fifth classical bit c[4]. In the
backend ibmqx4 with five quantum bits in IBM’s quantum computers, we use the
command “simulate” to execute the program in Listing 1.1. The result appears in
Fig. 1.3. From Fig. 1.3, we obtain the answer 00000 (c[4] = 0 = q[4] = |0, c[3] =
0 = q[3] = |0, c[2] = 0 = q[2] = |0, c[1] = 0 = q[1] = |0and c[0] = 0 = q[0]
= |0) with the probability one.
Using an electrical circuit that includes wires and logic gates builds a classical
computer. Similarly, using a quantum circuit that consists of wires and elemen-
tary quantum gates to complete and manipulate the quantum information builds a
quantum computer. Using the language of classical computation can illustrate occur-
ring of changing a classical state to another classical state. Analogous to the way,
using the language of quantum computation can introduce occurring of changing a
quantum state to another quantum state. In this section and in the later sections, we
introduce quantum gates on IBM’s quantum computers, propose quantum circuits
of many examples describing their application and describe how to use Open QASM
to write programs for implementing those examples.
For a classical computer, its circuits contain wires and logic gates. We use the
wires to carry information around the circuit and use the logic gates to complete
manipulation of information that is to convert it from one state to another state. For
8 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
example, we consider that one logic gate of classical single bit, NOT gate, whose
operation is to convert the state 0 to another state 1 and the state 1 to another state 0.
This is to say that the classical states 0 and 1 are interchanged.
Similarly, the quantum NOT gate takes the state l0 |0 + l1 |1 to the corre-
sponding state l0 |1 + l1 |0, where the role of |0 and |1 have been interchanged.
We assume that we denote a matrix X to represent the quantum NOT gate as follows:
01
X= . (1.4)
10
+
that X is the conjugate-transpose matrix of X and is equal
It is alsoassumed
01
to (X∗ )t = , where the * indicates complex conjugation and the t points out
10
∗ t 01 01
the transpose operation. Because X × (X ) = × = (X∗ )t × X =
10 10
01 01 10
× = , X is a unitary matrix or a unitary operator. If the
10 10 01
quantum state l0 |0 + l1 |1 is written in a vector notation as
l0
, (1.5)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the quantum NOT gate is
01 l l
× 0 = 1 = l1 |0 > +l0 | 1. (1.6)
10 l1 l0
Notice that the action of the quantum NOT gate is to that the state |0 is replaced
by the state corresponding to the first column of the matrix X and the state |1 is also
replaced by the state
corresponding
column of the matrix X. Because
to thesecond
01 01 10
X2 = X × X = × = , applying X twice to a state does
10 10 01
nothing to it. For IBM Q Experience, the graphical representation of the quantum
NOT gate appears in Fig. 1.4.
In Listing 1.2, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the second example in which we introduce how to program
with NOT gate operating one quantum bit. Figure 1.5 is the corresponding quantum
circuit of the program in Listing 1.2. The statement “OPENQASM 2.0;” on line one
of Listing 1.2 is to indicate that the program is written with version 2.0 of Open
QASM. Then, the statement “include “qelib1.inc”;” on line two of Listing 1.2 is
to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Experience (QE) Standard Header and the path is specified relative to the current
working directory. The statement “qreg q[5];” on line three of Listing 1.2 is to declare
that in the program there are five quantum bits. In the left top of Fig. 1.5, five quantum
bits are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum
bit is set to |0. Next, the statement “creg c[5];” on line four of Listing 1.2 is to
declare that there are five classical bits in the program. In the left bottom of Fig. 1.5,
five classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of
each classical bit is set to 0. Classical bit c[4] is the most significant bit and classical
bit c[0] is the least significant bit.
Listing. 1.2 The program to the use of five NOT gates operating five quantum bits.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. x q[0];
6. x q[1];
7. x q[2];
8. x q[3];
9. x q[4];
Fig. 1.5 The quantum circuit to five NOT gates operating five quantum bits
10 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
01
The statement “x q[0];” on line five of Listing 1.2 actually implements ×
10
1 0
= . This indicates that the statement “x q[0];” on line five of Listing 1.2
0 1
is to use NOT gate to convert q[0] from one state |0 to another state |1, where
“x” is to represent NOT gate.
Next,
the statement
“x q[1];” on line six of Listing
01 1 0
1.2 actually completes × = . This is to say that the statement “x
10 0 1
q[1];” on line six of Listing 1.2 is to make use of NOT gate to convert q[1] from one
state |0 to another state|1. Next,
thestatement “x q[2];” on line seven of Listing
01 1 0
1.2 actually implements × = . This implies that the statement “x
10 0 1
q[2];” on line seven of Listing 1.2 is to apply NOT gate to convert q[2] from one
state |0 to another state |1.Next, the statement
“x q[3];” on line eight of Listing
01 1 0
1.2 actually completes × = . This indicates that the statement
10 0 1
“x q[3];” on line eight of Listing 1.2 is to use NOT gate to convert q[3] from one
state |0 to another state
|1. Next,
the statement
“x q[4];” on line nine of Listing
01 1 0
1.2 actually performs × = . This is to say that the statement “x
10 0 1
q[4];” on line nine of Listing 1.2 is to make use of NOT gate to convert q[4] from
one state |0 to another state |1. After the five statements above are completed, the
state |0 of each quantum bit is converted as the state |1.
Next, the statement “measure q[0] → c[0];” on line ten of Listing 1.2 is to
measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. The statement “measure q[1] → c[1];”
on line eleven of Listing 1.2 is to measure the second quantum bit q[1] and to
record the measurement outcome by overwriting the second classical bit c[1].
Next, the statement “measure q[2] → c[2];” on line 12 of Listing 1.2 is to
measure the third quantum bit q[2] and to record the measurement outcome by
overwriting the third classical bit c[2]. The statement “measure q[3] → c[3];”
on line 13 of Listing 1.2 is to measure the fourth quantum bit q[3] and to record
the measurement outcome by overwriting the fourth classical bit c[3]. Next, the
statement “measure q[4] → c[4];” on line 14 of Listing 1.2 is to measure the
fifth quantum bit q[4] and to record the measurement outcome by overwriting the
fifth classical bit c[4]. In the backend ibmqx4 with five quantum bits in IBM’s
quantum computers, we use the command “simulate” to execute the program in
1.2 NOT Gate of Single Quantum Bit 11
Fig. 1.6 After the measurement to the program in Listing 1.2 is completed, we obtain the answer
11111 with the probability 1.000
Listing 1.2. The result appears in Fig. 1.6. From Fig. 1.6, we obtain the answer
11111 (c[4] = 1 = q[4] = |1, c[3] = 1 = q[3] = |1, c[2] = 1 = q[2] = |1 ,
c[1] = 1 = q[1] = |1, and c[0] = 1 = q[0] = |1) with the probability one.
√1 √1
1 1 1 2 2
H=√ = . (1.7)
2 1 −1 √1 − √12
2
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the Hadamard gate H is
l0√
+l1
1 1 1 l l0 + l1 l0 − l1
× 0 =
√ × 2 = √ 0 + √ 1. (1.9)
l0√
−l1
2 1 −1 l1 2 2 2
12 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
If in (1.8) the value of l0 is equal to one and the value of l1 is equal to zero, then
the Hadamard gate H turns a |0 into √12 ( |0 + |1) (the first column of H), which
is ‘halfway’ between |0 and |1. Similarly, if in (1.8) the value of l0 is equal to
zero and the value of l1 is equal to one, then the Hadamard gate H turns a |1 into
√1 ( |0 − |1) (the second column of H), which also is ‘halfway’ between |0 and
2
√1 √1 √1 √1
10
|1. Because H 2 = H × H = 12 2 × 2 2 = , using H twice
√ − √1 √1 − √1 01
2 2 2 2
to a state does nothing to it. For IBM Q Experience, the graphical representation of
the Hadamard gate H appears in Fig. 1.7.
In Listing 1.3, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the third example in which we describe how to program with the
Hadamard gate operating one quantum bit. Figure 1.8 is the corresponding quantum
circuit of the program in Listing 1.3. The statement “OPENQASM 2.0;” on line one
of Listing 1.3 is to point out that the program is written with version 2.0 of Open
QASM.
Listing. 1.3 The program to the use of the Hadamard gate operating a quantum bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. measure q[0] → c[0];
Then, the statement “include “qelib1.inc”;” on line two of Listing 1.3 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
1.3 The Hadamard Gate of Single Quantum Bit 13
Fig. 1.8 The quantum circuit to the Hadamard gate operating a quantum bit
The statement “qreg q[5];” on line three of Listing 1.3 is to declare that in the
program there are five quantum bits. In the left top of Fig. 1.8, five quantum bits are
subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum bit is
set to |0.
Next, the statement “creg c[5];” on line four of Listing 1.3 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 1.8, five classical bits
are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical
bit is set to 0. Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit. The statement “h q[0];” on line five of Listing 1.3 actually
√1 √1 √1
2 2 1 2 √1
1 √1
1 0
completes × = = = + =
√1 − √1 0 √1 2 1 2 0 1
2 2 2
√1 ( |0 + |1). This is to say that the statement “h q[0];” on line five of Listing
2
1.3 is to use the Hadamard gate to convert q[0] from one state |0 to another state
√1 ( |0 + |1) (its superposition), where “h” is to represent the Hadamard gate.
2
Next, the statement “measure q[0] → c[0];” on line six of Listing 1.3 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits
in IBM’s quantum computers, we apply the command “simulate” to execute the
program in Listing 1.3. The result appears in Fig. 1.9. From Fig. 1.9, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1)
with the probability 0.520. Or we obtain the answer 00000 with the probability
0.480 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0).
Fig. 1.9 After the measurement to the program in Listing 1.3 is completed, we obtain the answer
00001 with the probability 0.520 or the answer 00000 with the probability 0.480
14 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the Z gate is
1 0 l0 l0 1 0
× = = l0 + (−l1 ) = l0 | 0 + (−l1 )| 1. (1.12)
0 −1 l1 −l1 0 1
In Listing 1.4, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the fourth example in which we illustrate how to program with
the Z gate that leaves |0 unchanged and flips the sign of |1 to give −|1. Figure 1.11
is the corresponding quantum circuit of the program in Listing 1.4. The statement
“OPENQASM 2.0;” on line one of Listing 1.4 is to indicate that the program is written
with version 2.0 of Open QASM. Next, the statement “include “qelib1.inc”;” on line
two of Listing 1.4 is to continue parsing the file “qelib1.inc” as if the contents of the
file were pasted at the location of the include statement, where the file “qelib1.inc”
is Quantum Experience (QE) Standard Header and the path is specified relative
to the current working directory. The statement “qreg q[5];” on line three of Listing
1.4 is to declare that in the program there are five quantum bits. In the left top of
Fig. 1.11, five quantum bits are subsequently q[0], q[1], q[2], q[3] and q[4]. The
initial state of each quantum bit is set to |0. Next, the statement “creg c[5];” on line
four of Listing 1.4 is to declare that there are five classical bits in the program. In the
left bottom of Fig. 1.11, five classical bits are subsequently c[0], c[1], c[2], c[3] and
c[4]. The initial value of each classical bit is set to 0. Classical bit c[4] is the most
significant bit and classical bit c[0] is the least significant bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. z q[0];
7. measure q[0] → c[0];
Fig. 1.11 The corresponding quantum circuit of the program in Listing 1.4
16 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.12 After the measurement to the program in Listing 1.4 is completed, we obtain the answer
00001 with the probability 0.490 or the answer 00000 with the probability 0.510
√ √ π
where i = −1 is known as the imaginary unit and e− −1× 2 = cos π2 +
√ √ √ π √ √
−1 × sin π2 = −1 and e− −1× 2 = cos − π2 + −1 × sin − π2 = − −1.
√
* t 0 − −1 0 −i
(Y ) = √ = , where the * indicates complex conjuga-
−1 0 i 0
√
* t 0 − −1
tion and the t is the transpose operation. Since Y × (Y ) = √ ×
−1 0
√ √ √
0 − −1 0 − −1 0 − −1 10
√ = (Y * )t × Y = √ × √ = ,
−1 0 −1 0 −1 0 01
Y is a unitary matrix or a unitary operator. This is to say that the Y gate is one of
quantum gates with single quantum bit. If the quantum state l0 |0 + l1 |1 is written
in a vector notation as
l0
, (1.14)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the Y gate is
√ √
√ 1 √
0
0 − −1 l − −1l1
√ × 0 = √ = − −1l1 + −1l0
−1 0 l1 −1l0 0 1
√ √
= − −1l1 |0 + −1l0 |1. (1.15)
In Listing 1.5, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the fifth example in which we introduce how to program with
the Y gate that converts single quantum bit from one state √12 |0 + √12 |1 to another
√ √
state − −1 √12 |0 + −1 √12 |1. Figure 1.14 is the corresponding quantum
circuit of the program in Listing 1.5. The statement “OPENQASM 2.0;” on line one
18 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.14 The corresponding quantum circuit of the program in Listing 1.5
of Listing 1.5 is to point out that the program is written with version 2.0 of Open
QASM. Next, the statement “include “qelib1.inc”;” on line two of Listing 1.5 is
to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Experience (QE) Standard Header and the path is specified relative to the current
working directory. The statement “qreg q[5];” on line three of Listing 1.5 is to
declare that in the program there are five quantum bits. In the left top of Fig. 1.14,
five quantum bits are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state
of each quantum bit is set to |0. Next, the statement “creg c[5];” on line four of
Listing 1.5 is to declare that there are five classical bits in the program. In the left
bottom of Fig. 1.14, five classical bits are subsequently c[0], c[1], c[2], c[3] and c[4].
The initial value of each classical bit is set to zero (0). Classical bit c[4] is the most
significant bit and classical bit c[0] is the least significant bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. y q[0];
7. measure q[0] → c[0];
Fig. 1.15 After the measurement to the program in Listing 1.5 is completed, we obtain the answer
00001 with the probability 0.520 or the answer 00000 with the probability 0.480
gate. Next, the statement “y q[0];” on line√six of Listing 1.5 actually imple-
√ √1
√
− −1 × √12
0 − −1 2 √1
− −1
ments √ × = √ = √ =
−1 0 √1 −1 × √1 2 −1
√ 2 2
√1
− −1 0
+ √
2 0 −1
√ 1 √ 0 √ √
= − −1 √12 + −1 √12 = − −1 √12 |0 + − −1 √12 |1
0 1
√ √
= √12 − −1|0 + −1 |1 . This indicates that the statement “y q[0];” on line
six of Listing 1.5 is to apply the Y gate to convert q[0] from one state √12 ( |0 + |1)
√ √
to another state √12 − −1|0 + −1|1 .
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.5 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits
in IBM’s quantum computers, we apply the command “simulate” to execute the
program in Listing 1.5. The result appears in Fig. 1.15. From Fig. 1.15, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1)
with the probability 0.520. Or we obtain the answer 00000 with the probability
0.480 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0).
The S gate of single quantum bit that is the square root of the Z gate is
1 0 10 1 √0
S= √ = = π (1.16)
0 −1 0i 0 e −1× 2
√ √ π √
where i = −1 is known as the imaginary unit and e −1× 2 = cos π2 + −1 ×
π √
sin 2 = −1. It is assumed that S + is the conjugate-transpose matrix of S and is
20 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
* t 1 0 1 0
equal to (S ) = √ = , where the * points out complex conjuga-
0 − −1 0 −i
1 0
tion and the t indicates the transpose operation. Because S × (S * )t = √ ×
0 −1
1 0 1 0 1 0 10
√ = (S * )t × S = √ × √ = , S is a unitary
0 − −1 0 − −1 0 −1 01
matrix or a unitary operator. This implies that the S gate is one of quantum gates with
single quantum bit. If the quantum state l0 |0 + l1 |1 is written in a vector notation
as
l0
, (1.17)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the S gate is
√
0
1 0 l0 l0 1
√ × = √ = (l0 ) + −1l1
0 −1 l1 −1l1 0 1
√ √ π
= (l0 )0 > + −1l1 1 > = (l0 )0 > + e −1× 2 × l1 1. (1.18)
This indicates that the S gate converts single quantumbit from one state
l0 |0 +
√ √ π
l1 |1 to another state (l0 ) |0 + −1 l1 |1 = (l0 ) |0 + e −1× 2 × l1 |1. This is
also to say that
the S gate leaves |0 unchanged and modifies the phase of |1 to give
√
−1× π2
√
−1 |1= e |1 . The probability of measuring a |0 or |1 is unchanged
after applying the
S gate, however
it modifies
the phase
of the quantum state. Because
1 0 1 0 1 0
S2 = S × S = √ × √ = , applying S twice to a state is
0 −1 0 −1 0 −1
equivalent to do the Z gate to it. For IBM Q Experience, the graphical representation
of the S gate appears in Fig. 1.16.
In Listing 1.6, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the sixth example in which we describe how to program with
1.6 The S Gate of Single Quantum Bit 21
Fig. 1.17 The corresponding quantum circuit of the program in Listing 1.6
the S gate that converts single quantum bit from one state √12 ( |0 + |1) to another
state √12 |0 + −1 |1 . Figure 1.17 is the corresponding quantum circuit of
√
the program in Listing 1.6. The statement “OPENQASM 2.0;” on line one of Listing
1.6 is to indicate that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 1.6 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
The statement “qreg q[5];” on line three of Listing 1.6 is to declare that in the
program there are five quantum bits. In the left top of Fig. 1.17, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum bit
is set to |0. Next, the statement “creg c[5];” on line four of Listing 1.6 is to declare
that there are five classical bits in the program. In the left bottom of Fig. 1.17, five
classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each
classical bit is set to 0. Classical bit c[4] is the most significant bit and classical bit
c[0] is the least significant bit.
Listing. 1.6 The program to the use of the S gate.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. s q[0];
7. measure q[0] → c[0];
Fig. 1.18 After the measurement to the program in Listing 1.6 is completed, we obtain the answer
00001 with the probability 0.550 or the answer 00000 with the probability 0.450
Listing 1.6 is to use the Hadamard gate to convert q[0] from one state |0to
another state √12 ( |0 + |1) (its superposition), where “h” is to represent the
Hadamard gate. Next, the statement “s q[0];” on line six of Listing 1.6 actu-
√1 √1
1 0 2 2 1 1
ally completes √ × = √ = √2 √ =
0 −1 √1 −1 × √12 −1
2
√
√1
1 0 √1
1 √ 0
= √12 |0 + −1|1 .
+ √ = + −1
2 0 −1 2 0 1
This is to say that the statement “s q[0];” on line six of Listing 1.6is to use√the S gate
to convert q[0] from one state √12 ( |0 + |1) to another state √12 |0 + −1 |1 .
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.6 is to
measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. In the backend ibmqx4 with five quantum
bits in IBM’s quantum computers, we use the command “simulate” to execute the
program in Listing 1.6. The result appears in Fig. 1.18. From Fig. 1.18, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1)
with the probability 0.550. Or we obtain the answer 00000 with the probability 0.450
(c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0).
The S + gate of single quantum bit that is the conjugate-transpose matrix of the S gate
is
+ 1 0 1 0 1 0 1 √0
S = √ = = √ π = π , (1.19)
0 − −1 0 −i 0 −e −1× 2 0 e− −1× 2
√ √ π
where i = −1 is known as the imaginary unit and e− −1× 2 = cos − π2 +
√ √
−1 × sin − π2 = − −1. It is supposed that (S + )+ is the conjugate-transpose
+ + * t 1 0 10 1 √0
matrix of S and is equal to ((S ) ) = √ = = π ,
0 −1 0i 0 e −1× 2
where the * indicates complex conjugation and the t is the transpose operation.
1.7 The S+ Gate of Single Quantum Bit 23
+ + * t 1 0 1 0 + * t + 1 0
Since S × ((S ) ) = √ × √ = ((S ) ) × S = √
0 − −1 0 −1 0 −1
1 0 10
× √ = , S + is a unitary matrix or a unitary operator. This is to
0 − −1 01
say that the S + gate is one of quantum gates with single quantum bit. If the quantum
state l0 |0 + l1 |1 is written in a vector notation as
l0
, (1.20)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the S + gate is
√
1 0 l0 l0 1 0
√ × = √ = (l0 ) + (− −1l1 )
0 − −1 l1 − −1l1 0 1
√ √
− −1× π2
= (l0 ) |1 + − −1l1 |1 = (l0 ) |1 + e × l1 |1. (1.21)
This implies that the S + gate converts single quantum bit from one state
l0 |0 + l1 |1
√ √
− −1× π2
to another state (l0 ) |0 + − −1 l1 |1 = (l0 ) |0 + e × l1 |1. This also
+
indicates that the S gate leaves |0 unchanged and modifies the phase of |1 to give
√ √
− −1× π2
− −1 |1= e |1 . The probability of measuring a |0 or |1 is unchanged
+
after applying the S gate,
howeverit modifies
the phase
of the quantum
state. Because
+ 2 + + 1 0 1 0 1 0
(S ) = S × S = √ × √ = , applying S + twice
0 − −1 0 − −1 0 −1
to a state is equivalent to do the Z gate to it. For IBM Q Experience, the graphical
representation of the S + gate appears in Fig. 1.19.
In Listing 1.7, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the seventh example in which we illustrate how to program with
the S + gate that converts single quantum bit from one state √12 ( |0 + |1) to another
√
state √12 |0 + − −1 |1 . Figure 1.20 is the corresponding quantum circuit of
24 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.20 The corresponding quantum circuit of the program in Listing 1.7
the program in Listing 1.7. The statement “OPENQASM 2.0;” on line one of Listing
1.7 is to point out that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 1.7 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
The statement “qreg q[5];” on line three of Listing 1.7 is to declare that in the
program there are five quantum bits. In the left top of Fig. 1.20, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum bit
is set to |0. Next, the statement “creg c[5];” on line four of Listing 1.7 is to declare
that there are five classical bits in the program. In the left bottom of Fig. 1.20, five
classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each
classical bit is set to 0. Classical bit c[4] is the most significant bit and classical bit
c[0] is the least significant bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. sdg q[0];
7. measure q[0] → c[0];
Fig. 1.21 After the measurement to the program in Listing 1.7 is completed, we obtain the answer
00001 with the probability 0.500 or the answer 00000 with the probability 0.500
1 0
the statement “sdg q[0];” on line six of Listing 1.7 actually performs √ ×
0 − −1
√1 √1
2 2 √1
1 √1
1 0
= √ = √ = + √ =
√1 − −1 × √12 2 − −1 2 0 − −1
2
√ 0 √
√1
1 √1
2
+ − −1 = 2
|0 + − −1 |1 . This indicates that
0 1
+
the statement “sdg q[0];” on line six of Listing 1.7 is to
applythe√S gate
to convert
1 1
q[0] from one state √2 ( |0 + |1) to another state √2 |0 + − −1 |1 .
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.7 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits
in IBM’s quantum computers, we apply the command “simulate” to execute the
program in Listing 1.7. The result appears in Fig. 1.21. From Fig. 1.21, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = 1)
with the probability 0.500. Or we obtain the answer 00000 with the probability
0.500 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = 0).
The T gate of single quantum bit that is the square root of the S gate is
1 √0 1 0
√
1 0
T = = = , (1.22)
0 1+i
π 1+√ −1
0 e −1× 4 0 2
√
2
√ √ π √
where i = −1 is known as the imaginary unit and e −1× 4 = cos π4 + −1 ×
√
sin π4 = 1+ 2 −1 = 1+i
2
. It is assumed that T + is the conjugate-transpose matrix of
1 √0 1 √ 0 1 0
T and is equal to (T * )t = = = , where the *
0 1−i
π 1−√ −1
− −1× 4 0 √
0e 2 2
indicates complex conjugation and the t is the transpose operation. Since T × (T * )t
26 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
1 √0 1 √0 * t 1 √0 1 √0
= π × π = (T ) × T = π × π
0 e −1× 4 0 e− −1× 4 0 e− −1× 4 0 e −1× 4
10
= , T is a unitary matrix or a unitary operator. This indicates that the T gate
01
is one of quantum gates with single quantum bit. If the quantum state l0 |0 + l1 |1is
written in a vector notation as
l0
, (1.23)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the T gate is
1 √0 l0 √
l0
π × = π
0 e −1× 4 l1 e −1× 4 × l1
√ 0 √
1 −1× π4 π
= (l0 ) + e × l1 = (l0 ) |0 + e −1× 4 × l1 |1. (1.24)
0 1
In Listing 1.8, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the eighth example in which we describe how to program with
the T gate that converts single quantum bit from one state √12 ( |0 + |1) to another
1.8 The T Gate of Single Quantum Bit 27
Fig. 1.23 The corresponding quantum circuit of the program in Listing 1.8
√ π
state √12 |0 + e −1× 4 |1 ). Figure 1.23 is the corresponding quantum circuit
of the program in Listing 1.8. The statement “OPENQASM 2.0;” on line one of
Listing 1.8 is to indicate that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 1.8 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
The statement “qreg q[5];” on line three of Listing 1.8 is to declare that in the
program there are five quantum bits. In the left top of Fig. 1.23, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum bit
is set to |0 . Next, the statement “creg c[5];” on line four of Listing 1.8 is to declare
that there are five classical bits in the program. In the left bottom of Fig. 1.23, five
classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each
classical bit is set to 0. Classical bit c[4] is the most significant bit and classical bit
c[0] is the least significant bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. t q[0];
7. measure q[0] → c[0];
Fig. 1.24 After the measurement to the program in Listing 1.8 is completed, we obtain the answer
00001 with the probability 0.480 or the answer 00000 with the probability 0.520
superposition), where “h” is to represent the Hadamard gate. Next, the statement
√1
1 √0 2
“t q[0];” on line six of Listing 1.8 actually completes π × =
0 e −1× 4 √1
2
√1
2 1 1 1 1 0 1 1
√ π = √ √ π = √ ( + √ π ) = ( √ ) ( +
e −1× 4 × √12 2 e −1× 4 2 0 e −1× 4 2 0
√
√
−1× π4 0 π
(e ) ) = √12 (|0 + e −1× 4 |1). This is to say that the statement “t
1
q[0];” on line six of Listing 1.8 is to use the T gate to convert q[0] from one state √12
√ π
( |0 + |1) to another state √12 |0 + e −1× 4 |1 .
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.8 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits in
IBM’s quantum computers, we use the command “simulate” to execute the program
in Listing 1.8. The result appears in Fig. 1.24. From Fig. 1.24, we obtain the answer
00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = 1) with the
probability 0.480. Or we obtain the answer 00000 with the probability 0.520
(c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = 0).
The T + gate of single quantum bit that is the conjugate-transpose matrix of the T
gate is
+ 1 √0 1 0
√
1 0
T = = = , (1.25)
0 1−i
π 1−√ −1
0 e− −1× 4 0 2
√
2
√ √ π √
where i = −1 is known as the imaginary unit and e− −1× 4 = cos − π4 + −1
√
× sin − 4 = 1−√2−1 = 1−i
π
√ . It is supposed that (T + )+ is the conjugate-transpose
2
1.9 The T+ Gate of Single Quantum Bit 29
1 √0 1 √ 0 1 0
matrix of T + and is equal to ((T + )* )t = = = ,
0 1+i
π 1+√ −1
−1× 4 0 √
0e 2 2
where the * is thecomplex conjugation
and thet is the transpose operation.
Since
+ + * t 1 0 1 0 + * t + 1 0
T × ((T ) ) = √ π × √ π = ((T ) ) × T = √ π
0 e− −1× 4 0 e −1× 4 0 e −1× 4
1 √0 10
× π = , T + is a unitary matrix or a unitary operator. This is to
0 e− −1× 4 01
say that the T + gate is one of quantum gates with single quantum bit. If the quantum
state l0 |0 + l1 |1 is written in a vector notation as
l0
, (1.26)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the T + gate is
1 √0 l0 √
l0
π × = π
0 e− −1× 4 l1 e− −1× 4 × l1
√ 0 √
1 − −1× π4 π
= (l0 ) + e × l1 = (l0 ) |0 + e− −1× 4 × l1 |1. (1.27)
0 1
This indicates that the T + gateconverts singlequantum bit from one state l0 |0 +
√ π
l1 |1 to another state (l0 ) |0 + e− −1× 4 × l1 |1. This also is to say that the T +
√ π
gate leaves |0 unchanged and modifies the phase of |1 to give e− −1× 4 |1.
The probability of measuring a |0 or |1 is unchanged after applying the T + gate,
however
it modifies
the phase ofthe quantum
state.Because
(T + )2
= T+ × T+ =
1 √0 1 √0 1 √0 1 0
− −1× π4 × − −1× π4 = π = √ , applying T +
0e 0e 0 e− −1× 2 0 − −1
twice to a state is equivalent to do the S + gate to it. For IBM Q Experience, the
graphical representation of the T + gate appears in Fig. 1.25.
In Listing 1.9, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the ninth example in which we introduce how to program with
the T + gate that converts single quantum bit from one state √12 ( |0 + |1) to another
√ π
state √12 |0 + e− −1× 4 |1 . Figure 1.26 is the corresponding quantum circuit
of the program in Listing 1.9. The statement “OPENQASM 2.0;” on line one of
Listing 1.9 is to point out that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 1.9 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
The statement “qreg q[5];” on line three of Listing 1.9 is to declare that in the
program thereare five quantum bits. In the left top of Fig. 1.26, five quantum bits are
subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum bit is
set to |0. Next, the statement “creg c[5];” on line four of Listing 1.9 is to declare
that there are five classical bits in the program. In the left bottom of Fig. 1.26, five
classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each
classical bit is set to 0. Classical bit c[4] is the most significant bit and classical bit
c[0] is the least significant bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. tdg q[0];
7. measure q[0] → c[0];
Fig. 1.26 The corresponding quantum circuit of the program in Listing 1.9
1.9 The T+ Gate of Single Quantum Bit 31
Fig. 1.27 After the measurement to the program in Listing 1.9 is completed, we obtain the answer
00001 with the probability 0.550 or the answer 00000 with the probability 0.450
√1 √1
The statement “h q[0];” on line five of Listing 1.9 actually completes 2 2
√1 − √12
2
√1
1 √1
1 √1 (
1 0
× = 2 = = + ) = √12 ( |0 + |1). This is to
0 √1 2 1 2 0 1
2
say that the statement “h q[0];” on line five of Listing 1.9 is to use the Hadamard
gate to convert q[0] from one state |0 to another state √12 ( |0 + |1) (its super-
position), where “h” is to represent the Hadamard gate. Next, the statement “tdg
√1
1 √0 2
q[0];” on line six of Listing 1.9 actually implements − −1× π4 × =
0e √1
2
√1
1 1 0
√ 2
π = √1 √ π = √1 ( + √ π = √12
e− −1× 4 × √12 2 e− −1× 4 2 0 e− −1× 4
√ 0 √
1 − −1× π4
π
+ e = √12 |0 + e− −1× 4 |1 . This indicates that the
0 1
+
statement “tdg q[0];” on line six of Listing 1.9 is to use the
T√ gateπ to convert
q[0]
1 1 − −1×
from one state √2 ( |0 + |1) to another state √2 |0 + e 4 |1 .
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.9 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits
in IBM’s quantum computers, we apply the command “simulate” to execute the
program in Listing 1.9. The result appears in Fig. 1.27. From Fig. 1.27, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1)
with the probability 0.550. Or we obtain the answer 00000 with the probability
0.450 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0).
+
It is assumed
that id is the conjugate-transpose matrix of id and is equal to
10
((id)* )t = , where the * is the complex conjugation and the t is the transpose
01
* t 10 10 * t 10
operation. Because id × ((id) ) = × = ((id) ) × id = ×
01 01 01
10 10
= , id is a unitary matrix or a unitary operator. This indicates that
01 01
the identity gate id is one of quantum gates with single quantum bit. If the quantum
state l0 |0 + l1 |1 is written in a vector notation as
l0
, (1.29)
l1
with the top entry is the amplitude for |0 and the bottom entry is the amplitude
for |1, then the corresponding output from the identity gate id is
10 l0 l0 1 0
× = = (l0 ) + (l1 ) = (l0 )|0 > +(l1 )|1 > . (1.30)
01 l1 l1 0 1
This is to say that the identity gate id converts single quantum bit from one state
l0 |0+l1 |1 to another state (l0 ) |0+(l1 ) |1. This also is to say that the identity gate id
does not change |0 and |1 and only performs an idle operation on single quantum
bit for a time equal to one unit of time. The probability of measuring a|0 or |1
1 0
is unchanged after using the identity gate id. Since (id)2 = id × id = ×
01
10 10
= , applying the identity gate id twice to a state is equivalent to do
01 01
nothing to it. For IBM Q Experience, the graphical representation of the identity
gate id appears in Fig. 1.28.
Fig. 1.29 The corresponding quantum circuit of the program in Listing 1.10
In Listing 1.10, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the tenth example in which we introduce how to program with
the identity gate id that converts single quantum bit q[0] from one state √12 ( |0 + |1)
to another state √12 ( |0 + |1). Actually, the identity gate id only completes an idle
operation on q[0] for a time equal to one unit of time. Figure 1.29 is the corresponding
quantum circuit of the program in Listing 1.10. The statement “OPENQASM 2.0;”
on line one of Listing 1.10 is to indicate that the program is written with version 2.0 of
Open QASM. Next, the statement “include “qelib1.inc”;” on line two of Listing 1.10
is to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Experience (QE) Standard Header and the path is specified relative to the current
working directory. The statement “qreg q[5];” on line three of Listing 1.10 is to
declare that in the program there are five quantum bits. In the left top of Fig. 1.29,
five quantum bits
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. id q[0];
7. measure q[0] → c[0];
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial state of each quantum
bit is set to |0. Next, the statement “creg c[5];” on line four of Listing 1.10 is to
declare that there are five classical bits in the program. In the left bottom of Fig. 1.29,
34 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.30 After the measurement to the program in Listing 1.10 is completed, we obtain the answer
00001 with the probability 0.460 or the answer 00000 with the probability 0.540
five classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of
each classical bit is set to 0. Classical bit c[4] is the most significant bit and classical
bit c[0] is the least significant bit.
√1 √1
The statement “h q[0];” on line five of Listing 1.10 actually performs 2 2
√1 − √12
2
√1
1 1 1 0
× = 12 = √12 = √12 + = √12 ( |0 + |1). This implies
0 √
2
1 0 1
that the statement “h q[0];” on line five of Listing 1.10 is to apply the Hadamard gate
to convert q[0] from one state |0 to another state √12 ( |0 + |1) (its superposition),
where “h” is to represent the Hadamard gate. Next, the statement “id q[0];” on line
√1 √1
10 1 1
six of Listing 1.10actually completes 2
× 1 = 1 = 2 2 √ = √12
01 √
2
√
2
1
1 0
( + ) = √12 ( |0 + |1). This is to say that the statement “id q[0];” on line
0 1
six of Listing 1.10 is to use the identity gate id to convert q[0] from one state √12
( |0 + |1) to another state √12 ( |0 + |1). This also is to say that the identity gate
id only completes an idle operation on q[0] for a time equal to one unit of time.
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.10 is
to measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. In the backend ibmqx4 with five quantum
bits in IBM’s quantum computers, we apply the command “simulate” to execute the
program in Listing 1.10. The result appears in Fig. 1.30. From Fig. 1.30, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1)
with the probability 0.460. Or we obtain the answer 00000 with the probability
0.540 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0).
⎛ ⎞
1 0 0 0
⎜0 1 0 0⎟
UC N =⎜
⎝0
⎟. (1.31)
0 0 1⎠
0 0 1 0
00 10
⎛ ⎞ ⎛ ⎞
10 00 10 00
⎜01 00⎟ ⎜01 00⎟
the transpose operation. Since U CN × ((U CN )* )t = ⎜⎝ 0 0 0 1 ⎠×⎝ 0 0 0 1 ⎠=
⎟ ⎜ ⎟
00 10 00 10
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
10 00 10 00 10 00
⎜01 00⎟ ⎜01 00⎟ ⎜01 00⎟
((U CN )* )t × U CN = ⎜
⎝ 0 0 0 1 ⎠ × ⎝ 0 0 0 1 ⎠ = ⎝ 0 0 1 0 ⎠, U CN is a unitary
⎟ ⎜ ⎟ ⎜ ⎟
00 10 00 10 00 01
matrix or a unitary operator. This is to say that the controlled-NOT or CNOT gate
U CN is one of quantum gates with two quantum bits. If the quantum state l0 |00 +
l1 |01 + l 2 |10 + l3 |11 is written in a vector notation as
⎛ ⎞
l0
⎜ l1 ⎟
⎜ ⎟,
⎝ l2 ⎠ (1.32)
l3
with the first entry l0 is the amplitude for |00, the second entry l1 is the amplitude
for |01, the third entry l2 is the amplitude for |10 and the fourth entry l 3 is the
amplitude for |11, then the corresponding output from the CNOT gate U CN is
⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎞⎛ ⎛ ⎞ ⎛ ⎞
1 0 0 0 l0 l0 1 0 0 0
⎜0 1 0 0⎟ ⎜0 ⎜1⎟ ⎜0
⎟ + l2 ⎜ 0 ⎟
⎜ l1 ⎟ ⎜ l1 ⎟ ⎟ ⎟ ⎜ ⎟
⎜ ⎟×⎜ ⎟ = ⎜ ⎟ = l0 ⎜ ⎟ + l1 ⎜ ⎟ + l3 ⎜
⎝0 0 0 1 ⎠ ⎝ l2 ⎠ ⎝ l3 ⎠ ⎝0 ⎠ ⎝0⎠ ⎝1 ⎠ ⎝0⎠
0 0 1 0 l3 l2 0 0 0 1
= l0 |00 > +l1 |01 > +l3 |10 > +l2 |11 > . (1.33)
This indicates that the CNOT gate U CN converts two quantum bits from one state l0
|00 + l1 |01 + l 2 |10 + l3 |11to another state l 0 |00 + l1 |01 + l 3 |10 + l2 |11.
This is to say that in the CNOT gate U CN if the control quantum bit (the first quantum
bit) is set to 0, then the target quantum bit (the second quantum bit) is left alone. If
the control quantum bit (the first quantum bit) is set to 1, then the target quantum
36 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
bit (the second quantum bit) is flipped. The probability of measuring a |00 or |01
is unchanged, the probability of measuring a |10 is |l 3 |2 and the probability of
measuring a |11 is |l2 |2 after applying the CNOT gate U CN . Because (U CN )2 = U CN
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
10 00 10 00 10 00
⎜01 00⎟ ⎜01 00⎟ ⎜01 00⎟
× U CN = ⎜ ⎝ 0 0 0 1 ⎠ × ⎝ 0 0 0 1 ⎠ = ⎝ 0 0 1 0 ⎠, applying the CNOT gate
⎟ ⎜ ⎟ ⎜ ⎟
00 10 00 10 00 01
U CN twice to a state is equivalent to do nothing to it. For IBM Q Experience, the
graphical representation of the CNOT gate U CN appears in Fig. 1.31.
In the graphical representation of the CNOT gate U CN , the top wire carries the
controlled quantum bit and the bottom wire carries the target quantum bit.
Those authors that wrote textbooks write quantum algorithms with a fully connected
hardware in which one can apply a quantum gate of two quantum bits to any pair of
two quantum bits. In practice, ibmqx4 that is a real quantum computer may not have
full connectivity. In the ibmqx4 with five quantum bits, there are six connections. Six
connections of a CNOT gate appears in Fig. 1.32.The first CNOT gate in Fig. 1.32 has
the controlled quantum bit q[1] and the target quantum bit q[0] and the corresponding
instruction in version 2.0 of Open QASM is “cx q[1],q[0];”, where cx is to represent
the CNOT gate. The second CNOT gate in Fig. 1.32 has the controlled.
Fig. 1.32 A CNOT gate has six connections in ibmqx4 on IBM quantum computers
1.11 The Controlled-NOT Gate of Two Quantum Bits 37
quantum bit q[2] and the target quantum bit q[0] and the corresponding instruction
in version 2.0 of Open QASM is “cx q[2],q[0];”, where cx is to represent the CNOT
gate.
The third CNOT gate in Fig. 1.32 has the controlled quantum bit q[2] and the target
quantum bit q[1] and the corresponding instruction in version 2.0 of Open QASM is
“cx q[2],q[1];”, where cx is to represent the CNOT gate. The fourth CNOT gate in
Fig. 1.32 has the controlled quantum bit q[3] and the target quantum bit q[2] and the
corresponding instruction in version 2.0 of Open QASM is “cx q[3],q[2];”, where cx
is to represent the CNOT gate. The fifth CNOT gate in Fig. 1.32 has the controlled
quantum bit q[4] and the target quantum bit q[2] and the corresponding instruction
in version 2.0 of Open QASM is “cx q[4],q[2];”, where cx is to represent the CNOT
gate. The sixth CNOT gate in Fig. 1.32 has the controlled quantum bit q[3] and the
target quantum bit q[4] and the corresponding instruction in version 2.0 of Open
QASM is “cx q[3],q[4];”, where cx is to represent the CNOT gate.
In contrast, a fully connected hardware with five quantum bits would allow a
CNOT gate to apply to twenty different pairs of any two-quantum bits. This indicates
that there are fourteen “missing connections”. Fortunately, there are different ways
to yield connections by means of using clever gate sequences. For example, a CNOT
gate that has the controlled quantum bit q[j] and the target quantum bit q[k] for 0 ≤ j
and k ≤ 4 can be reversed by means of applying Hadamard gates on each quantum bit
both before and after the CNOT gate. This is to say that the new instruction (the new
connection) “cx q[k], q[j]” is implemented by means of applying the five instructions
that are subsequently “h q[j];”, “h q[k]”, “cx q[j], q[k]”, “h q[j]” and “h q[k]” for 0
≤ j and k ≤ 4. Similarly, there exists a gate sequence to make a CNOT gate with the
controlled bit q[j] and the target bit q[l] if one has connections between the controlled
bit q[j] and the target bit q[k], and the controlled bit q[k] and the target bit q[l] for 0
≤ j, k and l ≤ 4. This indicates that the new instruction (the new connection) “cx q[j],
q[l]” is implemented by means of applying the four instructions that are subsequently
“cx q[k], q[l]”, “cx q[j], q[k]”, “cx q[k], q[l]” and “cx q[j], q[k]” for 0 ≤ j, k and l ≤
4.
Offered its data input is initialized permanently with |0. Then, the CNOT gate emits
a copy of the controlled input on each output. Therefore, the CNOT gate actually is
a copy machine of one bit. In Listing 1.11, the program in the backend ibmqx4 with
five quantum bits in IBM’s quantum computer is the eleventh example in which we
describe how to program with the CNOT gate that converts
thecontrolled bit q[3]
1
and the target bit q[4] from one state √2 ( |0 + |1) ( |0)= √12 ( |00 + |10)
to another state √12 ( |00 + |11). Actually, the CNOT gate emits a copy of the
38 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.33 The corresponding quantum circuit of the program in Listing 1.11
controlled input on each output. Figure 1.33 is the corresponding quantum circuit
of the program in Listing 1.11. The statement “OPENQASM 2.0;” on line one of
Listing 1.11 is to point out that the program is written with version 2.0 of Open
QASM. Then, the statement “include “qelib1.inc”;” on line two of Listing 1.11 is
to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Experience (QE) Standard Header and the path is specified relative to the current
working directory.
The statement “qreg q[5];” on line three of Listing 1.11 is to declare that in the
program there are five quantum bits. In the left top of Fig. 1.33, five quantum bits are
subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit is
set to |0. Next, the statement “creg c[5];” on line four of Listing 1.11 is to declare
that there are five classical bits in the program. In the left bottom of Fig. 1.33, five
classical bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each
classical bit is set to 0. Classical bit c[4] is the most significant bit and classical bit
c[0] is the least significant bit.
Listing. 1.11 Implementing a copy machine of one bit with the CNOT gate.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[3];
6. cx q[3],q[4];
7. measure q[3] → c[3];
8. measure q[4] → c[4];
Hadamard gate to convert q[3] from one state |0 to another state √12 ( |0 + |1) (its
superposition), where “h” is to represent the Hadamard gate. Next, the statement “cx
⎛ ⎞ ⎛ √1 ⎞
10 00 2
⎜01 00⎟ ⎜ ⎜ 0 ⎟
⎟
q[3],q[4];” on line six of Listing 1.11 actually completes ⎝
⎜ ⎟ × ⎟=
0 0 0 1 ⎠ ⎝ √12 ⎠
⎜
00 10 0
⎛ 1 ⎞
√
⎛ ⎞ ⎛⎛ ⎞ ⎛ ⎞⎞
1 1 0
⎜ 2 ⎟
0 ⎜ 0
⎟ √1 ⎜ ⎟ √1 ⎜⎜ ⎟ ⎜ 0 ⎟
⎟ ⎜ ⎜ 0 ⎟ ⎜ ⎟
⎟ = √1 (|00 + |11). This indicates
⎟ = 2 ⎝ ⎠ = 2 ⎝⎝ ⎠ + ⎝ ⎟
⎜
⎝ 0 ⎠ 0 0 0 ⎠ 2
⎜ ⎠
1
√
2
1 0 1
that the statement “cx q[3],q[4];” on line six of Listing 1.11 is to apply the CNOT
gate to emit a copy of the controlled input q[3] on each output.
Next, the statement “measure q[3] → c[3];” on line seven of Listing 1.11 is to
measure the fourth quantum bit q[3] and to record the measurement outcome by
overwriting the fourth classical bit c[3]. The statement “measure q[4] → c[4];” on
line eight of Listing 1.11 is to measure the fifth quantum bit q[4] and to record
the measurement outcome by overwriting the fifth classical bit c[4].In the backend
ibmqx4 with five quantum bits in IBM’s quantum computers, we use the command
“simulate” to execute the program in Listing 1.11. The result appears in Fig. 1.34.
From Fig. 1.34,
we obtain the answer 00000 (c[4] = 0 = q[4] = |0, c[3] = 0 = q[3] = |0, c[2]
= 0, c[1] = 0 and c[0] = 0) with the probability 0.540. Or we obtain the answer
11000 with the probability 0.460 (c[4] = 1 = q[4] = |1, c[3] = 1 = q[3] = |1, c[2]
= 0, c[1] = 0 and c[0] = 0). If the answer is 00000, then this imply that the CNOT
gate copy the value 0 of the controlled input q[3] to the target bit q[4]. If the answer
is 11000, then this indicates that the CNOT gate copy the value 1 of the controlled
input q[3] to the target bit q[4].
Fig. 1.34 After the measurement to the program in Listing 1.11 is completed, we obtain the answer
00000 with the probability 0.540 or the answer 11000 with the probability 0.460
40 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
The U1(λ) gate that is the first physical gate of the Quantum Experience and is a
phase gate of single quantum bit of one parameter with zero duration is
1 √0
U 1(λ) = U 1(lambda) = , (1.34)
0 e −1×λ
with the first entry l0 is the amplitude for |0 and the second entry l1 is the ampli-
tude for |1, then the corresponding output from the phase gate U1(λ) (U1(lambda))
is
√ 0
1 √0 l0 l0 1 −1×λ
× = √ = l 0 + e × l 1
0 e −1×λ l1 e −1×λ × l1 0 1
√
= l0 |0 > + e −1×λ × l1 |1 > . (1.36)
1 √0 1 √ 0
= , using the phase gate U1(λ) (U1(lambda)) twice to
0 e −1×λ 0 e −1×2×λ
a state √
is equivalent to do that leaves |0 unchanged and modifies the phase of |1to
give (e −1×2×λ ) |1to it. For IBM Q Experience, the graphical representation of the
phase gate U1(λ) (U1(lambda)) appears in Fig. 1.35.
In Listing 1.12, in the backend ibmqx4 with five quantum bits in IBM’s quantum
computer, the program is the twelfth example in which we introduce how to program
with the phase gate U1(2 * pi) that converts single quantum bit q[0] from one state
( √12 ) ( |0 + |1) to another state ( √12 ) ( |0 + |1). Because the input value of the
first parameter
lambda for the
phase
gate U1(lambda) is (2 * pi) and U1(2 * pi) is
1 √ 0 10
equal to = , the phase gate U1(2 * pi) actually implements
0 e −1×2×π 01
one identity gate. Figure 1.36 is the corresponding quantum circuit of the program
in Listing 1.12.
The statement “OPENQASM 2.0;” on line one of Listing 1.12 is to indicate that
the program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 1.12 is to continue parsing the file “qelib1.inc”
as if the contents of the file were pasted at the location of the include statement,
where the file “qelib1.inc” is Quantum Experience (QE) Standard Header and
the path is specified relative to the current working directory. The statement “qreg
q[5];” on line three of Listing 1.12 is to declare that in the program there are five
Fig. 1.36 The corresponding quantum circuit of the program in Listing 1.12
42 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
quantum bits. In the left top of Fig. 1.36, five quantum bits are subsequently q[0],
q[1], q[2], q[3] and q[4]. The initial value of each quantum bit is set to |0. Then,
the statement “creg c[5];” on line four of Listing 1.12 is to declare that there are five
classical bits in the program. In the left bottom of Fig. 1.36, five classical bits are
subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit is
set to 0. Classical bit c[4] is the most significant bit and classical bit c[0] is the least
significant bit.
Listing. 1.12 Program of using the phase gate U1(2 * pi) with one parameter.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. u1(2 * pi) q[0];
7. measure q[0] → c[0];
Fig. 1.37 After the measurement to the program in Listing 1.12 is completed, we obtain the answer
00000 with the probability 0.530 or the answer 00001 with the probability 0.470
The phase gate U2(φ, λ) (U2(phi, lambda)) that is the second physical gate of the
Quantum Experience and is a phase gate of single quantum bit of two parameters
with duration one unit of gate time is
√
√1 −e√−1×λ
U 2(φ, λ) = U 2(phi, lambda) = √ 2 √ 2 , (1.37)
−1×φ
e √ e −1×(λ+φ)
√
2 2
where φ and λ (phi and lambda) are both real numbers. It is assumed that (U2(φ,
λ))+ ((U2(phi, lambda))+ ) is the conjugate-transpose matrix
√
of U2(φ, λ) (U2(phi,
√1 e− √−1×φ
lambda)) and is equal to ((U2(φ, λ))* )t = √2 √ 2
−e−√ −1×λ e− −1×(λ+φ)
, where the * is the
√
2 2
complex conjugation and the t is the transpose operation.
√
Because U2(φ, λ) ×√ (U2(φ,
√1 −e√−1×λ √1 e− √−1×φ
λ))+ = U2(φ, λ) × ((U2(φ, λ))* )t = √ 2
−1×φ
e √
√ 2
e −1×(λ+φ)
× √2 √ 2
−e−√ −1×λ e− −1×(λ+φ)
√ √
2 2 2 √ 2
− −1×φ
√1 e √
= (U2(φ, λ))+ × U2(φ, λ) = ((U2(φ, λ))* )t × U2(φ, λ) = √2
− −1×λ
−e √
√ 2
− −1×(λ+φ)
e √
√ 2 2
√1 −e√−1×λ
√ 2 2 10
× −1×φ
e √
√
e −1×(λ+φ)
= , U2(φ, λ) (U2(phi, lambda)) is a unitary matrix or
√ 01
2 2
a unitary operator. This implies that the phase gate U2(φ, λ) (U2(phi, lambda)) is
one of quantum gates that is a phase gate of single quantum bit of two parameters
with duration one unit of time. If the quantum state l0 |0 + |1nis written in a vector
notation as
l0
, (1.38)
l1
44 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
with the first entry l0 is the amplitude for |0nand the second entry l1 is the ampli-
tude for |1, then the corresponding output from the phase gate U2(φ, λ) (U2(phi,
lambda)) is
√ ⎛ √ ⎞
−1×λ
√1 −e√−1×λ l0 −e √ ×l1
√ 2 2 l0
−1×φ
e √
√
e −1×(λ+φ)
× = ⎝ e√−1×φ ×l +e√2 −1×(λ+φ) ×l ⎠
√ l1 0 √ 1
2 2 2
√ √ √
−1×λ −1×φ
l0 − e × l1 e × l0 + e −1×(λ+φ) × l1
= √ |0 + √ |1. (1.39)
2 2
For the phase gate U2(phi, lambda) the input values of the first parameter phi and the
second parameter lambda
√
are respectively (0 * pi) and (1 * pi), so U2(0*pi, 1*pi) is
−1×1×π
√1 −e √ √1 √1
equal to √ 2 √ 2 = 2 2 . Therefore, the phase gate U2(0*pi,
e −1×0×π √1 − √12
−1×(1×π+0×π)
√ e √
2 2 2
1*pi) actually implements one Hadamard gate. In Listing 1.13, in the backend ibmqx4
with five quantum bits in IBM’s quantum computer, the program is the thirteenth
example in which we illustrate how to program with the
phase gate U2(0*pi, 1*pi)
that converts single quantum bit q[0] from one state √12 ( |0 + |1) to another state
1.13 The U2(φ, λ) Gate of Single Quantum Bit with Two Parameters 45
Fig. 1.39 The corresponding quantum circuit of the program in Listing 1.13
( |0). Figure 1.39 is the corresponding quantum circuit of the program in Listing
1.13.
The statement “OPENQASM 2.0;” on line one of Listing 1.13 is to point out that
the program is written with version 2.0 of Open QASM. Then, the statement “include
“qelib1.inc”;” on line two of Listing 1.13 is to continue parsing the file “qelib1.inc”
as if the contents of the file were pasted at the location of the include statement,
where the file “qelib1.inc” is Quantum Experience (QE) Standard Header and
the path is specified relative to the current working directory. The statement “qreg
q[5];” on line three of Listing 1.13 is to declare that in the program there are five
quantum bits. In the left top of Fig. 1.39, five quantum bits are subsequently q[0],
q[1], q[2], q[3] and q[4]. The initial value of each quantum bit is set to |0. Next,
the statement “creg c[5];” on line four of Listing 1.13 is to declare that there are five
classical bits in the program. In the left bottom of Fig. 1.39, five classical bits are
subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit is
set to 0. Classical bit c[4] is the most significant bit and classical bit c[0] is the least
significant bit.
Listing. 1.13 nProgram of using the phase gate U2(0*pi, 1*pi) with two parameters.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. u2(0*pi,1*pi) q[0];
7. measure q[0] → c[0];
Fig. 1.40 After the measurement to the program in Listing 1.13 is completed, we obtain the answer
00000 with the probability 1.000
use the Hadamard gate to convert q[0] from one state |0 to another state √12
( |0 + |1) (its superposition), where “h” is to represent the Hadamard gate. Next,
the statement “u2(0*pi,1*pi)
√
q[0];” on line six of Listing 1.13 actually implements
−1×1×π
√1 −e √ √1 √1 √1 √1
2 2 2 2 2 2 1
√
e −1×0×π e
√
−1×(1×π+0×π) × 1 = 1 1 × 1 = = |0. This
√ √ √
2
√
2
− 2
√ √
2
0
2 2
indicates that the statement “u2(0*pi,1*pi) q[0];” on line six of Listing 1.13 is to
complete one Hadamard gate to q[0]. Therefore, applying the Hadamard gate twice
from line five and line six of Listing 1.13 to q[0] does nothing to it.
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.13 is
to measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. In the backend ibmqx4 with five quantum
bits in IBM’s quantum computers, we use the command “simulate” to execute the
program in Listing 1.13. The result appears in Fig. 1.40. From Fig. 1.40, we obtain
the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0)
with the probability 1.000.
The phase gate U3(θ, φ, λ) (U3(theta, phi, lambda)) that is the third physical gate of
the Quantum Experience and is a phase gate of single quantum bit of three parameters
with duration two units of gate time is
√
cos θ2 × sin θ2
−1×λ
−e
U 3(θ, φ, λ) = √ √ , (1.40)
e −1×φ × sin θ2 e −1×(λ+φ) × cos θ2
where θ, φ and λ (theta, phi and lambda) are all real numbers. It is
supposed that (U3(θ, φ, λ))+ ((U3(theta, phi, lambda))+ ) is the conjugate-
transpose matrix of U3(θ, φ, λ) (U3(theta, phi, lambda)) and is equal to
1.14 The U3(θ, φ, λ) Gate of Single Quantum Bit of Three Parameters 47
θ √
× sin θ2
* t cos 2
e√− −1×φ
((U3(θ, φ, λ)) ) = √ , where the
−e− −1×λ × sin 2 e− −1×(λ+φ) × cos θ2
θ
with the first entry l 0 is the amplitude for |0 and the second entry l 1 is the
amplitude for |1, then the corresponding output from the phase gate U3(θ, φ, λ)
(U3(theta, phi, lambda)) is
√
cos θ2 × sin θ2
−1×λ
−e l
√ √ × 0
e −1×φ × sin θ2 e −1×(λ+φ) × cos θ2
l1
√
l × cos θ2 − l1 × e −1×λ × sin θ2
= √0 √
l0 × e −1×φ × sin θ2 + l1 × e −1×(λ+φ) × cos θ2
√
θ −1×λ θ
= l0 × cos − l1 × e × sin |0
2 2
√
√
−1×φ θ −1×(λ+φ) θ
+ l0 × e × sin + l1 × e × cos |1. (1.42)
2 2
This indicates that the phase gate U3(θ, φ, λ) (U3(theta, phi, lambda))
converts single quantum bit from one state l0 |0 + l1 |1 to another
√ √
l0 × cos θ2 − l1 × e −1×λ × sin θ2
state |0 + (l0 × e −1×φ ×
√
sin θ2 + l1 × e −1×(λ+φ) × cos θ2 ) |1. Because (U3(θ, φ, λ))2 =
48 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
⎛ √
⎞
2 θ −1×(λ+φ) 2 θ
cos − e × sin
⎜
⎜ 2 2 ⎟
⎟
√ √
θ θ
⎜ ⎟
⎜ −1×λ −1×(λ+φ) ⎟
⎜ − cos × sin ×e × 1+e ⎟
⎜
⎜ 2 2 ⎟
⎟, applying the
√ √
⎜
⎜ cos θ θ −1×φ
−1×(λ+φ)
⎟
× sin ×e × 1+e ⎟
2 2
⎜ ⎟
⎜ ⎟
⎜ √
√
⎟
θ θ
sin2 − cos2 × e −1×(λ+φ) × −e −1×(λ+φ)
⎝ ⎠
2 2
phase gate U3(θ, φ, λ) (U3(theta, phi, lambda)) twice to a state is equivalent to
modify the amplitude to it. For IBM Q Experience, the graphical representation of
the phase gate U3(θ, φ, λ) (U3(theta, phi, lambda)) appears in Fig. 1.41.
For the phase gate U3(theta, phi, lambda), the input value of the first parameter theta
is (0.5 * pi), the input value of the second parameter phi is (0 * pi) and the input
value of the third parameter lambda is (1 * pi), so U3(0.5*pi, 0*pi, 1*pi) √
is equal
π √ π 1 −e √−1×1×π
−1×1×π √
cos −e × sin 4 2 2
to √−1×0×π 4 √ = √−1×0×π √
e −1×(1×π+0×π)
=
e × sin π4 e −1×(1×π+0×π ) × cos π4 e √ √
2 2
√1 √1
2 2 . Hence, the phase gate U3(0.5*pi, 0*pi, 1*pi) actually completes one
√1 − √12
2
Hadamard gate. In Listing 1.14, in the backend ibmqx4 with five quantum bits
in IBM’s quantum computer, the program is the fourteenth example in which we
describe how to program with the phase gateU3(0.5*pi, 0*pi, 1*pi) that converts
single quantum bit q[0] from one state √12 ( |0 + |1) to another state ( |0).
Figure 1.42 is the corresponding quantum circuit of the program in Listing 1.14.
The statement “OPENQASM 2.0;” on line one of Listing 1.14 is to indicate that
the program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 1.14 is to continue parsing the file “qelib1.inc”
as if the contents of the file were pasted at the location of the include statement,
where the file “qelib1.inc” is Quantum Experience (QE) Standard Header and
the path is specified relative to the current working directory. The statement “qreg
q[5];” on line three of Listing 1.14 is to declare that in the program there are five
quantum bits. In the left top of Fig. 1.42, five quantum bits are subsequently q[0],
1.14 The U3(θ, φ, λ) Gate of Single Quantum Bit of Three Parameters 49
Fig. 1.42 The corresponding quantum circuit of the program in Listing 1.14
q[1], q[2], q[3] and q[4]. The initial value of each quantum bit is set to |0. Next,
the statement “creg c[5];” on line four of Listing 1.14 is to declare that there are five
classical bits in the program. In the left bottom of Fig. 1.42, five classical bits are
subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit is
set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
Listing. 1.14 nProgram of using the phase gate U3(0.5*pi, 0*pi, 1*pi) of three
parameters.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. u3(0.5*pi,0*pi,1*pi) q[0];
7. measure q[0] → c[0];
√1 √1
The statement “h q[0];” on line five of Listing 1.14 actually performs 2 2
√1 − √12
2
√1
1 1 1 1 1 0
× = 2 = 2 √ = 2√ + = √12 ( |0 + |1). This
0 √1 1 0 1
2
indicates that the statement “h q[0];” on line five of Listing 1.14 is to apply the
Hadamard gate to convert q[0] from one state |0to another state √12 ( |0 + |1) (its
superposition), where “h” is to represent the Hadamard gate. Next, the statement
“u3(0.5*pi,0*pi,1*pi) q[0];”
on line six of Listing 1.14 actually completes
√
√1
π −1×1×π
π
cos 4
−e × sin 4 2
√ √ × =
e −1×0×π × sin π4 e −1×(1×π+0×π ) × cos π4 √1
2
50 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
Fig. 1.43 After the measurement to the program in Listing 1.14 is completed, we obtain the answer
00000 with the probability 1.000
√
−1×1×π
√1 −e √ √1 √1 √1 √1
2 2 2 2 2 2 1
√ √ × = × == |0.
e −1×0×π
√ e −1×(1×π+0×π)
√ √1 √1 − √12 √1 0
2 2 2 2 2
This is to say that the statement “u3(0.5*pi,0*pi,1*pi) q[0];” on line six of Listing
1.14 is to complete one Hadamard gate to q[0]. Hence, using the Hadamard gate
twice from line five and line six of Listing 1.14 to q[0] does nothing to it.
Next, the statement “measure q[0] → c[0];” on line seven of Listing 1.14 is
to measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. In the backend ibmqx4 with five quantum
bits in IBM’s quantum computers, we use the command “simulate” to execute the
program in Listing 1.14. The result appears in Fig. 1.43. From Fig. 1.43, we obtain
the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 0 = q[0] = |0)
with the probability 1.000.
1.15 Summary
In this chapter, we introduced single quantum bit, multiple quantum bits and their
superposition. We also described two statements of declaration and measurement
for quantum bits and classical bits in Open QASM (version 2.0) in the backend
ibmqx4 with five quantum bits in IBM’s quantum computers. We illustrated all
of the quantum gates with single quantum bit and the controlled-NOT or CNOT
gate of two quantum bits in the backend ibmqx4 with five quantum bits in IBM’s
quantum computers. Simultaneously, we also in detail introduced connectivity of
the controlled-NOT gate in the backend ibmqx4 with five quantum bits in IBM’s
quantum computers. We introduced how to program with each quantum gate of single
quantum bit completing each different kind of application and how to execute each
quantum program in the backend ibmqx4 with five quantum bits in IBM’s quantum
computers. We also described how to program with the controlled-NOT gate to
implement a copy machine of one bit.
1.16 Bibliographical Notes 51
A famous article that gives a detailed technical definition for quantum supremacy is
(Aaronson and Chen 2017). Popular textbooks (Nielsen and Chuang 2000; Imre and
Balazs 2007; Lipton and Regan 2014) give an excellent introduction for quantum
bits and quantum gates. A popular textbook (Silva 2018), a famous project (IBM
Q 2016) and two famous articles (Cross et al 2017; Coles et al 2018) give many
excellent examples to write quantum programs with quantum assembly language in
Open QASM (version 2.0) in the backend ibmqx4 with five quantum bits in IBM’s
quantum computers.
1.17 Exercises
1.1 Please compute the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the NOT
gate.
1.2 Please calculate the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate such that the U3(θ, φ, λ) gate with three parameters is equivalent to the
Hadamard gate.
1.3 Please figure out the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the Z
gate.
1.4 Please determine the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate such that the U3(θ, φ, λ) gate with three parameters is equivalent to the
Y gate.
1.5 Please compute the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the S
gate.
1.6 Please calculate the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate such that the U3(θ, φ, λ) gate with three parameters is equivalent to the
S+ gate.
1.7 Please figure out the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the T
gate.
1.8 Please determine the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate such that the U3(θ, φ, λ) gate with three parameters is equivalent to the
T + gate.
1.9 Please compute the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the
identity gate.
52 1 Introduction to Quantum Bits and Quantum Gates on IBM’s …
1.10 Please calculate the values of three parameters θ, φ, and λ in the U3(θ, φ, λ)
gate such that the U3(θ, φ, λ) gate with three parameters is equivalent to the
U1(λ) gate with one parameter.
1.11 Please figure out the values of three parameters θ, φ, and λ in the U3(θ, φ,
λ) gate so that the U3(θ, φ, λ) gate with three parameters is equivalent to the
U2(φ,λ) gate with two parameters.
References
Aaronson, S., Chen, L.: Complexity-theoretic foundations of quantum supremacy experiments. In:
The 32nd Computational Complexity Conference, vol. 79, pp. 22:1–22:67 (2017)
Adleman, L.M.: Molecular computation of solutions to combinatorial problems. Science 226,
10211024 (1994)
Coles, P.J., Eidenbenz, S., Pakin, S., Adedoyin, A., Ambrosiano, J., Anisimov, P., Casper, W.,
Chennupati, G., Coffrin, C., Djidjev, H., Gunter, D., Karra, S., Lemons, N., Lin, S., Lokhov, A.,
Malyzhenkov, A.,Mascarenas, D., Mniszewski, S., Nadiga, B., O’Malley, D., Oyen, D., Prasad,
L., Roberts, R., Romero, P., Santhi, N., Sinitsyn, N., Swart, P., Vuffray, M., Wendelberger, J.,
Yoon, B., Zamora, R., Zhu, W.: Quantum algorithm implementations for beginners. https://arxiv.
org/abs/1804.03719 (2018)
Cross, A.W., Bishop, L.S., Smolin, J. A., Gambetta, J.M.: Open quantum assembly language. https://
arxiv.org/abs/1707.03429 (2017).
Deutsch, D.: Quantum theory, the Church-turing principle and the universal quantum computer. In:
The Proceedings of Royal Society London A (1985), pp. 400497.
Imre, S., Balazs, F.: Quantum Computation and Communications: An Engineering Approach. Wiley,
UK (2007). ISBN-10: 047086902X and ISBN-13: 978-0470869024, 2005.
Lipton, R.J., Regan, K.W.: Quantum Algorithms via Linear Algebra: a Primer. The MIT Press
(2014). ISBN 978-0-262-02839-4.
Nielsen, M.A., Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge Univer-
sity Press, New York, NY (2000). ISBN-10: 9781107002173 and ISBN-13: 978-1107002173.
Silva, V.: Practical Quantum Computing for Developers: Programming Quantum Rigs in the Cloud
using Python, Quantum Assembly Language and IBM Q Experience. Apress, December 13,
2018, ISBN-10: 1484242173 and ISBN-13: 978-1484242179.
The IBM Quantum Experience. https://www.research.ibm.com/quantum/, accessed November
2016.
Turing, A.: On computable numbers, with an application to the entscheidungsproblem. In: The
Proceedings of the London Mathematical Society, Ser. 2, vol. 42 (19367), pp. 230265; corrections,
Ibid, vol. 43, pp. 544546 (1937)
von Neumann, J.: Probabilistic Logics and the Synthesis of Reliable Organisms from Unreliable
Components, pp. 329378. Princeton University Press (1956)
Chapter 2
Boolean Algebra and Its Applications
Using a field of mathematics called modern algebra designs and maintains classical
computers. Over a hundred years, algebraists have studied for mathematical systems
and they call them as Boolean algebras. The name Boolean algebra honors a fasci-
nating English mathematician, George Boole because in 1854 he published a classic
book, An Investigation of the Laws of Thought, on which Are Founded Mathematical
Theories of Logic and Probabilities. Boole that has stated objective (intention) was to
complete a mathematical analysis of logic. Boole’s investigation has enlightened the
calculus of propositions and algebra of sets. In this book, we designate the algebra
now used in the design and maintenance of quantum logical circuitry as Boolean
algebra.
There are several advantages in having a mathematical technique for the illus-
tration of the internal workings of a quantum algorithm (circuit) for solving each
different kind of applications in IBM’s quantum computers. The first advantage is
to that it is often far more convenient to calculate with algebraic expressions used
to describe the internal workings of a quantum algorithm (circuit) than it is to apply
schematic or even logical diagrams. The second advantage is to that an ordinary
algebraic expression that describes the internal workings of a quantum algorithm
(circuit) may be reduced or simplified. This enables that the designer of quantum
algorithms (circuits) achieves economy of construction and reliability of quantum
operation. Boolean algebra also provides an economical and straightforward way of
designing quantum algorithms (circuits) for solving each different kind of applica-
tions. In all, a knowledge of Boolean algebra is indispensable in the computing field.
In this chapter, we describe how to complete logic operations that appear in Fig. 2.1
and include NOT, AND, NAND, OR, NOR, Exclusive-OR (XOR) and Exclusive-
NOR (XNOR) with quantum logic gates in the backend ibmqx4 or a simulator in
IBM’s quantum computers. We also illustrate how to complete several applications
from Boolean algebra.
The NOT operation acquires a single input and yields one single output. It inverts
the value of a bit into the one’s complement of the bit. This is to say that the NOT
operation for a bit provides the following result:
NOT 1 = 0
NOT 0 = 1 (2.1)
The value of a Boolean variable (a bit) is only zero (0) or one (1). Therefore, NOT
of a Boolean variable (a bit) q[0], written as q[0] is equal to one (1) if and only if
q[0] is equal to zero (0). Similarly, q[0] is equal to zero (0) if and only if q[0] is equal
to one (1). The rules in (2.1) for the NOT operation may also be expressed in the
form of a truth table that appears in Table 2.1.
From (2.1) and Table 2.1, the NOT operation of a bit is to invert the value of
the bit into its one’s complement. The NOT operation of n bits is to provide the
corresponding one’s complement for each input in n inputs by means of implementing
the NOT operation of a bit of n times. The following subsections will be used to
illustrate how to design the quantum programs to complete the NOT operation of a
bit and the NOT operation of two bits.
Consider that two values for unsigned integer of one bit are, respectively, 0(010 )
and 1(110 ), where 010 is the decimal representation of zero and 110 is the decimal
representation of one. We want to implement simultaneously the one’s complement
of those two values.
In Listing 2.1, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the first example of the second chapter in which we
illustrate how to write a quantum program to invert 0(010 ) and 1(110 ) into their
one’s complement. Figure 2.2 is the corresponding quantum circuit of the program
in Listing 2.1. The statement “OPENQASM 2.0;” on line one of Listing 2.1 is to
indicate that the program is written with version 2.0 of Open QASM. Then, the
statement “include “qelib1.inc”;” on line two of Listing 2.1 is to continue parsing
the file “qelib1.inc” as if the contents of the file were pasted at the location of
the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
Listing 2.1 The program of taking the one’s complement to the input of a bit.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. x q[0];
7. measure q[0] → c[0];
Next, the statement “qreg q[5];” on line three of Listing 2.1 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.2, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit
is set to |0. We use a quantum bit q[0] to encode the input of a bit that is unsigned
Fig. 2.2 The corresponding quantum circuit of the program in Listing 2.1
56 2 Boolean Algebra and Its Applications
integer of a bit. Next, the statement “creg c[5];” on line four of Listing 2.1 is to
declare that there are five classical bits in the program. In the left bottom of Fig. 2.2,
five classical bits are respectively c[0], c[1], c[2], c[3] and c[4]. The initial value of
each classical bit is set to zero (0). Classical bit c[4] is the most significant bit and
classical bit c[0] is the least significant bit.
Next, the
statement “h q[0];”
on line five of Listing 2.1 actually completes
√1 √1 1
√
1 √1
1 √1
1 0
2 2 × = 2 = = + = √12 (|0 +
√1 − √1 0 √1 2 1 2 0 1
2 2 2
|1). This is to say that the statement “h q[0];” on line five of Listing 2.1 is to apply
the Hadamard gate to convert q[0] from one state |0 to another state √12 (|0 + |1)
(its superposition). In its superposition, |0 with the amplitude √1 encodes the value
2
zero (0) to the input of a bit and |1 with the amplitude √1
encodes the value 1 (one)
2
to the input of a bit. Next, the statement “x q[0];” on line six of Listing 2.1 actually
√1 √1
01 1 0 1
completes × 12 = 12 = √12 = √12 + = √12 (|1
10 √
2
√
2
1 1 0
+ |0). This indicates that the statement “x q[0];” on line six of Listing 2.1 inverts
|0 with the amplitude √12 (the input zero of a bit) into |1 with the amplitude √12 (its
corresponding one’s complement) and inverts |1 with the amplitude √1 (the input
2
one of a bit) into |0 with the amplitude √1(its corresponding one’s complement).
2
This also implies that two instructions (two NOT operations) of taking one’s comple-
ment to the input of a bit are completed by means of using one quantum instruction
“x q[0];”.
Next, the statement “measure q[0] → c[0];” on line seven of Listing 2.1 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. In the backend ibmqx4 with five quantum bits in
IBM’s quantum computers, we use the command “simulate” to execute the program
in Listing 2.1. The measured result appears in Fig. 2.3. From Fig. 2.3, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] = 1 = q[0] = |1) with
the probability 0.530. This is to say that we obtain the one’s complement (q[0] =
|1) with the probability 0.530 to the input zero (0) of a bit. Or we obtain the answer
00000 with the probability 0.470 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 and c[0] =
Fig. 2.3 After the measurement to the program in Listing 2.1 is completed, we obtain the answer
00001 with the probability 0.530 or the answer 00000 with the probability 0.470
2.1 Illustration to NOT Operation 57
0 = q[0] = |0). This is also to say that we obtain the one’s complement (q[0] = |0)
with the probability 0.470 to the input one (1) of a bit.
Consider that four values to unsigned integer of two bits are subsequently 00(010 ),
01(110 ), 10 (210 ) and 11(310 ), where 010 is the decimal representation of zero, 110 is
the decimal representation of one, 210 is the decimal representation of two and 310 is
the decimal representation of three. We want to implementsimultaneously the one’s
complement of those four values.
In Listing 2.2, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the second example of the second chapter in which we
describe how to write a quantum program to take the one’s complement of 00(010 ),
01(110 ), 10 (210 ) and 11 (310 ). Figure 2.4 is the corresponding quantum circuit of the
program in Listing 2.2. The statement “OPENQASM 2.0;” on line one of Listing
2.2 is to point to that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 2.2 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
Listing 2.2 The program of taking the one’s complement to the input of two bits.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[0];
6. h q[1];
Fig. 2.4 The corresponding quantum circuit of the program in Listing 2.2
58 2 Boolean Algebra and Its Applications
7. x q[0];
8. x q[1];
9. measure q[0] → c[0];
10. measure q[1] → c[1];
Then, the statement “qreg q[5];” on line three of Listing 2.2 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.4, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit
is set to |0. We use two quantum bits q[0] and q[1] to encode the input of two bits
that are unsigned integer of two bits. Next, the statement “creg c[5];” on line four of
Listing 2.2 is to declare that there are five classical bits in the program. In the left
bottom of Fig. 2.4, five classical bits are respectively c[0], c[1], c[2], c[3] and c[4].
The initial value of each classical bit is set to zero (0). Classical bit c[4] is the most
significant bit and classical bit c[0] is the least significant bit.
Then, the
statement “h q[0];” on line five of Listing 2.2 actually completes
√1
√1 √1
1 √1
1 √1
1 0
2 2 × = 2 = = + = √12 (|0 +
√1 − √1 0 √1 2 1 2 0 1
2 2 2
|1). This indicates that the statement “h q[0];” on line five of Listing 2.2 is to apply
the Hadamard gate to convert q[0] from one state |0 to another state √12 (|0 + |1)
(its superposition).
1 Next,
the statement “h q[1];” on line six of Listing 2.2 actually
√ √1
√1
2 2 1 2 1 1 1 1 0
completes × = = √2 = √2 + =
√1 − √1 0 √1
1 0 1
2 2 2
√1 (|0 + |1). This implies that the statement “h q[1];” on line six of Listing 2.2 is
2
to use the Hadamard gate to convert q[1] from one state |0 to another state √12 (|0
+ |1) (its superposition). Hence, the superposition of the two quantum bits q[0] and
q[1] is ( √12 (|0 + |1)) ( √12 (|0 + |1)) = 21 (|0 |0 + |0 |1 + |1 |0 + |1 |1) =
1
2
(|00 + |01 + |10 + |11). In the superposition, state |00 with the amplitude 21
encodes the value 00 (zero) to the input of two bits. State |01 with the amplitude
1
2
encodes the value 1 (one) to the input of two bits. State |10 with the amplitude
1
2
encodes the value 2 (two) to the input of two bits and |11 with the amplitude 21
encodes the value 3 (three) to the input of two bits.
Next, the statement “x q[0];” on line seven of Listing 2.2 actually completes
√1 √1
01 1 1 1 0 1
× 2 = 2 = 2
√ = 2 √ + = √12 (|1 + |0)
10 √1 √1 1 1 0
2 2
01
and the statement “x q[1];” on line eight of Listing 2.2 actually completes
10
1 1
√ √ 1 0 1
× 2 = 2 = √12 = √12 + = √12 (|1 + |0). This is to
√1 √1 1 1 0
2 2
say that the two statements “x q[0];” and “x q[1];” on line seven and line eight of
Listing 2.2 inverts |00 with the amplitude 21 (the input zero of two bits) into |11 with
2.1 Illustration to NOT Operation 59
the amplitude 21 (its one’s complement). They inverts |01 with the amplitude 21 (the
input one of two bits) into |10 with the amplitude 21 (its one’s complement), inverts
|10 with the amplitude 21 (the input two of two bits) into |01 with the amplitude 21
(its one’s complement) and inverts |11 with the amplitude 21 (the input three of two
bits) into |00 with the amplitude 21 (its one’s complement). This indicates that eight
instructions (eight NOT operations) of taking one’s complement to the input of two
bits are completed by means of applying two quantum operations “x q[0];” and “x
q[1];”.
Next, the statement “measure q[0] → c[0];” on line nine of Listing 2.2 is to
measure the first quantum bit q[0] and to record the measurement outcome by over-
writing the first classical bit c[0]. The statement “measure q[1] → c[1];”on line ten of
Listing 2.2 is to measure the second quantum bit q[1] and to record the measurement
outcome by overwriting the second classical bit c[1]. In the backend ibmqx4 with
five quantum bits in IBM’s quantum computers, we apply the command “simulate”
to execute the program in Listing 2.2. The measured result appears in Fig. 2.5. From
Fig. 2.5, we obtain the answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 = q[1]
= |0 and c[0] = 1 = q[0] = |1) with the probability 0.330. This is to say that we
obtain the one’s complement (q[1] = |0 and q[0] = |1) with the probability 0.330
to the input two (10) of two bits. Or we obtain the answer 00010 (c[4] = 0, c[3] =
0, c[2] = 0, c[1] = 1 = q[1] = |1 and c[0] = 0 = q[0] = |0) with the probability
0.260. This indicates that we obtain the one’s complement (q[1] = |1 and q[0] = |0)
with the probability 0.260 to the input one (01) of two bits. Or we obtain the answer
00011 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 1 = q[1] = |1 and c[0] = 1 = q[0] =
|1) with the probability 0.210. This implies that we obtain the one’s complement
(q[1] = |1 and q[0] = |1) with the probability 0.210 to the input zero (00) of two
bits. Or we obtain the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0, c[1] = 0 = q[1]
= |0 and c[0] = 0 = q[0] = |0) with the probability 0.200. This is to say that we
obtain the one’s complement (q[1] = |0 and q[0] = |0) with the probability 0.200
to the input three (11) of two bits.
Fig. 2.5 After the measurement to the program in Listing 2.2 is completed, we obtain the answer
00001 with the probability 0.330 or the answer 00010 with the probability 0.260 or the answer
00011 with the probability 0.210 or the answer 00000 with the probability 0.200
60 2 Boolean Algebra and Its Applications
+
It is assumed
⎛that U CCN is the⎞
conjugate-transpose matrix of U CCN and is equal to
10000000
⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟
⎜0 0 0 1 0 0 0 0⎟
⎜ ⎟
t
((U CCN )*) = ⎜ ⎟, where the * is the complex conjugation and the
⎜0 0 0 0 1 0 0 0⎟
⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠
00000010
⎛ ⎞
10000000
⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟
⎜0 0 0 1 0 0 0 0⎟
⎜ ⎟
t
t is the transpose operation. Because U CCN × ((U CCN )*) = ⎜
⎜0 0 0 0 1 0 0 0⎟
⎟
⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠
00000010
⎛ ⎞ ⎛ ⎞
10000000 10000000
⎜0 1 0 0 0 0 0 0⎟ ⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟ ⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 1 0 0 0 0⎟ 0 0 0 1 0 0 0 0
⎜ ⎟ ⎜ ⎟
× ⎜ ⎟ = ((U CCN )*)t × U CCN = ⎜ ⎟ ×
⎜ ⎟
⎜0 0 0 0 1 0 0 0⎟ ⎜0 0 0 0 1 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟ ⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟ ⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠ ⎝0 0 0 0 0 0 0 1⎠
00000010 00000010
2.2 The Toffoli Gate of Three Quantum Bits 61
⎛ ⎞ ⎛ ⎞
10000000 10000000
⎜0 1 0 0 0 0 0 0⎟ ⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟ ⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 1 0 0 0 0⎟ ⎜0 0 0 1 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜ ⎟=⎜ ⎟, U CCN is a unitary matrix or a unitary
⎜0 0 0 0 1 0 0 0⎟ ⎜0 0 0 0 1 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟ ⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟ ⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠ ⎝0 0 0 0 0 0 1 0⎠
00000010 00000001
operator. This indicates that the controlled-controlled-NOT or CCNOT gate (the
Toffoli gate) U CCN is one of quantum gates with three quantum bits. The quantum
state l 0 |000 + l1 |001 + l2 |010 + l 3 |011 + l4 |100 + l5 |101 + l6 |110 + l7 |111
is written in a vector notation as
⎛ ⎞
l0
⎜ l1 ⎟
⎜ ⎟
⎜ l ⎟
⎜ 2 ⎟
⎜ l3 ⎟
⎜ ⎟
⎜ ⎟ (2.3)
⎜ l4 ⎟
⎜ ⎟
⎜ l5 ⎟
⎜ ⎟
⎝ l6 ⎠
l7
The first entry l0 is the amplitude for |000. The second entry l1 is the amplitude
for |001. The third entry l2 is the amplitude for |010. The fourth entry l3 is the
amplitude for |011. The fifth entry l4 is the amplitude for |100. The sixth entry l5 is
the amplitude for |101. The seventh entry l6 is the amplitude for |110 and the eighth
entry l7 is the amplitude for |111. The CCNOT gate U CCN takes it as its input state
vector and generates the output to be
⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞
1 0 0 0 0 0 0 0 l0 l0 1
⎜0 1 0 0 0 0 0 0⎟ ⎜ l1 ⎟ ⎜ l1 ⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟ ⎜0⎟
⎟ ⎜ l2 ⎟ ⎜ l2 ⎟
⎜ ⎟ ⎜ ⎟
⎜ ⎜ ⎟
⎜0 0 0 1 0 0 0 0 ⎟ ⎜ l3 ⎟ ⎜ l3 ⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ × ⎜ ⎟ = ⎜ ⎟ = l0 ⎜ ⎟
⎜0 0 0 0 1 0 0 0 ⎟ ⎜ l4 ⎟ ⎜ l4 ⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟ ⎜ l5 ⎟ ⎜ l5 ⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝0 0 0 0 0 0 0 1 ⎠ ⎝ l6 ⎠ ⎝ l7 ⎠ ⎝0⎠
0 0 0 0 0 0 1 0 l7 l6 0
62 2 Boolean Algebra and Its Applications
⎛ ⎞ ⎛ ⎞ ⎛ ⎞ ⎛ ⎞
0 0 0 0
⎜1⎟ ⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0⎟ ⎜1⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜1⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
+ l1 ⎜ ⎟ + l2 ⎜ ⎟ + l3 ⎜ ⎟ + l4 ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜0⎟ ⎜1⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝0⎠ ⎝0⎠ ⎝0⎠ ⎝0⎠
0 0 0 0
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
0 0 0
⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
+ l5 ⎜ ⎟ + l7 ⎜ ⎟ + l6 ⎜ ⎟
⎜0⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜1⎟ ⎜0⎟ ⎜0⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝0⎠ ⎝1⎠ ⎝0⎠
0 0 1
= l0 |000 + l1 |001 + l2 |010 + l3 |011
+ l4 |100 + l5 |101 + l7 |110 + l6 |111. (2.4)
This is to say that the CCNOT gate U CCN converts three quantum bits from one
state l0 |000 + l1 |001 + l 2 |010 + l 3 |011 + l 4 |100 + l 5 |101 + l 6 |110 + l7 |111 to
another state l0 |000 + l1 |001 + l2 |010 + l3 |011 + l4 |100 + l5 |101 + l7 |110 +
l6 |111. This implies that in the CCNOT gate U CCN if the two control quantum bits
(the first quantum bit and the second quantum bit) is set to 0, then the target quantum
bit (the third quantum bit) is left alone. If the two control quantum bits (the first
quantum bit and the second quantum bit) is both set to 1, then the CCNOT gate U CCN
flips the target quantum bit (the third quantum bit). The probability of measuring a |0
|000, |1 |001, |010, |011, |100 or |101 is unchanged, the probability of measuring
a |0 |110 is |l7 |2 and the probability of measuring a |0 |111⎛is |l6 |2 after using⎞the
10000000
⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟
0 0 0 1 0 0 0 0
⎜ ⎟
CCNOT gate U CCN . Since (U CCN )2 = U CCN × U CCN = ⎜ ⎟×
⎜ ⎟
⎜0 0 0 0 1 0 0 0⎟
⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠
00000010
2.2 The Toffoli Gate of Three Quantum Bits 63
⎛ ⎞ ⎛ ⎞
10000000 10000000
⎜0 1 0 0 0 0 0 0⎟ ⎜0 1 0 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 1 0 0 0 0 0⎟ ⎜0 0 1 0 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 1 0 0 0 0⎟ ⎜0 0 0 1 0 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜ ⎟=⎜ ⎟, using the CCNOT gate U CCN twice to
⎜0 0 0 0 1 0 0 0⎟ ⎜0 0 0 0 1 0 0 0⎟
⎜ ⎟ ⎜ ⎟
⎜0 0 0 0 0 1 0 0⎟ ⎜0 0 0 0 0 1 0 0⎟
⎜ ⎟ ⎜ ⎟
⎝0 0 0 0 0 0 0 1⎠ ⎝0 0 0 0 0 0 1 0⎠
00000010 00000001
a state is equivalent to do nothing to it. The graphical representation of the CCNOT
gate U CCN appears in Fig. 2.6. In Fig. 2.6, the left top two wires are control bits that
are unchanged by the action of the CCNOT gate. The bottom wire is a target bit
that the action of the CCNOT gate flips it if both control bits are set to one (1), and
otherwise is left alone, where ⊕ is addition modulo two.
The Toffoli gate has three input bits and three output bits. Its truth table appears
in Table 2.2. In IBM Q Experience, in the backend simulator, it now provides
one quantum instruction (operation) of implementing the CCNOT gate (the Toffoli
gate) with three quantum bits. But in the backend ibmqx4, it does not support the
Toffoli gate. We decompose CCNOT gate into six CNOT gates and nine gates of
one quantum bit that appear in Fig. 2.7. In Fig. 2.7, H is the Hadamard gate, T =
1 √0 1 0
−1× π4 and T + = −1× −1× π4 . In IBM Q Experience, the available gates
√
0e 0e
are that CNOT is the only gate with two quantum bits and the other gates act on
single quantum bit and they are introduced in the previous chapter. In the backend
ibmqx4 with five quantum bits, there are only six pairs of CNOT gates. Connectivity
of the CNOT gate in the backend ibmqx4 with five quantum bits appears in Fig. 1.32
in Sect. 1.11.1.
64 2 Boolean Algebra and Its Applications
Table 2.2 The truth table for the Toffoli gate with three input bits and three output bits
Input Output
C1 C2 T C1 C2 T
0 0 0 0 0 0
0 1 0 0 1 0
1 0 0 1 0 0
1 1 0 1 1 1
0 0 1 0 0 1
0 1 1 0 1 1
1 0 1 1 0 1
1 1 1 1 1 0
Fig. 2.7 Decomposing CCNOT gate into six CNOT gates and nine gates of one bit
In Listing 2.3, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the third example of the second chapter in which we
illustrate how to write a quantum program to implement a Toffoli gate (a CCNOT
gate) of three quantum bits. Figure 2.8 is the corresponding quantum circuit of the
program in Listing 2.3. The statement “OPENQASM 2.0;” on line one of Listing
2.3 is to indicate that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 2.3 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
Fig. 2.8 The corresponding quantum circuit of the program in Listing 2.3
2.2 The Toffoli Gate of Three Quantum Bits 65
Listing 2.3 The program of implementing a CCNOT gate of three quantum bits.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[1];
6. h q[2];
7. h q[0];
8. cx q[1], q[0];
9. tdg q[0];
10. cx q[2], q[0];
11. t q[0];
12. cx q[1], q[0];
13. tdg q[0];
14. cx q[2], q[0];
15. t q[0];
16. t q[1];
17. h q[0];
18. cx q[2], q[1];
19. tdg q[1];
20. t q[2];
21. cx q[2], q[1];
22. measure q[0] → c[0];
23. measure q[1] → c[1];
24. measure q[2] → c[2];
Next, the statement “qreg q[5];” on line three of Listing 2.3 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.8, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit
is set to |0. We apply three quantum bits q[2], q[1] and q[0] to encode subsequently
the first control bit, the second control bit and the target bit. For the convenience of
our explanation, q[k]0 for 0 ≤ k ≤ 4 is to represent the value 0 of q[k] and q[k]1 for
0 ≤ k ≤ 4 is to represent the value 1 of q[k]. Similarly, for the convenience of our
explanation, an initial state vector of implementing a Toffoli gate is as follows:
|0 = q[2]0 q[1]0 q[0]0 = |0|0|0 = |000.
Then, the statement “creg c[5];” on line four of Listing 2.3 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.8, five classical bits
are respectively c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
66 2 Boolean Algebra and Its Applications
Next, the two statements “h q[1];” and “h q[2];”on line five and line six of Listing
2.3 implement two Hadamard gates of the first time slot of the quantum circuit in
√1 √1
√1
2 2 1 2 √1
1
Fig. 2.8 and both actually complete × = = =
√1 − √1 0 √1 2 1
2 2 2
√1
1 0
2
+ = √12 (|0 + |1). This is to say that converting q[1] from one
0 1
state |0 to another state √12 (|0 + |1) (its superposition) and converting q[2] from
one state |0 to another state √1 (|0 + |1) (its superposition) are completed. Thus,
2
the superposition of the two quantum bits q[2] and q[1] is ( √12 (|0 + |1)) ( √12 (|0 +
|1)) = 21 (|0 |0 + |0 |1 + |1 |0 + |1 |1) = 21 (|00 + |01 + |10 + |11). Because
in the first time slot of the quantum circuit in Fig. 2.8 there is no quantum gate to act
on the quantum bit q[0], its state |0 is not changed. Therefore, after using the two
statements “h q[1];” and “h q[2];”on line five and line six of Listing 2.3 implement
two Hadamard gates in the first time slot of the quantum circuit in Fig. 2.8, the
following new state vector is
1
|1 = (|0|0|0 + |0|1|0 + |1|0|0 + |1|1|0)
2
1
= (|000 + |010 + |100 + |110).
2
The next 12 time slots in the quantum circuit of Fig. 2.8 implement a Toffoli gate.
Next, the statement “h q[0];” on line seven of Listing 2.3 takes the new state vector
|1 = 21 (|000 + |010 + |100 + |110) as its input and completes one Hadamard
gate for q[0] in the second time slot. This is to say that the statement “h q[0];”
converts q[0] from one state |0 to another state √12 (|0 + |1) (its superposition).
Because there is no other quantum gate in the second time slot to act on quantum
bits q[2] and q[1], their states are not changed. Therefore, after using the statement
“h q[0];” on line seven of Listing 2.3 completes one Hadamard gate for q[0] in the
second time slot, the following new state vector is
1 1 1
|2 = |0|0 √ (|0 + |1) + |0|1 √ (|0 + |1)
2 2 2
1 1
+|1|0 √ (|0 + |1) + |1|1 √ (|0 + |1)
2 2
1
= √ (|000 + |001 + |010 + |011
2 2
+|100 + |101 + |110 + |111).
Next, the statement “cx q[1], q[0];” on line eight of Listing 2.3 takes the new state
vector |2 = 2√1 2 (|000 + |001 + |010 + |011 + |100 + |101 + |110 + |111)
as its input and completes one CNOT gate for q[1] and q[0] in the third time slot. If
2.2 The Toffoli Gate of Three Quantum Bits 67
the value of the control bit q[1] is equal to one, then the statement “cx q[1], q[0];”
flips the value of the target bit q[0]. Otherwise, it is not changed. Because there is no
other quantum gate in the third time slot to act on quantum bit q[2], its state is not
changed. Hence, after using the statement “cx q[1], q[0];” on line eight of Listing
2.3 implements one CNOT gate for q[1] and q[0] in the third time slot, the following
new state vector is
1
|3 = √ (|000 + |001 + |011 + |010
2 2
+|100 + |101 + |111 + |110).
Next, the statement “tdg q[0];” on line nine of Listing 2.3 takes the new state
vector |3 = 2√1 2 (|000 + |001 + |011 + |010 + |100 + |101 + |111 + |110)
as its input and completes one T + gate for q[0] in the fourth time slot. If the √
value of
π
q[0] is equal to one, then the statement “tdg q[0];” changes its phase as (e−1× −1× 4 ).
Otherwise, its phase is not changed. There is no other quantum gate in the fourth
time slot to act on quantum bits q[2] and q[1], so their states are not changed. Thus,
after using the statement “tdg q[0];” on line nine of Listing 2.3 completes one T +
gate for q[0] in the fourth time slot, the following new state vector is
1 √ π
√ π
|4 = √ |000 + e−1× −1× 4 |001 + e−1× −1× 4 |011 + |010
2 2
√ π
√ π
+|100 + e−1× −1× 4 |101 + e−1× −1× 4 |111 + |110 .
Next, the statement “cx q[2], √q[0];” on line ten of√Listing 2.3 takes the new state
π π
vector |4 = 2√1 2 (|000 + e−1× −1× 4 |001 + e−1× −1× 4 |011 + |010 + |100 +
√ π
√ π
e−1× −1× 4 |101 + e−1× −1× 4 |111 + |110) as its input and completes one CNOT
gate for q[2] and q[0] in the fifth time slot. If the value of the control bit q[2] is
equal to one, then the statement “cx q[2], q[0];” flips the value of the target bit q[0].
Otherwise, it is not changed. Because there is no other quantum gate in the fifth time
slot to act on quantum bit q[1], its state is not changed. Thus, after using the statement
“cx q[2], q[0];” on line ten of Listing 2.3 completes one CNOT gate for q[2] and
q[0] in the fifth time slot, the following new state vector is
1 √ π
√ π
|5 = √ |000 + e−1× −1× 4 |001 + e−1× −1× 4 |011 + |010 + |101
2 2
√ π
√ π
+ e−1× −1× 4 |100 + e−1× −1× 4 |110 + |111 .
Next, the statement “cx q[1], q[0];” on line twelve of Listing√2.3 takes the
π
new state vector |6 = 2√1 2 (|000 + |001 + |011 + |010 + e1× −1× 4 |101 +
√ π
√ π
√ π
e−1× −1× 4 |100 + e−1× −1× 4 |110 + e1× −1× 4 |111) as its input and performs
one CNOT gate for q[1] and q[0] in the seventh time slot. If the value of the control
bit q[1] is equal to one, then the statement “cx q[1], q[0];” flips the value of the target
bit q[0]. Otherwise, it is not changed. There is no other quantum gate in the seventh
time slot to act on quantum bit q[2], so its state is not changed. Therefore, after using
the statement “cx q[1], q[0];” on line twelve of Listing 2.3 implements one CNOT
gate for q[1] and q[0] in the seventh time slot, the following new state vector is
1 √ π
|7 = √ |000 + |001 + |010 + |011 + e1× −1× 4 |101
2 2
√ √ √
−1× π4 −1× π4 −1× π4
+e−1× |100 + e−1× |111 + e1× |110 .
1 √ π
√ π
|8 = √ |000 + e−1× −1× 4 |001 + |010 + e−1× −1× 4 |011
2 2
2.2 The Toffoli Gate of Three Quantum Bits 69
√ π
√ π
√ π
+ e−1× −1× 4 × e1× −1× 4 |101 + e−1× −1× 4 |100
√ π
√ π
√ π
+ e−1× −1× 4 × e−1× −1× 4 |111 + e1× −1× 4 |110 .
1 √ π
√ π
|9 = √ |000 + e−1× −1× 4 |001 + |010 + e−1× −1× 4 |011
2 2
√ π
√ π
√ π
+ |100 + e−1× −1× 4 |101 + e−1× −1× 4 × e−1× −1× 4 |110
√ π
+e1× −1× 4 |111 .
1 √ π
√ π
|10 = √ |000 + e1× −1× 4 × e−1× −1× 4 |001 + |010
2 2
√ π
√ π
+ e1× −1× 4 × e−1× −1× 4 |011 + |100
√ π
√ π
+ e1× −1× 4 × e−1× −1× 4 |101
√ π
√ π
√ π
√ π
+ e−1× −1× 4 × e−1× −1× 4 |110 + e1× −1× 4 × e1× −1× 4 |111
1
= √ (|000 + |001 + |010 + |011 + |100 + |101
2 2
√ π
√ π
√ π
√ π
+ e−1× −1× 4 × e−1× −1× 4 |110 + e1× −1× 4 × e1× −1× 4 |111 .
70 2 Boolean Algebra and Its Applications
Next, the statement “t q[1];” on line sixteen of Listing 2.3 takes the new
√
state
π
vector |10 = 2√1 2 (|000 + |001 + |010 + |011 + |100 + |101 + (e−1× −1× 4
√ π
√ π
√ π
× e−1× −1× 4 )|110 + (e1× −1× 4 × e1× −1× 4 )|111) as its input and completes
one T gate for q[1] in the tenth time slot. If the√value of q[1] is equal to one, then
π
the statement “t q[1];” changes its phase as (e1× −1× 4 ). Otherwise, its phase is not
changed. Because there is no other quantum gate in the tenth time slot to act on
quantum bit q[2], so its state is not changed. Therefore, after using the statement “t
q[1];” on line sixteen of Listing 2.3 completes one T gate for q[1] in the tenth time
slot, the following new state vector is
1 √ π
|11 = √ |000 + |001 + e1× −1× 4 |010
2 2
√ π
+ e1× −1× 4 |011 + |100 + |101
√ π
√ π
√ π
+ e1× −1× 4 × e−1× −1× 4 × e−1× −1× 4 |110
√ π
√ π
√ π
+ e1× −1× 4 × e1× −1× 4 × e1× −1× 4 |111 .
1 √ π
√ π
|13 = |000 + e1× −1× 4 |010 + |110 + e−1× −1× 4 |101 .
2
Next, the statement “tdg q[1];”
√
on line nineteen of Listing
√
2.3 takes the new state
π π
vector |13 = 21 (|000 + e1× −1× 4 |010 + |110 + e−1× −1× 4 |101) as its input
and completes one T + gate for q[1] in the twelfth time slot. If the value
√
of q[1] is equal
π
to one, then the statement “tdg q[1];” changes its phase as (e−1× −1× 4 ). Otherwise,
its phase is not changed. There is no other quantum gate in the twelfth time slot to act
on quantum bit q[0], so its state is not changed. Hence, after using the statement “tdg
q[1];” on line nineteen of Listing 2.3 completes one T + gate for q[1] in the twelfth
time slot, the following new state vector is
1 √ π
√ π
|14 = |000 + e−1× −1× 4 × e1× −1× 4 |010
2
√ √
−1× π4 −1× π4
+e−1× |110 + e−1× |101 .
Next, the statement “t q[2];” √ on line twenty√ of Listing 2.3 takes√the new state
1 −1× −1× π4 π π
vector |14 = 2 (|000 + (e × e1× −1× 4 )|010 + e−1× −1× 4 |110 +
√ π
e−1× −1× 4 |101) as its input and completes one T gate for q[2] in the twelfth time
slot. If the
√
value of q[2] is equal to one, then the statement “t q[2];” changes its phase
1× −1× π4
as (e ). Otherwise, its phase is not changed. There is no other quantum gate
in the twelfth time slot to act on quantum bit q[0], so its state is not changed. Hence,
after using the statement “t q[2];” on line twenty of Listing 2.3 completes one T gate
for q[2] in the twelfth time slot, the following new state vector is
1 √ π
√ π
|15 = |000 + |010 + e1× −1× 4 × e−1× −1× 4 |110
2
√ π
√ π
+ e1× −1× 4 × e−1× −1× 4 |101
1
= (|000 + |010 + |110 + |101).
2
Next, the statement “cx q[2], q[1];” on line twenty-one of Listing 2.3 takes the
new state vector |15 = 21 (|000 + |010 + |110 + |101) as its input and performs
one CNOT gate for q[2] and q[1] in the thirteenth time slot. If the value of the control
bit q[2] is equal to one, then the statement “cx q[2], q[1];” flips the value of the target
bit q[1]. Otherwise, its value is not changed. Because there is no other quantum gate
in the thirteenth time slot to act on quantum bit q[0], its state is not changed. Thus,
after one CNOT gate for q[2] and q[1] in the thirteenth time slot is completed by the
statement “cx q[2], q[1];” on line twenty-one of Listing 2.3, the following new state
vector is obtained:
1
|16 = (|000 + |010 + |100 + |111).
2
72 2 Boolean Algebra and Its Applications
Fig. 2.9 After the measurement to the program in Listing 2.3 is completed, we obtain the answer
00100 with the probability 0.260 or the answer 00010 with the probability 0.250 or the answer
00111 with the probability 0.250 or the answer 00000 with the probability 0.240
Next, the statement “measure q[0] → c[0];” on line twenty-two of Listing 2.3
is to measure the first quantum bit q[0] and to record the measurement outcome by
overwriting the first classical bit c[0]. The statement “measure q[1] → c[1];” on line
twenty-three of Listing 2.3 is to measure the second quantum bit q[1] and to record
the measurement outcome by overwriting the second classical bit c[1]. Next, the
statement “measure q[2] → c[2];” on line twenty-four of Listing 2.3 is to measure
the third quantum bit q[2] and to record the measurement outcome by overwriting
the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s
quantum computers, we use the command “simulate” to execute the program in
Listing 2.3. The measured result appears in Fig. 2.9.
From Fig. 2.9, we obtain the answer 00100 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2]
= |1, c[1] = 0 = q[1] = |0 and c[0] = 0 = q[0] = |0) with the probability 0.260.
Because the value of the first control bit q[2] is equal to one and the value of the
second control bit q[1] is equal to zero, the value of the target bit q[0] is not changed
and is equal to zero with the probability 0.260. Or we obtain the answer 00010 (c[4]
= 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 1 = q[1] = |1 and c[0] = 0 = q[0] =
|0) with the probability 0.250. The value of the first control bit q[2] is equal to zero
and the value of the second control bit q[1] is equal to one, so the value of the target
bit q[0] is not changed and is equal to zero with the probability 0.250.
Or we obtain the answer 00111 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] =
1 = q[1] = |1 and c[0] = 1 = q[0] = |1) with the probability 0.250. The value of
the first control bit q[2] is equal to one and the value of the second control bit q[1] is
also equal to one. Therefore, the CCNOT gate that implements one AND operation
flips the value of the target bit q[0] and the value of the target bit q[0] is equal to
one with the probability 0.250. Or we obtain the answer 00000 (c[4] = 0, c[3] = 0,
c[2] = 0 = q[2] = |0, c[1] = 0 = q[1] = |0 and c[0] = 0 = q[0] = |0) with the
probability 0.240. Because the value of the first control bit q[2] is equal to zero and
the value of the second control bit q[1] is equal to zero, the value of the target bit
q[0] is not changed and is equal to zero with the probability 0.240.
2.3 Introduction to AND Operation 73
The AND operation of a bit obtains two inputs of a bit and produces one single
output of a bit. If the value of the first input is one (1) and the value of the second
input is one (1), then it generates a result (output) of one (1). Otherwise, the result
is zero (0). A symbol “∧” is used to represent the AND operation. Therefore, the
AND operation of a bit that has two inputs of a bit is the following four possible
combinational results:
0∧0=0
0∧1=0
1∧0=0
1∧1=1 (2.5)
The value of a Boolean variable (a bit) is only zero (0) or one (1). Thus, AND of
two Boolean variables (two inputs of a bit) q[2] and q[1], written as q[2] ∧ q[1] is
equal to one (1) if and only if q[2] and q[1] are both one (1). Similarly, q[2] ∧ q[1]
is equal to zero (0) if and only if either q[2] or q[1], or both, are zero (0). We use a
truth table with logic operation to represent all possible combinations of inputs and
the corresponding outputs. Hence, the rules in (2.5) for the AND operation of a bit
that has two inputs of a bit and generates one output of a bit may be expressed in the
form of a truth table that appears in Table 2.3.
We use one CCNOT gate that has three quantum input bits and three quantum output
bits to implement AND operation of a classical bit that has two inputs of a classical
bit and generates one output of a classical bit. We use the two control bits C 1 and
C 2 of the CCNOT gate to encode two inputs q[2] and q[1] of a classical bit in AND
operation of a classical bit. We apply the target bit T of the CCNOT gate to store one
output q[2] ∧ q[1] of a classical bit in AND operation of a classical bit. Using the
Table 2.3 The truth table for the AND operation of a bit that has two inputs of a bit and generates
one output of a bit
Input Output
q[2] q[1] q[2] ∧ q[1]
0 0 0
0 1 0
1 0 0
1 1 1
74 2 Boolean Algebra and Its Applications
form of a truth table that appears in Table 2.4 expresses the rule of using one CCNOT
gate to implement AND operation. Its graph representation appears in Fig. 2.10. The
initial state of the target bit in the CCNOT gate in Fig. 2.10 is set to |0.
In Listing 2.4, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the fourth example of the second chapter in which we
describe how to write a quantum program to implement AND operation of a classical
bit by means of using one CCNOT gate of three quantum bits. Figure 2.11 is the
corresponding quantum circuit of the program in Listing 2.4. The statement “OPEN-
QASM 2.0;” on line one of Listing 2.4 is to point out that the program is written with
version 2.0 of Open QASM. Next, the statement “include “qelib1.inc”;” on line two
of Listing 2.4 is to continue parsing the file “qelib1.inc” as if the contents of the file
were pasted at the location of the include statement, where the file “qelib1.inc” is
Quantum Experience (QE) Standard Header and the path is specified relative to
the current working directory.
Table 2.4 The truth table of using one CCNOT gate to implement AND operation
Input Output
C1 C2 T C1 C2 T = q[2] ∧ q[1]
0 0 0 0 0 0
0 1 0 0 1 0
1 0 0 1 0 0
1 1 0 1 1 1
Fig. 2.10 The quantum circuit of implementing AND operation of a classical bit
Fig. 2.11 The corresponding quantum circuit of the program in Listing 2.4
2.3 Introduction to AND Operation 75
Listing 2.4 The program of using one CCNOT gate to implement AND operation.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[1];
6. h q[2];
7. h q[0];
8. cx q[1], q[0];
9. tdg q[0];
10. cx q[2], q[0];
11. t q[0];
12. cx q[1], q[0];
13. tdg q[0];
14. cx q[2], q[0];
15. t q[0];
16. t q[1];
17. h q[0];
18. cx q[2], q[1];
19. tdg q[1];
20. t q[2];
21. cx q[2], q[1];
22. measure q[0] → c[0];
23. measure q[1] → c[1];
24. measure q[2] → c[2];
Then, the statement “qreg q[5];” on line three of Listing 2.4 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.11, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit
is set to |0. We use three quantum bits q[2], q[1] and q[0] to encode respectively the
first control bit, the second control bit and the target bit. This is to say that we apply
quantum bits q[2] and q[1] to encode two inputs of a classical bit in AND operation
of a classical bit and use quantum bit q[0] to store the result of AND operation of
a classical bit. For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is to
represent the value of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4 is to represent
the value of q[k] to be one (1). Similarly, for the convenience of our explanation, an
initial state vector of implementing AND operation of a classical bit is as follows:
|A0 = q[2]0 q[1]0 q[0]0 = |0|0|0 = |000.
Next, the statement “creg c[5];” on line four of Listing 2.4 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.11, five classical bits
76 2 Boolean Algebra and Its Applications
are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
Next, the two statements “h q[1];” and “h q[2];”on line five and line six of Listing
2.4 implement two Hadamard gates of the first time slot of the quantum circuit in
Fig. 2.11. This implies that the statement “h q[1];” converts q[1] from one state |0
to another state √12 (|0 + |1) (its superposition) and the statement “h q[2];” converts
q[2] from one state |0 to another state √12 (|0 + |1) (its superposition). In the first
time slot of the quantum circuit in Fig. 2.11 there is no quantum gate to act on the
quantum bit q[0], so its state |0 is not changed. Thus, after using the two statements “h
q[1];” and “h q[2];”on line five and line six of Listing 2.4 implement two Hadamard
gates in the first time slot of the quantum circuit in Fig. 2.11, the following new state
vector is
1
|A1 = (|0|0|0 + |0|1|0 + |1|0|0 + |1|1|0)
2
1
= (|000 + |010 + |100 + |110).
2
In the new state vector |A1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
inputs for AND operation of a classical bit. The initial state of quantum bit q[0] in
four combinational states of quantum bits q[2] and q[1] is |0 and it stores the result
for AND operation of a classical bit.
The next 12 time slots in the quantum circuit of Fig. 2.11 implement AND opera-
tion of a classical bit by means of implementing one CCNOT gate. There are fifteen
statements from line seven through line twenty-one in Listing 2.4. The front eight
statements are “h q[0];”, “cx q[1], q[0];”, “tdg q[0];”, “cx q[2], q[0];”, “t q[0];”, “cx
q[1], q[0];”, “tdg q[0];” and “cx q[2], q[0];”. The last seven statements are “t q[0];”,
“t q[1];”, “h q[0];”, “cx q[2], q[1];”, “tdg q[1];”, “t q[2];” and “cx q[2], q[1];”. They
implement each quantum gate from the second time slot through the thirteenth time
slot in Fig. 2.11. They take the new state vector |A1 = 21 (|000 + |010 + |100 +
|110) as the input in the second time slot and complete AND operation of a classical
bit. This gives that the following new state vector is
1
|A16 = (|000 + |010 + |100 + |111).
2
Next, three measurements from the fourteenth time slot through the sixteenth time
slot in Fig. 2.11 were implemented by the three statements “measure q[0] → c[0];”,
“measure q[1] → c[1];” and “measure q[2] → c[2];” on line twenty-two through line
twenty-four of Listing 2.4. They are to measure the first quantum bit q[0], the second
quantum bit q[1] and the third quantum bit q[2]. They are to record the measurement
outcome by overwriting the first classical bit c[0], the second classical bit c[1] and
the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s
2.3 Introduction to AND Operation 77
Fig. 2.12 After the measurement to the program in Listing 2.4 is completed, we obtain the answer
00010 with the probability 0.290 or the answer 00111 with the probability 0.280 or the answer
00100 with the probability 0.240 or the answer 00000 with the probability 0.190
The NAND operation of a bit takes two inputs of a bit and generates one single output
of a bit. If the value of the first input is one (1) and the value of the second input
is one (1), then it yields a result (output) of zero (0). Otherwise, the result (output)
is one (1). A symbol “∧” is applied to represent the NAND operation. Hence, the
NAND operation of a bit that has two inputs of a bit is the following four possible
combinational results:
0∧0=1
0∧1=1
78 2 Boolean Algebra and Its Applications
1∧0=1
1∧1=0 (2.6)
The value of a Boolean variable (a bit) is only one (1) or zero (0). Hence, NAND
of two Boolean variables (two inputs of a bit) q[2] and q[1], written as q[2] ∧ q[1] is
equal to zero (0) if and only if q[2] and q[1] are both one (1). Similarly, q[2] ∧ q[1]
is equal to one (1) if and only if either q[2] or q[1], or both, are zero (0). Usually
using a truth table with logic operation is to represent all possible combinations of
inputs and the corresponding outputs. Therefore, using the form of a truth table that
appears in Table 2.5 may express the rules in (2.6) for the NAND operation of a bit
that has two inputs of a bit and produces one single output of a bit.
We apply one CCNOT gate that has three quantum input bits and three quantum
output bits to implement NAND operation of a classical bit that has two inputs of
a classical bit and produces one output of a classical bit. We make use of the two
control bits C 1 and C 2 of the CCNOT gate to encode two inputs q[2] and q[1] of
a classical bit in NAND operation of a classical bit and use the target bit T of the
CCNOT gate to store one output q[2] ∧ q[1] of a classical bit in NAND operation of
a classical bit. Using the form of a truth table that appears in Table 2.6 may express
the rule of applying one CCNOT gate to implement NAND operation. Its graph
Table 2.5 The truth table for the NAND operation of a bit that has two inputs of a bit and produces
one output of a bit
Input Output
q[2] q[1] q[2] ∧ q[1]
0 0 1
0 1 1
1 0 1
1 1 0
Table 2.6 The truth table of applying one CCNOT gate to implement NAND operation
Input Output
C1 C2 T C1 C2 T = q[2] ∧ q[1]
0 0 1 0 0 1
0 1 1 0 1 1
1 0 1 1 0 1
1 1 1 1 1 0
2.4 Introduction to NAND Operation 79
Fig. 2.13 The quantum circuit of implementing NAND operation of a classical bit
Fig. 2.14 The corresponding quantum circuit of the program in Listing 2.5
representation appears in Fig. 2.13. The initial state of the target bit in the CCNOT
gate in Fig. 2.13 is set to |1.
In Listing 2.5, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the fifth example of the second chapter in which we
illustrate how to write a quantum program to implement NAND operation of a
classical bit by means of applying one CCNOT gate of three quantum bits. Figure 2.14
is the corresponding quantum circuit of the program in Listing 2.5. The statement
“OPENQASM 2.0;” on line one of Listing 2.5 is to indicate that the program is written
with version 2.0 of Open QASM. Then, the statement “include “qelib1.inc”;” on line
two of Listing 2.5 is to continue parsing the file “qelib1.inc” as if the contents of the
file were pasted at the location of the include statement, where the file “qelib1.inc”
is Quantum Experience (QE) Standard Header and the path is specified relative
to the current working directory.
Listing 2.5 The program of applying one CCNOT gate to implement NAND
operation.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. x q[0];
6. h q[1];
80 2 Boolean Algebra and Its Applications
7. h q[2];
8. h q[0];
9. cx q[1], q[0];
10. tdg q[0];
11. cx q[2], q[0];
12. t q[0];
13. cx q[1], q[0];
14. tdg q[0];
15. cx q[2], q[0];
16. t q[0];
17. t q[1];
18. h q[0];
19. cx q[2], q[1];
20. tdg q[1];
21. t q[2];
22. cx q[2], q[1];
23. measure q[0] → c[0];
24. measure q[1] → c[1];
25. measure q[2] → c[2];
Next, the statement “qreg q[5];” on line three of Listing 2.5 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.14, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum
bit is set to |0. We make use of three quantum bits q[2], q[1] and q[0] to encode
respectively the first control bit, the second control bit and the target bit. This is to
say that we use quantum bits q[2] and q[1] to encode two inputs of a classical bit
in NAND operation of a classical bit and apply quantum bit q[0] to store the result
of NAND operation of a classical bit. For the convenience of our explanation, q[k]0
for 0 ≤ k ≤ 4 is to represent the value of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4
is to represent the value of q[k] to be one (1). Similarly, for the convenience of our
explanation, an initial state vector of implementing NAND operation of a classical
bit is as follows:
|B0 = q[2]0 q[1]0 q[0]0 = |0|0|0 = |000.
Next, the statement “creg c[5];” on line four of Listing 2.5 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.14, five classical bits
are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
Next, the three statements “x q[0];”, “h q[1];” and “h q[2];”on line five through
line seven of Listing 2.5 implement one NOT gate and two Hadamard gates of the
first time slot of the quantum circuit in Fig. 2.14. This is to say that the statement
2.4 Introduction to NAND Operation 81
“x q[0];” converts q[0] from one state |0 to another state |1 (its negation). The
statement “h q[1];” converts q[1] from one state |0 to another state √12 (|0 + |1)
(its superposition) and the statement “h q[2];” converts q[2] from one state |0 to
another state √12 (|0 + |1) (its superposition). Therefore, after one NOT gate and
two Hadamard gates in the first time slot of the quantum circuit in Fig. 2.14 are
implemented by means of applying the three statements “x q[0];”, “h q[1];” and “h
q[2];”on line five through line seven of Listing 2.5, the following new state vector is
obtained:
1
|B1 = (|0|0|1 + |0|1|1 + |1|0|1 + |1|1|1)
2
1
= (|001 + |011 + |101 + |111).
2
In the new state vector |B1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
inputs for NAND operation of a classical bit. The initial state of quantum bit q[0] in
four combinational states of quantum bits q[2] and q[1] is |1 and it stores the result
for NAND operation of a classical bit.
The next 12 time slots in the quantum circuit of Fig. 2.14 implement NAND
operation of a classical bit by means of implementing one CCNOT gate. There
are fifteen statements from line eight through line twenty-two in Listing 2.5. The
front eight statements are “h q[0];”, “cx q[1], q[0];”, “tdg q[0];”, “cx q[2], q[0];”, “t
q[0];”, “cx q[1], q[0];”, “tdg q[0];” and “cx q[2], q[0];”. The rear seven statements
are “t q[0];”, “t q[1];”, “h q[0];”, “cx q[2], q[1];”, “tdg q[1];”, “t q[2];” and “cx q[2],
q[1];”. They implemented each quantum gate from the second time slot through the
thirteenth time slot in Fig. 2.14. They take the new state vector |B1 = 21 (|001 +
|011 + |101 + |111) as the input in the second time slot and complete NAND
operation of a classical bit. This gives that the following new state vector is
1
|B16 = (|001 + |011 + |101 + |110).
2
Then, three measurements from the fourteenth time slot through the sixteenth time
slot in Fig. 2.14 were implemented by the three statements “measure q[0] → c[0];”,
“measure q[1] → c[1];” and “measure q[2] → c[2];” on line twenty-three through
line twenty-five of Listing 2.5. They are to measure the first quantum bit q[0], the
second quantum bit q[1] and the third quantum bit q[2]. They record the measurement
outcome by overwriting the first classical bit c[0], the second classical bit c[1] and
the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s
quantum computers, we make use of the command “simulate” to execute the program
in Listing 2.5. The measured result appears in Fig. 2.15. From Fig. 2.15, we obtain
the answer 00011 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 1 = q[1] =
|1 and c[0] = 1 = q[0] = |1) with the probability 0.340. Because the value of the
first control bit q[2] is equal to zero (0) and the value of the second control bit q[1]
82 2 Boolean Algebra and Its Applications
Fig. 2.15 After the measurement to the program in Listing 2.5 is completed, we obtain the answer
00011 with the probability 0.340 or the answer 00001 with the probability 0.300 or the answer
00101 with the probability 0.200 or the answer 00110 with the probability 0.160
is equal to one (1), the value of the target bit q[0] is not changed and is equal to one
(1) with the probability 0.340.
Or we obtain the answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] =
0 = q[1] = |0 and c[0] = 1 = q[0] = |1) with the probability 0.300. Since the value
of the first control bit q[2] is equal to zero (0) and the value of the second control bit
q[1] is equal to zero (0), the value of the target bit q[0] is not changed and is equal to
one (1) with the probability 0.300. Or we obtain the answer 00101 (c[4] = 0, c[3] =
0, c[2] = 1 = q[2] = |1, c[1] = 0 = q[1] = |0 and c[0] = 1 = q[0] = |1) with the
probability 0.200. The value of the first control bit q[2] is equal to one (1) and the
value of the second control bit q[1] is equal to zero (0), so the value of the target bit
q[0] is not changed and is equal to one (1) with the probability 0.200. Or we obtain
the answer 00110 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 1 = q[1] = |1
and c[0] = 0 = q[0] = |0) with the probability 0.160. The value of the first control
bit q[2] is equal to one (1) and the value of the second control bit q[1] is also equal to
one (1). Therefore, the CCNOT gate that implements one NAND operation flips the
value of the target bit q[0] so that the value is equal to zero (0) with the probability
0.160.
The OR operation of a bit acquires two inputs of a bit and yields one single output
of a bit. If the value of the first input is one (1) or the value of the second input is
also one (1) or their values are both one (1), then it produces a result (output) of 1
(one). Otherwise, the result (output) is zero (0). A symbol “∨” is used to represent
the OR operation. Thus, the OR operation of a bit that takes two inputs of a bit is
the following four possible combinational results:
0∨0=0
0∨1=1
1∨0=1
1∨1=1 (2.7)
2.5 Introduction to OR Operation 83
Table 2.7 The truth table for the OR operation of a bit that takes two inputs of a bit and generates
one single output of a bit
Input Output
q[2] q[1] q[2] ∨ q[1]
0 0 0
0 1 1
1 0 1
1 1 1
The value of a Boolean variable (a bit) is only one (1) or zero (0). Therefore, OR
of two Boolean variables (two inputs of a bit) q[2] and q[1], written as q[2] ∨ q[1]
is equal to one (1) if and only if the value of q[2] is one (1) or the value of q[1] is
one (1) or their values are both one (1). Similarly, q[2] ∨ q[1] is equal to zero (0)
if and only if the value of q[2] and the value of q[1] are both zero (0). Often using
a truth table with logic operation is to represent all possible combinations of inputs
and the corresponding outputs. Hence, the rules in (2.7) for the OR operation of a
bit that obtains two inputs of a bit and generates one single output of a bit may also
be expressed in the form of a truth table that appears in Table 2.7.
We make use of one CCNOT gate that has three quantum input bits and three quantum
output bits to implement OR operation of a classical bit that obtains two inputs of
a classical bit and yields one output of a classical bit. We use the two control bits
C 1 and C 2 of the CCNOT gate to encode two inputs q[2] and q[1] of a classical bit
in OR operation of a classical bit. We apply the target bit T of the CCNOT gate to
store one output q[2] ∨ q[1] = q[2] ∨ q[1] = q[2] ∧ q[1] of a classical bit in OR
operation of a classical bit.
Using the form of a truth table that appears in Table 2.8 may express the rule of
using one CCNOT gate to implement OR operation. Its graph representation appears
in Fig. 2.16. In Fig. 2.16, the first control bit (the top first wire) C 1 and the second
Table 2.8 The truth table of applying one CCNOT gate to implement OR operation
Input Output
C1 C2 T C1 C2 T = q[2] ∨ q[1] = q[2] ∧ q[1]
0 0 1 0 0 0
0 1 1 0 1 1
1 0 1 1 0 1
1 1 1 1 1 1
84 2 Boolean Algebra and Its Applications
control bit (the second wire) C 2 of the CCNOT gate respectively encode the first
input q[2] and the second input q[1] of a classical bit in OR operation of a classical
bit. In Fig. 2.16, the target bit (the bottom wire) T of the CCNOT gate is to store one
output q[2] ∨ q[1] = q[2] ∧ q[1] in OR operation of a classical bit.
The initial state of the target bit T in the CCNOT gate in Fig. 2.16 is set to |1.
Implementing OR operation of a classical bit takes two inputs q[2] and q[1] of a
classical bit and produces one output q[2] ∧ q[1] of a classical bit. It is equivalent
to implement NAND operation of a classical bit that takes two inputs q[2] and q[1]
of a classical bit and yields one output (q[2] ∧ q[1]). Therefore, in Fig. 2.16, we use
two NOT gates to operate the two control bits C 1 and C 2 of the CCNOT gate that
encode two inputs q[2] and q[1] of a classical bit and to generate their negations q[2]
and q[1]. Next, in Fig. 2.16, we apply one CCNOT gate to take their negations q[2]
and q[1] as the input and to complete NAND operation of a classical bit. From Table
2.8, two inputs q[2] and q[1] of a classical bit in OR operation of a classical bit that
is encoded by the two control bits C 1 and C 2 of the CCNOT gate in Fig. 2.16 are not
changed. Next, we again make use of two NOT gates to operate the two control bits
C 1 and C 2 of the CCNOT gate in Fig. 2.16 and to generate the result q[2] = q[2]
and q[1] = q[1]. This is to say that using NOT gate twice to the first control bit C 1
and the second control bit C 2 of the CCNOT gate in Fig. 2.16 does nothing to them.
In Listing 2.6, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the sixth example of the second chapter. We introduce how to
write a quantum program to implement OR operation of a classical bit by means of
using one CCNOT gate of three quantum bits and four NOT gates of one quantum
bit. Figure 2.17 is the corresponding quantum circuit of the program in Listing 2.6.
The statement “OPENQASM 2.0;” on line one of Listing 2.6 is to point out that the
program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 2.6 is to continue parsing the file “qelib1.inc” as
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current working directory.
Listing 2.6 The program of using one CCNOT gate and four NOT gates to
implement OR operation.
2.5 Introduction to OR Operation 85
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. x q[0];
6. h q[1];
7. h q[2];
8. x q[1];
9. x q[2];
10. h q[0];
11. cx q[1], q[0];
12. tdg q[0];
13. cx q[2], q[0];
14. t q[0];
15. cx q[1], q[0];
16. tdg q[0];
17. cx q[2], q[0];
18. t q[0];
19. t q[1];
20. h q[0];
21. cx q[2], q[1];
22. tdg q[1];
23. t q[2];
24. cx q[2], q[1];
25. x q[1];
26. x q[2];
27. measure q[0] → c[0];
28. measure q[1] → c[1];
29. measure q[2] → c[2];
Then, the statement “qreg q[5];” on line three of Listing 2.6 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.17, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum
Fig. 2.17 The corresponding quantum circuit of the program in Listing 2.6
86 2 Boolean Algebra and Its Applications
bit is set to |0. We use three quantum bits q[2], q[1] and q[0] to encode respectively
the first control bit, the second control bit and the target bit. This indicates that we
make use of quantum bits q[2] and q[1] to encode two inputs of a classical bit in
OR operation of a classical bit and apply quantum bit q[0] to store the result of
OR operation of a classical bit. For the convenience of our explanation, q[k]0 for
0 ≤ k ≤ 4 is to represent the value of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4
is to represent the value of q[k] to be one (1). Similarly, for the convenience of our
explanation, an initial state vector of implementing OR operation of a classical bit
is as follows:
|C0 = q[2]0 q[1]0 q[0]0 = |0|0|0 = |000.
Next, the statement “creg c[5];” on line four of Listing 2.6 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.17, five classical bits
are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
Next, the three statements “x q[0];”, “h q[1];” and “h q[2];”on line five through
line seven of Listing 2.6 implement one NOT gate and two Hadamard gates of the
first time slot of the quantum circuit in Fig. 2.17. This implies that the statement
“x q[0];” converts q[0] from one state |0 to another state |1 (its negation), the
statement “h q[1];” converts q[1] from one state |0 to another state √12 (|0 + |1) (its
superposition) and the statement “h q[2];” converts q[2] from one state |0 to another
state √12 (|0 + |1) (its superposition). Hence, after one NOT gate and two Hadamard
gates in the first time slot of the quantum circuit in Fig. 2.17 are implemented by
means of using the three statements “x q[0];”, “h q[1];” and “h q[2];”on line five
through line seven of Listing 2.6, the following new state vector is obtained:
1
|C1 = (|0|0|1 + |0|1|1 + |1|0|1 + |1|1|1)
2
1
= (|001 + |011 + |101 + |111).
2
In the new state vector |C 1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
inputs for OR operation of a classical bit. The initial state of quantum bit q[0] in
four combinational states of quantum bits q[2] and q[1] is |1 and it stores the result
for OR operation of a classical bit.
Then, the two statements “x q[1];” and “x q[2];” on line eight through line nine of
Listing 2.6 implement two NOT gates of the second time slot of the quantum circuit
in Fig. 2.17. They take the new state vector |C 1 = 21 (|001 + |011 + |101 + |111)
as the input in the second time slot of Fig. 2.17. This is to say that in the new state
vector |C 1 the state (|0 + |1) of q[2] is converted into the state (|1 + |0) and the
state (|0 + |1) of q[1] is converted into the state (|1 + |0). Because there is no gate
to act on q[0], its state is not changed. Therefore, after two NOT gates in the second
2.5 Introduction to OR Operation 87
time slot of the quantum circuit in Fig. 2.17 are implemented by means of applying
the two statements “x q[1];” and “x q[2];” on line eight through line nine of Listing
2.6, the following new state vector is obtained:
1
|C2 = (|1|1|1 + |1|0|1 + |0|1|1 + |0|0|1)
2
1
= (|111 + |101 + |011 + |001).
2
The next 12 time slots in the quantum circuit of Fig. 2.17 implement OR operation
(q[2] ∨ q[1] = q[2] ∧ q[1]) of a classical bit that is equivalent to implement NAND
operation of a classical bit with two inputs q[2] and q[1] by means of implementing
one CCNOT gate. There are fifteen statements from line ten through line twenty-
four in Listing 2.6. The front eight statements are “h q[0];”, “cx q[1], q[0];”, “tdg
q[0];”, “cx q[2], q[0];”, “t q[0];”, “cx q[1], q[0];”, “tdg q[0];” and “cx q[2], q[0];”.
The rear seven statements are “t q[0];”, “t q[1];”, “h q[0];”, “cx q[2], q[1];”, “tdg
q[1];”, “t q[2];” and “cx q[2], q[1];”. They implemented each quantum gate from the
third time slot through the fourteenth time slot in Fig. 2.17. They take the new state
vector |C 2 = 21 (|111 + |101 + |011 + |001) as the input in the third time slot
and complete OR operation (q[2] ∨ q[1] = q[2] ∧ q[1]) of a classical bit. This gives
that the following new state vector is
1
|C17 = (|110 + |101 + |011 + |001).
2
Next, the two statements “x q[1];” and “x q[2];” on line twenty-five through line
twenty-six of Listing 2.6 implement two NOT gates of the fifteenth time slot of the
quantum circuit in Fig. 2.17. They take the new state vector |C 17 = 21 (|110 + |101
+ |011 + |001) as the input in the fifteenth time slot of Fig. 2.17. This is to say that
in the new state vector |C 17 the state (|110) is converted into the state (|000), the
state (|101) is converted into the state (|011), the state (|011) is converted into the
state (|101) and the state (|001) is converted into the state (|111). Because there
is no gate to act on q[0], its state is not changed. Thus, after two NOT gates in the
fifteenth time slot of the quantum circuit in Fig. 2.17 were implemented by means of
applying the two statements “x q[1];” and “x q[2];” on line twenty-five through line
twenty-six of Listing 2.6, the following new state vector is obtained:
1
|C18 = (|000 + |011 + |101 + |111).
2
Next, three measurements from the sixteenth time slot through the eighteenth time
slot in Fig. 2.17 were implemented by the three statements “measure q[0] → c[0];”,
“measure q[1] → c[1];” and “measure q[2] → c[2];” on line twenty-seven through
line twenty-nine of Listing 2.6. They are to measure the first quantum bit q[0], the
second quantum bit q[1] and the third quantum bit q[2]. They record the measurement
88 2 Boolean Algebra and Its Applications
Fig. 2.18 After the measurement to the program in Listing 2.6 is completed, we obtain the answer
00101 with the probability 0.290 or the answer 00011 with the probability 0.270 or the answer
00000 with the probability 0.250 or the answer 00111 with the probability 0.190
outcome by overwriting the first classical bit c[0], the second classical bit c[1] and
the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s
quantum computers, we use the command “simulate” to execute the program in
Listing 2.6. The measured result appears in Fig. 2.18. From Fig. 2.18, we obtain the
answer 00101 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 0 = q[1] = |0 and
c[0] = 1 = q[0] = |1) with the probability 0.290. In OR operation of a classical bit,
the value of the first input (the first control bit) q[2] is equal to one (1) and the value
of the second input (the second control bit) q[1] is equal to zero (0). Therefore, the
value of the output (the target bit) q[0] is equal to one (1) with the probability 0.290.
Or we obtain the answer 00011 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] =
1 = q[1] = |1 and c[0] = 1 = q[0] = |1) with the probability 0.270. In OR operation
of a classical bit, the value of the first input (the first control bit) q[2] is equal to zero
(0) and the value of the second input (the second control bit) q[1] is equal to one
(1). Hence, the value of the output (the target bit) q[0] is equal to one (1) with the
probability 0.270. Or we obtain the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0 =
q[2] = |0, c[1] = 0 = q[1] = |0 and c[0] = 0 = q[0] = |0) with the probability
0.250. In OR operation of a classical bit, the value of the first input (the first control
bit) q[2] is equal to zero (0) and the value of the second input (the second control
bit) q[1] is also equal to zero (0). Thus, the value of the output (the target bit) q[0]
is equal to zero (0) with the probability 0.250. Or we obtain the answer 00111 (c[4]
= 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 1 = q[1] = |1 and c[0] = 1 = q[0] =
|1) with the probability 0.190. In OR operation of a classical bit, the value of the
first input (the first control bit) q[2] is equal to one (1) and the value of the second
input (the second control bit) q[1] is also equal to one (1). Therefore, the value of
the output (the target bit) is equal to one (1) with the probability 0.190.
The NOR operation of a bit obtains two inputs of a bit and produces one single
output of a bit. If the value of the first input is zero (0) and the value of the second
input is zero (0), then it produces a result (output) of one (1). However, if either the
value of the first input or the value of the second input, or both of them, are one (1),
then it yields a result (output) of zero (0). A symbol “∨” is applied to represent the
2.6 Introduction of NOR Operation 89
Table 2.9 The truth table for the NOR operation of a bit that takes two inputs of a bit and generates
one single output of a bit
Input Output
q[2] q[1] q[2] ∨ q[1]
0 0 1
0 1 0
1 0 0
1 1 0
NOR operation. Therefore, the NOR operation of a bit that acquires two inputs of a
bit is the following four possible combinational results:
0∨0=1
0∨1=0
1∨0=0
1∨1=0 (2.8)
The value of a Boolean variable (a bit) is only zero (0) or one (1). Hence, NOR
operation of two Boolean variables (two inputs of a bit) q[2] and q[1], written as
q[2] ∨ q[1] is equal to one (1) if and only if the value of q[2] is zero (0) and the
value of q[1] is zero (0). Similarly, q[2] ∨ q[1] is equal to zero (0) if and only if
either the value of q[2] is one (1) or the value of q[1] is one (1) or both of them are
one (1). Usually using a truth table with logic operation is to represent all possible
combinations of inputs and the corresponding outputs. Thus, the rules in (2.8) for the
NOR operation of a bit that takes two inputs of a bit and generates one single output
of a bit may also be expressed in the form of a truth table that appears in Table 2.9.
We use one CCNOT gate that has three quantum input bits and three quantum output
bits to implement NOR operation of a classical bit that acquires two inputs of a
classical bit and generates one output of a classical bit. We apply the two control
bits C 1 and C 2 of the CCNOT gate to encode two inputs q[2] and q[1] of a classical
bit in NOR operation of a classical bit and make use of the target bit T of the
CCNOT gate to store one output q[2] ∨ q[1] = q[2] ∧ q[1] of a classical bit in NOR
operation of a classical bit. Using the form of a truth table that appears in Table 2.10
may express the rule of applying one CCNOT gate to complete NOR operation. Its
graph representation appears in Fig. 2.19. In Fig. 2.19, the first control bit (the top
first wire) C 1 and the second control bit (the second wire) C 2 of the CCNOT gate
respectively encode the first input q[2] and the second input q[1] of a classical bit in
90 2 Boolean Algebra and Its Applications
Table 2.10 The truth table of using one CCNOT gate to implement NOR operation
Input Output
C1 C2 T C1 C2 T = q[2] ∨ q[1] = q[2] ∧ q[1]
0 0 0 0 0 1
0 1 0 0 1 0
1 0 0 1 0 0
1 1 0 1 1 0
Fig. 2.19 The quantum circuit of implementing NOR operation of a classical bit
NOR operation of a classical bit. In Fig. 2.19, the target bit (the bottom wire) T of
the CCNOT gate is to store one output q[2] ∨ q[1] = q[2] ∧ q[1] in NOR operation
of a classical bit.
The initial state of the target bit T in the CCNOT gate in Fig. 2.19 is set to |0.
Implementing NOR operation of a classical bit takes two inputs q[2] and q[1] of a
classical bit and generates one output q[2] ∨ q[1] = q[2] ∧ q[1] of a classical bit. It
is equivalent to complete AND operation of a classical bit that acquires two inputs
q[2] and q[1] of a classical bit and produces one output (q[2] ∧ q[1]). Hence, in
Fig. 2.19, we apply two NOT gates to operate the two control bits C 1 and C 2 of the
CCNOT gate that encode two inputs q[2] and q[1] of a classical bit and to yield their
negations q[2] and q[1]. Next, in Fig. 2.19, we make use of one CCNOT gate to
obtain their negations q[2] and q[1] as the input and to implement AND operation
of a classical bit. From Table 2.10, two inputs q[2] and q[1] of a classical bit in NOR
operation of a classical bit that is encoded by the two control bits C 1 and C 2 of the
CCNOT gate in Fig. 2.19 are not changed. Therefore, we again use two NOT gates
to operate the two control bits C 1 and C 2 of the CCNOT gate in Fig. 2.19 and to
produce the result q[2] = q[2] and q[1] = q[1]. This implies that using NOT gate
twice to the first control bit C 1 and the second control bit C 2 of the CCNOT gate in
Fig. 2.19 does nothing to them.
In Listing 2.7, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the seventh example of the second chapter. We describe how to
write a quantum program to implement one NOR operation with one CCNOT gate
and four NOT gates.
2.6 Introduction of NOR Operation 91
Fig. 2.20 The corresponding quantum circuit of the program in Listing 2.7
Figure 2.20 is the corresponding quantum circuit of the program in Listing 2.7.
The statement “OPENQASM 2.0;” on line one of Listing 2.7 is to indicate that the
program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 2.7 is to continue parsing the file “qelib1.inc” as
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current workingdirectory.
Listing 2.7 The program of applying one CCNOT gate and four NOT gates to
implement NOR operation.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[1];
6. h q[2];
7. x q[1];
8. x q[2];
9. h q[0];
10. cx q[1], q[0];
11. tdg q[0];
12. cx q[2], q[0];
13. t q[0];
14. cx q[1], q[0];
15. tdg q[0];
16. cx q[2], q[0];
17. t q[0];
18. t q[1];
19. h q[0];
20. cx q[2], q[1];
21. tdg q[1];
22. t q[2];
92 2 Boolean Algebra and Its Applications
Next, the statement “qreg q[5];” on line three of Listing 2.7 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.20, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum
bit is set to |0. We use three quantum bits q[2], q[1] and q[0] to encode respectively
the first control bit, the second control bit and the target bit. This is to say that we use
quantum bits q[2] and q[1] to encode two inputs of a classical bit in NOR operation of
a classical bit and make use of quantum bit q[0] to store the result of NOR operation
of a classical bit. For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is
to represent the value of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4 is to represent
the value of q[k] to be one (1). Similarly, for the convenience of our explanation, an
initial state vector of implementing NOR operation of a classical bit is as follows:
|D0 = q[2]0 q[1]0 q[0]0 = |0|0|0 = |000.
Next, the statement “creg c[5];” on line four of Listing 2.7 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.20, five classical bits
are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0] is
the least significant bit.
Next, the two statements “h q[1];” and “h q[2];”on line five through line six of
Listing 2.7 implement two Hadamard gates of the first time slot of the quantum circuit
in Fig. 2.20. This is to say that the statement “h q[1];” converts q[1] from one state |0
to another state √12 (|0 + |1) (its superposition) and the statement “h q[2];” converts
q[2] from one state |0 to another state √12 (|0 + |1) (its superposition). Because
there is no gate to act on quantum bit q[0], its state is not changed. Therefore, after
applying the two statements “h q[1];” and “h q[2];”on line five through line six of
Listing 2.7 implements two Hadamard gates in the first time slot of the quantum
circuit in Fig. 2.20, the following new state vector is
1
|D1 = (|0|0|0 + |0|1|0 + |1|0|0 + |1|1|0)
2
1
= (|000 + |010 + |100 + |110).
2
In the new state vector |D1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
2.6 Introduction of NOR Operation 93
inputs for NOR operation of a classical bit. The initial state of quantum bit q[0] in
four combinational states of quantum bits q[2] and q[1] is |0 and it stores the result
for NOR operation of a classical bit.
Next, the two statements “x q[1];” and “x q[2];” on line seven through line eight
of Listing 2.7 complete two NOT gates of the second time slot of the quantum circuit
in Fig. 2.20. They take the new state vector |D1 = 21 (|000 + |010 + |100 + |110)
as the input in the second time slot of Fig. 2.20. This indicates that in the new state
vector |D1 the state (|0 + |1) of q[2] is converted into the state (|1 + |0) and the
state (|0 + |1) of q[1] is converted into the state (|1 + |0). There is no gate to act
on quantum bit q[0], so its state is not changed. Thus, after two NOT gates in the
second time slot of the quantum circuit in Fig. 2.20 are implemented by means of
using the two statements “x q[1];” and “x q[2];” on line seven through line eight of
Listing 2.7, the following new state vector is obtained:
1
|D2 = (|1|1|0 + |1|0|0 + |0|1|0 + |0|0|0)
2
1
= (|110 + |100 + |010 + |000).
2
The next 12 time slots in the quantum circuit of Fig. 2.20 implement NOR opera-
tion (q[2] ∨ q[1] = q[2] ∧ q[1]) of a classical bit that is equivalent to complete AND
operation of a classical bit with two inputs q[2] and q[1] by means of implementing
one CCNOT gate. From line nine through line twenty-three in Listing 2.7, there are
fifteen statements. The front eight statements are “h q[0];”, “cx q[1], q[0];”, “tdg
q[0];”, “cx q[2], q[0];”, “t q[0];”, “cx q[1], q[0];”, “tdg q[0];” and “cx q[2], q[0];”.
The rear seven statements are “t q[0];”, “t q[1];”, “h q[0];”, “cx q[2], q[1];”, “tdg
q[1];”, “t q[2];” and “cx q[2], q[1];”. They implement each quantum gate from the
third time slot through the fourteenth time slot in Fig. 2.20. They take the new state
vector |D2 = 21 (|110 + |100 + |010 + |000) as the input in the third time slot and
complete NOR operation (q[2] ∨ q[1] = q[2] ∧ q[1]) of a classical bit. This gives
that the following new state vector is
1
|D17 = (|111 + |100 + |010 + |000).
2
Next, the two statements “x q[1];” and “x q[2];” on line twenty-four through line
twenty-five of Listing 2.7 implement two NOT gates of the fifteenth time slot of the
quantum circuit in Fig. 2.20. They take the new state vector |D17 = 21 (|111 + |100
+ |010 + |000) as the input in the fifteenth time slot of Fig. 2.20. This indicates that
in the new state vector |D17 the state (|111) is converted into the state (|001), the
state (|100) is converted into the state (|010), the state (|010) is converted into the
state (|100) and the state (|000) is converted into the state (|110). Because there is
no gate to act on quantum bit q[0], its state is not changed. Therefore, after two NOT
gates in the fifteenth time slot of the quantum circuit in Fig. 2.20 were implemented
by means of using the two statements “x q[1];” and “x q[2];” on line twenty-four
94 2 Boolean Algebra and Its Applications
through line twenty-five of Listing 2.7, the following new state vector is obtained:
1
|D18 = (|001 + |010 + |100 + |110).
2
Next, three measurements from the sixteenth time slot through the eighteenth time
slot in Fig. 2.20 were implemented by the three statements “measure q[0] → c[0];”,
“measure q[1] → c[1];” and “measure q[2] → c[2];” on line twenty-six through
line twenty-eight of Listing 2.7. They are to measure the first quantum bit q[0], the
second quantum bit q[1] and the third quantum bit q[2]. They record the measurement
outcome by overwriting the first classical bit c[0], the second classical bit c[1] and
the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s
quantum computers, we apply the command “simulate” to execute the program in
Listing 2.7. The measured result appears in Fig. 2.21. From Fig. 2.21, we obtain the
answer 00001 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 0 = q[1] = |0 and
c[0] = 1 = q[0] = |1) with the probability 0.270. In NOR operation of a classical
bit the value of the first input (the first control bit) q[2] is equal to zero (0) and the
value of the second input (the second control bit) q[1] is equal to zero (0). Therefore,
the value of the output (the target bit) q[0] is equal to one (1) with the probability
0.270.
Or we obtain the answer 00010 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1]
= 1 = q[1] = |1 and c[0] = 0 = q[0] = |0) with the probability 0.270. In NOR
operation of a classical bit the value of the first input (the first control bit) q[2] is
equal to zero (0) and the value of the second input (the second control bit) q[1] is
equal to one (1). Hence, the value of the output (the target bit) q[0] is equal to zero
(0) with the probability 0.270. Or we obtain the answer 00100 (c[4] = 0, c[3] = 0,
c[2] = 1 = q[2] = |1, c[1] = 0 = q[1] = |0 and c[0] = 0 = q[0] = |0) with the
probability 0.260. In NOR operation of a classical bit, the value of the first input (the
first control bit) q[2] is equal to one (1) and the value of the second input (the second
control bit) q[1] is equal to zero (0). Thus, the value of the output (the target bit) q[0]
is equal to zero (0) with the probability 0.260. Or we obtain the answer 00110 (c[4]
= 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 1 = q[1] = |1 and c[0] = 0 = q[0] =
|0) with the probability 0.200. In NOR operation of a classical bit the value of the
first input (the first control bit) q[2] is equal to one (1) and the value of the second
input (the second control bit) q[1] is also equal to one (1). Therefore, the value of
the output (the target bit) q[0] is equal to zero (0) with the probability 0.200.
Fig. 2.21 After the measurement to the program in Listing 2.7 is completed, we obtain the answer
00001 with the probability 0.270 or the answer 00010 with the probability 0.270 or the answer
00100 with the probability 0.260 or the answer 00110 with the probability 0.200
2.7 Introduction for Exclusive-OR Operation 95
The Exclusive-OR (XOR) operation of a bit takes two inputs of a bit and generates
single output of a bit. If the value of the first input is the same as that of the second
input, then it produces a result (output) of zero (0). However, if the value of the
first input and the value of the second input are both different, then it generates an
output of one (1). A symbol “⊕” is used to represent the XOR operation. Hence, the
XOR operation of a bit that gets two inputs of a bit is the following four possible
combinational results:
0⊕0=0
0⊕1=1
1⊕0=1
1⊕1=0 (2.9)
The value of a Boolean variable (a bit) is only one (1) or zero (0). Therefore,
XOR operation of two Boolean variables (two inputs of a bit) q[2] and q[1], written
as q[2] ⊕ q[1] is equal to one (1) if and only if the value of q[2] and the value of q[1]
are different. Similarly, q[2] ⊕ q[1] is equal to zero (0) if and only if the value of
q[2] and the value of q[1] are the same. Often using a truth table with logic operation
is to represent all possible combinations of inputs and the corresponding outputs.
Therefore, using the form of a truth table that appears in Table 2.11 may express
the rules in (2.9) for the XOR operation of a bit that obtains two inputs of a bit and
yields one single output of a bit.
We apply one CNOT gate that has two quantum input bits and two quantum output
bits to implement XOR operation of a classical bit that takes two inputs of a classical
bit and produces one output of a classical bit. We make use of the control bit C 1 and
the target bit T of the CNOT gate to encode two inputs q[2] and q[1] of a classical bit
Table 2.11 The truth table for the XOR operation of a bit that acquires two inputs of a bit and
produces one single output of a bit
Input Output
q[2] q[1] q[2] ⊕ q[1]
0 0 0
0 1 1
1 0 1
1 1 0
96 2 Boolean Algebra and Its Applications
in XOR operation of a classical bit. We use the target bit T of the CNOT gate to store
one output q[2] ⊕ q[1] of a classical bit in XOR operation of a classical bit. Using
the form of a truth table that appears in Table 2.12 may express the rule of using
one CNOT gate to implement XOR operation. Its graph representation appears in
Fig. 2.22.
In Fig. 2.22, the first control bit (the top wire) C 1 and the target bit (the bottom
wire) T of the CNOT gate respectively encode the first input q[2] and the second
input q[1]of a classical bit in XOR operation of a classical bit in Table 2.11. In
Fig. 2.22, the target bit (the bottom wire) T of the CNOT gate also stores one output
q[2] ⊕ q[1] of a classical bit in XOR operation of a classical bit in Table 2.11.
Implementing XOR operation of a classical bit acquires two inputs q[2] and q[1]
of a classical bit and yields one output q[2] ⊕ q[1] of a classical bit. It is equivalent to
implement one CNOT gate that its control bit and its target bit encode two inputs q[2]
and q[1] of a classical bit and its target bit stores one output q[2] ⊕ q[1]. Therefore,
in Fig. 2.22, we use one CNOT gate to implement XOR operation of a classical bit.
In Listing 2.8, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is the eighth example of the second chapter in which we illustrate
how to write a quantum program to complete XOR operation of a classical bit by
means of using one CNOT gate of two quantum bits. Figure 2.23 is the corresponding
quantum circuit of the program in Listing 2.8. The statement “OPENQASM 2.0;” on
line one of Listing 2.8 is to point out that the program is written with version 2.0 of
Open QASM. Then, the statement “include “qelib1.inc”;” on line two of Listing 2.8
is to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Table 2.12 The truth table of using one CNOT gate to implement XOR operation
Input Output
C1 T C1 T = q[2] ⊕ q[1]
0 0 0 0
0 1 0 1
1 0 1 1
1 1 1 0
Fig. 2.22 The quantum circuit of implementing XOR operation of a classical bit
2.7 Introduction for Exclusive-OR Operation 97
Fig. 2.23 The corresponding quantum circuit of the program in Listing 2.8
Experience (QE) Standard Header and the path is specified relative to the current
working directory.
Listing 2.8 The program of using one CNOT gate to implement XOR operation.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[1];
6. h q[2];
7. cx q[2], q[1];
8. measure q[1] → c[1];
9. measure q[2] → c[2];
Next, the statement “qreg q[5];” on line three of Listing 2.8 is to declare that in
the program there are five quantum bits. In the left top of Fig. 2.23, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit
is set to |0. We make use of two quantum bits q[2] and q[1] to encode respectively
the control bit and the target bit of one CNOT gate. This implies that we apply
quantum bits q[2] and q[1] to encode two inputs of a classical bit in XOR operation
of a classical bit and use quantum bit q[1] to store the result of XOR operation of
a classical bit. For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is to
represent the value of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4 is to represent
the value of q[k] to be one (1). Similarly, for the convenience of our explanation, an
initial state vector of implementing XOR operation of a classical bit is as follows:
|E 0 = q[2]0 q[1]0 = |0|0 = |00.
Next, the statement “creg c[5];” on line four of Listing 2.8 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.23, five classical
bits are respectively c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical
98 2 Boolean Algebra and Its Applications
bit is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0]
is the least significant bit.
Then, the two statements “h q[1];” and “h q[2];”on line five through line six of
Listing 2.8 implement two Hadamard gates of the first time slot of the quantum
circuit in Fig. 2.23. This indicates that the statement “h q[1];” converts q[1] from one
state |0 to another state √12 (|0 + |1) (its superposition) and the statement “h q[2];”
converts q[2] from one state |0 to another state √12 (|0 + |1) (its superposition).
Hence, after the two statements “h q[1];” and “h q[2];” on line five through line six
of Listing 2.8 implement two Hadamard gates in the first time slot of the quantum
circuit in Fig. 2.23 are implemented, the following new state vector is
1
|E 1 = (|0|0 + |0|1 + |1|0 + |1|1)
2
1
= (|00 + |01 + |10 + |11).
2
In the new state vector |E 1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
inputs in XOR operation of a classical bit in Table 2.11. Quantum bit q[1] stores the
result for XOR operation of a classical bit in Table 2.11.
Next, the statement “cx q[2], q[1];” on line seven of Listing 2.8 complete one
CNOT gates of the second time slot of the quantum circuit in Fig. 2.23. They take
the new state vector |E 1 = 21 (|00 + |01 + |10 + |11) as the input in the second
time slot of Fig. 2.23. This is to say that in the new state vector |E 1 the state (|00) of
quantum bits q[2] and q[1] is not changed and the state (|01) of quantum bits q[2]
and q[1] is also not changed because the value of the control bit q[2] is equal to zero
(0). However, the value of the control bit q[2] is equal to one (1) and the statement
“cx q[2], q[1];” flips the target bit q[1]. Therefore, the state (|10) of quantum bits
q[2] and q[1] is converted into the state (|11) and the state (|11) of quantum bits
q[2] and q[1] is converted into the state (|10). Therefore, after one CNOT gate in
the second time slot of the quantum circuit in Fig. 2.23 is implemented by means
of applying the statement “cx q[2], q[1];” on line seven of Listing 2.8, the following
new state vector is obtained:
1
|E 2 = (|0|0 + |0|1 + |1|1 + |1|0)
2
1
= (|00 + |01 + |11 + |10).
2
Next, two measurements from the third time slot through the fourth time slot
in Fig. 2.23 were implemented by the two statements “measure q[1] → c[1];” and
“measure q[2] → c[2];” on line eight through line nine of Listing 2.8. They are to
measure the second quantum bit q[1] and the third quantum bit q[2]. They record
the measurement outcome by overwriting the second classical bit c[1] and the third
classical bit c[2]. In the backend ibmqx4 with five quantum bits in IBM’s quantum
2.7 Introduction for Exclusive-OR Operation 99
Fig. 2.24 After the measurement to the program in Listing 2.8 is completed, we obtain the answer
00010 with the probability 0.260 or the answer 00110 with the probability 0.260 or the answer
00100 with the probability 0.250 or the answer 00000 with the probability 0.230
computers, we use the command “simulate” to run the program in Listing 2.8. The
measured result appears in Fig. 2.24. From Fig. 2.24, we get the answer 00010 (c[4]
= 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 1 = q[1] = |1 and c[0] = 0) with the
probability 0.260. Because in XOR operation of a classical bit the value of the first
input (the control bit) q[2] is equal to zero (0) and the value of the second input (the
target bit) q[1] is equal to one (1), the value of the output (the target bit) q[1] is equal
to one (1) with the probability 0.260.
Or we obtain the answer 00110 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1]
= 1 = q[1] = |1 and c[0] = 0) with the probability 0.260. Since in XOR operation
of a classical bit the value of the first input (the control bit) q[2] is equal to one (1)
and the value of the second input (the target bit) q[1] is equal to zero (0), the value
of the output (the target bit) q[1] is equal to one (1) with the probability 0.260. Or
we acquire the answer 00100 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 0
= q[1] = |0 and c[0] = 0) with the probability 0.250. Since in XOR operation of a
classical bit the value of the first input (the control bit) q[2] is equal to one (1) and
the value of the second input (the target bit) q[1] is equal to one (1), the value of the
output (the target bit) q[1] is equal to zero (0) with the probability 0.250. Or we get
the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 0 = q[1] =
|0 and c[0] = 0) with the probability 0.230. In XOR operation of a classical bit the
value of the first input (the control bit) q[2] is equal to zero (0) and the value of the
second input (the target bit) q[1] is also equal to zero (0). Therefore, the value of the
output (the target bit) q[1] is equal to zero (0) with the probability 0.230.
The one’s complement of the Exclusive-OR (XOR) operation of a bit that acquires
two inputs of a bit and yields one single output of a bit is known as the Exclusive-
NOR (XNOR) operation of a bit. The Exclusive-NOR (XNOR) operation of a bit
obtains two inputs of a bit and produces one single output of a bit. If the value of the
first input is the same as that of the second input, then it generates a result (output)
of one (1). However, if the value of the first input and the value of the second input
are both different, then it produces an output of zero (0). A symbol “⊕” is applied
to represent the XNOR operation of a bit. Therefore, the XNOR operation of a bit
100 2 Boolean Algebra and Its Applications
Table 2.13 The truth table for the XNOR operation of a bit that acquires two inputs of a bit and
generates one single output of a bit
Input Output
q[2] q[1] q[2] ⊕ q[1]
0 0 1
0 1 0
1 0 0
1 1 1
that takes two inputs of a bit is the following four possible combinational results:
0⊕0=1
0⊕1=0
1⊕0=0
1⊕1=1 (2.10)
The value of a Boolean variable (a bit) is just zero (0) or one (1). Hence, XNOR
operation of two Boolean variables (two inputs of a bit) q[2] and q[1], written as
q[2] ⊕ q[1] is equal to one (1) if and only if the value of q[2] and the value of q[1]
are the same. Similarly, q[2] ⊕ q[1] is equal to zero (0) if and only if the value of q[2]
and the value of q[1] are different. Usually applied a truth table with logic operation
is to represent all possible combinations of inputs and the corresponding outputs.
Hence, the rules in (2.10) for the XNOR operation of a bit that gets two inputs of a
bit and produces one single output of a bit may also be expressed in the form of a
truth table that appears in Table 2.13.
We make use of one CNOT gate and one NOT gate to implement XNOR operation
of a classical bit that acquires two inputs of a classical bit and generates one output
of a classical bit. We use the control bit C 1 and the target bit T of the CNOT gate to
encode two inputs q[2] and q[1] of a classical bit in XNOR operation of a classical
bit. We also use the target bit T of the CNOT gate to store one output q[2] ⊕ q[1]
of a classical bit in XNOR operation of a classical bit. Using the form of a truth
table that appears in Table 2.14 may express the rule of applying one CNOT gate
and one NOT gate to complete XNOR operation. Its graph representation appears
in Fig. 2.25.
In Fig. 2.25, the first control bit (the top wire) C 1 and the target bit (the bottom
wire) T of the CNOT gate respectively encode the first input q[2] and the second
input q[1]of a classical bit in XNOR operation of a classical bit in Table 2.13. In
2.8 Introduction of Exclusive-NOR Operation 101
Table 2.14 The truth table of using one CNOT gate and one NOT gate to implement XNOR
operation
Input Output
C1 T C1 T = q[2] ⊕ q[1]
0 0 0 1
0 1 0 0
1 0 1 0
1 1 1 1
Fig. 2.25 The quantum circuit of implementing XNOR operation of a classical bit
Fig. 2.25, the target bit (the bottom wire) T of the CNOT gate also stores one output
q[2] ⊕ q[1] of a classical bit in XNOR operation of a classical bit in Table 2.13.
Implementing XNOR operation of a classical bit takes two inputs q[2] and q[1] of
a classical bit and produces one output q[2] ⊕ q[1] of a classical bit. It is equivalent
to implement one CNOT gate and one NOT gate in which the control bit and the
target bit encode two inputs q[2] and q[1] of a classical bit and its target bit also
stores one output q[2] ⊕ q[1]. Hence, in Fig. 2.25, we first apply one CNOT gate
to generate an output q[2] ⊕ q[1] of XOR operation that is stored in the target bit
(the bottom wire) T. Next, we use one NOT gate to produce the negation of XOR
operation (q[2]⊕q[1]) that is to complete XNOR operation q[2] ⊕ q[1] that is stored
in the target bit T.
In Listing 2.9, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is the ninth example of the second chapter in which we
describe how to write a quantum program to complete XNOR operation of a clas-
sical bit by means of applying one CNOT gate and one NOT gate. Figure 2.26 is the
corresponding quantum circuit of the program in Listing 2.9. The statement “OPEN-
QASM 2.0;” on line one of Listing 2.9 is to indicate that the program is written with
version 2.0 of Open QASM. Next, the statement “include “qelib1.inc”;” on line two
of Listing 2.9 is to continue parsing the file “qelib1.inc” as if the contents of the file
were pasted at the location of the include statement, where the file “qelib1.inc” is
Quantum Experience (QE) Standard Header and the path is specified relative to
the current working directory.
102 2 Boolean Algebra and Its Applications
Fig. 2.26 The corresponding quantum circuit of the program in Listing 2.9
Listing 2.9 The program of using one CNOT gate and one NOT gate to implement
XNOR operation.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. h q[1];
6. h q[2];
7. cx q[2], q[1];
8. x q[1];
9. measure q[1] → c[1];
10. measure q[2] → c[2];
Next, the statement “qreg q[5];” on line three of Listing 2.9 is to declare that in the
program there are five quantum bits. In the left top of Fig. 2.26, five quantum bits are
subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum bit is
set to |0. We use two quantum bits q[2] and q[1] to encode respectively the control
bit and the target bit of one CNOT gate. This implies that we apply quantum bits q[2]
and q[1] to encode two inputs of a classical bit in XNOR operation of a classical bit
and use quantum bit q[1] to store the result of XNOR operation of a classical bit.
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is to represent the value
of q[k] to be zero (0) and q[k]1 for 0 ≤ k ≤ 4 is to represent the value of q[k] to be
one (1). Similarly, for the convenience of our explanation, an initial state vector of
implementing XNOR operation of a classical bit is as follows:
|F0 = q[2]0 q[1]0 = |0|0 = |00.
2.8 Introduction of Exclusive-NOR Operation 103
Then, the statement “creg c[5];” on line four of Listing 2.9 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 2.26, five classical
bits are respectively c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical
bit is set to zero (0). Classical bit c[4] is the most significant bit and classical bit c[0]
is the least significant bit.
Next, the two statements “h q[1];” and “h q[2];”on line five through line six of
Listing 2.9 implement two Hadamard gates of the first time slot of the quantum
circuit in Fig. 2.26. This is to say that the statement “h q[1];” converts q[1] from one
state |0 to another state √12 (|0 + |1) (its superposition) and the statement “h q[2];”
converts q[2] from one state |0 to another state √12 (|0 + |1) (its superposition).
Hence, after the two statements “h q[1];” and “h q[2];”on line five through line six
of Listing 2.9 implement two Hadamard gates in the first time slot of the quantum
circuit in Fig. 2.26, the following new state vector is
1
|F1 = (|0|0 + |0|1 + |1|0 + |1|1)
2
1
= (|00 + |01 + |10 + |11).
2
In the new state vector |F 1 , four combinational states of quantum bits q[2] and
q[1] with that the amplitude of each combinational state is 21 encode all of the possible
inputs in XNOR operation of a classical bit in Table 2.13. Quantum bit q[1] stores
the result for XNOR operation of a classical bit in Table 2.13.
Next, the statement “cx q[2], q[1];” on line seven of Listing 2.9 implements one
CNOT gates of the second time slot of the quantum circuit in Fig. 2.26. They take
the new state vector |F 1 = 21 (|00 + |01 + |10 + |11) as the input in the second
time slot of Fig. 2.26. Inthe input state vector |F 1 , because the value of the control
bit q[2] is equal to zero (0), the value of the target bit q[1] is not changed. Hence,
the state (|00) of quantum bits q[2] and q[1] is not changed and the state (|01) of
quantum bits q[2] and q[1] is also not changed. Because the value of the control bit
q[2] is equal to one (1), the statement “cx q[2], q[1];” flips the value of the target
bit q[1]. Therefore, the state (|10) of quantum bits q[2] and q[1] is converted into
the state (|11) and the state (|11) of quantum bits q[2] and q[1] is converted into
the state (|10). Hence, after one CNOT gate in the second time slot of the quantum
circuit in Fig. 2.26 is implemented by means of using the statement “cx q[2], q[1];”
on line seven of Listing 2.9, we obtain the following new state vector
1
|F2 = (|0|0 + |0|1 + |1|1 + |1|0)
2
1
= (|00 + |01 + |11 + |10).
2
Next, the statement “x q[1];” on line eight of Listing 2.9 implements one NOT
gate in the third time slot of the quantum circuit in Fig. 2.26. It takes the new state
vector |F 2 = 21 (|00 + |01 + |11 + |10) as the input in the third time slot of the
104 2 Boolean Algebra and Its Applications
quantum circuit in Fig. 2.26. This is to say that there is no quantum gate to act on
quantum bit q[2] in the input state vector |F 2 and the value of quantum bit q[2] is
not changed. Because the statement “x q[1];” flips the value of quantum bit q[1] in
the input state vector |F 2 , the states (|00), (|01), (|11) and (|10) of quantum bits
q[2] and q[1] are subsequently converted into the new states (|01), (|00), (|10) and
(|11). Therefore, after one NOT gate in the third time slot of the quantum circuit in
Fig. 2.26 is implemented by means of applying the statement “x q[1];” on line eight
of Listing 2.9, the following new state vector is obtained:
1
|F3 = (|0|1 + |0|0 + |1|0 + |1|1)
2
1
= (|01 + |00 + |10 + |11).
2
Next, two measurements from the fourth time slot through the fifth time slot of
the quantum circuit in Fig. 2.26 were implemented by the two statements “measure
q[1] → c[1];” and “measure q[2] → c[2];” on line nine through line ten of Listing
2.9. They are to measure the second quantum bit q[1] and the third quantum bit
q[2]. They record the measurement outcome by overwriting the second classical bit
c[1] and the third classical bit c[2]. In the backend ibmqx4 with five quantum bits in
IBM’s quantum computers, we make use of the command “simulate” to execute the
program in Listing 2.9. The measured result appears in Fig. 2.27. From Fig. 2.27,
we obtain the answer 00000 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] = 0 =
q[1] = |0 and c[0] = 0) with the probability 0.370. Since in XNOR operation of a
classical bit the value of the first input (the control bit) q[2] is equal to zero (0) and
the value of the second input (the target bit) q[1] is equal to one (1), the value of the
output (the target bit) q[1] is equal to zero (0) with the probability 0.370. Or we get
the answer 00100 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 0 = q[1] = |0
and c[0] = 0) with the probability 0.260. Because in XNOR operation of a classical
bit the value of the first input (the control bit) q[2] is equal to one (1) and the value
of the second input (the target bit) q[1] is equal to zero (0), the value of the output
(the target bit) q[1] is equal to zero (0) with the probability 0.260.
Or we acquire the answer 00010 (c[4] = 0, c[3] = 0, c[2] = 0 = q[2] = |0, c[1] =
1 = q[1] = |1 and c[0] = 0) with the probability 0.230. Because in XNOR operation
of a classical bit the value of the first input (the control bit) q[2] is equal to zero (0)
and the value of the second input (the target bit) q[1] is equal to zero (0), the value of
Fig. 2.27 After the measurement to the program in Listing 2.9 is completed, we obtain the answer
00000 with the probability 0.370 or the answer 00100 with the probability 0.260 or the answer
00010 with the probability 0.230 or the answer 00110 with the probability 0.140
2.8 Introduction of Exclusive-NOR Operation 105
the output (the target bit) q[1] is equal to one (1) with the probability 0.230. Or we
obtain the answer 00110 (c[4] = 0, c[3] = 0, c[2] = 1 = q[2] = |1, c[1] = 1 = q[1]
= |1 and c[0] = 0) with the probability 0.140. In XNOR operation of a classical
bit, the value of the first input (the control bit) q[2] is equal to one (1) and the value
of the second input (the target bit) q[1] is also equal to one (1). Therefore, the value
of the output (the target bit) q[1] is equal to one (1) with the probability 0.140.
2.9 Summary
The textbooks written by these authors in Mano (1979, 1993), Chang and Vasilakos
(2014) is a good illustration to logic operations including NOT, AND, NAND, OR,
NOR, Exclusive-OR (XOR) and Exclusive-NOR (XNOR) on bits. A good intro-
duction of decomposing CCNOT gate into six CNOT gates and nine gates of one bit
can be found in the textbook (Nielsen and Chuang 2000) and in the famous article
(Shende and Markov 2009). The famous menu in IBM Q (2016) is a good guide of
writing nine quantum programs from Listing 2.1 to Listing 2.9. A good illustration
to Boolean’s functions discussed in exercises in Sect. 2.11 is Mano (1979, 1993),
Brown and Vranesic (2007), Chang and Vasilakos (2014).
2.11 Exercises
2.1 The unary operator “−” denotes logical operation NOT and the binary operator
“∨” denotes logical operation OR. For a logical operation, x ∨ y, x and y are
Boolean variables that are subsequently the first input and the second input. Its
truth table appears in Table 2.15. Please write a quantum program to implement
the function of the logical operation, x ∨ y.
2.2 The unary operator “−” denotes logical operation NOT and the binary operator
“∨” denotes logical operation OR. For a logical operation, x ∨ y, x and y are
Boolean variables that are respectively its first input and its second input. Its
truth table appears in Table 2.16. Please write a quantum program to implement
the function of the logical operation, x ∨ y.
2.3 The unary operator “−” denotes logical operation NOT and the binary operator
“∧” denotes logical operation AND. For a logical operation, y ∧ (x ∨ x) = y
∧ 1 = y, x and y are Boolean variables that are subsequently the first input and
the second input. Its truth table appears in Table 2.17. Please write a quantum
program to implement the function of the logical operation, y ∧ (x ∨ x) = y
∧ 1 = y.
2.4 The unary operator “−” denotes logical operation NOT and the binary operator
“∧” denotes logical operation AND. For a logical operation, x ∧ (y ∨ y) = x
∧ 1 = x, x and y are Boolean variables that are respectively the first input and
the second input. Its truth table appears in Table 2.18. Please write a quantum
program to implement the function of the logical operation, x ∧ (y ∨ y) = x
∧ 1 = x.
2.5 The unary operator “−” denotes logical operation NOT and the binary operator
“∧” denotes logical operation AND. For a logical operation, x ∧ y, x and y are
Boolean variables that are subsequently the first input and the second input. Its
truth table appears in Table 2.19. Please write a quantum program to implement
the function of the logical operation, x ∧ y.
2.6 The unary operator “−” denotes logical operation NOT and the binary operator
“∧” denotes logical operation AND. For a logical operation, x ∧ y, x and y are
Boolean variables that are respectively its first input and its second input. Its
truth table appears in Table 2.20. Please write a quantum program to implement
the function of the logical operation, x ∧ y.
References
Brown, S., Vranesic, Z.: Fundamentals of Digital Logic With Verilog Design. McGraw-Hill, New
York (2007). ISBN: 978-0077211646
Chang, W.-L., Vasilakos, A.V.: Molecular Computing: Towards a Novel Computing Architecture
for Complex Problem Solving. Springer, Berlin (2014). ISBN-10: 3319380982 and ISBN-13:
978-3319380988
Mano, M.M.: Digital Logic and Computer Design. Prentice-Hall, USA (1979). ISBN: 0-13-214510-
3
Mano, M.M.: Computer System Architecture. Prentice Hall, USA (1993). ISBN: 978-0131755635
Nielsen, M.A., Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge
University Press, New York (2000). ISBN-10: 9781107002173 and ISBN-13: 978-1107002173
Shende, V.V., Markov, I.L.: On the CNOT-cost of TOFFOLI gates. Quantum Inf. Comput. 9(5),
461–486 (2009)
The IBM Quantum Experience. https://www.research.ibm.com/quantum/. Accessed Nov 2016
Chapter 3
Quantum Search Algorithm and Its
Applications
Because in IBM’s quantum computers, they only provide quantum gates of single
quantum bit and two quantum bits, quantum gates of three quantum bits and many
quantum bits must manually be decomposed into quantum gates of single quantum bit
and two quantum bits. A good quantum algorithm of solving any given problem with
the size of the input of n bits must have a constant successful probability of measuring
its answer(s) that is close to one as soon as possible. In this chapter, we first illustrate
how to decompose quantum gates of three quantum bits and many quantum bits
into quantum gates of single quantum bit and two quantum bits. Next, we introduce
how to write quantum programs with version 2.0 of Open QASM to implement
decomposition among various kinds of quantum gates. A quantum search algorithm
that is sometimes known as Grover’s algorithm to find an item in unsorted databases
with 2n items that satisfies any given condition can give a quadratic speed-up and is
the best one known. Hence, we then describe how to write quantum programs with
version 2.0 of Open QASM to implement the quantum search algorithm in order to
solve various applications.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 109
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1_3
110 3 Quantum Search Algorithm and Its Applications
question is to find values of each Boolean variable so that the whole formula has the
value 1. This is the same as finding values of each Boolean variable that make each
clause have the value 1.
From Definition 3.1, a satisfiability problem contains a Boolean formula of the form
C1 ∧ C2 . . . ∧ Cm , where each clause C j for 1 ≤j ≤ m is a formula of the form
x1 ∨ x2 ∨ . . . xn−1 ∨ xn for each Boolean variable x k to 1 ≤ k ≤ n. For solving the
satisfiability problem with n Boolean variables and m clause, we need to use auxiliary
Boolean variables r j, k for 1 ≤ j ≤ m and 0 ≤ k ≤ n and auxiliary Boolean variables
sj for 0 ≤ j ≤ m. Because we use auxiliary Boolean variables r j, 0 for 1 ≤ j ≤ m as
the first operand of the first logical or operation (“∨”) in each clause, the initial value
of each auxiliary Boolean variable r j, 0 for 1 ≤ j ≤ m is set to zero (0). This is to
say that this setting does not change the correct result of the first logical or operation
in each clause. We use CCNOT gates and NOT gates to implement the logical or
operations in each clause and we apply auxiliary Boolean variables r j, k for 1 ≤ j
≤ m and 1 ≤ k ≤ n to store the result of implementing the logical or operations in
each clause. This indicates that each auxiliary Boolean variable r j, k for 1 ≤ j ≤ m
and 1 ≤ k ≤ n is actually the target bit of a CCNOT gate of implementing a logical
or operation. Therefore, the initial value of each auxiliary Boolean variable r j, k for
1 ≤ j ≤ m and 1 ≤ k ≤ n is set to one (1).
We use an auxiliary Boolean variable s0 as the first operand of the first logical and
operation (“∧”) in a Boolean formula of the form C1 ∧ C2 . . . ∧ Cm . The initial value
of the auxiliary Boolean variable s0 is set to one (1). This implies that this setting does
not change the correct result of the first logical and operation in C1 ∧ C2 . . . ∧ Cm .
We use CCNOT gates to implement the logical and operations in C1 ∧ C2 . . . ∧ Cm
and we apply auxiliary Boolean variables sj for 1 ≤ j ≤ m to store the result of
implementing the logical and operations in C1 ∧ C2 . . . ∧ Cm . This is to say that each
auxiliary Boolean variable sj for 1 ≤ j ≤ m is actually the target bit of a CCNOT gate
of implementing a logical and operation. Thus, the initial value of each auxiliary
Boolean variable sj for 1 ≤ j ≤ m is set to zero (0). For the convenience of our
presentation, we assume that |C j | is the number of Boolean variable in the jth clause
Cj .
112 3 Quantum Search Algorithm and Its Applications
Fig. 3.1 Flowchart of solving the satisfiability problem with n Boolean variables and m clauses
3.2 Introduction to the Satisfiability Problem 113
in the jth clause C j . Next, in statement S 6 , it increases the value of the index variable
k to the second loop. Repeat to execute statement S 4 through statement S 6 until in
statement S 4 the conditional judgement becomes a false value.
When the value of the index variable k in the second loop is greater than the number
of Boolean variables in the jth clause C j , next executed instruction is statement
S 7 . In statement S 7 , it executes a logical and operation “sj ← sj − 1 ∧ r j, k − 1 ” in
C1 ∧ C2 . . . ∧ Cm . Boolean variable sj − 1 is the first operand of the logical and
operation and stores the result of the previous logical and operation. Because the
value of k is equal to C j + 1, the value of (k − 1) is equal to |C j |. Boolean variable
r j, k − 1 is the second operand of the logical and operation and stores the result of
implementing a formula of the form x1 ∨ x2 ∨ . . . xn − 1 ∨ xn in the jth clause C j .
Boolean variable sj stores the result of implementing the jth logical and operation
in. Next, in statement S 8 , it increases the value of the index variable j to the first
loop. Repeat to execute statement S 2 through statement S 8 until in statement S 2
the conditional judgement becomes a false value. Because from Definition 3.1 each
clause C j for 1 ≤ j ≤ m is a formula of the form x1 ∨ x2 ∨ . . . xn−1 ∨ xn , from Fig. 3.1
the total number of logical and operation and logical or operation is m logical and
m
operations and j=1 |C j | = (m × n) logical or operations. This is the cost of
implementing m clauses of one time for one of 2n inputs to n Boolean variables.
Therefore, the cost of implementing m clauses of 2n times for 2n inputs of n Boolean
variables is (2n × m) logical and operations and (2n × m × n) logical or operations.
A data dependence arises from two statements that access or modify the same
resource. Data dependence analysis is to judge whether it is safe to reorder or paral-
lelize statements. In a satisfiability problem with n Boolean variables and m clauses,
it consists of 2n inputs that are 2n combinational states of n Boolean variables. The
0
first input is x10 x20 . . . xn−1 0
xn0 , the second input is x10 x20 . . . xn−1 xn1 and so on with that
1 1 1 1
the last input is x1 x2 . . . xn−1 xn . Each input needs to complete those operations in
Fig. 3.1. Each input needs to use (m × (n + 1)) auxiliary Boolean variables r j, k for
1 ≤ j ≤ m and 0 ≤ k ≤ n and (m + 1) auxiliary Boolean variables sj for 0 ≤ j ≤ m.
Since 2n inputs of n Boolean variables implement those operations in Fig. 3.1 not to
access or modify the same input and the same auxiliary Boolean variables, we can
parallelize them without any error.
Let us consider another example that is F(x1 , x2 ) = x1 ∧ x2 , where two variables
x 1 and x 2 are two Boolean variables and their values could be 0 or 1. In the formula
F(x1 , x2 ) = x1 ∧ x2 , the first clause contains (x 1 ) and the second clause includes
(x 2 ). The satisfiability problem for the Boolean formula F(x1 , x2 ) = x1 ∧ x2 with
two Boolean variable x 1 and x 2 is to find values of each Boolean variable so that the
whole formula has the value 1. This is the same as finding values of each Boolean
variable that make each clause have the value 1.
114 3 Quantum Search Algorithm and Its Applications
{x1 x2 ∀xd ∈ {0, 1} for 1 ≤ d ≤ 2}. We make use of (1, 0, 0, 0) to encode Boolean
variable x 01 and Boolean variable x 02 . Next, we use (0, 1, 0, 0) to encode Boolean
variable x 11 and Boolean variable x 02 . We apply (0, 0, 1, 0) to encode Boolean variable
x 01 and Boolean variable x 12 . Finally, we use (0, 0, 0, 1) to encode Boolean variable
x 11 and Boolean variable x 12 .
We use a linear combination of each element in the basis that is √1 2 (1, 0, 0, 0) +
2
√1 × (0, 1, 0, 0) + √1 × (0, 0, 1, 0) + √1 × (0, 0, 0, 1) = √1 , √1 , √1 , √1
2 2 2 2 2 2 2 2 2 2 2 2 2 2
In Listing 3.1, the program in the backend ibmqx4 with five quantum bits in
IBM’s quantum computer is to solve an instance of the satisfiability problem with
F(x1 , x2 ) = x1 ∧ x2 in which we illustrate how to write a quantum program to find
values of each Boolean variable so that the whole formula has the value 1. Figure 3.2
is the quantum circuit of constructing solution space to an instance of the satisfiability
problem with F(x1 , x2 ) = x1 ∧ x2 . The statement “OPENQASM 2.0;” on line one of
Listing 3.1 is to indicate that the program is written with version 2.0 of Open QASM.
Next, the statement “include “qelib1.inc”;” on line two of Listing 3.1 is to continue
parsing the file “qelib1.inc” as if the contents of the file were pasted at the location
Fig. 3.2 The quantum circuit of constructing solution space to an instance of the satisfiability
problem with F(x1 , x2 ) = x1 ∧ x2
116 3 Quantum Search Algorithm and Its Applications
of the include statement, where the file “qelib1.inc” is Quantum Experience (QE)
Standard Header and the path is specified relative to the current working directory.
Listing 3.1 The program of solving an instance of the satisfiability problem with
F(x1 , x2 ) = x1 ∧ x2 .
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
Next, the statement “qreg q[5];” on line three of Listing 3.1 is to declare that in
the program there are five quantum bits. In the left top of Fig. 3.2, five quantum bits
are subsequently q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum
bit is set to |0 > . We use quantum bit q[3] to encode Boolean variable x 1 . We make
use of quantum bit q[4] to encode Boolean variable x 2 . We apply quantum bit q[2]
to encode auxiliary Boolean variable s2 . We use quantum bit q[0] as an auxiliary
working bit. We do not use quantum bit q[1].
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is to represent the value
0 of q[k] and q[k]1 for 0 ≤ k ≤ 4 is to represent the value 1 of q[k]. Similarly, for the
convenience of our explanation, an initial state vector of constructing solution space
to an instance of the satisfiability problem with F(x1 , x2 ) = x1 ∧ x2 is as follows:
|0 = q[4]0 q[3]0 q[2]0 q[1]0 q[0]0 = |0 |0 |0 |0 |0 = |00000 .
Then, the statement “creg c[5];” on line four of Listing 3.1 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 3.2, five classical bits
are respectively c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical bit
is set to 0. Classical bit c[4] is the most significant bit and classical bit c[0] is the
least significant bit.
Next, the three statements “x q[0];”, “h q[3];” and “h q[4];” on line five
through seven of Listing 3.1 is to implement one X gate (one NOT gate) and
two Hadamard gates of the first time slot of the quantum circuit in Fig. 3.2.
01 1 0
The statement “x q[0];” actually completes × = = (|1 ).
10 0 1
This 1indicates
that the statement “x q[0];” on line five of Listing 3.1 inverts into
q[0] (|1 ). The two statements “h q[3];” and “h q[4];” both actually complete
√1 √1 1 √1 1 1 0
2 2 × = 2 √1 = √1 + = √1 (|0 + |1 ).
√1 − √12 0 √1 2 1 2 0 1 2
2 2
3.2 Introduction to the Satisfiability Problem 117
This is to say that converting q[3] from one state |0 to another state √12 (|0 + |1 ) (its
superposition) and converting q[4] from one state |0 to another state (its superposi-
tion) are completed. Therefore,
the superposition
of the two quantum bits q[4] and
1 1 1
q[3] is √2 (|0 + |1 ) √ (|0 + |1 ) = (|0 |0 + |0 |1 + |1 |0 + |1 |1 ) =
2 2
1
2 (|00
+ |01 + |10 + |11 ). Because in the first time slot of the quantum circuit in
Fig. 3.2
there is no quantum gate to act on quantum bits q[2] and q[1], their current
states q[2]0 and q[1]0 are not changed. This is to say that we obtain the following
1 1
√ q[4]0 + q[4]1 √ q[3]0 + q[3]1 q[2]0 q[1]0 q[0]1
|1 =
2 2
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1
=
2 0
q[2] q[1]0 q[0]1
1
= (|0 |0 + |0 |1 + |1 |0 + |1 |1 )(|0 |0 |1 ).
2
Next, the statement “h q[0];” on line eight of Listing 3.1 is to implement one
Hadamard gate of the second time slot of the quantum circuit in Fig. 3.2. The state-
√1 √1 0 √1 1
ment “h q[0];” actually completes 12 2 × = 2 = √12 =
√ − √1 1 − 2 √1
−1
2 2
√1
1 0
2
− = √12 (|0 − |1 ). This indicates that converting q[0] from one
0 1
state |1 to another state √12 (|0 − |1 ) (its superposition) is completed. Because
in the second time slot of the quantum circuit in Fig. 3.2 there is no quantum gate
to act on quantum bits q[4] through q[1], their current states are not changed. This
indicates that we obtain the following new state vector
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1
|2 =
2
1
q[2]0 q[1]0 √ q[0]0 −q[0]1
2
1
= (|0 |0 + |0 |1 + |1 |0 + |1 |1 )
2
1
(|0 |0 ) √ (|0 − |1 ) .
2
q[4]0 q[3]0 encodes Boolean variable x 0 and
In the new state vector | 2 , state 1
Boolean variable x02 . State q[4]0 q[3]1 encodes Boolean variable x 11 and Boolean
variables x 02 . State q[4]1 q[3]0 encodes Boolean variable x 01 and Boolean variable
x 12 . State q[4]1 q[3]1 encodes Boolean variable x 11 and Boolean variable x 12 . The
118 3 Quantum Search Algorithm and Its Applications
amplitude of each choice is √1 and the cost (the successful possibility) of becoming
22
1
the answer(s) to each choice is the same and is equal to 22
= 1/4.
The Oracle is to have the ability to recognize solutions to the given oracular function
O f = F(x1 , x2 ) = x1 ∧ x2 of the satisfiability problem. The Oracle is to multiply
the probability amplitude of the answer(s) by −1 and leaves any other amplitude
unchanged. The Oracle of solving the satisfiability problem with the given orac-
2 2
ular
⎛ function O ⎞f = F(x1 , x2 ) = x1 ∧ x2 is a (2 × 2 ) matrix B that is equal to
10 00
⎜ 01 00 ⎟
⎜ ⎟
⎜ ⎟
⎝ 00 10 ⎠
0 0 0 −1 4×4
We assume that a (22 × 22 ) matrix B+ is the conjugate transpose of B. Because
the transpose of B is equal to B and each element in the transpose of B is a
real, the conjugate transpose of B is also equal to B. Hence, we obtain B+ = B.
Because B and B+ are almost a (22 × 22 ) identity matrix, B × Bm+ = I22 ×22 , and
B + × B = I22 ×22 . Thus, we obtain B × B + = B + × B. This is to say that it is
a unitary matrix (operator) to solve the satisfiability problem with the given orac-
ular function O f = F(x1 , x2 ) = x1 ∧ x2 . Implementing the given oracular function
O f = F(x1 , x2 ) =⎛x1 ∧ x2 in the ⎞ satisfiability problem is equivalent to implement
10 00
⎜ 01 00 ⎟
⎜ ⎟
the Oracle that is ⎜ ⎟ ×
⎝ 00 10 ⎠
0 0 0 −1 4×4
⎛ ⎞ ⎛ ⎞
√1 √1 ⎞ ⎛⎛ ⎞
22 22 1 0
√1 √1
⎜ ⎟ ⎜ ⎟
⎜
22
⎟ ⎜
22
⎟1 ⎜ 0 ⎟ 1 ⎜ 1 ⎟
⎜ ⎟ =⎜ = √ ×⎜
⎟ ⎟ + √ ×⎜ ⎟
⎜ √1 ⎟ ⎜ √1 ⎟
22 ⎝ 0 ⎠ 22 ⎝ 0 ⎠
⎝ 22 ⎠ ⎝ 22 ⎠
√1 −1
√ 0 4×1 0 4×1
22 4×1 22
4×1
⎛ ⎞ ⎛ ⎞
0 0
1 ⎜ 0 ⎟ −1 ⎜ 0 ⎟
+ √ ×⎜ ⎟ + √ ×⎜ ⎟
22 ⎝ 1 ⎠ 22 ⎝ 0 ⎠
0 4×1 1 4×1
1 1 1 −1
= √ |00 + √ |01 + √ |10 + √ |11 .
2 2 2 2 2 2 22
3.2 Introduction to the Satisfiability Problem 119
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
1 0 0
⎜ 0 ⎟ ⎜ 1 ⎟ ⎜ 0 ⎟
Four computational basis vectors ⎜ ⎝ 0 ⎠ ,⎝ 0 ⎠ , ⎝ 1 ⎠
⎟ ⎜ ⎟ ⎜ ⎟ and
0 4×1 0 4×1 0 4×1
⎛ ⎞
0
⎜ 0 ⎟
⎜ ⎟
⎝ 0 ⎠ encode four states |00 , |01 , |10 and |11 and their current amplitudes
1 4×1
are respectively √1 2 , √1 2 , √1 2 and √−12 . State |00 q[4]0 q[3]0 with
2 2 2 2
the amplitude √1 2 encodes Boolean variable x 01 and Boolean variable x 02 . State
2
|01 q[4] q[3] with the amplitude √1 2 encodes Boolean variable x 11 and
0 1
2
Boolean variable x 2 . State |10 q[4] q[3] with the amplitude √1 2 encodes
1 0
0
2
|11 q[4]1 q[3]1 with the
0 1
Boolean variable
x 1 and Boolean variable x 2 . State
amplitude √−12 encodes Boolean variable x 11 and Boolean variable x 12 . This is to
2
say that the Oracle multiplies the probability amplitude of the answer with Boolean
variable x 11 and Boolean variable x 12 by −1 and leaves any other amplitude unchanged.
1
q[4]0 q[3]0 q[2]0 +q[4]0 q[3]1 q[2]0 + q[4]1 q[3]0 q[2]0
|3 =
2
1
+ q[4]1 q[3]1 q[2]1 q[1]0 √ q[0]0 − q[0]1
2
1
= (|0 |0 |0 + |0 |1 |0 + |1 |0 |0 + |1 |1 |1 )
2
Fig. 3.3 The quantum circuit of implementing the Oracle to an instance of the satisfiability problem
with F(x1 , x2 ) = x1 ∧ x2
3.2 Introduction to the Satisfiability Problem 121
1
(|0 ) √ (|0 − |1 ) .
2
Next, from line twenty-four in Listing 3.1, the statement “cx q[2],q[0];” takes the
new state |3 as its input. It multiplies the probability amplitude of the answer.
vector
q[4]1 q[3]1 encoding x 1 x 1 by −1 and leaves any other amplitude unchanged.
1 2
This is to say that after the statement “cx q[2],q[0];” implements the CNOT gate
in the twelfth time slot in Fig. 3.3, we obtain the following new state vector.
1
q[4]0 q[3]0 q[2]0 + q[4]0 q[3]1 q[2]0 + q[4]1 q[3]0 q[2]0
|4 =
2
1
+ (−1)q[4]1 q[3]1 q[2]1 )) |q[1]0 √ q[0]0 − q[0]1
2
1
= (|0 |0 |0 + |0 |1 |0 + |1 |0 |0 + (−1)|1 |1 |1 )
2
1
(|0 ) √ (|0 − |1 ) .
2
Because quantum operations are reversible by nature, executing the reversed order
of implementing the CCNOT gate can restore the auxiliary quantum bits to their
initial states. From line twenty-five through line thirty-nine in Listing 3.1, there are
the fifteen statements. They are “cx q[3],q[4];”, “tdg q[4];”, “t q[3];”, “h q[2];”, “cx
q[3],q[4];”, “t q[2];”, “t q[4];”, “cx q[3],q[2];”, “tdg q[2];”, “cx q[4],q[2];”, “t q[2];”,
“cx q[3],q[2];”, “tdg q[2];”, “cx q[4],q[2];” and “h q[2];”. They run the reversed
order of implementing the CCNOT gate that completes the given oracular function
O f = F(x1 , x2 ) = x1 ∧ x2 . They take the new state vector |4 as their input. After
they actually complete six CNOT gates, two Hadamard gates, three T + gates and
four T gates from the thirteenth time slot through the last time slot in Fig. 3.3, we
obtain the following new state vector
1
q[4]0 q[3]0 +q[4]0 q[3]1 + q[4]1 q[3]0
|5 =
2
1
+ (−1)g[4]1 q[3]1 )) q[2]0 q[1]0 √ q[0]0 − q[0]1
2
122 3 Quantum Search Algorithm and Its Applications
1 1
= (|0 |0 + |0 |1 + |1 |0 + (−1)|1 |1 ) (|0 |0 ) √ (|0 − |1 ) .
2 2
In the state vector |2 , the amplitude of each element in solution space
{x1 x2 ∀xd ∈ {0, 1} for 1 ≤ d ≤ 2} is (1/2). In the state vector |5 , the amplitude
to three elements x 01 x 02 , x 01 x 12 , x 11 x 02 in solution space is all (1/2) and the amplitude
to the element x 11 x 12 in solution space is (−1/2). This indicates that ⎛ thirty-one state-
⎞
10 00
⎜ 01 00 ⎟
⎜ ⎟
ments from line nine through thirty-nine in Listing 3.1 complete ⎜ ⎟
⎝ 00 10 ⎠
0 0 0 −1 4×4
⎛ ⎞
√1
22
√1
⎜ ⎟
⎜ ⎟
×⎜ 22 ⎟ that is to complete the Oracle of solving the satisfiability problem
⎜ √1 ⎟
⎝ 22 ⎠
√1
22 4×1
with the given oracular function O f = F(x1 , x2 ) = x1 ∧ x2 .
3.2 Introduction to the Satisfiability Problem 123
u 2n ,1 2n ×1
⎛ ⎞
u 1,1 × v1,1 · · · u 1,1 × v1,2n
=⎝
⎜ .. .. .. ⎟
.
. . . ⎠
u 2n ,1 × v1,1 · · · u 2n ,1 × v1,2n 2n ×2n
We assumethat H ⊗n represents n Hadamard gates and 0⊗n represents n quantum
bits in which the value of each quantum bit is equal to |0 . After we use a unitary
operator H ⊗n to operate n quantum bits, 0⊗n , the state |0 of each quantum bit is
converted into its superposition √12 (|0 + |1 ). This is to say that the superposition
2n −1
to n quantum bits with 0⊗n is |ψ = ( ⊗nk=1 √12 (|0 + |1 ) = √12n k=0 |k =
⎛⎛ ⎞ ⎞ ⎛ 1 ⎞
⎛ ⎞ √
1 0 2n
⎜⎜ ⎟
0 ⎜ 0⎟ ⎟ ⎜ √1 ⎟
⎟ = ⎜ 2n ⎟
⎜ ⎟ ⎜ ⎟
√1 ⎜⎜ . ⎟ + · · · + . The state vector |ψ is
⎜ ⎟ ⎜ ⎟
. .
⎝⎝ .. ⎠ ⎝ .. ⎠
2 n
⎝ .. ⎠
⎜ ⎜ ⎟ ⎟ ⎜ ⎟
⎠
0 1 √1
2n ×1 2n ×1 2n 2n ×1
the uniform superposition of states, is a (2n × 1) vector and its length is one. This
indicates that it is a unit vector.
124 3 Quantum Search Algorithm and Its Applications
The matrix D that is a (2n × 2n ) matrix defines the Grover diffusion operator D
as follows:
2 2
Da,b = n
if a = b and Da,a = n − 1.
2 2
This
diffusion
transform,
D, can be implemented as D = 2|ψ ψ| − I2 n ×2 n =
n n
H ⊗n 20⊗n 0⊗n − I2n ×2n H ⊗n . The
⊗nrotation
⊗n matrix R that is a (2 × 2 ) matrix
defines a phase shifter operator, 20 0 − I2 n ×2 n , as follows:
Lemma
3.1
The Grover diffusion operator, D = 2|ψ ψ| − I2n ×2n =
H ⊗n 20⊗n 0⊗n − I2n ×2n H ⊗n , is a unitary operator.
Proof The outer product ⎛ |ψ1 ⎞ψ| of the (2n × 1) vector |ψ with itself leads to a (2n
⎛ 1
· · · 21n
√
⎞
2n 2n
. ⎟ = ⎝ ... ... ... ⎠
× √12n · · · √12n
⎜ ⎟
× 2n ) matrix V that is ⎜
⎝ .. ⎠ .
⎜ ⎟
n 2 ×1
√1 1
· · · 21n
2n 2n ×1 2n 2n ×2n
Subtracting the identity matrix I2n ×2n from
⎛ the double⎞of the (2n⎛
× 2 ) matrix n
⎞ V, we
2 2
2n
· · · 2n
1 · · · 0
obtain a new (2n × 2n ) matrix D that is ⎝ ... ... ... ⎠ − ⎝ ... ... ... ⎠ =
⎜ ⎟ ⎜ ⎟
2 2
2n
··· 2n 2n ×2n
0 ··· 1 2n ×2n
2 2
⎛ ⎞
2n
−1 ··· 2n
⎜ .. .. .. ⎟ .
⎝ . . . ⎠
2 2
2n
··· 2n
− 1 2n ×2n
it is at once
inferred Grover diffusion operator, D = 2 |ψ ψ| − I2n ×2n =
that the
H ⊗n 20⊗n 0⊗n − I2n ×2n H ⊗n is a unitary operator.
are independent each other. Amplifying the amplitude of each answer in the satis-
fiability problem with the given oracular function O f = F(x1 , x2 ) = x1 ∧ x2
just needs to consider the first subsystem in the new state vector |5 .
Because for the satisfiability problem with the ⎛ given⎞ oracular function
√1
22
√1
⎜ ⎟
2
⎜ ⎟
Of = F(x1 , x2 ) = x1 ∧ x2 the (2 × 1) vector ⎜ 22 ⎟ encodes the first
⎜ √1 ⎟
⎝ 22 ⎠
−1
√
22 4×1
⎛ 2 2 2 2 ⎞
22
−1 22 22 22
2 2 2 2
⎜
22 22
−1 22 22
⎟
subsystem of the new state vector |5 and ⎜ 2 2 2 2
⎟
⎝
22 22 22
−1 22
⎠
2 2 2 2
22
− 1 22 ×22
22 22 22
2 2
is a (2 × 2 ) diffusion operator, amplifying the amplitude
⎛ ⎞of the answer is to
⎛ 2 2 2 2 ⎞ √1 ⎛ ⎞
22
− 1 22 22 22
2
⎜ 12 ⎟ 0
⎜ 22 2
− 1 2 2 ⎟ ⎜ √ ⎟ ⎜ 0 ⎟
complete ⎜ 2 22 22 22 ⎟ ⎜ 22 ⎟ = ⎜ ⎟ .
⎝ 22 2 2 2 1 ⎟
2 2 2 2 2 − 1 2 2
⎠ ⎜ √
2
⎝ 0 ⎠
⎝ 2 ⎠
2 2 2 2 −1
22 22 22 22
− 1 22 ×22 √ 2 1 4×1
2 4×1
This is to say that the amplitude of the answer x 11 x 12 is one and the amplitude of
other three possible choices x 01 x 02 , x 01 x 12 and x 11 x 02 is all zero.
The
quantum
circuit in Fig. 3.4 implements the Grover diffusion operator,
H ⊗2 20⊗2 0⊗2 − I22 ×22 H ⊗2 . The statements “h q[3];” and “h q[4];”
from line forty through line forty-one in Listing 3.1 complete
the first
H ⊗2 gate in the diffusion operator, H ⊗2 20⊗2 0⊗2 − I22 ×22 H ⊗2 . They
1
takes q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + (−1)q[4]1 q[3]1
2
as their input and complete two Hadamard gates in the first time
1
0 0
slot of Fig. 3.4. State 2
q[4] q[3] is converted into state
126 3 Quantum Search Algorithm and Its Applications
Fig.
⊗23.4
⊗2The
quantum circuit of implementing the Grover diffusion operator, H ⊗2
0 − I22 ×22 H ⊗2 , to an instance of the satisfiability problem with F(x 1 , x 2 ) = x 1 ∧
2 0
x2
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1 . State
4 1 1
q[4]0 q[3]1 is converted into state q[4] 0
q[3] 0
− q[4]0 q[3]1
2 4
+q[4]1 q[3]0 − q[4]1 q[3]1 . State 21 q[4]1 q[3]0 is converted into
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + (−1)q[4]1 q[3]1 .
|6 =
2
Next, from line forty-two through forty-three in Listing 3.1 the two statements
“x q[3]” and “x q[4]” twoNOT gates
implement in the
second
time slot ofFig. 3.4.
They take 21 q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + (−1)
q[4]1 q[3]1
1
in the new state vector |6 > as their input. State 2 q[4]0 q[3]0 is
converted into state 21 q[4]1 q[3]1 State 21 q[4]0 q[3]1 is converted into state
1
q[4]1 q[3]0 . State 21 q[4]1 q[3]0 is converted into state 21 q[4]0 q[3]1 .
2
State − 21 q[4]1 q[3]1 is converted into state − 21 q[4]0 q[3]0 . This indicates
1
(−1)q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1 .
|7 =
2
3.2 Introduction to the Satisfiability Problem 127
Next, from line forty-four in Listing 3.1 the statement “h q[4];” imple-
ments
1 one 0Hadamard
gate in the third
time
slot 3.4. They take
of Fig.
(−1) q[4] q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1 in the new
2 1
q[4]0 q[3]0 is converted into state
state vector |7 as their input. State − 2
− 2√1 2 q[4]0 q[3]0 + q[4]1 q[3]0 . State 21 q[4]0 q[3]1 is converted into state
1
q[4]0 q[3]1 + q[4]1 q[3]1 . State 21 q[4]1 q[3]0 is converted into state
√
2 2
1
q[4]0 q[3]0 − q[4]1 q[3]0 . State 21 q[4]1 q[3]1 is converted into state
√
2 2
1
√
2 2
q[4]0 q[3]1 − q[4]1 q[3]1 . This is to say that we obtain the following
newstate vector
1
√ q[4]0 q[3]1 − q[4]1 q[3]0 .
|8 =
2
Next, from the line forty-five in Listing 3.1 the statement “cx q[3],q[4];”
implements one CNOT
gate in the
fourth time slot of Fig. 3.4. They
take √12 q[4]0 q[3]1 − q[4]1 q[3]0 in the new state vector |8 as their
input. State √12 q[4]0 q[3]1 is converted into state √12 q[4]1 q[3]1 . State
− √12 q[4]1 q[3]0 is converted into state − √12 q[4]1 q[3]0 . This indicates that
1
√ q[4]1 q[3]1 − q[4]1 q[3]0 .
|9 =
2
Next, from the line forty-six in Listing 3.1 the statement “h q[4];” imple-
one Hadamard
ments
gate in the
fifth time slot of Fig. 3.4. They take
1 1 1 1
0
√ q[4] q[3] − q[4] q[3]
in the new state vector |9 as their input. State
2
√1 q[4]1 q[3]1 1
0 1
2
is converted into state 2
q[4] q[3] − q[4]1 q[3]1 . State
− √12 q[4]1 q[3]0 is converted into state − 12 q[4]0 q[3]0 − q[4]1 q[3]0 . This
1
(−1)q[4]0 q[3]0 + q[4]0 q[3]1
|10 =
2
+q[4]1 q[3]0 + (−1)q[4]1 q[3]1 .
Next, from the line forty-seven through line forty-eight in Listing 3.1 the
two statements “x q[4];” and “x q[3];” implements X (NOT)
two gates in
the sixth time slot of Fig. 3.4. They take 21 (−1)q[4]0 q[3]0 + q[4]0 q[3]1
+q[4]1 q[3]0 + (−1)q[4]1 q[3]1 . in the new state vector |10 as their
1
q[4]0 q[3]0 is converted into state − 1 q[4]1 q[3]1 . State
input. State − 2 2
1
q[4]0 q[3]1 is converted into state 1 q[4]1 q[3]0 . State 1 q[4]1 q[3]0 is
2 1 2 2
converted into state 2 q[4]0 q[3]1 . State − 21 q[4]1 q[3]1 is converted into state
1
− 2 q[4]0 q[3]0 . This is to say that we obtain the following new state vector.
1
(−1)q[4]0 q[3]0 + q[4]0 q[3]1
|11 =
2
+q[4]1 q[3]0 + (−1)q[4]1 q[3]1 .
Next, from line forty-nine in Listing 3.1, the statement “u3(2*pi,0*pi,0*pi) q[3];”
−1 0
completes one u3(2*pi,0*pi,0*pi) gate that is a (2 × 2) matrix in
0 −1 2×2
1
the seventh time slot of Fig. 3.4. It takes (−1) q[4]0 q[3]0 + q[4]0 q[3]1
2
+q[4]1 q[3]0 + (−1)q[4]1 q[3]1 in the new state vector |11 as its input. State
3.2 Introduction to the Satisfiability Problem 129
− 21 q[4]0 q[3] 0
one phase (−1). State 21 q[4]0 q[3] 1
receives
receives one phase
(−1). State 21 q[4]1 q[3]0 receives one phase (−1). State 21 q[4]1 q[3]1 receives
one phase (−1). This indicates that we obtain the following new state vector
1
(−1 × −1)q[4]0 q[3]0 + (−1)q[4]0 q[3]1 + (−1)q[4]1 q[3]0
|12 =
2
+(−1 × −1)q[4]1 q[3]1
1
= q[4]0 q[3]0 + (−1)q[4]0 q[3]1
2
+(−1)q[4]1 q[3]0 + q[4]1 q[3]1 .
Fig. 3.5 After the measurement to solve an instance of the satisfiability problem with F(x 1 , x 2 ) =
x 1 ∧ x 2 is completed, we obtain the answer 11,000 with the probability 1 (100%)
Next, from line fifty-two in Listing 3.1 the statement “measure q[4] -> c[4];”
is to measure the fifth quantum bit q[4] and to record the measurement outcome
by overwriting the fifth classical bit c[4]. From line fifty-three in Listing 3.1 the
statement “measure q[3] -> c[3];” is to measure the fourth quantum bit q[3] and to
record the measurement outcome by overwriting the fourth classical bit c[3]. They
complete the measurement from the ninth time slot through the tenth time slot of
Fig. 3.4.
In the backend ibmqx4 with five quantum bits in IBM’s quantum computers, we
use the command “simulate” to execute the program in Listing 3.1 . The measured
result appears in Fig. 3.5. From Fig. 3.5, we obtain the answer 11,000 (c[4] = 1
= q[4] = |1 , c[3] = 1 = q[3] = |1 , c[2] = 0, c[1] = 0 and c[0] = 0) with the
probability 1 (100%). This is to say that with the possibility 1 (100%) we obtain
that the value of quantum bit q[3] is |1 and the value of quantum bit q[4] is |1 .
For solving an instance of the satisfiability problem with F(x 1 , x 2 ) = x 1 ∧ x 2 we use
quantum bit q[3] to encode Boolean variable x 1 and use quantum bit q[4] to encode
Boolean variable x 2 . Therefore, the answer is to that the value of Boolean variable
x 1 is 1 (one) and the value of Boolean variable x 2 is 1 (one).
A satisfiability problem has n Boolean variables and any given oracular function
Of (x 1 x 2 … x n − 1 x n ). Any given oracular function Of (x 1 x 2 … x n − 1 x n ) is a Boolean
3.2 Introduction to the Satisfiability Problem 131
Fig. 3.6 Circuit of implementing the quantum search algorithm to solve an instance of the
satisfiability problem with n Boolean variables and m clauses
132 3 Quantum Search Algorithm and Its Applications
In Fig. 3.6, the first stage of the quantum search problem to solve an instance
of the satisfiability problem with n Boolean variables and m clauses
is to use n
Hadamard gates H ⊗n to operate the first quantum register 0⊗n . This indicates
|x +|x 1
0
that it generates the superposition of n quantum bits that is (⊗nk=1 k √2 k ) =
√1 |x 1 x 2 . . . x n . Solution space of solving an instance
2 n {x 1 x 2 ...x n |∀x d ∈{0,1}for1≤d≤n}
of the satisfiability problem with n Boolean variables and m clauses is {x 1 x 2 …
x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n}. In the first stage of the quantum search
algorithm, state (x10 x20 · · · xn0 ) with the amplitude ( √12n ) encodes the first element x 01
x 0 … x 0 in solution space and so on with that state (x 1 x 1 · · · x 1 ) with the amplitude
2 n 1 2 n
( √12n ) encodes the last element x 11 x 12 … x 1n in solution space. In the first stage of the
quantum search algorithm, it uses one Hadamard gate to operate the third quantum
register (|1 ). This is to say that it generates the superposition of the third quantum
register (|1 ) that is ( √12 (|0 |1 )).
In Fig. 3.6, the second stage of the quantum search algorithm to solve an instance of
the satisfiability problem with n Boolean variables and m clauses is to complete the
Oracle. The Oracle is to have the ability to recognize solutions in the satisfiability
problem with n Boolean variables and m clauses. The Oracle is to multiply the prob-
ability amplitude of the answer(s) by − 1 and leaves any other amplitude unchanged.
Any given oracular function Of (x 1 x 2 … x n − 1 x n ) is a Boolean formula of the form
C 1 ∧ C 2 … ∧ C m . Each clause C j for 1 ≤ j ≤ m is a formula of the form x 1 ∨ x 2 ∨ …
x n − 1 ∨ x n for each Boolean variable x k to 1 ≤ k ≤ n. Therefore, for implementing
any given oracular function Of (x 1 x 2 … x n − 1 x n ), we need to complete (m × n)
logical or operations and (m) logical and operations.
In oracle workspace in the second stage of the quantum search algorithm, we use
auxiliary quantum bits r j, k for 1 ≤ j ≤ m and 0 ≤ k ≤ n to encode auxiliary
Boolean
variables r j, k for 1 ≤ j ≤ m and 0 ≤ k ≤ n. We use auxiliary quantum bits s j for 0 ≤ j
≤ m to encode auxiliary
Boolean variables sj for 0 ≤ j ≤ m. Since we use auxiliary
quantum bits r j, 0 for 1 ≤ j ≤ m as the first operand of the first logical
oroperation
(“∨”) in each clause, the initial value of each auxiliary quantum bit r j, 0 for 1 ≤ j
≤ m is set to |0 . This implies that this setting does not change the correct result of the
first logical or operation in each clause. We use a CCNOT gate and four NOT gates
to implement
each logical or operation in each clause. We apply auxiliary quantum
bits r j, k for 1 ≤ j ≤ m and 1 ≤ k ≤ n to store the result of implementing the logical
3.2 Introduction to the Satisfiability Problem 133
or operations in each clause. This is to say that each auxiliary quantum bit r j, k for
1 ≤ j ≤ m and 1 ≤ k ≤ n is actually the target bit of a CCNOT gate of implementing
a logical or operation. Thus, the initial value of each auxiliary quantum bit r j, k for
1 ≤ j ≤ m and 1 ≤ k ≤ n is set to |1 .
We use an auxiliary quantum bit |s0 as the first operand of the first logical and
operation (“∧”) in any given oracular function Of (x 1 x 2 … x n − 1 x n ) with a Boolean
formula of the form C 1 ∧ C 2 … ∧ C m . The initial value of the auxiliary quantum bit
|s0 is set to |1 . This is to say that this setting does not change the correct result of the
first logical and operation in any given oracular function Of (x 1 x 2 … x n − 1 x n ) with a
Boolean formula of the form C 1 ∧ C 2 … ∧ C m . We use a CCNOT gate to implement
each logical and operation in any given oracular function Of (x 1 x 2 … x n − 1 x n ) with
a Boolean
formula of the form C 1 ∧ C 2 … ∧ C m . We apply auxiliary quantum bits
s j for 1 ≤ j ≤ m to store the result of implementing the logical and operations in any
given oracular function Of (x 1 x 2 … x n − 1 x n ) with a Boolean formula of the form
C 1 ∧ C 2 … ∧ C m . This indicates that each auxiliary quantum bit s j for 1 ≤ j ≤ m
is actually the target bit of a CCNOT gate of implementing a logical and operation.
Therefore, the initial value of each auxiliary quantum bit s j for 1 ≤ j ≤ m is set to
|0 .
A CCNOT gate and four NOT gate can implement a logical or operation. A
CCNOT gate can implement a logical and operation. From Fig. 3.1, implementing
any given oracular function Of (x 1 x 2 … x n − 1 x n ) is to complete (m × n) logical or
operations and (m) logical and operations. This is to say that implementing any given
oracular function Of (x 1 x 2 … x n − 1 x n ) is to complete (m × n + m) CCNOT gates
and (4 × m × n) NOT gates. Quantum bit |sm > is to store the result of implementing
any given oracular function Of (x 1 x 2 … x n − 1 x n ). If the value to quantum bit |sm is
equal to |1 , then any given oracular function Of (x 1 x 2 … x n − 1 x n ) has the value 1
(one). Otherwise, it has thevalue 0 (zero).
We use one CNOT gate |0 √−|1 ⊕ sm to multiply the probability amplitude of the
2
answer(s)
by − 1 and to leave any other amplitude unchanged, where quantum bit
|0 √−|1
2
is the target bit of the CNOT gate and quantum bit (|sm ) is the control bit
of the CNOTgate. When the value
ofthe control bit (|sm )is equal to (|1 ), the target
|1 √
−|0 |0 √
−|1
bit becomes 2
= (−1) 2
. This is to multiply the probability amplitude
of the answer(s) by −1. When the value of the control bit (|sm ) is equal to (|0 > ),
|0 √
−|1
the target bit still is 2
. This is to leave any other amplitude unchanged.
Because quantum operations are reversible by nature, executing the reversed order
of implementing any given oracular function Of (x 1 x 2 … x n − 1 x n ) can restore the
auxiliary quantum bits to their initial states. This is to say that the second stage
of the quantum search algorithm to solve the satisfiability problem with n Boolean
variables and m clauses converts √12n |x
{x1 x2 ...xn |∀xd ∈{0,1}for1≤d≤n} 1 2x . . . x n into
(−1) O f (x1 ···xn ) √12n {x1 x2 ...xn |∀xd ∈{0,1}for1≤d≤n} |x 1 x 2 . . . x n . The cost of completing
the Oracle in the second stage of the quantum search algorithm in Fig. 3.6 is to
134 3 Quantum Search Algorithm and Its Applications
In Fig. 3.6, thethird stage of the quantum search algorithm to solve an instance of
the satisfiability problem with n Boolean variables and m clauses is to complete
the Grover diffusion operator. Implementing
⊗n ⊗n the
Grover diffusion operator is
⊗n I2n ×2n H ⊗n . A phase shifter operator,
equivalent
⊗n ⊗n to implement
H 2 0 0
2 0 0 − I2n ×2n negates all the states except for (x10 . . . xn0 ). In Fig. 3.6,
the third
stage
of the quantum search problem is to use that the phase shift oper-
ator, 20⊗n 0⊗n − I2n ×2n , negates all the states except for x10 · · · xn0 sandwiched
between H ⊗n gates. This indicates that the third stage of the quantum search algo-
rithm to solve an instance of the satisfiability problem with n Boolean variables and
m clauses is to increase the amplitude of the answer(s) and to decrease the amplitude
of the non-answer(s).
For solving an instance of the satisfiability problem with n Boolean variables
and m clauses, we regard the Oracle and the Grover diffusion operator from the
second stage through the third stage of the quantum search algorithm in Fig. 3.6 as
a subroutine. We call the subroutine
as the Grover iteration. After repeat to execute
N
the Grover iteration of O M
times, the successful probability of measuring the
answer(s) is at least (1/2). When the value of (M/N) is equal to (1/4), the successful
probability of measuring the answer(s) is one (100%) with the Grover iteration of
one time. This is the best case of the quantum search algorithm to solve an instance
of the satisfiability problem with n Boolean variables and m clauses. When the value
of M is equal to one, the successful √ probability of measuring the answer(s) is at least
(1/2) with the Grover iteration of O( N ) times. This is the worst case of the quantum
search algorithm to solve an instance of the satisfiability problem with n Boolean
variables and m clauses. This indicates that the quantum search algorithm to solve
an instance of the satisfiability problem with n Boolean variables and m clauses only
gives a quadratic speed-up.
We assume that a graph G = (V, E) has n vertices and θ edges, where V is a set of
vertices in G and E is a set of edges in G. For a graph G = (V, E) with n vertices and θ
edges, its complementary graph is to contain all edges missing in the original graph
G = (V, E) with n vertices and θ edges. Therefore, we assume that its complementary
graph G = (V, E) has n vertices and m edges in which each edge in E is out of E,
3.3 Introduction to the Maximal Clique Problem 135
Fig. 3.7 a The graph has three vertices and two edges. b Its complementary graph has the same
vertices and one edge missing in the original graph
(v1 , v3 )} in Fig. 3.7a. Because the quantum program of implementing this example
uses more quantum bits that exceed five quantum bits in the backend ibmqx4 with
five quantum bits in IBM’s quantum computers, we just use this example to explain
what the maximal clique problem is. Next, we give Definition 3.3 to describe the
maximal clique problem.
Definition 3.3 Mathematically, a clique for a graph G = (V, E) with n vertices and
θ edges is defined as a set of vertices in which every vertex is connected to every
other vertex by an edge. The maximal clique problem to a graph G = (V, E) with n
vertices and θ edges is to find a maximum-sized clique in the graph G = (V, E) with
n vertices and θ edges.
From Definition 3.3, all of the possible solutions to the clique problem of graph
G with n vertices and θ edges consist of 2n possible choices. Every possible choice
corresponds to a subset of vertices (a possible clique in G). Hence, we assume that a
set X contains 2n possible choices and the set X is equal to {x 1 x 2 … x n − 1 x n | ∀ x d
∈ {0, 1} for 1 ≤ d ≤ n}. This is to say that the length of each element in {x 1 x 2 …
x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n} is n bits and every element stands for one of
2n possible choices.
For the sake of presentation, we suppose that x 0d denotes the fact that the value of
x d is zero and x 1d denotes the fact that the value of x d is one. If an element x 1 x 2 …
x n − 1 x n in {x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n} is a clique and the value
of x d for 1 ≤ d ≤ n is one, then x 1d represents that the dth vertex is within the clique.
If an element x 1 x 2 … x n − 1 x n in {x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n}
is a clique and the value of x d for 1 ≤ d ≤ n is zero, then x 0d stands for that the dth
vertex is not within the clique.
From {x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n}, the first element x 01 x 02 …
0
x n − 1 x 0n encodes an empty set of vertices that is {}. The second element x 01 x 02 …
x 0n − 1 x 1n encodes a set of vertices {vn }. The third element x 01 x 02 … x 1n − 1 x 0n encodes a
set of vertices {vn − 1 } and so on with that the last element x 11 x 12 … x 1n − 1 x 1n encodes
a set of vertices {v1 v2 … vn − 1 vn }. We regard {x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1}
for 1 ≤ d ≤ n} as an unsorted database containing 2n possible choices (2n possible
cliques) to the maximal clique problem of graph G with n vertices and θ edges.
From Definition 3.3, solving the clique problem for a graph G = (V, E) with n
vertices and θ edges and its complementary graph G = (V, E) with n vertices and m
edges in which each edge is out of E is to find a subset V 1 of vertices with size r that
satisfies V 1 to be a maximum-sized clique in G. This indicates that all of the possible
solutions are 2n subset of vertices in which each subset of vertices corresponds to a
possible clique. Any two vertices connected in the complementary graph G = (V,
3.3 Introduction to the Maximal Clique Problem 137
E) with n vertices and m edges are disconnected in the original graph with G = (V,
E) with n vertices and θ edges. This is to say that any two vertices connected in the
complementary graph cannot be members of the same clique.
If any one of 2n possible choices (cliques) does not include any edge in the
complementary graph G, then it is a clique in the original graph G. Otherwise, it is
not a clique in the original graph G. Boolean variables x i and x j encode vertices vi
and vj for 1 ≤ i ≤ n and 1 ≤ j ≤ n. In the complementary graph G, the kth edge
is ek = (vi , vj ) to 1 ≤ k ≤ m. The requested condition of deciding whether any
one of 2n subsets of vertices does notinclude the kth edge ek = (vi, vj ) or not
is to
satisfy a formula of the form xi ∧ x j that is the true value,
where x i ∧ x j is one
NAND gate. We regard a formula of the form xi ∧ x j as a clause. When the value
of Boolean variable x i is 1 (one) and the value of Boolean variable x j is 1 (one),
the output (result) of implementing xi ∧ x j is 0 (zero). This indicates that each
possible choice containing the two vertices vi and vj is not a clique. When the value
of Boolean variable x i is 1 (one)and the value of Boolean variable x j is 0 (zero), the
output (result) of implementing xi ∧ x j is 1 (one). This is to say that each possible
choice consisting of vertex vi and not containing vertex vj is perhaps a clique. When
the value of Boolean variable x i is 0 (zero) and the value
of Boolean variable x j is
1 (one), the output (result) of implementing xi ∧ x j is 1 (one). This implies that
each possible choice consisting of vertex vj and not containing vertex vi is perhaps
a clique. When the value of Boolean variable x i is 0 (zero) and the value of Boolean
variable x j is 0 (zero), the output (result) of implementing xi ∧ x j is 1 (one). This
indicates that each possible choice not containing vertex vi and vertex vj is perhaps
a clique.
The requested condition of checking whether any one of 2n subsets of vertices
does not include m edges in the complementary graph or not is to satisfy a formula
of theform (∧k=1 m x i ∧ x j ) that is the true value. We regard a formula of the form
(∧k=1
m x i ∧ x j ) as a Boolean formula of the form C 1 ∧ C 2 … ∧ C m , where each
clause C j for 1 ≤ j ≤ m is a formula of the form xi ∧ x j to Boolean variables x i
and x j for 1 ≤ i ≤ n and 1 ≤ j ≤ n. Any one of 2n subsets of vertices is a clique if
finding values of each Boolean variable satisfy the whole formula has the value 1
(one). This is the same as finding values of each Boolean variable that make each
clause have the value 1 (one).
Recognizing
clique(s) is equivalent to implement a formula of the form
(∧k=1
m xi ∧ x j ). Therefore, we need to make use of auxiliary Boolean variables
r k for 1 ≤ k ≤ m and auxiliary Boolean variables sk for 0 ≤ k ≤ m. We use CCNOT
gates to implement the only NAND gate (xi ∧ x j ) in each clause and we apply auxil-
iary Boolean variables r k for 1 ≤ k ≤ m to store the result of implementing the
only NAND gate (xi ∧ x j ) in each clause. This is to say that each auxiliary Boolean
variable r k for 1 ≤ k ≤ m is actually the target bit of a CCNOT gate of implementing
a NAND gate (xi ∧ x j ). Hence, the initial value of each auxiliary Boolean variable
r k for 1 ≤ k ≤ m is set to one (1).
We make use of an auxiliary Boolean variable s0 as the first operand of the
first
logical and operation (“∧”) in a Boolean formula of the form (∧k=1 m xi ∧ x j ). The
initial value of the auxiliary Boolean variable s0 is set to one (1). This is to say that
138 3 Quantum Search Algorithm and Its Applications
this setting
does not change the correct result of the first logical and operation in
(∧k=1
m i x ∧ x j ). We apply CCNOT gates to implement the logical and operations in
(∧k=1
m x i ∧ x j ) and we use auxiliary Boolean variables sk
for 1 ≤ k ≤ m to store the
result of implementing the logical and operations in (∧k=1 m xi ∧ x j ). This indicates
that each auxiliary Boolean variable sk for 1 ≤ k ≤ m is actually the target bit of a
CCNOT gate of implementing a logical and operation. Therefore, the initial value
of each auxiliary Boolean variable sk for 1 ≤ k ≤ m is set to zero (0).
Figure 3.8 is to flowchart of recognizing cliques to the maximal clique problem
for a graph G = (V, E) with n vertices and θ edges and its complementary graph
G = (V, E) with n vertices and m edges. In Fig. 3.8, in statement S 1 , it sets the
index variable k of the first loop to one (1). Next, in statement S 2 , it executes the
conditional judgement of the first loop. If the value of k is less than or equal to the
value of m, then next executed instruction is statement S 3 . Otherwise, in statement
S 6 , it executes an End instruction to terminate the task that is to find values of each
Boolean variable so that the whole formula has the value 1 and this is the same as
finding values of each Boolean variable that make each clause have the value 1.
In statement S 3 , it implements a NAND gate “r k ← xi ∧ x j ”. Boolean variables
x i and x j respectively encode vertex vi and vertex vj that are connected by the kth
edge in the complementary graph G = (V,E) with n vertices and m edges. Boolean
Fig. 3.8 Flowchart of recognizing cliques to the maximal clique problem for a graph G = (V, E)
with n vertices and θ edges and its complementary graph G = (V, E) with n vertices and m edges
3.3 Introduction to the Maximal Clique Problem 139
variable r k stores the result of implementing ( xi ∧ x j ) (the kth NAND gate). Next,
in statement S 4 , it executes
a logical and operation “sk ← r k ∧ sk − 1 ” that is the kth
clause in (∧k=1
m x i ∧ x j ). Boolean variable r k stores the result of implementing the
kth NAND gate and is the first operand of the logical and operation. Boolean variable
sk − 1 is the second operand of the logical and operation and stores the result of the
previous logical and operation. Boolean variable sk for 1 ≤ k ≤ m stores the result
of implementing r k ∧ sk − 1 that is the kth clause that is the kth AND gate. Next, in
statement S 5 , it increases the value of the index variable k to the first loop. Repeat
to execute statement S 2 through statement S 5 until in statement S 2 the conditional
judgement becomes a false value. From Fig. 3.8, the total number of NAND gate is m
NAND gates. The total number of logical and operation is m AND gates (logical and
operations). Therefore, the cost of recognizing clique(s) is to implement m NAND
gates and m AND gates.
For a graph G = (V, E) with n vertices and θ edges and its complementary graph G
= (V, E) with n vertices and m edge, solution space of the maximal clique problem
is 2n subsets of vertices. We use {x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n} to
encode 2n subsets of vertices. After each element encoding one subset of vertices in
{x 1 x 2 … x n − 1 x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n} completes each operation in Fig. 3.8,
Boolean variable sm stores the result of deciding whether it is a clique or not. If the
value of Boolean variable sm is equal to 1 (one), then it is a clique. Otherwise, it is
not a clique.
For computing the number of vertex, we need auxiliary Boolean variables zi+1, j
and zi+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i. Auxiliary Boolean variables zi+1, j and
zi+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i are set to the initial value 0 (zero). Boolean
variable zi+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i is to store the number of vertex in
a clique after figuring out the influence of Boolean variable x i + 1 that encodes the
(i + 1)th vertex to the number of ones (vertices). If the value of Boolean variable
zi+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i is equal to 1 (one), then this indicates that
there are (j + 1) ones (vertices) in the clique. Boolean variable zi+1, j for 0 ≤ i ≤ n
− 1 and 0 ≤ j ≤ i is to store the number of vertex in a clique after figuring out the
influence of Boolean variable x i + 1 that encodes the (i + 1)th vertex to the number
of ones (vertices). If the value of Boolean variable zi+1, j for 0 ≤ i ≤ n − 1 and 0 ≤ j
≤ i is equal to 1 (one), then this is to say that there are j ones (vertices) in the clique.
In a clique, Boolean variable x 1 encodes the first vertex v1 . If the value of Boolean
variable x 1 is equal to 1 (one), then the first vertex v1 is within the clique and it
increases the number of vertex to the clique. If the value of Boolean variable x 1 is
equal to 0 (zero), then the first vertex v1 is not within the clique and it reserves the
number of vertex to the clique. Therefore, the influence of Boolean variable x 1 to
140 3 Quantum Search Algorithm and Its Applications
increase the number of vertex to a clique is to satisfy the formula (sm ∧ x 1 ) that is
the true value. Similarly, the influence of Boolean variable x 1 to reserve the number
of vertex to a clique is to satisfy the formula (sm ∧ x1 ) that is the true value.
In a clique, Boolean variable x i + 1 encodes the (i + 1)th vertex vi + 1 for 1 ≤ i ≤ n
– 1. If the value of Boolean variable x i + 1 is equal to 1 (one), then the (i + 1)th vertex
vi + 1 is within the clique and it increases the number of vertex to the clique. If the
value of Boolean variable x i + 1 is equal to 0 (zero), then the (i + 1)th vertex vi + 1 is
not within the clique and it reserves the number of vertex to the clique. Therefore,
the influence of Boolean variable x i + 1 to increase the number of vertex to a clique
that has currently has j vertices is to satisfy the formula (x i + 1 ∧ zi, j ) that is the true
value. Similarly, the influence of Boolean variable x i + 1 to reserve the number of
vertex to a clique that has currently has j vertices is to satisfy the formula ((xi+1 ) ∧
zi, j ) that is the true value.
Figure 3.9 is the logical flowchart of counting the number of vertex in each clique
to the maximal clique problem for a graph G = (V, E) with n vertices and θ edges
and its complementary graph G = (V,E) with n vertices and m edges. In Fig. 3.9,
in statement S 1 , it implements a logical and operation “z1,1 ← sm ∧ x 1 ” that is one
AND gate. Boolean variable z1, 1 stores the result of implementing one AND gate
(sm ∧ x 1 ). If the value of Boolean variable z1, 1 is equal to 1 (one), then it increases
the number of vertex so that the number of vertex in each clique with the first vertex
v1 is one. Next, in statement S 2 , it implements a logical and operation “z1,0 ← sm
∧ x1 ” that is one AND gate (sm ∧ x1 ). Boolean variable z1, 0 stores the result of
implementing one AND gate (sm ∧ x1 ). If the value of Boolean variable z1, 0 is equal
to 1 (one), then it reserves the number of vertex so that the number of vertex in each
clique without the first vertex v1 is zero.
Next, in statement S 3 , it sets the index variable i of the first loop to one. Next,
in statement S 4 , it executes the conditional judgement of the first loop. If the value
of i is less than or equal to the value of (n − 1), then next executed instruction is
statement S 5 . Otherwise, in statement S 11 , it executes an End instruction to terminate
the task that is to count the number of vertex in each clique. In statement S 5 , it sets
the index variable j of the second loop to the value of the index variable i in the
first loop. Next, in statement S 6 , it executes the conditional judgement of the second
loop. If the value of j is greater than or equal to zero, then next executed instruction
is statement S 7 . Otherwise, next executed instruction is statement S 10 .
In statement S 7 , it implements a logical and operation “zi+1, j+1 ← x i+1 ∧ zi, j ” that
is one AND gate. Boolean variable x i+1 encodes the (i + 1)th vertex and is the first
operand of the logical and operation. Boolean variable zi, j is the second operand of
the logical and operation. Boolean variable zi, j stores the number of vertex in a clique
after figuring out the influence of Boolean variable x i that encodes the ith vertex to
the number of ones (vertices). If the value of Boolean variable zi, j is equal to 1 (one),
then this indicates that there are j ones (vertices) in the clique. Boolean variable
zi+1, j+1 stores the result of implementing the logical and operation “zi+1, j+1 ← x i+1
∧ zi, j ”. This is to say that Boolean variable zi+1, j+1 stores the number of vertex in
a clique after figuring out the influence of Boolean variable x i + 1 that encodes the
(i + 1)th vertex to the number of ones (vertices). If the value of Boolean variable
3.3 Introduction to the Maximal Clique Problem 141
Fig. 3.9 Flowchart of computing the number of vertex in each clique to the maximal clique problem
for a graph G = (V, E) with n vertices and θ edges and its complementary graph G = (V, E) with
n vertices and m edges
zi+1, j+1 is equal to 1 (one), then this implies that there are (j + 1) ones (vertices) in
the clique.
Next, in statement S 8 , it implements a logical and operation “zi+1, j ← xi+1 ∧
zi, j ” that is one AND gate. Boolean variable x i+1 encodes the (i + 1)th vertex and its
negation xi+1 is the first operand of the logical and operation. Boolean variable zi, j
is the second operand of the logical and operation. Boolean variable zi, j stores the
number of vertex in a clique after figuring out the influence of Boolean variable x i
that encodes the ith vertex to the number of ones (vertices). If the value of Boolean
variable zi, j is equal to 1 (one), then this is to say that there are j ones (vertices) in
the clique. Boolean variable zi+1, j stores the result of implementing the logical and
operation “zi+1, j ← xi+1 ∧ zi, j ”. This indicates that Boolean variable zi+1, j stores
the number of vertex in a clique after figuring out the influence of Boolean variable
142 3 Quantum Search Algorithm and Its Applications
x i + 1 that encodes the (i + 1)th vertex to the number of ones (vertices). If the value
of Boolean variable zi+1, j is equal to 1 (one), then this is to say that there are j ones
(vertices) in the clique.
Next, in statement S 9 , it decreases the value of the index variable j in the second
loop. Repeat to execute statement S 6 through statement S 9 until in statement S 6 the
conditional judgement becomes a false value. Next, in statement S 10 , it increases
the value of the index variable i in the first loop. Repeat to execute statement S 4
through statement S 10 until in statement S 4 the conditional judgement becomes a false
value. When in statement S 4 the conditional judgement becomes a false value, next
executed statement is statement S 11 . In statement S 11 , it executes an End instruction
to terminate the task that is to count the number of vertex in each clique. The cost of
one time to complete each operation in Fig. 3.9 is to implement (n × (n + 1)) AND
gates and ( n×(n+1)
2
) NOT gates. This is to say that the cost of counting the number of
vertex to a clique is to implement (n × (n + 1)) AND gates and ( n×(n+1)2
) NOT gates.
Therefore, for counting the number of vertex in all cliques, the cost is to implement
(2n × n × (n + 1)) AND gates and (2n × n×(n+1) 2
) NOT gates.
A data dependence arises from two statements that read or write the same memory.
Data dependence analysis is to decide whether to reorder or parallelize statements
is safe or not. In a maximal clique problem for a graph G = (V, E) with n vertices
and θ edges and its complementary graph G = (V,E) with n vertices and m edges, it
contains 2n subsets of vertices (2n possible choices). We use a set {x 1 x 2 … x n − 1 x n |
∀ x d ∈ {0, 1} for 1 ≤ d ≤ n} to encode 2n subsets of vertices. In the set {x 1 x 2 … x n − 1
x n | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ n}, the first element x 01 x 02 … x 0n − 1 x 0n encodes an empty
subset without any vertex. The second element x 01 x 02 … x 0n − 1 x 1n encodes {vn }. The
third element x 01 x 02 … x 1n − 1 x 0n encodes {vn − 1 } and so on with that the last element
x 11 x 12 … x 1n − 1 x 1n encodes {v1 v2 … vn − 1 vn } that contains each vertex. Each element
needs to implement those operations in Figs. 3.8 and 3.9. Each element needs to use
m auxiliary Boolean variables r k for 1 ≤ k ≤ m, (m + 1) auxiliary Boolean variables
sk for 0 ≤ k ≤ m and ((n × (n + 3)) / 2) auxiliary Boolean variables zi+1, j and zi+1, j+1
for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i. Because 2n subsets of vertices (2n inputs) implement
those instructions from Fig. 3.8 through Fig. 3.9 not to access or modify the same
input and the same auxiliary Boolean variables, we can parallelize them without any
error.
Let us consider another graph in Fig. 3.10a and its complementary graph in
Fig. 3.10b. In Fig. 3.10a, the graph has two vertices {v1 , v2 } and one edge {(v1 ,
v2 )}. In Fig. 3.10b, its complementary graph has the same two vertices {v1 , v2 } and
no edge missing in the original graph in Fig. 3.10a. For the graph in Fig. 3.10a
with two vertices {v1 , v2 } and one edge {(v1 , v2 )} and its complementary graph in
3.3 Introduction to the Maximal Clique Problem 143
Fig. 3.10 a The graph has two vertices and one edge. b Its complementary graph has the same
vertices and no edge missing in the original graph
Fig. 3.10b with the same two vertices {v1 , v2 } and no edge, solving the maximal
clique problem is to find a subset V 1 of vertices with size r that satisfies V 1 to be a
maximum-sized clique. This is to say that the value of n is equal to two, the value of
θ is equal to one and the value of m is equal to zero.
We regard the maximal clique problem for the graph with two vertices {v1 , v2 } and
one edge {(v1 , v2 )} in Fig. 3.10a as a search problem. Any given oracular function Of
is to implement those instructions from Fig. 3.8 to Fig. 3.9 to recognize the maximal
clique(s). Its domain is {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2} to encode 22 subsets
of vertices. In the domain {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2}, the first element x 01
x 02 encodes an empty subset without vertex. The second element x 01 x 12 encodes {v2 }.
The third element x 11 x 02 encodes {v1 }. The fourth element x 11 x 12 encodes {v1 , v2 }.
From the domain {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2}, there are four inputs x 01
x 2 , x 01 x 12 , x 11 x 02 and x 11 x 12 . Because the value of m is equal to zero, there is no edge
0
in its complementary graph in Fig. 3.10b. Therefore, each input does not need to
implement those instructions in Fig. 3.8. Each input is a clique. This indicates that
an empty subset {}, {v2 }, {v1 } and {v1 , v2 } are all cliques. Next, for computing the
number of vertex in each clique, each input needs to implement “z1,1 ← s10 ∧ x 1 ”,
“z1,0 ← s10 ∧ x1 ”, “z2, 2 ← x 2 ∧ z1,1 ”, “z2,1 ← x2 ∧ z1,1 ”, “z2, 1 ← x 2 ∧ z1,0 ” and
“z2,0 ← x2 ∧ z1,0 ” in Fig. 3.9. After each input completes six instructions above, the
input x 11 x 12 has the result z12, 2 and other inputs x 01 x 02 , x 01 x 12 and x 11 x 02 have the same
result z02, 2 . Because Boolean variable z12, 2 indicates that the input x 11 x 12 encodes {v1 ,
v2 } to be the maximal clique, the answer is to {v1 , v2 } and the number of vertex in
the answer is two. Because 22 subsets of vertices implement six instructions above
not to access or modify the same input and the same auxiliary Boolean variable, we
can parallelize them without any error.
In the graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a, an empty
subset {}, {v2 }, {v1 } and {v1 , v2 } are all cliques. The maximal clique problem to the
graph in Fig. 3.10a is to find a maximum-sized clique in which the number of vertex
is two. Implementing the instruction “z1,1 ← s10 ∧ x 1 ” is equivalent to implement
144 3 Quantum Search Algorithm and Its Applications
the instruction “z1,1 ← x 1 ” in which Boolean variable z1,1 actually stores the value
of x 1 . Therefore, implementing the instruction “z2, 2 ← x 2 ∧ z1,1 ” is equivalent to
implement the instruction “z2, 2 ← x 2 ∧ x 1 ”. So, any given oracular function Of to
recognize a maximal-sized clique for the graph in Fig. 3.10a is to implement the
instruction “z2, 2 ← x 2 ∧ x 1 ”. Its domain is {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2} and
its range is {0, 1}.
We regard its domain as its solution space in which there are four possible choices
that satisfy Of = F(x 1 , x 2 ) = x 1 ∧ x 2 = 1. We make use of a basis {(1, 0, 0, 0), (0, 1, 0,
0), (0, 0, 1, 0), (0, 0, 0, 1)} of the four-dimensional Hilbert space to construct solution
space {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2}. We use (1, 0, 0, 0) to encode Boolean
variable x 01 and Boolean variable x 02 that represent an empty clique {} without any
vertex. Next, we apply (0, 1, 0, 0) to encode Boolean variable x 11 and Boolean variable
x 02 that represent a clique {v1 }. We make use of (0, 0, 1, 0) to encode Boolean variable
x 01 and Boolean variable x 12 that represent a clique {v2 }. Finally, we apply (0, 0, 0, 1)
to encode Boolean variable x 11 and Boolean variable x 12 that represent the maximal
clique {v1 , v2 }.
We use a linear combination of each element in the basis that is √1 2 (1, 0, 0, 0) +
2
√1 × (0, 1, 0, 0) + √1 2 × (0, 0, 1, 0) + √1 2 × (0, 0, 0, 1) = ( √1 2 , √1 2 , √1 2 , √1 2 )
22 2 2 2 2 2 2
to construct solution space {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2}. The amplitude of
each possible choice is all √1 2 and the sum to the square of the absolute value of
2
each amplitude is one. The length of the vector is one, so it is a unit vector. This
indicates that we make use of a unit vector to encode all of the possible choices that
satisfy Of = F(x 1 , x 2 ) = x 1 ∧ x 2 . We call the square of the absolute value of each
amplitude as the cost (the successful probability) of that choice that satisfies the given
oracular function Of = F(x 1 , x 2 ) = x 1 ∧ x 2 . The cost (the successful probability) of
the answer(s) is close to one as soon as possible.
In Listing 3.2, the program in the backend ibmqx4 with five quantum bits in IBM’s
quantum computer is to solve an instance of the maximal clique problem to the graph
with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a. Because the given
oracular function of recognizing the maximal clique(s) is Of = F(x 1 , x 2 ) = x 1 ∧ x 2 ,
in Listing 3.2, we introduce how to write a quantum program to find values of each
Boolean variable so that the whole formula has the value 1. Figure 3.11 is the quantum
circuit of constructing solution space to the question. The statement “OPENQASM
2.0;” on line one of Listing 3.2 is to point out that the program is written with
version 2.0 of Open QASM. Next, the statement “include “qelib1.inc”;” on line two
of Listing 3.2 is to continue parsing the file “qelib1.inc” as if the contents of the file
were pasted at the location of the include statement, where the file “qelib1.inc” is
3.3 Introduction to the Maximal Clique Problem 145
Fig. 3.11 The quantum circuit of constructing solution space to an instance of the maximal clique
problem to the graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a
Quantum Experience (QE) Standard Header and the path is specified relative to
the current working directory.
Listing 3.2 The program of solving an instance of the maximal clique problem to
the graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[5];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
Next, the statement “qreg q[5];” on line three of Listing 3.2 is to declare that in
the program there are five quantum bits. In the left top of Fig. 3.11, five quantum
bits are respectively q[0], q[1], q[2], q[3] and q[4]. The initial value of each quantum
bit is set to |0 . We apply quantum bit q[3] to encode Boolean variable x 1 . We use
quantum bit q[4] to encode Boolean variable x 2 . We make use of quantum bit q[2]
to encode auxiliary Boolean variable z2, 2 . We apply quantum bit q[0] as an auxiliary
working bit. We do not use quantum bit q[1].
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 4 is to represent the
value 0 of q[k] and q[k]1 for 0 ≤ k ≤ 4 is to represent the value 1 of q[k]. Similarly,
for the convenience of our explanation, an initial state vector of constructing solution
146 3 Quantum Search Algorithm and Its Applications
space to an instance of the maximal clique problem in the graph with two vertices
{v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a is as follows:
|α0 = q[4]0 q[3]0 q[2]0 q[1]0 q[0]0 = |0 |0 |0 |0 |0 = |00000 .
Next, the statement “creg c[5];” on line four of Listing 3.2 is to declare that there
are five classical bits in the program. In the left bottom of Fig. 3.11, five classical
bits are subsequently c[0], c[1], c[2], c[3] and c[4]. The initial value of each classical
bit is set to 0. Classical bit c[4] is the most significant bit and classical bit c[0] is the
least significant bit.
Next, the three statements “x q[0];”, “h q[3];” and “h q[4];” on line five through
seven of Listing 3.2 is to complete one X gate (one NOT gate) and two Hadamard
gates of the first time slot of the quantum circuit in Fig. 3.11. The statement “x
01 1 0
q[0];” actually implements × = = (|1 ). This is to say that the
10 0 1
statement “x q[0];” on line five of Listing 3.2 inverts q[0]0 (|0 ) into q[0]1 (|1 ) .
√1 √1 1
The two statements “h q[3];” and “h q[4];” both actually run 2 2 ×
√1 − √12 0
2
√1 1 1 0
= 2 = √1 = √1 + = √1 (|0 + |1 ). This implies that
√1 2 1 2 0 1 2
2
converting q[3] from one state |0 > to another state √1 (|0 + |1 ) (its superposition)
2
and converting q[4] from one state |0> to another state √12 (|0 + |1 ) (its superpo-
sition) are
implemented. Thus, the superposition
of the two quantum bits q[4] and
1 1 1
q[3] is √2 (|0 + |1 ) . √2 (|0 + |1 ) = 2 (|0 |0 + |0 |1 + |1 |0 + |1 |1 ) =
1
2 (|00
+ |01 + |10 + |11 ) . Since in the first time slot of the quantum circuit in
Fig. 3.11
there
is no
quantum gate to act on quantum bits q[2] and q[1], their current
states q[2]0 and q[1]0 are not changed. This indicates that we obtain the following
1 1
|α1 = √ q[4]0 +q[4]1 √ q[3]0 +q[3]1
2 2
q[2]0 q[1]0 q[0]1
1
= q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1
2 0
q[2] q[1]0 q[0]1
1
= (|0 |0 + |0 |1 + |1 |0 + |1 |1 )(|0 |0 |1 ).
2
Then, the statement “h q[0];” on line eight of Listing 3.2 is to execute one
Hadamard gate of the second time slot of the quantum circuit in Fig. 3.11. The
3.3 Introduction to the Maximal Clique Problem 147
√1 √1 0 √1
statement “h q[0];” actually implements 2 2 × = 2 = √1
√1 − √12 1 − √12 2
2
1 1 0
= √12 − = √12 (|0> − |1>). This implies that converting q[0]
−1 0 1
from one state |1> to another state √12 (|0> − |1>) (its superposition) is performed.
Because in the second time slot of the quantum circuit in Fig. 3.11 there is no quantum
gate to act on quantum bits q[4] through q[1], their current states are not changed.
This is to say that we obtain the following new state vector
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + q[4]1 q[3]1
|α2 =
2
1
q[2]0 q[1]0 √ q[0]0 − q[0]1
2
1 1
= (|0 |0 + |0 |1 + |1 |0 + |1 |1 ) (|0 |0 ) √ (|0 − |1 ) .
2 2
In the new state vector |α2 , state q[4]0 q[3]0 encodes Boolean variable x 01
0
and
Boolean variable x 2 that represent a possible choice without any vertex. State
q[4]0 q[3]1 encodes Boolean variable x 1 and Boolean variable x 0 that represent a
1 2
possible choice with the first vertex v1 . State q[4]1 q[3]0 encodes Boolean variable
x 01 and Boolean
variable
1
x 2 that represent a possible1 choice with the second vertex
1 1
v2 . State q[4] q[3] encodes Boolean variable x 1 and Boolean variable x 12 that
represent a possible choice with the two vertices v1 and v2 . The amplitude of each
possible choice is √1 2 and the cost (the successful possibility) of becoming the
2
1
answer(s) to each possible choice is the same and is equal to 22
= 1/4.
For solving the maximal clique problem of the graph with two vertices {v1 , v2 } and
one edge {(v1 , v2 )} in Fig. 3.10a, the Oracle is to have the ability to recognize the
maximal-sized clique(s). The Oracle is to multiply the probability amplitude of the
maximal-sized clique(s) by −1 and leaves any other amplitude unchanged. Since the
given oracular function of recognizing the maximal-sized clique(s) is Of = F(x 1 , x 2 )
= x 1 ∧ x 2 , the Oracle to solve an instance of the maximal clique problem is a (22 ×
⎛ ⎞
1000
⎜0 1 0 0 ⎟
22 ) matrix B that is equal to ⎜⎝0 0 1 0 ⎠ .
⎟
0 0 0 −1 4×4
148 3 Quantum Search Algorithm and Its Applications
⎜ 01 0 0 ⎟
⎜ ⎟ ⎜ √2 ⎟
×⎜ 2 ⎟
⎜
⎝ 00 1 0 ⎠
⎟ ⎜ √1 ⎟
⎝ 22 ⎠
0 0 0 −1 4×4 √1
22 4×1
⎛ ⎞
√ 1 ⎛ ⎞
⎜ 12 ⎟
2 1
⎜ √2 ⎟ 1 ⎜ 0 ⎟ 1
=⎜ 2 ⎟ = √ ×⎜ ⎟ +√
⎜ √1 ⎟ 2 0
⎝ 22 ⎠ 2 ⎝ ⎠ 22
−1
√ 0 4×1
22 4×1
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
0 0 0
⎜ 1 ⎟ 1 ⎜ 0 ⎟ −1 ⎜ 0 ⎟
×⎜ ⎝ 0 ⎠
⎟ + √ ×⎜ ⎟ + √ ×⎜ ⎟
22 ⎝ 1 ⎠ 22 ⎝ 0 ⎠
0 4×1
0 4×1
1 4×1
1 1 1 −1
= √ |00 + √ |01 + √ |10 + √ |11 .
22 22 22 22
⎛ ⎞ ⎛ ⎞ ⎛ ⎞
1 0 0
⎜ 0 ⎟ ⎜ 1 ⎟ ⎜ 0 ⎟
Four computational basis vectors ⎜ ⎝ 0 ⎠ ,⎝ 0 ⎠ , ⎝ 1 ⎠
⎟ ⎜ ⎟ ⎜ ⎟ and
0 4×1 0 4×1 0 4×1
⎛ ⎞
0
⎜ 0 ⎟
encode four states |00 q[4]0 q[3]0 , |01 q[4]0 q[3]1 ,
⎜ ⎟
⎝ 0 ⎠
1 4×1
|10 q[4]1 q[3]0 and |11 q[4]1 q[3]1 and their current amplitudes are
respectively √1 2 , √1 2 , √1 2 and √−12 . State |00 q[4]0 q[3]0 with the
2 2 2 2
amplitude √1 2 encodes Boolean variable x 01 and Boolean variable x 02 that represent
2
a possible choice without any vertex. State |01 q[4]0 q[3]1 with the amplitude
3.3 Introduction to the Maximal Clique Problem 149
√1 encodes Boolean variable x 11 and Boolean variable x 02 that represent a possible
22
choice with the first vertex v1 . State |10 q[4]1 q[3]0 with the amplitude √1 2
2
encodes Boolean variable x 01 and Boolean variable 1
x 2 that represent a possible
1 1
choice with the second vertex v 2 . State |11 q[4] q[3] with the amplitude
−1
√ encodes Boolean variable x 11 and Boolean variable x 12 that represent the
22
maximal-sized clique with the two vertices v1 and v2 . This indicates that the Oracle
multiplies the probability amplitude of the maximal-sized clique with the two
vertices v1 and v2 by −1 and leaves any other amplitude unchanged.
We apply one CCNOT gate to run the given oracular function Of = F(x 1 , x 2 ) = x 1 ∧
x 2 that recognizes the maximal-sized clique(s) to the maximal clique problem in the
graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a. We make use of
quantum bit q[3] to encode Boolean variable x 1 , we use quantum bit q[4] to encode
Boolean variable x 2 and we apply quantum bit q[2] to encode Boolean variable
z2, 2 . Therefore, quantum bits q[3], q[4], q[2] are subsequently the first control bit,
the second control bit and the target bit of the CCNOT gate. We make use of the
CCNOT gate to implement a logical and operation, so the initial value to quantum
bit q[2] is set to |0 .
From line nine through line twenty-three in Listing 3.2, there are the fifteen state-
ments. They are subsequently “h q[2];”, “cx q[4],q[2];”, “tdg q[2];”, “cx q[3],q[2];”,
“t q[2];”, “cx q[4],q[2];”, “tdg q[2];”, “cx q[3],q[2];”, “t q[4];”, “t q[2];”, “cx
q[3],q[4];”, “h q[2];”, “t q[3];”, “tdg q[4];” and “cx q[3], q[4];”. They complete the
CCNOT gate that implements the given oracular function O f = F(x1 , x2 ) = x1 ∧ x2
that recognizes the maximal-sized clique(s) to the maximal clique problem in the
graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a. Figure 3.12 is
the quantum circuit of implementing the given oracular function O f = F(x1 , x2 ) =
x1 ∧ x2 that recognizes the maximal-sized clique(s) to the maximal clique problem
in the graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a.
Fig. 3.12 The quantum circuit of implementing the given oracular function Of = F(x 1 , x 2 ) = x 1
∧ x 2 that recognizes the maximal-sized clique(s) to the maximal clique problem in the graph with
two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a
150 3 Quantum Search Algorithm and Its Applications
1
They take the state vector |α2 = q[4]0 q[3]0 + q[4]0 q[3]1
2
q[2]0 q[1]0 √1 q[0]0 − q[0]1
+q[4]1 q[3]0 + q[4]1 q[3]1 2
as their
input. After they actually perform six CNOT gates, two Hadamard gates, three T +
gates and four T gates from the first time slot through the eleventh time slot in
Fig. 3.12, we gain the following new state vector
1
q[4]0 q[3]0 q[2]0 + q[4]0 q[3]1 q[2]0 + q[4]1 q[3]0 q[2]0
|α3 =
2
1
+ q[4]1 q[3]1 q[2]1 q[1]0 √ q[0]0 − q[0]1
2
1
= (|0 |0 |0 + |0 |1 |0 + |1 |0 |0 + |1 |1 |1 )
2
1
(|0 ) √ (|0 − |1 ) .
2
Then, from line twenty-four in Listing 3.2, the statement “cx q[2],q[0];” takes
the new
state vector
|α3 > as its input. It multiplies the probability amplitude of the
answer. q[4]1 q[3]1 encoding the maximal-sized clique {v1 , v2 } by −1 and leaves
any other amplitude unchanged. This implies that after the statement “cx q[2],q[0];”
completes the CNOT gate in the twelfth time slot in Fig. 3.12, we get the following
new state vector.
3.3 Introduction to the Maximal Clique Problem 151
1
q[4]0 q[3]0 q[2]0 + q[4]0 q[3]1 q[2]0 + q[4]1 q[3]0 q[2]0
|α4 =
2
1
+ (−1)q[4]1 q[3]1 q[2]1 q[1]0 √ q[0]0 − q[0]1
2
1
= (|0 |0 |0 + |0 |1 |0 + |1 |0 |0 + (−1)|1 |1 |1 )
2
1
(|0 ) √ (|0 − |1 ).
2
Since quantum operations are reversible by nature, executing the reversed order
of implementing the CCNOT gate can restore the auxiliary quantum bits to their
initial states. From line twenty-five through line thirty-nine in Listing 3.2, there are
the fifteen statements. They are “cx q[3],q[4];”, “tdg q[4];”, “t q[3];”, “h q[2];”, “cx
q[3],q[4];”, “t q[2];”, “t q[4];”, “cx q[3],q[2];”, “tdg q[2];”, “cx q[4],q[2];”, “t q[2];”,
“cx q[3],q[2];”, “tdg q[2];”, “cx q[4],q[2];” and “h q[2];”. They run the reversed
order of implementing.
31. t q[4];
32. cx q[3],q[2];
33. tdg q[2];
34. cx q[4],q[2];
35. t q[2];
36. cx q[3],q[2];
37. tdg q[2];
38. cx q[4],q[2];
39. h q[2];
the CCNOT gate that performs the given oracular function O f = F(x1 , x2 ) =
x1 ∧ x2 that recognizes the maximal-sized clique(s). They take the new state vector
|α4 > as their input. After they actually complete six CNOT gates, two Hadamard
gates, three T + gates and four T gates from the thirteenth time slot through the last
time slot in Fig. 3.12, we obtain the following new state vector.
1
q[4]0 q[3]0 + q[4]0 q[3]1 + q[4]1 q[3]0 + (−1)q[4]1 q[3]1
|α5 =
2
1
q[2]0 q[1]0 √ g[0]0 − q[0]1
2
1 1
= (|0 |0 + |0 |1 + |1 |0 + (−1)|1 |1 ) (|0 |0 ) √ (|0 − |1 ) .
2 2
In the state vector |α5 , the amplitude of each element in solution space {x 1 x 2 |
∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2} is (1/2). In the state vector |α5 , the amplitude to
three elements x 01 x 02 encoding an empty clique, x 01 x 12 encoding a clique {v2 }, x 11 x 02
encoding a clique {v1 } in solution space is all (1/2) and the amplitude to the element
x 11 x 12 encoding the maximal-sized clique {v1 , v2 } in solution space is (−1/2). This
is to say that thirty-one statements ⎞ nine through thirty-nine in Listing 3.2
⎛ from line
√1
⎛ ⎞
10 00 2
⎜ 12 ⎟
⎜ 01 00 ⎟
⎜ ⎟ ⎜ √2 ⎟
complete ⎜ ×⎜ 2 ⎟ that is to implement the Oracle that
⎝ 00 10 ⎠
⎟ ⎜ √1 ⎟
⎝ 22 ⎠
0 0 0 −1 4×4 √1
2
2 4×1
recognizes the maximal-sized clique(s) to solve the maximal clique problem in the
graph with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a.
3.3 Introduction to the Maximal Clique Problem 153
amplitude of each solution in the maximal clique problem in the graph with two
vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a only needs to consider the
first subsystem in the new state vector ⎛ |α5 . Since
⎞ for an instance of the maximal
√1
22 ⎟
√1 ⎟
⎜
2
⎜
clique problem the (2 × 1) vector ⎜ 22 ⎟ encodes the first subsystem of
⎜ √1 ⎟
⎝ 22 ⎠
−1
√
22 4×1
⎛ 2 2 2 2 ⎞
22
−1 2 2 22 22
2 2 2 2
⎜ −1 ⎟
the new state vector |α5 and ⎜ 22
2
22
2 2
22 22
2
⎟ is a (22 ×
⎝
22 22 22
−1 22
⎠
2 2 2 2
22 22 22 22
−1 22 ×22
22 ) diffusion operator, amplifying the amplitude
⎛ of⎞the solution is to implement
⎛ 2 2 2 2 ⎞ √1 ⎛ ⎞
22
− 1 22 22 22 22 ⎟ 0
√1 ⎟
⎜
⎜ 22 2
− 1 2 2 ⎟ ⎜ ⎜ 0 ⎟
⎜ 2 22 22 22 ⎟ ×⎜ 22 ⎟ =⎜ ⎟ . This indi-
⎝ 22 2 2 2 1 ⎟
2 2 2 2 2 − 1 2 2
⎠ ⎜ √
2
⎝ 0 ⎠
⎝ 2 ⎠
2 2 2 2 −1
22 22 22 22
− 1 22 ×22 √
2
1 4×1
2 4×1
cates that the amplitude of the solution that encodes the maximal-sized clique {v1 ,
v2 } is one and the amplitude of other three possible choices is all zero.
The
quantum Fig. 3.13 implements the Grover diffusion operator,
circuit in
H ⊗2 20⊗2 0⊗2 − I22 ×22 H ⊗2 . From line forty through line fifty-one in Listing
3.2, there are the twelve statements. They are subsequently “h q[3];”, “h q[4];”,
“x q[3];”, “x q[4];”, “h q[4];”, “cx q[3],q[4];”, “h q[4];”, “x q[4];”, “x q[3];”,
“u3(2*pi,0*pi,0*pi) q[3];”, “h q[4];” and “h q[3];”.
Fig. 3.13 The quantum circuit of implementing the Grover diffusion operator, H ⊗2 (2 0⊗2 0⊗2 −
⊗2
I22 ×22 ) H , to an instance of the maximal clique problem in the graph with two vertices {v1 , v2 }
and one edge {(v1 , v2 )} in Fig. 3.10a
42. xq[3];
43. xq[4];
44. h q[4];
45. cx q[3],q[4];
46. h q[4];
47. xq[4];
48. xq[3];
49. u3(2*pi,0*pi,0*pi) q[3];
50. h q[4];
51. h q[3];
the new state vector 21 q[4]0 q[3]0 + q[4]0 q[3]1
They1 take
+q[4] q[3]0 + (−1) q[4]
1
q[3]
1
as their input. They complete the diffu-
sion operator, H ⊗2 20⊗2 0⊗2 − I22 ×22 H ⊗2 from the first time slot through the
eighth time slot in Fig. 3.13. This is to say that we obtain the following new state
vector.
|α6 =q[4]1 q[3]1 .
Next, from line fifty-two in Listing 3.2the statement “measure q[4] -> c[4];”
is to measure the fifth quantum bit q[4] and to record the measurement outcome
by overwriting the fifth classical bit c[4]. From line fifty-three in Listing 3.2 the
statement “measure q[3] -> c[3];” is to measure the fourth quantum bit q[3] and to
record the measurement outcome by overwriting the fourth classical bit c[3]. They
implement the measurement from the ninth time slot through the tenth time slot of
Fig. 3.13.
3.3 Introduction to the Maximal Clique Problem 155
Fig. 3.14 After the measurement to solve an instance of the maximal clique problem in the graph
with two vertices {v1 , v2 } and one edge {(v1 , v2 )} in Fig. 3.10a is completed, we obtain the answer
11,000 with the probability 1 (100%)
In the backend ibmqx4 with five quantum bits in IBM’s quantum computers, we
apply the command “simulate” to execute the program in Listing 3.2. The measured
result appears in Fig. 3.14. From Fig. 3.14, we get the answer 11,000 (c[4] = 1 = q[4]
= |1 , c[3] = 1 = q[3] = |1 , c[2] = 0, c[1] = 0 and c[0] = 0) with the probability
1 (100%). This implies that with the possibility 1 (100%) we gain that the value of
quantum bit q[3] is equal to |1 and the value of quantum bit q[4] is equal to |1 .
Therefore, the maximal-sized clique is to {v1 , v2 }.
A maximal clique problem to a graph G = (V, E) with n vertices and θ edges and its
complementary graph G = (V, E) with n vertices and m edges in which each edge
in E is out of E is to find the maximal-sized clique(s) among 2n subsets of vertices.
Any given oracular function O f (x1 x2 . . . xn−1 xn ) is to recognize the maximal-sized
clique(s) among 2n subsets of vertices. It implements flowchart of recognizing cliques
in Fig. 3.8. Next, it implements flowchart of computing the number of vertex in each
clique in Fig. 3.9. We make use of the quantum search algorithm to find one of M
solutions among 2n subsets of vertices, where 0 ≤ M ≤ 2n .
The quantum circuit in Fig. 3.15 is to complete the quantum search algorithm to
solve an instance of the maximal clique problem in a graph G = (V, E) with n vertices
and θ edges. The first quantum register in the left top of Fig. 3.15 is (0⊗n ). This
implies that the initial value of each quantum bit is |0 . The second quantum register
156 3 Quantum Search Algorithm and Its Applications
Fig. 3.15 Circuit of implementing the quantum search algorithm to solve an instance of the maximal
problem in a graph G = (V, E) with n vertices and θ edges
in the left bottom of Fig. 3.15 has (((n × (n + 3)) / 2) + 2 × m + 1) quantum bits and
is an auxiliary quantum register. The initial value of each quantum bit in the second
quantum register is |0 or |1 that is dependent on implementing NAND operations
or AND operations. The third quantum register in the left bottom of Fig. 3.15 is (|1 ).
In Fig. 3.15, the first stage of the quantum search problem to solve an instance
of the maximal clique problem in a graph G = (V, E) with n vertices and
θ edges is to apply n Hadamard gates ( H ⊗n ) to operate the first quantum
register (0⊗n ). This is to say that it produces the superposition of n quantum
|x +|x 1
0
bits to be (⊗nk=1 k √2 k ) = √12n ( {x1 x2 ...xn |∀xd ∈{0,1}for1≤d≤n} |x1 x2 . . . xn ). The
superposition √12n ( {x1 x2 ...xn |∀xd ∈{0,1}for1≤d≤n} |x1 x2 . . . xn ) encodes solution space
clique problem
in a graph G = (V, E) with n vertices and θ edges. This indicates
that state (x10 x20 . . . xn0 ) with the amplitude ( √12n ) encodes an empty subset (choice)
of vertex, state (x10 x20 . . . xn1 ) with the amplitude ( √12n ) encodes {vn } and so on with
that state (x11 x21 . . . xn1 ) with the amplitude ( √12n ) encodes v1 v2 · · · vn . In the first
stage of the quantum search algorithm, it uses another Hadamard gate to operate the
third quantum register (|1 ). This implies that it yields the superposition of the third
quantum register (|1 ) that is √12 (|0 − |1 ) .
In Fig. 3.15, the second stage of the quantum search algorithm to solve an instance
of the maximal clique problem in a graph G = (V, E) with n vertices and θ edges
and its complementary graph G = (V, E) with n vertices and m edges in which
each edge in E is out of E is to implement the Oracle. The Oracle is to have the
ability to recognize solutions that are the maximal-sized cliques among 2n subsets
(possible choices) of vertices. The Oracle is to multiply the probability amplitude of
the maximal-sized clique(s) by − 1 and leaves any other amplitude unchanged.
The first main task of the Oracle is to that recognizing clique(s) among 2n
possible choices
(subsets)
of vertices is equivalent to implement a formula of the
form (∧k=1m xi ∧ x j ). This is to say that for completing the first main task of the
Oracle we need to use (2 × m + 1) auxiliary quantum bits and to complete m NAND
operations and m AND operations that is equivalent to implement (2 × m) CCNOT
gates.
Next, the second main task of the Oracle is to that computing the number of vertex
in each clique is equivalent to decide that the influence of each vertex is to increase
the number of vertex to each clique or to reserve the number of vertex to each clique.
This indicates that for performing the second main task of the Oracle we need to
use ((n × (n + 3)) / 2) auxiliary quantum bits and to implement (n × (n + 1)) AND
operations and ( n×(n+1)2
) NOT operations that is equivalent to complete (n × (n +
1)) CCNOT gates and ( n×(n+1) 2
) NOT gates.
In oracle workspace in the second stage of the quantum search algorithm, we
make use of auxiliary quantum bits |rk for 1 ≤ k ≤ m to encode auxiliary Boolean
variables r k for 1 ≤ k ≤ m. We apply auxiliary quantum bits |sk for 0 ≤ k ≤ m to
encode auxiliary
Boolean
variables sk for 0 ≤ k ≤ m. We use auxiliary quantum bits
z i+1, j and z i+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i to encode auxiliary Boolean
variables zi+1, j and zi+1, j+1 for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i.
We use CCNOT to implement each NAND gate (xi ∧ x j ) in a formula of
gates
k=1
the form (∧m xi ∧ x j ) and we apply auxiliary quantum bits |rk for 1 ≤ k ≤ m to
store the result of implementing each NAND gate (xi ∧ x j ). This is to say that each
auxiliary quantum bit |rk for 1 ≤ k ≤ m is actually the target bit of a CCNOT gate of
158 3 Quantum Search Algorithm and Its Applications
implementing a NAND gate (xi ∧ x j ). Therefore, the initial value of each auxiliary
quantum bit |rk for 1 ≤ k ≤ m is set to one |1 .
We use an auxiliary quantum bit |S0 as the first operand of the first logical and
operation (“∧”) in a formula of the form (∧k=1 m x i ∧ x j ). The initial value of the
auxiliary quantum bit |S0 is set to |1 . This indicates that this setting does not change
the correct result of the first logical and operation. We make use of a CCNOT gate
to
implement each logical and operation in a formula of the form (∧k=1 m x i ∧ x j ). We
apply auxiliary quantum bits |Sk for 1 ≤ k ≤ m to store the result of implementing
each logical and operation. This implies that each auxiliary quantum bit |sk > for
1 ≤ k ≤ m is actually the target bit of a CCNOT gate of implementing a logical and
operation. Thus, the initial value of each auxiliary quantum bit |sk > for 1 ≤ k ≤ m
is set to |0 > .
We use a CCNOT gate to implement (sm ∧ x1 ) that is to compute the influence of
the first vertex to increase the number of vertex in each clique. We apply a CCNOT
gate and two NOT gates to implement (sm ∧ x1 ) that is to compute the influence
of the first vertex to reserve
the number of vertex in each clique. We make use of
auxiliary quantum bits z 1,1 to store the result of implementing (sm ∧ x1 ) and apply
auxiliary quantum bits z 1,0 to store theresult of implementing
(sm ∧ x1 ). This is to
say that the two auxiliary quantum bits z 1,1 and z 1,0 are actually the target bits of
two CCNOT gates of implementing two logical and operations.
Therefore, the initial
value of the two auxiliary quantum bits z 1,1 and z 1,0 is set to |0 .
We use a CCNOT gate to implement xi + 1 ∧ z i, j that is to figure the influence
of the (i + 1)th vertex vi + 1 for 1 ≤ i ≤ n − 1 to increase the number ofvertex in each
clique. We apply a CCNOT gate and two NOT gates to implement (xi+1 ) ∧ z i, j
that is to deal with the influence of the (i + 1)th vertex vi + 1 for 1 ≤ i ≤ n – 1
to reserve
the number
of vertex
in each clique. We make use of auxiliary quantum
bits z i+1, j+1 and z i+1, j for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i to store the result of
implementing them. This is to say that auxiliary quantum bit z i+1, j+1 and z i+1, j
for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i are the target bits of the corresponding CCNOT
gates of implementing
logical
and operations. Hence, the initial value of auxiliary
quantum bit z i+1, j+1 and z i+1, j for 0 ≤ i ≤ n − 1 and 0 ≤ j ≤ i is set to |0>.
Quantum bit z n, j for n ≥ j ≥ 0 is to store the result that has j ones after computing
the influence of n vertices to the number of vertex in each clique. If the value of
quantum bit z n, j for n ≥ j ≥ 0 is equal to |1 , then it has j vertices.
We use one CNOT gate |0 √−|1 ⊕ z n, j to multiply the probability amplitude of
2
the maximal-sized clique(s)
by − 1 and to leave any other amplitude unchanged,
|0 √
−|1
where quantum bit 2
is the target bit of the CNOT gate and quantum bit
z n, j is the control bit of the CNOT gate. When the value of the control bit z n, j
is equal to (|1>), the target bit becomes |1 √−|0 2
= (−1) |0 √−|1 2
. This is to multiply
the probability amplitude of the answer(s) by −1. When the value of the control bit
Z n, j is equal to (|0>), the target bit still is |0 √−|1 . This is to leave any other
2
amplitude unchanged.
3.3 Introduction to the Maximal Clique Problem 159
the Oracle in the second stage of the quantum search algorithm in Fig. 3.15 is to
implement (2 × ((2 × m) + (n × (n + 1)))) CCNOT gates, (n × (n + 1)) NOT gates
and one CNOT gate.
Because the number of the vertices to the maximal clique(s) is from n through
zero, we first check whether there is/are clique(s)
with n vertices. The condition is
|0 √−|1
to implement one CNOT gate 2 ⊕ z n,n to multiply the probability amplitude
of the answer(s) with n vertices by − 1 and to leave any other amplitude unchanged.
If the answer(s) with n vertices are found, then we terminate the execution of the
program. Otherwise, we need to continue to test whether there is/are clique(s) with (n
− 1) vertices, (n − 2) vertices and so on with that has one vertex until the answer(s)
are found.
In Fig. 3.15, the third stage of the quantum search algorithm to solve an instance
of the maximal clique problem in a graph G = (V, E) with n vertices and θ edges
is to perform the Grover diffusion operator that is a (2n × 2n ) matrix D in which
Da, b = 22n if a = b and Da, a = 22n − 1. Executing
the Grover diffusion operator is
⊗n
equivalent to implement H (2 0⊗n 0⊗n − I2n ×2n ) H ⊗n . A phase shifter operator,
(2 0⊗n 0⊗n − I2n ×2n ) negates all the states except for (x10 . . . xn0 ). In Fig. 3.15, the
⊗n stage
third of the quantum search problem is to apply that thephase shift operator, 2
0 0⊗n − I2n ×2n , negates all the states except for (x10 . . . xn0 ) sandwiched between
H ⊗n gates. This is to say that the third stage of the quantum search algorithm to solve
an instance of the maximal clique problem in a graph G = (V, E) with n vertices and
θ edges is to increase the amplitude of the maximal-sized clique(s) and to decrease
the amplitude of the non-answer(s).
For solving an instance of the maximal clique problem in a graph G = (V, E) with
n vertices and θ edges, we regard the Oracle and the Grover diffusion operator from
the second stage through the third stage of the quantum search algorithm in Fig. 3.15
as a subroutine. We call the subroutine as the Grover iteration. The number of the
vertices to the maximal clique(s) is from n through zero, but in advanced it is unknown.
Therefore, we first test whether there is/are clique(s)
with n vertices. The condition
|0 √−|1
is to implement one CNOT gate 2 ⊕ z n,n to multiply the probability amplitude
of the answer(s) with n vertices by −1 and to leave any other amplitude unchanged. If
160 3 Quantum Search Algorithm and Its Applications
thereis/are
clique(s) with n vertices, then after repeat to execute the Grover iteration
N
of O M
times, the successful probability of measuring the answer(s) with n
vertices is at least (1/2). Otherwise, the measurement has a failed result and we need
to continue to check whether there is/are clique(s) with (n − 1) vertices, (n − 2)
vertices and so on with that has one vertex until the answer(s) is/are found.
When the value of (M/N) is equal to (1/4), the successful probability of measuring
the answer(s) is one (100%) with the Grover iteration of one time. This is the best case
of the quantum search algorithm to solve an instance of the maximal clique problem
in a graph G = (V, E) with n vertices and θ edges. When the value of M is equal
to one, the successful probability
√ of measuring the answer(s) is at least (1/2) with
the Grover iteration of O( N ) times. This is the worst case of the quantum search
algorithm to solve an instance of the maximal clique problem in a graph G = (V, E)
with n vertices and θ edges. This implies that the quantum search algorithm to solve
an instance of the maximal clique problem in a graph G = (V, E) with n vertices
and θ edges only gives a quadratic speed-up.
3.4 Summary
In this chapter, a more detailed introduction to the Search Problem can be found in
the recommended books that are (Imre and Balazs 2007; Lipton and Regan 2014;
Nielsen and Chuang 2000; Silva 2018). A complete description for the satisfiability
problem with m clauses and m Boolean variables can be found in the famous article
3.5 Bibliographical Notes 161
(Cook 1971) and in the famous textbook (Garey and Johnson 1979). A complete
quantum algorithm for solving an instance of the satisfiability problem with m clauses
and m Boolean variables can be found in the famous article (Chang et al. 2018).
A complete illustration to the maximal clique problem to a graph G = (V, E)
with n vertices and θ edges and its complementary graph G = (V, E) has n vertices
and m edges in which each edge in E is out of E can be found in the famous article
(Karp 1975) and in the famous textbook (Garey and Johnson 1979). A complete
quantum algorithm for solving an instance of the maximal clique problem in a graph
G = (V, E) with n vertices and θ edges and its complementary graph G = (V, E)
has n vertices and m edges in which each edge in E is out of E can be found in the
famous article (Chang et al. 2018).
A complete description of quantum search algorithm can be found in the famous
article (Grover 1996) and in the famous textbooks (Imre and Balazs 2007; Lipton
and Regan 2014; Nielsen and Chuang 2000; Silva 2018). A detailed introduction
(quantum circuit) of implementing the Grover diffusion operator can be found in
the famous articles (Coles et al. 2018; Mandviwalla et al. 2018) and in the famous
textbooks (Imre and Balazs 2007; Lipton and Regan 2014; Nielsen and Chuang 2000;
Silva 2018).
3.6 Exercises
3.1. The unary operator “ ” denotes logical operation NOT and the binary operator
“∧” denotes logical operation AND. We regard the satisfiability problem for
the Boolean formula F(x 1 , x 2 ) = x1 ∧ x 2 with two Boolean variables x 1 and
x 2 as a search problem in which any given oracular function Of is the Boolean
formula F(x 1 , x 2 ) = x1 ∧ x 2 . Its domain is {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d
≤ 2}, its range is {0, 1} and the logical operation x1 is the negation of Boolean
variable x 1 . In the given oracular function Of = F(x 1 , x 2 ) = x1 ∧ x 2 of the
search problem, there are M inputs of two bits from its domain, say λM = x 1
x 2 , that satisfies the condition Of (λM ) = Of (x 1 x 2 ) = F(x 1 , x 2 ) = x1 ∧ x 2 = 1.
Please use quantum search algorithms to write a quantum program with what
possibility to find the answer (λM = x 1 x 2 ) that satisfies Of (λM ) = Of (x 1 x 2 ) =
F(x 1 , x 2 ) = x1 ∧ x 2 = 1.
3.2. The binary operator “∧” denotes logical operation AND. The unary operator
“ ” denotes logical operation NOT. Any given oracular function Of is the
Boolean formula F(x 1 , x 2 ) = x 1 ∧ x2 with two Boolean variables x 1 and x 2 .
Its domain is {x 1 x 2 | ∀ x d ∈ {0, 1} for 1 ≤ d ≤ 2}, its range is {0, 1} and the
logical operation x2 is the negation of Boolean variable x 2 . Please make use of
quantum search algorithms to write a quantum program with what possibility
to compute each value of two Boolean variables x 1 and x 2 that satisfies the
given oracular function Of (x 1 x 2 ) = F(x 1 , x 2 ) = x 1 ∧ x2 = 1.
3.3 The binary operator “ ” denotes logical operation NAND. Any given oracular
function Of is the Boolean formula F(x 1 , x 2 ) = x 1 ∧ x2 with two Boolean
162 3 Quantum Search Algorithm and Its Applications
References
Chang, W.-L., Ren, T.-T., Luo, J.n., Feng, M., Guo, M., Lin, K.W.: Quantum algorithms for bio-
molecular solutions of the satisfiability problem on a quantum machine. IEEE Trans. Nanobiosci.
7(3), 215–222 (2008)
Chang, W.-L., Yu, Q., Li, Z., Chen, J., Peng, X., Feng, M.: Quantum speedup in solving the maximal-
clique problem. Phys. Rev. A 97, 032344 (2018)
Coles, P.J., Eidenbenz, S., Pakin, S., Adedoyin, A., Ambrosiano, J., Anisimov, P., Casper, W.,
Chennupati, G., Coffrin, C., Djidjev, H., Gunter, D., Karra, S., Lemons, N., Lin, S., Lokhov, A.,
Malyzhenkov, A., Mascarenas, D., Mniszewski, S., Nadiga, B., O’Malley, D., Oyen, D., Prasad,
L., Roberts, R., Romero, P., Santhi, N., Sinitsyn, N., Swart, P., Vuffray, M., Wendelberger, J.,
Yoon, B., Zamora, R., Zhu, W.: Quantum algorithm implementations for beginners (2018). https://
arxiv.org/abs/1804.03719
Cook, S.: The complexity of theorem proving procedures. In: Proceedings of Third Annual ACM
Symposium on Theory of Computing, pp. 151–158 (1971)
Garey, M.R., Johnson, D.S.: Computer and Intractability: A Guide to the Theory of NP-
Completeness. W. H. Freeman Company (1979). ISBN-13: 978-0716710448
Grover, L.K.: A fast quantum mechanical algorithm for database search. In: Proceedings of the
Twenty-Eighth Annual ACM Symposium on Theory of Computing, pp. 212–219 (1996)
Imre, S., Balazs, F.: Quantum Computation and Communications: An Engineering Approach. Wiley,
UK (2007). ISBN-10: 047086902X and ISBN-13: 978-0470869024, 2005
Karp, R.: On the computational complexity of combinatorial problems. Networks 5(45), 68 (1975)
Lipton, R.J., Regan, K.W.: Quantum Algorithms via Linear Algebra: A Primer. The MIT Press
(2014). ISBN 978-0-262-02839-4
Mandviwalla, A., Ohshiro, K. Ji, B.: Implementing Grover’s algorithm on the IBM quantum
computers. In: 2018 IEEE International Conference on Big Data (Big Data), pp. 1–7 (2018).
https://doi.org/10.1109/bigdata.2018.8622457
Nielsen, M.A., Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge Univer-
sity Press, New York, NY (2000). ISBN-10: 9781107002173 and ISBN-13: 978-1107002173
Silva, V.: Practical Quantum Computing for Developers: Programming Quantum Rigs in the Cloud
using Python, Quantum Assembly Language and IBM Q Experience. Apress, December 13,
2018. ISBN-10: 1484242173 and ISBN-13: 978-1484242179
Chapter 4
Quantum Fourier Transform and Its
Applications
When in 1822 Fourier published his most famous article (work), people originally
used his transform in thermodynamics. Now, the most common use for the Fourier
transform is in signal processing. A signal is given in the time domain: as a function
mapping time to amplitude. The Fourier transform allows us to express the signal
as a weighted sum of phase-shifted sinusoids of varying frequencies. The phases
and the weights associated with frequencies characterize the signal in the frequency
domain. The Fourier transform establishes the bridge between signal representation
in time and frequency domains.
The success of the Fourier transform is due to its discrete version and we call it
as the Discrete Fourier Transform (or DFT). The discrete Fourier transform has
a computationally very efficient implementation in the form of the Fast Fourier
Transform (or FFT). In this chapter, we introduce complex roots of unity and study
their properties. Next, we illustrate the discrete Fourier transform and the inverse
operation of the discrete Fourier transform. Next, we introduce the Quantum Fourier
Transform (or QFT) that is the quantum version of the Fourier transform and is
analogous to the fast Fourier transform. Next, we describe how to write a quantum
program for implementing the discrete Fourier transform and the inverse operation
of the discrete Fourier transform.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 163
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1_4
164 4 Quantum Fourier Transform and Its Applications
The value of θ in Eq. (4.1) is a real number. The distance between any point (a,
b) at the complex plane and the origin (0, 0) of the complex plane is
(a − 0)2 + (b − 0)2 = a 2 + b2 . (4.2)
The value of a is a real number and the value of b is also a real number.
Because according √to Eq. (4.2) the distance between the exponential of a
complex number e −1×θ in Eq. (4.1)
and the origin of the complex √plane is
(cos(θ ) − 0)2 + (sin(θ ) − 0)2 = (cos(θ ))2 + (sin(θ ))2 = 1, each e −1×θ is
equally spaced around the circle of unit radius centered at the origin of the complex
plane.
A complex nth root of unity is a complex number ω such that
ωn = 1. (4.3)
√
There are actually n complex nth roots of unity: e −1×2×π×k/n to k = 0,
1, 2, …, √
n − 2, n − 1. The distance between each complex nth root of
−1×2×π×k/n
unity, e , for
k = 0, 1, 2, …, n − 2, n − 1 and the origin of
the
complex plane is (cos(2 × π × k/n) − 0)2 + (sin(2 × π × k/n) √
− 0)2 =
2 2 −1×2×π×k/n
(cos(2 × π × k/n)) + (sin(2 × π × k/n)) = 1. Therefore, each e
for k = 0, 1, 2, …, n − 2, n − 1 that is a complex nth root of unity is equally spaced
around the circle of unit radius centered at the origin of the complex plane.
The principal nth root of unity is
√
−1×2×π/n
ωn = e . (4.4)
√
√
Because all of the other complex nth roots of unity are e −1×2×π×k/n =
(e −1×2×π/n )k = ωnk for k = 0, 2, 3, …, n − 2, n − 1, they are powers of ωn in
Eq. (4.4). Therefore, n complex nth roots of unity are subsequently
Figure 4.1 shows that in (4.5) ωn0 , ωn1 , ωn2 , . . . , ωnn−2 , ωnn−1 that are the n complex
n-th roots of unity are equally spaced around the circle of unit radius centered at the
origin of the complex plane. This is to say that ωn0 , ωn1 , ωn2 , . . . , ωnn−2 , ωnn−1 lie in the
circumference of the circle of unit radius centered at the origin of the complex plane.
4.2 Illustration of an Abelian Group for n Complex … 165
Fig. 4.1 The values of ω0n , ω1n , ω2n , …, ωnn − 2 , ωnn − 1 in the complex plane
A group (S, *) is a set S together with a binary operation * denoted on S for which
the following four properties hold. The first property is to that for all a, b ∈ S we
have a * b ∈ S. This is to say that the group (S, *) has closure. The second property
is to that there is an element e ∈ S, called the identity of the group (S, *), such that e
* a = a * e = a for all a ∈ S. The third property is to that for all a, b, c ∈ S we have
(a * b) * c = a * (b * c). This indicates that the group (S, *) has associativity. The
fourth property is to that for each a ∈ S, there exists a unique element b, called the
inverse of a, such that a * b = b * a = e. If a group (S, *) satisfies the commutative
law a * c = c * a for all a, c ∈ S, then it is an abelian group.
We assume that a set S is {ωn0 , ωn1 , ωn2 , . . . , ωnn−2 , ωnn−1 } that is a set of n complex
nth roots of unity. We also assume that a binary operation × is multiplication denoted
on S. We use Lemma 4.1 to demonstrate that (S = {ωn0 , ωn1 , ωn2 , . . . , ωnn−2 , ωnn−1 },
×) is an Abelian group.
j
For all ωn , ωkn , ωln ∈ S to 0 ≤ j ≤ (n − 1) and 0 ≤ k ≤ (n − 1) and 0 ≤ l ≤ (n −
j (j+k)+l j+(k+l) j j
1), we have (ωn × ωkn ) × ωln = ωn = ωn = ωn × ωnk+l = ωn × (ωkn × ωln ).
This implies that it satisfies associativity. For each element ωkn ∈ S to 0 ≤ k ≤ (n -
1), we have ωkn × ωnn−k = ωnn−k × ωkn = ωnn = 1 = ω0n . This is to say that ωnn−k is the
j
inverse of ωkn . For all ωn , ωkn ∈ S to 0 ≤ j ≤ (n − 1) and 0 ≤ k ≤ (n − 1), we have
j j+k k+j j
ωn × ωn = ωn = ωn = ωkn × ωn . This is to say that it satisfies the commutative
k
law. Therefore, from the statements above, we infer at once that (S = {ω0n , ω1n , ω2n ,
…, ωnn − 2 , ωnn − 1 }, ×) is an abelian group.
We use the following lemmas to introduce essential properties of the complex nth
roots of unity.
Lemma 4.4 For any positive even integer n, the squares of the n complex nth roots
of unity are the (n/2) complex (n/2)th roots of unity.
Proof The complex nth roots of unity are ωkn for any nonnegative integer k. The
complex (n/2)th roots of unity are ωkn/2 for any nonnegative integer k. Because
2
the squares of the complex nth roots of unity are (ωkn )2 , we have ωnk =
√ 2 √ √
−1×2×π×k/n −1×2×π×k×2/( n2 )×2 −1×2×π×k/( n2 ) k
e =e =e = ωn/2 . This is to say
that the squares of the complex nth roots of unity are the complex (n/2)th roots of
unity.
4.3 Description to Properties of Complex … 167
2 k+(n/ 2) 2
From Lemma 4.3, we have ωn/ n = −1. This implies that ωn = ωkn × ωn/
n
k k k+(n/ 2) 2 k 2 k 2
= ωn × (−1) = −ωn . Thus, we obtain (ωn ) = (−ωn ) = (ωn ) . This indicates
that ωkn and ωnk+(n/ 2) have the same square. This is also to say that if we square all of
the complex nth roots of unity, then we actually obtain each complex (n/2)th roots
of unity twice. Therefore, from the statements above, we at once infer that for any
positive even integer n, the squares of the n complex nth roots of unity are the (n/2)
complex (n/2)th roots of unity.
Lemma 4.5 For any integer n ≥ 1 and nonzero integer k not divisible by n,
n−1
kj
ωn = 0. (4.8)
j=0
n−1 j
Proof The common ratio to ωnk in Eq. (4.8) is ωkn . Because nonzero integer k is
j=0
n−1 j
not divisible by n, the value of ωkn is not equal to one. Therefore, we have ωnk =
j=0
k n
k
ωn − 1 / ωnk − 1 = ωnn − 1 / ωnk − 1 = (1)k − 1 / ωnk − 1 = (1 −
1)/ ωnk − 1 = (0)/ ωnk − 1 = 0.
n−1
1
yk = √ × al × ωnk×l (4.10)
n l=0
n−1
1
ak = √ × yl × ωn−k×l . (4.12)
n l=0
The matrix IDFT of the inverse discrete Fourier transform in Eq. (4.11) is actually
to the conjugate transpose of the matrix DFT of the discrete Fourier transform in
Eq. (4.9). Similarly, the matrix DFT of the discrete Fourier transform in Eq. (4.9) is
actually to the conjugate transpose of the matrix IDFT of the inverse discrete Fourier
transform in Eq. (4.11). We use the following two lemmas to show that the matrix
DFT of the discrete Fourier transform in Eq. (4.9) is a unitary matrix (a unitary
operator) and the matrix IDFT of the inverse discrete Fourier transform in Eq. (4.11)
is also a unitary matrix (a unitary operator).
Lemma 4.6 The matrix DFT of the discrete Fourier transform in Eq. (4.9) is a
unitary matrix (a unitary operator).
Without losing generality, we assume that n is a power of two (2). The length of
each element in the n dimensional orthonormal basis α is unity. The inner product of
any two elements in the n dimensional orthonormal basis α is zero. Each element in
170 4 Quantum Fourier Transform and Its Applications
α is a computational basis vector. We use computational basis state |0 to encodes the
first computational basis vector (1, 0, 0, …, 0)T . We apply computational basis state
|1 to encode the second computational basis vector (0, 1, 0, …, 0)T . We make use
of computational basis state |2 to encode the third computational basis vector (0, 0,
1, …, 0)T and so on with that we use computational basis state |n − 1 to encode the
last computational basis vector (0, 0, 0, …, 1)T . This is to say that the basis {|0, |1,
|2, …, |n − 1} encodes a n dimensional orthonormal basis α of a Hilbert space.
A superposition |β is a linear combination of each computational basis vector
(or each computational basis state) in α such that the superposition |β is as follows
n−1
n−1
β = β × a = βa |a . (4.14)
a
a=0 a=0
In the new superposition |y in Eq. (4.15), the Fourier coefficient yk for 0 ≤ k ≤
(n − 1) is as follows
n−1
1
yk = √ × βa × ωnk×a . (4.16)
n a=0
Applying QFT (the quantum Fourier transform) to computational basis state |a
for 0 ≤ a ≤ (n − 1) generates
n−1
1
QFT × (|a) = QFT(|a) = √ × ωnk×a |k . (4.17)
n k=0
4.5 The Quantum Fourier Transform of Implementing … 171
QFT(|a) = QFT(|a1 a2 a3 . . . a N )
⎛ √ ⎞
0+e −1×2×π×0.a N |1
=⎝ √ ⎠
2
⎛ √ ⎞
0+e −1×2×π×0.a N −1 a N |1
⊗⎝ √ ⎠
2
⎛ √ ⎞
0+e −1×2×π×0.a N −2 a N −1 a N |1
⊗⎝ √ ⎠
2
⎛ √ ⎞
0+e −1×2×π×0.a1 a2··· a N |1
⊗ ... ⊗ ⎝ √ ⎠ (4.18)
2
Lemma 4.8 The product representation (4.18) is equivalent to the definition (4.17)
for the quantum Fourier transform and is to the definition of the quantum Fourier
transform.
We write computational basis state |k for 0 ≤ k ≤ (2N − 1) using the binary
representation k = k 1 k 2 k 3 … k N = k 1 × 2N − 1 + k 2 × 2N − 2 + k 3 × 2N − 3 +
… + k N × 20 . We also write the notation 0. k 1 k 2 k 3 … k N to represent the binary
fraction k 1 × 2−1 + k 2 × 2−2 + k 3 × 2−3 + … + k N × 2−N . The computation of
the division k/2N is to k/2N = k 1 k 2 k 3 … k N /2N = (k 1 × 2N − 1 + k 2 × 2N − 2 + k 3
× 2N − 3 + … + k N × 20 )/2N = k 1 × 2−1 + k 2 × 2−2 + k 3 × 2−3 + … + k N ×
N 2 N −1
2−N = kl × 2−l . The computation of the sum is to complete the sum of
l=1 k=0
1 1
2N items. The computation of the sum ··· also is to complete the sum of 2N
k1 =0 k N =0
N
2 −1 1 1
items. If they process the same items, then = ··· . Therefore,
k=0 k1 =0 k N =0
we rewrite the definition (4.19) of the quantum Fourier transform as follows
⎛
1 1 √ N
1 −1×2×π×a× kl ×2−l
QFT(|a) = √ ×⎝ ··· e l=1 |k 1 · · · k N ) .
2N k1 =0 k N =0
(4.20)
√
e( −1×2×π×a×kl ×2 ) |kl ) is equal
N −l
The
√ product representation (⊗l=1
√
e( −1×2×π×a×k1 ×2 ) |k1
−1
e( −1×2×π×a×k2 ×2 ) |k2
−2
to ⊗ ⊗ … ⊗
√
e(
−N
−1×2×π×a×k N ×2 ) |k N =
√ √
e( −1×2×π×a×k1 ×2 ) × · · · × e( −1×2×π×a×k N ×2 ) |k1 · · · k N =
−1 −N
√
e( −1×2×π×a×(k1 ×2 +···+k N ×2 ) |k1 · · · k N
−1 −N
⎛ ⎞
√ N
−1×2×π×a× kl ×2−l
= ⎝e l=1 |k1 · · · k N ⎠. Hence, we rewrite the definition (4.20)
1 √ √
e( −1×2×π×a×0×2 ) 0 + e( −1×2×π×a×1×2 ) 1
−l −l
N
= √ × ⊗l=1 (4.23)
2N
4.5 The Quantum Fourier Transform of Implementing … 173
1 √
0 + e( −1×2×π×a×1×2 ) 1 .
−1
N
= √ × ⊗l=1 (4.24)
2N
= e( √1 N −l. N −l+1 N )
−1×2×π×a ···a a ···a
= √ e( −1×2×π×(a 1 ···a
N −l +0.a N −l+1 ···a N ))
= e( −1×2×π×(a 1 ···a N −l )
)
× e( −1×2×π×(0.a N −l+1 ···a N )
)
=
√ √
1 × e( −1×2×π×(0.a N −l+1 ···a N )) = e( −1×2×π×(0.a N −l+1 ···a N )) . Next, we rewrite the
definition (4.24) of the quantum Fourier transform as follows
QFT(|a) = QFT(|a1 a2 a3 . . . a N )
⎛ √ ⎞ ⎛ √ ⎞
0+e −1×2×π×0.a N |1 0 + e −1×2×π×0.a N −1 a N |1
=⎝ √ ⎠⊗⎝ √ ⎠
2 2
⎛ √ ⎞
0+e −1×2×π×0.a N −2 a N −1 a N |1
⊗⎝ √ ⎠
2
⎛ √ ⎞
0+e −1×2×π×0.a1 a2 ···a N |1
⊗ ... ⊗ ⎝ √ ⎠. (4.25)
2
Therefore, from the statements above we at once infer that the product represen-
tation (4.18) is equivalent to the definition (4.17) for the quantum Fourier transform
and is to the definition of the quantum Fourier transform.
“If C is true, then do D”. This type of controlled operations is one of the most useful
in deriving a very efficient circuit for the quantum Fourier transform. A (2 × 2)
matrix Rk, c is as follows
1 √ 0 1 0
Rk,c = k = . (4.26)
0 e −1×2×π/2 0 ω21k
1 0 1 0 1 0
Because Rk, c × Rk,c = ( × ) = ( ) =
0 ω21k 0 ω2−1
k 0 ω21k × ω2−1
k
1 0 1 0 10 1 0 1 0
( 1−1 ) = ( 0 )=( ) and Rk,c × Rk, c = ( −1 × )=
0 ω2k 0 ω2 k 01 0 ω2k 0 ω21k
1 0 1 0 1 0 10
( 1 )=( −1+1 ) = ( 0 )=( ), matrix Rk, c is a unitary
0 ω2−1
k × ω2k
0 ω2k
0 ω2k
0 1
matrix (a unitary operator) and matrix Rk,c is a unitary matrix (a unitary operator). A
controlled-Rk, c operation is an operation of two quantum bits that contain a control
bit and a target bit. If the control bit is set to |1, then matrix Rk, c is applied to the
target bit. Otherwise, matrix Rk, c does not change the target bit.
Similarly, a controlled-Rk,c operation is an operation of two quantum bits that
include a control bit and a target bit. If the control bit is set to |1, then matrix Rk,c
is applied to the target bit. Otherwise, matrix Rk,c does not change the target bit.
The matrix representation of the controlled-Rk, c operation and the controlled-Rk,c
operation is
⎡ ⎤ ⎡ ⎤
1 0 0 0 1 0 0 0
⎢0 1 0 0 ⎥ ⎢0 1 0 0 ⎥
C Rk,c =⎢ ⎥ and C Rk,c = ⎢ ⎥. (4.28)
⎣0 0 1 √ 0 ⎦ ⎣0 0 1 √ 0 ⎦
−1×2×π/2k − −1×2×π/2k
0 0 0e 0 0 0e
Matrix C Rk,c in Eq. (4.28) is the conjugate transpose of matrix CRk, c in Eq. (4.28).
⎡ ⎤ ⎡ ⎤
10 00 10 00
⎢01 00 ⎥ ⎢01 00⎥
Because C Rk,c ×C Rk,c = ⎢ ⎣00 1 ⎦ ⎣ 0 0 1 0 ⎦ and C Rk,c
⎥=⎢ ⎥
√
0
k
0 0 0 e −1×2×π/2 ×(1−1) 00 01
⎡ ⎤ ⎡ ⎤
10 00 10 00
⎢01 0 0 ⎥ ⎢01 00⎥
× CRk, c = ⎢ ⎦ ⎣ 0 0 1 0 ⎦, CRk, c × C Rk,c is an
⎥=⎢ ⎥
⎣00 1 0
√ k
0 0 0 e −1×2×π/2 ×(−1+1) 00 01
identity matrix and C Rk,c × C Rk,c also is an identity matrix. Therefore, C Rk,c ×
Ci Rk,c = C Rk,c × CRk, c and matrix CRk, c and matrix C Rk,c are both a unitary matrix
(a unitary operator). Figure 4.2 shows the circuit representation of the controlled-Rk, c
and Fig. 4.3 shows the circuit representation of the controlled-Rk,c operation.
A (4 × 4) matrix swap and its conjugate transpose swap are respectively
4.6 Quantum Circuits of Implementing the Quantum … 175
Fig. 4.2 Controlled-Rk, c operation with that the bottom line is the control bit and the top line is
the target bit
Fig. 4.3 Controlled-Rk,c operation with that the bottom line is the control bit and the top line is
the target bit
⎡ ⎤ ⎡ ⎤
1 0 0 0 10 0 0
⎢0 0 1 0⎥ ⎢0 0 1 0⎥
swap = ⎢ ⎥ and swap = ⎢ ⎥. (4.29)
⎣0 1 0 0⎦ ⎣0 1 0 0⎦
0 0 0 1 00 0 1
⎡ ⎤
10 00
⎢01 00⎥
Because swap × swap = ⎢ ⎣ 0 0 1 0 ⎦ = swap × swap, matrix swap and matrix
⎥
00 01
swap are both a unitary matrix (a unitary operator). Matrix swap is to matrix repre-
sentation of a swap gate of two quantum bits. The functionality of the swap gate
is to exchange the information contained in two quantum bits. The left picture in
Fig. 4.4 is the circuit representation of a swap gate with two quantum bits and the
right picture in Fig. 4.4 is to the circuit representation of implementing the swap gate
by means of using three CNOT gates.
An initial state vector is |β0 = |A ⊗ |Bfor the left circuit and the right circuit
in Fig. 4.4, where A ∈ {0, 1} and B ∈ {0, 1}. The output of the swap gate in the left
circuit of Fig. 4.4 is |β1 = |B ⊗ |A. The output of the first CNOT gate with the
control bit |A and the target bit |B in the right circuit of Fig. 4.4 is |θ1 = |A ⊗ |A
⊕ B. Next, the output of the second CNOT gate with the control bit |A ⊕ B and
176 4 Quantum Fourier Transform and Its Applications
Fig. 4.4 Circuit Representation of a swap gate with two quantum bits
the target bit |A in the right circuit of Fig. 4.4 is |θ2 = |A ⊕ (A ⊕ B) ⊗ |A ⊕ B =
|A ⊕ A ⊕ B ⊗ |A ⊕ B = |0 ⊕ B ⊗ |A ⊕ B = |B ⊗ |A ⊕ B. Finally, the output of
the third CNOT gate with the control bit |B and the target bit |A ⊕ B in the right
circuit of Fig. 4.4 is |θ3 = |B ⊗ |(A ⊕ B) ⊕ B = |B ⊗ |A ⊕ B ⊕ B = |B ⊗ |A
⊕ 0 = |B ⊗ |A. Because the two state vectors |β1 and |θ3 are the same, the left
circuit of Fig. 4.4 is equivalent to the right circuit of Fig. 4.4.
The product representation (4.18) makes it easy to infer a very efficient circuit
of implementing the quantum Fourier transform. Such a circuit appears in Fig. 4.5.
Consider what happens when the state |a1 · · · a N is an input for the pictured circuit in
Fig. 4.5 that computes the quantum Fourier transform. Making use of the Hadamard
gate to the first quantum bit generates the following state
1 √
√ |0 + e −1×2×π×0.a1 |1 |a2 · · · a N (4.30)
2
√ √ √ 1
−1×2×π×( 2 )
√ When a1 = 1, we obtain e −1×2×π×0.a√1 = e −1×2×π×0.1 √ = e =
−1×π −1×2×π×0.a1 −1×2×π×0.0
e = −1. When a1 = 0, we obtain e =e = 1. This
satisfies the functionality of the Hadamard gate.
Next, using the controlled-R2, 2 gate produces the following state
1 √ √
√ |0 + e −1×2×π×0.a1 × e −1×2×π×0.0a2 |1 |a2 · · · a N
2
1 √
= √ |0 + e −1×2×π×(0.a1 +0.0a2 ) |1 |a2 · · · a N
2
1 √
= √ |0 + e −1×2×π×0.a1 a2 |1 |a2 · · · a N . (4.31)
2
4.6 Quantum Circuits of Implementing the Quantum … 177
√
When a2 = 1 is√ equivalent to the √
control bit |a2 to be |1, it adds the phase
1
e −1×2×π×( 4 ) = e −1×2×π×0.01 = e −1×2×π×0.0a2 to the coefficient of the first |1.
When a2 = 0 is equivalent to the√control bit |a2 to√be |0, it does not change the
coefficient of the first |1 because e −1×2×π×0.0a2 = e −1×2×π×0.00 = 1. This satisfies
the functionality of the controlled-R2, 2 gate.
Next, applying the controlled-R3, 3 gate generates the following state
1 √ √
√ | 0 + e −1×2×π×0.a1 a2 × e −1×2×π×0.00a3 | 1 | a2 · · · a N
2
1 √
= √ | 0 + e −1×2×π×(0.a1 a2 +0.00a3 ) | 1 | a2 · · · a N
2
1 √
= √ | 0 + e −1×2×π×0.a1 a2 a3 | 1 | a2 · · · a N (4.32)
2
√
When a3 = 1 is√ equivalent to the √control bit |a3 to be |1, it adds the phase
1
e −1×2×π×( 8 ) = e −1×2×π×0.001 = e −1×2×π×0.00a3 to the coefficient of the first
|1. When a3 = 0 is equivalent to the control
√
bit |a3 to be√ |0, it does not change
the coefficient of the first |1 because e −1×2×π×0.00a3 = e −1×2×π×0.000 = 1. This
satisfies the functionality of the controlled-R3, 3 gate.
We continue making use of the √
controlled-R4, 4 , R5, 5 through√
RN, N gates,
−1×2×π×0.000a4 −1×2×π×0.0000a5
they subsequently add √
the phase (e ), the phase (e )
through the phase (e −1×2×π×0.00···a N ) to the coefficient of the first |1. At the end of
this procedure, we obtain the following state
√ √
−1×2×π×0.a1 a2 a3
1 | 0 + e × e −1×2×π×0.000a4
√ √ √ | a2 · · · a N
2 ×e −1×2×π×0.0000a5 × · · · × e −1×2×π×0.00···a N | 1
1 √
= √ | 0 + e −1×2×π×(0.a1 a2 a3 +0.000a4 +0.0000a5 +0.00···a N ) | 1 | a2 · · · a N
2
1 √
= √ | 0 + e −1×2×π×0.a1 a2 a3 a4 a5 ···a N | 1 | a2 · · · a N (4.33)
2
Next, we complete a similar procedure on the second quantum bit. Applying the
Hadamard gate to the second quantum bit produces the following state
√ √
−1×2×π×0.a1 a2 ···a N −1×2×π×0.a2
|0 + e |1 |0 + e |1
√ √ |a3 · · · a N . (4.34)
2 2
We continue in this fashion for each quantum bit, giving a final state
√ √
−1×2×π×0.a1 a2 ···a N −1×2×π×0.a2 ···a N
|0 + e |1 |0 + e |1
√ √
2 2
√
−1×2×π×0.a N
|0 + e |1
... √ . (4.36)
2
Next, applying swap operations reverses the order of the quantum bits. After the
swap operations, the state of the quantum bits is
⎛ √ ⎞ ⎛ √ ⎞
0+e −1×2×π×0.a N |1 0+e −1×2×π×0.a N −1 a N |1
⎝ √ ⎠⊗⎝ √ ⎠
2 2
⎛ √ ⎞ ⎛ √ ⎞
0+e −1×2×π×0.a N −2 a N −1 a N |1 0+e −1×2×π×0.a1 a2 ···a N |1
⊗⎝ √ ⎠ ⊗ ... ⊗ ⎝ √ ⎠
2 2
(4.37)
Comparing with Eq. (4.18) and Eq. (4.37), we look at the desired output from the
quantum Fourier transform. This construction also demonstrates that the quantum
Fourier transform is unitary because each gate in the circuit of Fig. 4.5 is unitary.
How many gates does the circuit of implementing the quantum Fourier transform in
Fig. 4.5 use? We begin by means of doing a Hadamard gate and (n − 1) conditional
rotations on the first quantum bit. This requires a total of n gates. Next step (the
second step) is to do a Hadamard gate and (n − 2) conditional rotations on the
second quantum bit. This requires a total of n + (n − 1) gates. Next step (the third
step) is to do a Hadamard gate and (n − 3) conditional rotations on the third quantum
bit. This requires a total of n + (n − 1) + (n − 2) gates. Continuing in this way, this
requires a total of n + (n − 1) + (n − 2) + … + 1 = n × (n + 1)/2 gates, plus at most
n/2 swap gate s that reverse the order of the quantum bits. Because each swap gate
can be implemented by means of using three controlled-NOT gates, this circuit in
Fig. 4.5 provides a O(n2 ) algorithm for completing the quantum Fourier transform.
The fast Fourier transform is the best classical algorithm to compute the discrete
Fourier transform on 2n elements applying O(n × 2n ) gates. This is to say that it
requires exponentially more operations to compute the discrete Fourier transform
on a digital (classical) computer than it does to implement the quantum Fourier
transform on a quantum computer. The Fourier transform is a critical step for many
4.7 Assessment of Time Complexity for Implementing … 179
applications in the real world. Computer speech recognition is one of the most impor-
tant applications to the Fourier transform. The first step in phoneme recognition of
computer speech recognition is to Fourier transform the digitized sound. Can we
make use of the quantum Fourier transform to enhance the performance of these
Fourier transforms? Unfortunately, there is no known way to complete this. Because
measurement in a quantum computer cannot directly access the amplitudes, there is
no way of computing the Fourier transformed amplitudes of the original state. Worse
still, a problem is to that there is in general no way to efficiently prepare the original
state to be Fourier transformed.
The matrix IQFT of the inverse quantum Fourier transform is the same as the matrix
IDFT of the inverse discrete Fourier transform in Eq. (4.11). Because IQFT and
IDFT are the same matrix and IDFT is a unitary matrix (a unitary operator) from
Lemma 4.7, IQFT is also a unitary matrix (a unitary operator). The inverse quantum
Fourier transform is a computationally very efficient implementation to the inverse
discrete Fourier transform in the form of tracing back IQFT to its tensor product
decomposition. In the following, we take n = 2N , where N is the number of quantum
bit in a quantum computer and the basis {|0, |1, |2, …, |2N − 1} is
the computa-
= ω2−k×a
−k×a
tional
√ basis for the quantum
computer. We mention here that ωn N =
e ( − −1×2×π×k×a )/2 N
.
Applying IQFT (the inverse quantum Fourier transform) to computational basis
state |a for 0 ≤ a ≤ (2N − 1) yields
IQFT × (| a ) = IQFT(| a )
⎛ N
⎞
2 −1
1
= √ ×⎝ ω2−k×a
N |k ⎠
2N k=0
⎛ N
⎞
2 −1 √
1 (− −1×2×π×k×a )/2 N
= √ ×⎝ e |k ⎠. (4.38)
2N k=0
It is very useful to write computational basis state |a for 0 ≤ a ≤ (2N − 1) using
the binary representation a = a1 a2 a3 … aN = a1 × 2N − 1 + a2 × 2N − 2 + a3 ×
2N − 3 + … + aN × 20 . It is also very useful to write the notation 0. a1 a2 a3 … aN
to represent the binary fraction a1 × 2−1 + a2 × 2−2 + a3 × 2−3 + … + aN × 2−N .
The following very useful product representation is to tensor product decomposition
of the inverse quantum Fourier transform:
180 4 Quantum Fourier Transform and Its Applications
IQFT(| a ) = IQFT |a1 a2 a3 . . . a N
⎛ √ ⎞ ⎛ √ ⎞
0 + e− −1×2×π×0.a N 1 0 + e− −1×2×n×0.a N −1 a N 1
= ⎝ √ ⎠ ⊗ ⎝ √ ⎠
2 2
⎛ √ ⎞
0 + e− −1×2×π×0.a N −2 a N −1 a N 1
⊗⎝ √ ⎠
2
⎛ √ ⎞
0 + e− −1×2×π×0.a1 a2 ···a N 1
⊗ ... ⊗ ⎝ √ ⎠ (4.39)
2
Lemma 4.9 The product representation (4.39) is equivalent to the definition (4.38)
for the inverse quantum Fourier transform and is to the definition of the inverse
quantum Fourier transform.
Proof Because we take n = 2N to use IQFT (the inverse quantum Fourier transform)
to computational basis state |a for 0 ≤ a ≤ (2N − 1), the definition (4.38) of the
inverse quantum Fourier transform is
⎛ N
⎞ ⎛ N
⎞
2 −1 2 −1 √
1 1 − −1×2×π×k×a)/2 N
IQFT(|a) = √ ⎝ ω2−k×a
N |k⎠ = √ ⎝ e |k⎠
2 N 2N
k=0 k=0
(4.40)
We write computational basis state |k for 0 ≤ k ≤ (2N − 1) applying the binary
representation k = k 1 k 2 k 3 … k N = k 1 × 2N − 1 + k 2 × 2N − 2 + k 3 × 2N − 3 +
… + k N × 20 . We also write the notation 0. k 1 k 2 k 3 … k N to represent the binary
fraction k 1 × 2−1 + k 2 × 2−2 + k 3 × 2−3 + … + k N × 2−N . The computation of
the division k/2N is to k/2N = k 1 k 2 k 3 … k N /2N = (k 1 × 2N −1 + k 2 × 2N − 2 + k 3
× 2N − 3 + … + k N × 20 )/2N = k 1 × 2−1 + k 2 × 2−2 + k 3 × 2−3 + … + k N ×
N 2 N −1
2−N = kl × 2−l . The computation of the sum is to complete the sum of
l=1 k=0
1 1
2N items. The computation of the sum ··· also is to complete the sum of
k1 =0
kNN =0
2 −1 1 1
2N items. If they process the same items, then = ··· . Hence, we
k=0 k1 =0 k N =0
rewrite the definition (4.40) of the inverse quantum Fourier transform as follows
4.8 The Inverse Quantum Fourier Transform of Implementing … 181
⎛ ⎞
1 1 √ N
1 − −1×2×π×a× kl ×2−l
IQFT( |a ) = √ ×⎝ ··· e l=1 |k1 . . . k N ⎠
2N k1 =0 k N =0
(4.41)
√
e(− −1×2×π×a×kl ×2 ) |kl
N −l
The product representation ⊗l=1 is equal
√ √
e(− −1×2×π×a×k1 ×2 ) |k1
−1
e(− −1×2×π×a×k2 ×2 ) |k2
−2
to ⊗ ⊗ … ⊗
√
e(− −1×2×π×a×k N ×2 ) |k N =
−N
√ √
e(− −1×2×π×a×k1 ×2 ) × · · · × e(− −1×2×π×a×k N ×2 ) |k1 · · · k N =
−1 −N
√
e(− −1×2×π×a×(k1 ×2 +···+k N ×2 ) |k1 · · · k N =
−1 −N
⎛ ⎞
√ N
− −1×2×π×a× kl ×2−l
⎝e l=1 |k1 · · · k N ⎠. Therefore, we rewrite the definition
1 √ √
e(− −1×2×π×a×0×2 ) 0+e(− −1×2×π×a×1×2 ) 1 (4.44)
−l −l
N
= √ × ⊗l=1
2N
1 √
0 + e(− −1×2×π×a×1×2 ) 1 .
−l
N
= √ × ⊗l=1 (4.45)
2N
IQFT(| a ) = IQFT(|a1 a2 a3 . . . a N )
⎛ √ ⎞ ⎛ √ ⎞
0 + e− −1×2×π×0.a N 1 0 + e− −1×2×π×0.a N −1 a N 1
=⎝ √ ⎠⊗⎝ √ ⎠
2 2
182 4 Quantum Fourier Transform and Its Applications
⎛ √ ⎞
0 + e− −1×2×π×0.a N −2 a N −1 a N 1
⊗⎝ √ ⎠
2
⎛ √ ⎞
0 + e− −1×2×π×0.a1 a2 ...a N 1
⊗ ... ⊗ ⎝ √ ⎠ (4.46)
2
Therefore, from the statements above we at once derive that the product repre-
sentation (4.39) is equivalent to the definition (4.38) for the inverse quantum Fourier
transform and is to the definition of the inverse quantum Fourier transform.
The product representation (4.39) makes it easy to design a very efficient circuit
of implementing the inverse quantum Fourier transform. Such a circuit appears in
Fig. 4.6. Consider what happens when the state |a1 · · · a N is an input for the pictured
circuit in Fig. 4.6 that computes the inverse quantum Fourier transform. Applying
the Hadamard gate to the first quantum bit yields the following state
1 √
√ |0 + e− −1×2×π×0.a1 |1 |a2 · · · a N (4.47)
2
√ √ √1
When
√ a1 = 1, we obtain e− −1×2×π×0.a√ 1
= e− −1×2×π×0.1√= e− −1×2×π×( 2 ) =
− −1×π − −1×2×π×0.a1 − −1×2×π×0.0
e = −1. When a1 = 0, we obtain e =e = 1. This
satisfies the functionality of the Hadamard gate.
Next, using the controlled-R2,2 gate generates the following state
1 √ √
√ | 0 + e− −1×2×π×0.a1 × e− −1×2×π×0.0a2 | 1 | a2 · · · a N
2
1 √
= √ | 0 + e− −1×2×π×(0.a1 +0.0a2 ) | 1 | a2 · · · a N
2
Fig. 4.6 Efficient circuit of implementing the inverse quantum Fourier transform
4.9 Quantum Circuits of Implementing the Inverse … 183
1 √
= √ | 0 + e− −1×2×π×0.a1 a2 | 1 | a2 · · · a N . (4.48)
2
When a2 = 1
√
is equivalent
√
to the control
√
bit |a2 to be |1, it adds the phase
− −1×2×π×( 41 ) − −1×2×π×0.01 − −1×2×π×0.0a2
e =e =e to the coefficient of the first
|1. When a2 = 0 is equivalent to the control
√
bit |a 2 to be √|0, it does not change
the coefficient of the first |1 because e− −1×2×π×0.0a2 = e− −1×2×π×0.00 = 1. This
satisfies the functionality of the controlled-R2,2 gate.
Next, making use of the controlled-R3,3 gate produces the following state
1 √ √
√ | 0 + e− −1×2×π×0.a1 a2 × e− −1×2×π×0.00a3 | 1 | a2 · · · a N
2
1 √
= √ | 0 + e− −1×2×π×(0.a1 a2 +0.00a3 ) | 1 | a2 · · · a N
2
1 √
= √ | 0 + e− −1×2×π×0.a1 a2 a3 | 1 | a2 · · · a N . (4.49)
2
√
When a3 = 1 is equivalent
√
to the control
√
bit |a3 to be |1, it adds the phase
1
e− −1×2×π×( 8 ) = e− −1×2×π×0.001 = e− −1×2×π×0.00a3 to the coefficient of the first
|1. When a3 = 0 is equivalent to the control
√
bit |a3 to be |0,
√
it does not change the
coefficient of the first |1 because e− −1×2×π×0.00a3 = e− −1×2×π×0.000 = 1. This
satisfies the functionality of the controlled-R3,3 gate.
We continueusing the controlled-R 4,4 , R5,5 through
√ R N ,N gates, they respectively
√
− −1×2×π×0.000a4 − −1×2×π×0.0000a5
add the phase e , the phase e through the
√
phase e− −1×2×π×0.00···a N to the coefficient of the first |1. At the end of this
procedure, we get the following state
√ √
− −1×2×π×0.a1 a2 a3
1 | 0 + e × e− −1×2×π×0.000a4
√ √ √ | a2 · · · a N
2 ×e− −1×2×π×0.0000a5 × · · · × e− −1×2×π×0.00···a N | 1
1 √
= √ | 0 + e− −1×2×π×(0.a1 a2 a3 +0.000a4 +0.0000a5 +0.00···a N ) | 1 | a2 · · · a N
2
1 √
= √ | 0 + e− −1×2×π×0.a1 a2 a3 a4 a5 ···a N | 1 | a2 · · · a N . (4.50)
2
Next, we complete a similar procedure on the second quantum bit. Using the
Hadamard gate to the second quantum bit yields the following state
√ √
−1×2×π×0.a1 a2 ···a N
|0 + e− |1 |0 + e− −1×2×π×0.a2
|1
√ √ |a3 · · · a N . (4.51)
2 2
184 4 Quantum Fourier Transform and Its Applications
Next, applying the controlled-R2,3 , R3,4 through R N −1,N gates produces the
following state
√ √
−1×2×π×0.a1 a2 ···a N
|0 + e− |1 |0 + e− −1×2×π×0.a2 ···a N
|1
√ √ |a3 · · · a N .
2 2
(4.52)
We continue in this fashion for each quantum bit, giving a final state
√ √
−1×2×π×0.a1 a2 ···a N
|0 + e− |1 |0 + e− −1×2×π×0.a2 ···a N
|1
√ √
2 2
√
− −1×2×π×0.a N
|0 + e |1
... √ . (4.53)
2
Next, making use of swap operations reverses the order of the quantum bits. After
the swap operations, the state of the quantum bits is
⎛ √ ⎞ ⎛ √ ⎞
0 + e− −1×2×π×0.a N 1 0 + e− −1×2×π×0.a N −1 a N 1
⎝ √ ⎠⊗⎝ √ ⎠
2 2
√
|0 + e− −1×2×π×0.a N −2 a N −1 a N |1
⊗ √
2
⎛ √ ⎞
0 + e− −1×2×π×0.a1 a2 ···a N 1
⊗ ... ⊗ ⎝ √ ⎠. (4.54)
2
Comparing with Eq. (4.39) and Eq. (4.54), we look at the desired output from
the inverse quantum Fourier transform. This construction also shows that the inverse
quantum Fourier transform is unitary because each gate in the circuit of Fig. 4.6 is
unitary.
How many gates does the circuit of implementing the inverse quantum Fourier trans-
form in Fig. 4.6 apply? We start by means of doing a Hadamard gate and (n − 1)
conditional rotations on the first quantum bit. This requires a total of n gates. Next
similar procedure is to implement a Hadamard gate and (n − 2) conditional rotations
on the second quantum bit. This requires a total of n + (n − 1) gates. Next similar
4.10 Assessment of Time Complexity for Implementing … 185
However, we can use another way (the second way) to obtain the period r and the
frequency f to a given oracular function Of . In another way, the value √1 4 is the
2
magnitude of the amplitude (the weight) to each input a1 a2 a3 a4 . The square of the
absolute value for the magnitude of the amplitude (the weight) to an input a1 a2 a3
a4 is the possibility of obtaining the output of Of that takes the input a1 a2 a3 a4 as
its input value. The value (2 × π × (1/2) × a4 ) is the phase among the amplitudes
of these inputs. The phase can take any value from 0 degree to 360 degrees. The
phase of each input from a01 a02 a03 a04 through a11 a12 a13 a14 is subsequently 0°, 180°,
0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0° and 180°. We think of
the input domain of Of as the time domain and the phases from its output as signals.
Computing the period r and the frequency f of Of is equivalent to determine the
period r and the frequency f of signals in the time domain (the input domain).
Because the phase of the output of each input from a01 a02 a03 a04 through a11 a12
1 1
a3 a4 is subsequently 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°,
180°, 0° and 180°, we take the sixteen input values as the corresponding sixteen time
units and the sixteen phases as the sixteen samples of signals. Each sample encodes
an angle. The angle can take 0° or 180°. The sixteen input values from a01 a02 a03 a04
through a11 a12 a13 a14 corresponds to sixteen time units from zero through fifteen. We
use Fig. 4.7 to explain the reason of why computing the period r and the frequency
f of Of is equivalent to determine the period r and the frequency f of signals in the
time domain (the input domain). In Fig. 4.7, the horizontal axis is to represent the
time domain in which it contains the input domain of Of and the vertical axis is to
represent signals in which it consists of the sixteen phases from its output. From
Fig. 4.7, the signal rotates back to its starting value 0° eight times. This is to say that
there are eight periods of signals per sixteen time units and the frequency f of signals
is equal to eight. This gives that the period r of signals is equal to (16/8)
= (2).
Because the magnitude of each output to Of is the same and is equal to √1 4 and
2
the signal (the phase of each output to Of ) rotatesbackto its starting value 0° eight
times, its output rotates back to its starting value √1 4 eight times. This indicates
2
that the number of the period per sixteen outputs is eight and the frequency f of Of
is equal to eight. Therefore, this gives that the period r of Of is 16/8 = 2.
Because in Fig. 4.7 in subsection 4.11.1 sampling sixteen points just encodes sixteen
phases from sixteen output of Of , we use circle notation (another concept) to explain
how figuring out the period r and the frequency f of Of is equivalent to determine
the period r and the frequency f of signals in the time domain (the input domain).
In a circle, the filled radius represents the magnitude of the amplitude to an input a1
a2 a3 a4 of Of . This implies that the size (the shaded area) of the circle is directly
proportional to the square of the absolute value of the magnitude of the amplitude to
that input a1 a2 a3 a4 of Of . This means that the size (the shaded area) of the circle
is directly proportional to the possibility of obtaining the output of Of that takes that
input a1 a2 a3 a4 as its input value. A darker line drawn in the circle indicates that the
phase (a positive angle) rotates the circle counterclockwise or the phase (a negative
angle) rotates the circle clockwise. A number a1 a2 a3 a4 below the circle encodes
an input to Of . In Fig. 4.8, it contains sixteen outputs of a given oracular
√
function
1
that is Of : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π× 2 ×a4 |a4
2
∈ {0, 1}}. In Fig. 4.8, sixteen circles encode sampling sixteen points in Fig. 4.7 and
sixteen numbers below each circle encode sixteen time units of the time domain of
Fig. 4.7.
Because in Fig. 4.8 the shaded area of each circle is directly proportional to
2
√1 , the shaded area of each circle is the same. In Fig. 4.8, the radius of the left
24
first circle is √1 4 = (1/4) that is the magnitude of output to Of (a01 a02 a03 a04 ). The
2
darker line drawn in the left first circle indicates that the phase to output of Of (a01
a02 a03 a04 ) is a 0° and gives a 0° rotation to the left first circle. A number a01 a02 a03 a04
Fig. 4.8 Sixteen outputs of a given oracular function that is Of : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1
√
−1×2×π × 21 ×a4
≤ d ≤ 4} → { √1 4 × e | a4 ∈ {0, 1}}
2
188 4 Quantum Fourier Transform and Its Applications
below the left first circle encodes the input that istaken by Of (a01 a02 a03 a04 ). Similarly,
in Fig. 4.8, the radius of the left second circle is √1 = (1/4) that is the magnitude
24
of output to Of (a01 a02 a03 a14 ).
The darker line drawn in the left second circle indicates
that the phase to output of Of (a01 a02 a03 a14 ) is a 180° and gives a 180° rotation to
the left second circle. A number a01 a02 a03 a14 below the left second circle encodes
the input that is taken by Of (a01 a02 a03 a14 ). Similarly,
in Fig. 4.8, the radius of the
left third circle through the last circle is all √1 = (1/4) that is the magnitude of
24
output to Of (a01 a02 a13 a04 )
through Of (a11 a12 a13 a14 ).
The darker line drawn in the left
third circle through the last circle indicates that the phase to output of Of (a01 a02 a13
a04 ) through Of (a11 a12 a13 a14 ) is subsequently 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°,
180°, 0°, 180°, 0° and 180°. They subsequently give a 0° rotation and a 180° rotation
of seven times to the left third circle through the last circle. Fourteen numbers a01 a02
a13 a04 through a11 a12 a13 a14 below the left third circle trough the last circle encode the
fourteen inputs that are subsequently taken by Of (a01 a02 a13 a04 ) through Of (a11 a12 a13
a14 ).
From Fig. 4.8, hidden patterns and information stored in signals of sixteen circles
(sampling sixteen points) are that the signal (the phase) rotates back to its starting
value 0° eight times. This indicates that there are eight periods of signals per sixteen
inputs (per sixteen time units) and the frequency f of signals is equal to eight. This
gives that the period r of signals is equal to (16/8) = (2). Because the signal (the
phase of each output to Of ) rotates back
to its starting value 0° eight times, its output
rotates back to its starting value √1 4 eight times. This implies that the number of
2
the period per sixteen outputs is eight and the frequency f of Of is equal to eight.
Therefore, this gives that the period r of Of is 16/8 = 2. So, when a given oracular
function Of takes two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 2 as its input values, it
gives the same output to the two inputs. This indicates that Of (a1 a2 a3 a4 ) = Of (a1
a2 a3 a4 + 2) to any two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 2. In this way, for
obtaining the period r and the frequency f of Of , it only needs to implement eight
multiplications (2 × π × (1/2) × 1) (180°) and eight multiplications (2 × π × (1/2)
× 0) (0°). The second way very significantly enhances the performance of the first
way to determine the period r and the frequency f of Of .
In Listing 4.1, the program is in the backend that is simulator of Open QASM with
thirty-two quantum bits in IBM’s quantum computer. The program is to determine
the frequency f and the period r of Of so that Of (a1 a2 a3 a4 ) = Of (a1 a2 a3 a4 + r)
to any two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + r. In Listing 4.1, we introduce how to
4.11 Compute the Period and the Frequency of a Given Oracular Function 189
Fig. 4.9 The quantum circuit of computing the frequency f and the√period r to a given oracular
1
function Of : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π × 2 ×a4 | a4 ∈ {0, 1}}
2
use the inverse quantum Fourier transform to find the frequency f and the period r of
Of . Figure 4.9 is the quantum circuit of computing the frequency f and the period r of
Of . The statement “OPENQASM 2.0;” on line one of Listing 4.1 is to point out that
the program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 4.1 is to continue parsing the file “qelib1.inc” as
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current working directory.
Listing 4.1 The program of determining the frequency f and the period r to a given
oracular function Of : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 ×
√ 24
−1×2×π× 21 ×a4
e |a4 ∈ {0, 1}}.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[4];
4. creg c[4];
Next, the statement “qreg q[4];” on line three of Listing 4.1 is to declare that in
the program there are four quantum bits. In the left top of Fig. 4.9, four quantum bits
are respectively q[0], q[1], q[2] and q[3]. The initial value of each quantum bit is set
to state |0. We use four quantum bits q[0], q[1], q[2] and q[3] to encode the input
domain {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} of Of . This is to say that quantum
bit q[0] encodes bit a1 , quantum bit q[1] encodes bit a2 , quantum bit q[2] encodes
bit a3 and quantum bit q[3] encodes bit a4 .
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 3 is to represent the
value 0 of q[k] and q[k]1 for 0 ≤ k ≤ 3 is to represent the value 1 of q[k]. Similarly,
for the convenience of our explanation, an initial state vector of determining the
frequency f and the period r of Of so that Of (a1 a2 a3 a4 ) = Of (a1 a2 a3 a4 + r) to
any two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + r is
|Ω0 =|q[0]0 q[1]0 q[2]0 q[3]0 =0|0|0|0 = |0000 .
190 4 Quantum Fourier Transform and Its Applications
The corresponding decimal value of the initial state vector |0 is 23 × q[0]0 + 22
× q[1]0 + 21 × q[2]0 + 20 × q[3]0 . This implies that quantum bit q[0]0 is the most
significant bit and quantum bit q[3]0 is the least significant bit. Then, the statement
“creg c[4];” on line four of Listing 4.1 is to declare that there are four classical bits
in the program. In the left bottom of Fig. 4.9, four classical bits are subsequently
c[0], c[1], c[2] and c[3]. The initial value of each classical bit is set to zero (0). For
the convenience of our explanation, c[k]0 for 0 ≤ k ≤ 3 is to represent the value 0 of
c[k] and c[k]1 for 0 ≤ k ≤ 3 is to represent the value 1 of c[k]. The corresponding
decimal value of the four initial classical bits c[3]0 c[2]0 c[1]0 c[0]0 is 23 × c[3]0 +
22 × c[2]0 + 21 × c[1]0 + 20 × c[0]0 . This indicates that classical bit c[3]0 is the
most significant bit and classical bit c[0]0 is the least significant bit.
Next, the four statements “h q[0];”, “h q[1];” “h q[2];” and “h q[3];” on line five
through eight of Listing 4.1 are to implement four Hadamard gates of the first time
slot
of the quantum circuit in Fig. 4.9. The four statements “h q[0];”, “h q[1];” “h
q[2];” and “h q[3];” take the initial state vector |0 = |q[0]0 |q[1]0 |q[2]0 |q[3]0
as their input state vector. Because the initial state of each quantum bit is set to state
|0,
1the four statements “hq[0];”,
“h q[1];” “h q[2];” and “h q[3];” actually implement
√ √1 1 √1
1 1 0
2 2 × = 12 = √12 = √12 ( + ) = √12 (|0 + |1).
√1 − √1 0 √ 1 0 1
2 2 2
This is to say that the four statements convert four quantum bits q[0], q[1], q[2] and
q[3] from one state |0 to another state √12 (|0 + |1) (their superposition). Hence,
the superposition of the four quantum bits q[0], q[1], q[2] and q[3] is ( √12 (|0 + |1))
( √12 (|0 + |1)) ( √12 (|0 + |1)) ( √12 (|0 + |1)). This implies that we obtain the
following new state vector
1 1
√ q[0]0 + q[0]1 √ q[1]0 +q[1]1
| Ω1 =
2 2
1 0
1 1
√ q[3] +q[3]1 .
0
√ q[2] +q[2]
2 2
4.11 Compute the Period and the Frequency of a Given Oracular Function 191
Next, the statement “u1(2*pi*1/2) q[3];” on line 9 in Listing 4.1 actually imple-
1 √ 0
ments one rotation gate of the second time slot of the quantum
0 e −1×2×π×1/2
circuit in Fig. 4.9. The statement “u1(2*pi*1/2) q[3];” takes the new state vector
(|1 ) as its input state vector. Because the state of quantum bit q[3] is ( √12 (|q[3]0 +
1 √ 0
|q[3]1 )), the statement “u1(2*pi*1/2) q[3];” actually completes
0 e −1×2×π×1/2
1 1
√ √ 1 1
× 1
2 = 1
√ 2
−1×2×π×1/2 = √12 √
−1×2×π×1/2 = √12 ( +
√
2
√ ×e
2
e 0
0 1 √ 0 √
√ )= √1 ( + e −1×2×π×1/2 ) = √12 (|0 + e −1×2×π×1/2
−1×2×π×1/2 2 0 1
e
√
1 −1×π 1
|1) = √2 (|0 + e |1) = √2 (|0 + (−1) |1). This indicates that the statement
√ √ √
“u1(2*pi*1/2) q[3];”
√ adds the phase e −1×2×π×1/2 = e −1×π = cos(π) + −1 ×
sin(π) = −1 + −1 × 0 = −1 to the coefficient of the state |1 in the superposition
of the quantum bit q[3] and does not change the coefficient of the state |0 in the
superposition of the quantum bit q[3]. Because in the second time slot of the quantum
circuit in Fig. 4.9 there is no quantum gate to act on quantum bits q[0] through q[2],
their current states are not changed. This is to say that we obtain the following new
state vector
1 1 1
√ q[0]0 +q[0]1 √ q[1]0 +q[1]1 √ q[2]0 +q[2]1
| 2 =
2 2 2
1 √
√ q[3]0 + e −1×2×π×1/2 q[3]1
2
1 1 1
= √ q[0]0 +q[0]1 √ q[1]0 +q[1]1 √ q[2]0 +q[2]1
2 2 2
1
√ q[3]0 + (−1)q[3]1
2
In the new state vector (|2 ), the sixteen amplitudes from state |q[0]0 |q[1]
0
0 0 1 1 1 1 1
|q[2] |q[3] through state |q[0] |q[1] |q[2] |q[3] are subsequently √ 4 ,
2
− √ 4 , √ 4 , − √ 4 , √ 4 , − √ 4 , √ 4 , − √ 4 , √1 4 , − √1 4 ,
1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2
√1 √1
, − 4 , √1
, − 4 ,
√ 1 √1 1
and − 4 . This is to say that in the
√
24 2 24 2 24 2
new state vector (|2 ), it uses the amplitude of each state to encode sixteen outputs
to a given oracular function Of : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4
√ 2
1
× e −1×2×π× 2 ×a4 |a4 ∈ {0, 1}}. Hidden patterns and information stored in the new
state vector (|2 ) are to that the amplitude rotates back to its starting value √1 4
2
eight times.
192 4 Quantum Fourier Transform and Its Applications
Similarly, in the new state vector (|2 ), it makes use of the magnitude √1 4 =
2
(1/4) of the amplitude of each state as the radius of each circle in Fig. 4.8. Because
in Fig. 4.8 the shaded area of each circle is directly proportional to (1/4)2 , the shaded
area of each circle is the same. In the new state vector (|2 ), the sixteen phases
from state |q[0]0 |q[1]0 |q[2]0 |q[3]0 through state |q[0]1 |q[1]1 |q[2]1 |q[3]1
are subsequently 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°, 0°, 180°,
0° and 180°. They subsequently give a 0° rotation and a 180° rotation of eight times
to the left first circle through the last circle in Fig. 4.8. In the new state vector (|2 ),
it uses the darker line drawn in the left first circle through the last circle in Fig. 4.8
to indicate a 0° rotation and a 180° rotation of eight times to the sixteen circles in
Fig. 4.8. This is to say that hidden patterns and information stored in the new state
vector (|2 ) are to that the phase rotates back to its starting value 0° eight times.
Next, the twelve statements from line 10 through line 21 in Listing 4.1 implement the
inverse quantum Fourier transform with four quantum bits. The statement “h q[0];”
on line 10 in Listing 4.1 implements one Hadamard gate in the third time slot
of Fig. 4.9. It takes the new state vector |2 as its input state vector. Because the
current state of quantum bit q[0] in |2 is ( √12 (|q[0]0 + |q[0]1 )), the statement “h
1 1
√ √1 √ 1
q[0];” on line 10 in Listing 4.1 actually implements 1 2 2 × 12 =
1
√ −√
2 2
√
2
0
= |0. This indicates that the statement “h q[0];” converts quantum bit q[0] from one
state ( √12 (|q[0]0 + |q[0]1 )) (its superposition) to another state |q[0]0 . Because in
the third time slot of the quantum circuit in Fig. 4.9 there is no quantum gate to act
4.11 Compute the Period and the Frequency of a Given Oracular Function 193
on quantum bits q[1] through q[3], their current states are not changed. This is to say
that we obtain the following new state vector
1 1
| 3 = |q[0]0 √ |q[1]0 + |q[1]1 √ |q[2]0 + |q[2]1
2 2
1
√ |q[3]0 + (−1)|q[3]1 .
2
1
|Ω4 = |q[0]0 √ q[1]0 +q[1]1
2
1 1
√ q[2]0 +q[2]1 √ |q[3]0 + (−1) |q[3]1 .
2 2
1 1
| Ω5 = |q[0]0 √ q[1]0 +q[1]1 √ q[2]0 +q[2]1
2 2
1
√ |q[3]0 + (−1) |q[3]1 .
2
1 1
|Ω6 = |q[0]0 √ q[1]0 +q[1]1 √ q[2]0 +q[2]1
2 2
1
√ |q[3]0 + (−1) |q[3]1 .
2
Next, the statement “h q[1];” on line 14 in Listing 4.1 completes one Hadamard
gate in the seventh time slot of Fig. 4.9. It takes the new state vector |6 as its input
state vector. Because the current state of quantum bit q[1] in |6 is ( √12 (|q[1]0
+ |q[1]1 )),the statement
1 “h q[1];” on line 14 in Listing 4.1 actually implements
√1 √1 √ 1
2 2 × 2 = = |0. This implies that the statement “h q[1];”
1 1 √1
√ −√
2 2 2
0
converts quantum bit q[1] from one state ( √12 (|q[1]0 + |q[1]1 )) (its superposition)
to another state |q[1]0 . Because in the seventh time slot of the quantum circuit in
Fig. 4.9 there is no quantum gate to act on quantum bits q[0], q[2] and q[3], their
current states are not changed. This indicates that we obtain the following new state
vector
1
| Ω7 = |q[0]0 |q[1]0 √ q[2]0 +q[2]1
2
4.11 Compute the Period and the Frequency of a Given Oracular Function 195
1
√ q[3]0 + (−1)q[3]1
2
1
| 8 = q[0]0 q[1]0 √ q[2]0 + q[2]1
2
1
√ q[3]0 + (−1) q[3]1 .
2
1
| Ω9 = |q[0]0 |q[1]0 √ q[2]0 +q[2]1
2
1
√ q[3]0 + (−1)q[3]1
2
196 4 Quantum Fourier Transform and Its Applications
Next, the statement “h q[2];” on line 17 in Listing 4.1 performs one Hadamard
gate in the tenth time slot of Fig. 4.9. It takes the new state vector |9 as its input
state
vector. Because the current state of quantum bit q[2] in |9 is ( √12 (|q[2]0 +
|q[2]1 )), the
1 “h q[2];” on line 17 in Listing 4.1 actually implements
statement
√1 √1 √ 1
2 2 × 2 = = |0. This is to say that the statement “h q[2];”
√1 − √1 √1 0
2 2 2
converts quantum bit q[2] from one state ( √12 (|q[2]0 + |q[2]1 )) (its superposition)
to another state |q[2]0 . Because in the tenth time slot of the quantum circuit in
Fig. 4.9 there is no quantum gate to act on quantum bits q[0], q[1] and q[3], their
current states are not changed. This is to say that we obtain the following new state
vector
1
| Ω10 = |q[0]0 |q[1]0 |q[2]0 √ q[3]0 + (−1)q[3]1 .
2
1
| Ω11 = |q[0]0 |q[1]0 |q[2]0 √ q[3]0 + (−1)q[3]1 .
2
4.11 Compute the Period and the Frequency of a Given Oracular Function 197
Next, the statement “h q[3];” on line 19 in Listing 4.1 implements one Hadamard
gate in the twelfth time slot of the quantum circuit in Fig. 4.9. It takes the new state
vector |11 as its input state vector. Because the current state of quantum bit q[3] in
|11 is ( √12 (|q[3]0 + (−1) |q[3]1 )), the statement “h q[3];” on line 19 in Listing
1 1
√ √1 √ 0
4.1 actually implements 1 2 2 × 2 = = |1. This indicates that
1 √1
√ −√
2 2
− 2
1
the statement “h q[3];” converts quantum bit q[3] from one state ( √12 (|q[3]0 + (−1)
|q[3]1 )) to another state |q[3]1 . Because in the twelfth time slot of the quantum
circuit in Fig. 4.9 there is no quantum gate to act on quantum bits q[0], q[1] and q[2],
their current states are not changed. This implies that we obtain the following new
state vector
Next, the statement “swap q[0],q[3];” on line 20 in Listing 4.1 is a swap gate
⎡ ⎤
10 00
⎢00 10⎥
⎣ 0 1 0 0 ⎦ that is to exchange the information contained in the two quantum
⎢ ⎥
00 01
bits q[0] and q[3]. It takes the new state vector |12 as its input state vector and
implements one swap gate in the thirteenth time slot of the quantum circuit in Fig. 4.9.
Because in the thirteenth time slot of the quantum circuit in Fig. 4.9 there is no
quantum gate to act on quantum bits q[1] and q[2], their current states are not changed.
Therefore, after the statement “swap q[0],q[3];” on line 20 in Listing 4.1, we obtain
the following new state vector
|13 = (|q[0]1 ) (|q[1]0 ) (|q[2]0 ) (|q[3]0 ).
Next, the statement “swap q[1],q[2];” on line 21 in Listing 4.1 is a swap gate
⎡ ⎤
10 00
⎢00 10⎥
⎣ 0 1 0 0 ⎦ that is to exchange the information contained in the two quantum
⎢ ⎥
00 01
bits q[1] and q[2]. It takes the new state vector |13 as its input state vector and
implements one swap gate in the fourteenth time slot of the quantum circuit in
Fig. 4.9. Because in the fourteenth time slot of the quantum circuit in Fig. 4.9 there
is no quantum gate to act on quantum bits q[0] and q[3], their current states are not
changed. Hence, after the statement “swap q[1],q[2];” on line 21 in Listing 4.1, we
obtain the following new state vector
Quantum bit q[0] is the most significant bit and quantum bit q[3] is the least significant
bit. Classical bit c[3] is the most significant bit and classical bit c[0] is the
least significant bit. Therefore, the four statements “measure q[0] → c[3];”, “measure
q[1] → c[2];”, “measure q[2] → c[1];” and “measure q[3] → c[0];” from line 22
through line 25 in Listing 4.1 are to measure the most significant quantum bit q[0]
through the least significant quantum bit q[3]. They record the measurement outcome
by means of overwriting the most significant classical bit c[3] through the least
significant classical bit c[0]. They complete the measurement from the fifteenth time
slot through the eighteenth time slot of the quantum circuit in Fig. 4.9.
In the backend that is simulator of Open QASM with thirty-two quantum bits in
IBM’s quantum computer, we use the command “run” to execute the program in
Listing 4.1. The measured result appears in Fig. 4.10. From Fig. 4.10, we obtain the
outcome 1000 (c[3] = 1 = q[0] = |1, c[2] = 0 = q[1] = |0, c[1] = 0 = q[2] = |0
and c[0] = 0 = q[3] = |0) with the probability 1 (100%). This is to say that with the
possibility 1 (100%) we obtain that the value of quantum bit q[0] is |1, the value of
quantum bit q[1] is |0, the value of quantum bit q[2] is |0 and the value of quantum
bit q[3] is |0. The measured outcome 1000 (eight) with the probability 1 (100%)
Fig. 4.10 The signal frequency that is the number of the period of signals per quantum register
with four quantum bits is 1000 with the probability 1 (100%) for a given oracular function Of : {a1
√
−1×2×π × 21 ×a4
a2 a3 a4 |∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e |a4 ∈ {0, 1}}
2
4.11 Compute the Period and the Frequency of a Given Oracular Function 199
After the five statements from line 5 through line 9 in Listing 4.1, sampling sixteen
points in Fig. 4.7 encodes sixteen phases of outputs of a given√
oracular function that
1
is Of : {a1 a2 a3 a4 |∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π× 2 ×a4 |a4 ∈ {0,
2
1}}. Next, after the twelve statements from line 10 through line 21 in Listing 4.1, the
quantum state vector with the four quantum bits q[0], q[1], q[2] and q[3] encodes
the frequency of signals and the strength of the frequency of signals. Next, after the
four statements from line 22 through line 25 in Listing 4.1, the measured outcome
in Fig. 4.10 is 1000 with the possibility 100%. We use Fig. 4.11 to explain that the
inverse Quantum Fourier transform has the same power as the inverse discrete Fourier
transform to find the frequency f and the period r of the same oracular function Of .
We take the horizontal axis of Fig. 4.10 as the new horizontal axis of Fig. 4.11 and
take the vertical axis of Fig. 4.10 as the new vertical axis of Fig. 4.11. Because the
horizontal axis of Fig. 4.10 is to represent the measured outcome that is the value
of the quantum register with four quantum bits and is the frequency of signals, the
Fig. 4.11 The frequency of signal is the measured outcome of the quantum register with four
quantum bits and its strength is the possibility of obtaining the outcome after the inverse quantum
Fourier transform of sampling sixteen points in Fig. 4.7
200 4 Quantum Fourier Transform and Its Applications
new horizontal axis of Fig. 4.11 is to represent the frequency of signals. Similarly,
since the vertical axis of Fig. 4.10 is to represent the possibility of obtaining each
measured outcome that is the strength of the frequency of signals, the new vertical
axis of Fig. 4.11 is to represent the strength of the frequency of signals. Because
the measured outcome in Fig. 4.10 is 1000 (eight) with the possibility 100%, the
frequency of signals in Fig. 4.11 is 1000 (eight) and the strength of the frequency of
signals is 100%.
From Fig. 4.11, the frequency f of signals to the phase of each output of Of is
equal to 1000 (eight). This indicates that the signal rotates back to its starting value
0° eight times and there are eight periods of signals per sixteen time units. This gives
that the period r of signals to the phase of each output of Of is equal to (16/8)
= (2).
Because the magnitude of each output to Of is the same and is equal to √1 4 and
2
the signal (the phase of each output to Of ) rotates backto its starting value 0° eight
times, its output rotates back to its starting value √1 4 eight times. This is to say
2
that the number of the period per sixteen outputs is 1000 (eight) and the frequency f
of Of is equal to eight. Therefore, this gives that the period r of Of is 16/8 = 2. This
implies that the inverse quantum Fourier transform and the inverse discrete Fourier
transform have the same power to find the frequency f and the period r of the same
oracular function Of .
say that the number of the period per sixteen outputs is four and the frequency f of
S f is equal to four. Therefore, because the value of r × f is equal to sixteen (16), this
gives that the period r of S f is equal to (16/4) = 4. This implies that when S f takes
two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 4 as its input values, two outputs of S f is the
same to the two inputs. This indicates that S f (a1 a2 a3 a4 ) = S f (a1 a2 a3 a4 + 4). For
gaining the frequency f and the √
period r of S f , it needs to complete at least sixteen
−1×2×π×0.a3 a4
exponential computations to e and multiplication of sixteen times.
Fig. 4.12 Sampling sixteen points from each phase of sixteen outputs for the √
second given oracular
function that is S f : {a1 a2 a3 a4 |∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π ×0.a3 a4 |a3 and
2
√
−1×2×π × 21 ×a3 + 41 ×a4
a4 ∈ {0, 1}} = { √1 4 × e |a3 and a4 ∈ {0, 1}}
2
starting value 0° four times, its output rotates back to its starting value √1 4 four
2
times. This is to say that the number of the period per sixteen outputs is four and
the frequency f of S f is equal to four. Therefore, this gives that the period r of S f is
(16/4) = (4).
In Fig. 4.12 in subsection 4.12.1 sampling sixteen points only encodes sixteen phases
from sixteen output of S f , so we make use of circle notation to show how determining
the frequency f and the period r of S f is equivalent to deal with the frequency f and
the period r of signals in the time domain (the input domain). In a circle, the filled
radius encodes the magnitude of the amplitude to that S f takes an input a1 a2 a3 a4
as its input value. This is to say that the size (the shaded area) of the circle is directly
proportional to the square of the absolute value of the magnitude of the amplitude
to S f (a1 a2 a3 a4 ). This gives that the size (the shaded area) of the circle is directly
proportional to the possibility of obtaining the output of S f that takes that input a1 a2
a3 a4 as its input value. A darker line drawn in the circle points out that the phase (a
negative angle) rotates the circle clockwise or the phase (a positive angle) rotates the
circle counterclockwise. A number a1 a2 a3 a4 below the circle encodes an input to
S f . In Fig. 4.13,√it includes sixteen outputs of the second given √oracular function that
1 1
is S f : { √1 × e −1×2×π×0.a3 a4 | a3 and a4 ∈ {0, 1}} = { √1 × e −1×2×π×( 2 ×a3 + 4 ×a4 )
24 24
| a3 and a4 ∈ {0, 1}}. In Fig. 4.13, sixteen circles encode sampling sixteen points
4.12 Determine the Frequency and the Period of the Second … 203
√
Fig. 4.13 Sixteen outputs of the second given oracular function that is S f : { √1 4 × e −1×2×π ×0.a3 a4
2
√
1 1
−1×2×π × 2 ×a3 + 4 ×a4
|a3 and a4 ∈ {0, 1}} = { √1 4 × e | a3 and a4 ∈ {0, 1}}
2
in Fig. 4.12 and sixteen numbers below each circle encode sixteen time units of the
time domain (the input domain) of Fig. 4.12.
2
Since the shaded area of each circle in Fig. 4.13 is directly proportional to √1 4 ,
2
the shaded area of each circle is the same. In Fig. 4.13, a number a01 a02 a03 a04 below
the left first circle encodes a value zero that is an input to S f (a01 a02 a03 a04 ). The darker
line drawn in the left first circle points out that the phase to output of S f (a01 a02 a03
a04 ) is a 0°
and gives a 0° rotation to the left first circle. The radius of the left first
circle is √1 = (1/4) that is the magnitude of output to S f (a01 a02 a03 a04 ). Similarly,
24
in Fig. 4.13, a number a01 a02 a03 a14 below the left second circle encodes a value one
that is an input to S f (a01 a02 a03 a14 ). The darker line drawn in the left second circle
indicates that the phase to output of S f (a01 a02 a03 a14 ) is a 90° and gives
a 90° rotation
to the left second circle. The radius of the left second circle is √1 = (1/4) that is
24
the magnitude of output to S f (a01 a02 Similarly, in Fig. 4.13, a number a01 a02 a13
a03 a14 ).
0
a4 below the left third circle encodes a value two that is an input to S f (a01 a02 a13 a04 ).
The darker line drawn in the left third circle points out that the phase to output of
S f (a01 a02 a13 a04 ) is a 180°and gives a 180° rotation to the left third circle. The radius
of the left third circle is √1 = (1/4) that is the magnitude of output to S f (a01 a02 a13
24
a04 ). Similarly, in Fig. 4.13, a number a01 a02 a13 a14 below the left fourth circle encodes
a value three that is an input to S f (a01 a02 a13 a14 ). The darker line drawn in the left
fourth circle indicates that the phase to output of S f (a01 a02 a13 a14 ) is a 270° andgives
a 270° rotation to the left fourth circle. The radius of the left fourth circle is √1
24
= (1/4) that is the magnitude of output to S f (a01 a02 a13 a14 ).
Similarly,
in Fig. 4.13, the radius of the left fifth circle through the last circle is
1
all √
4
= (1/4) that is the magnitude of output to S f (a01 a12 a03 a04 ) through S f (a11
2
a12 a13 a14 ). The darker line drawn in the left fifth circle through the last circle points
out that the phase to output of S f (a01 a12 a03 a04 ) through S f (a11 a12 a13 a14 ) is respectively
0°, 90°, 180°, 270°, 0°, 90°, 180°, 270°, 0°, 90°, 180° and 270°. They respectively
give a 0° rotation, a 90° rotation, a 180° rotation and a 270° rotation three times to
the left fifth circle through the last circle. Twelve numbers a01 a12 a03 a04 through a11
a2 1 a13 a14 below the left fifth circle trough the last circle encode twelve values four
through fifteen that are subsequently twelve inputs to S f (a01 a12 a03 a04 ) through Of (a11
a12 a13 a14 ).
204 4 Quantum Fourier Transform and Its Applications
From Fig. 4.13, hidden patterns and information stored in signals of sixteen circles
(sampling sixteen points) are that the signal (the phase) rotates back to its starting
value 0° four times. This is to say that there are four periods of signals per sixteen
inputs (per sixteen time units) and the frequency f of signals is equal to four. This
means that the period r of signals is equal to (16/4) = (4). Since the signal (the phase
of each output to S f ) rotates
back
to its starting value 0° four times, its output rotates
back to its starting value √1 4 four times. This indicates that the number of the
2
period per sixteen outputs is four and the frequency f of S f is equal to four. Thus,
this gives that the period r of S f is 16/4 = 4. So, when the second given oracular
function S f takes two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 4 as its input values, it
gives the same output to the two inputs. This is to say that S f (a1 a2 a3 a4 ) = S f (a1 a2
a3 a4 + 4) to any two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 4. In this way, for gaining
the frequency f and the period r of S f , it just needs to implement four multiplications
(2 × π × 0.00) (0°), four multiplications (2 × π ×0.01) (90°), four multiplications
(2 × π × 0.10) (180°), and four multiplications (2 × π × 0.11) (270°). It very
significantly enhances the performance of the first way to compute the frequency f
and the period r of S f .
In Listing 4.2, the program is in the backend that is simulator of Open QASM with
thirty-two quantum bits in IBM’s quantum computer. The program is to compute the
frequency f and the period r of S f so that S f (a1 a2 a3 a4 ) = S f (a1 a2 a3 a4 + r) to any
two inputs a1 a2 a3 a4 and a1 a2 a3 a4 + r. In Listing 4.2, we illustrate how to apply
quantum gates with one quantum bit and the inverse quantum Fourier transform to
determine the frequency f and the period r of S f . Figure 4.14 is the quantum circuit
of finding the frequency f and the period r of S f . The statement “OPENQASM 2.0;”
Fig. 4.14 The quantum circuit of figuring out the frequency f and the period r √to the second given
oracular function S f : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π ×0.a3 a4 |a3
2
√
−1×2×π × 21 ×a3 + 41 ×a4
and a4 ∈ {0, 1}} = { √1 4 × e |a3 and a4 ∈ {0, 1}}
2
4.12 Determine the Frequency and the Period of the Second … 205
on line one of Listing 4.2 is to indicate that the program is written with version 2.0 of
Open QASM. Next, the statement “include “qelib1.inc”;” on line two of Listing 4.2
is to continue parsing the file “qelib1.inc” as if the contents of the file were pasted
at the location of the include statement, where the file “qelib1.inc” is Quantum
Experience (QE) Standard Header and the path is specified relative to the current
working directory.
Listing 4.2 The program of computing the frequency f and the period r to the second
given oracular function S f : {a1 a2 a3 a4 |∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 ×
√ √ 2
1 1
e −1×2×π×0.a3 a4 | a3 and a4 ∈ {0, 1}} = { √1 × e −1×2×π×( 2 ×a3 + 4 ×a4 ) |a3 and a4 ∈
24
{0, 1}}.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[4];
4. creg c[4];
Next, the statement “qreg q[4];” on line three of Listing 4.2 is to declare that in the
program there are four quantum bits. In the left top of Fig. 4.14, four quantum bits
are subsequently q[0], q[1], q[2] and q[3]. The initial value of each quantum bit is
set to state |0. We make use of four quantum bits q[0], q[1], q[2] and q[3] to encode
the input domain {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} of S f . This indicates
that quantum bit q[0] encodes bit a1 , quantum bit q[1] encodes bit a2 , quantum bit
q[2] encodes bit a3 and quantum bit q[3] encodes bit a4 .
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 3 is to encode the value
0 of q[k] and q[k]1 for 0 ≤ k ≤ 3 is to encode the value 1 of q[k]. Similarly, for the
convenience of our explanation, an initial state vector of figuring out the frequency f
and the period r of S f so that S f (a1 a2 a3 a4 ) = S f (a1 a2 a3 a4 + r) to any two inputs
a1 a2 a3 a4 and a1 a2 a3 a4 + r is
|B0 = |q[0]0 |q[1]0 |q[2]0 |q[3]0 = |0 |0 |0 |0 = |0000.
The corresponding decimal value of the initial state vector |B0 is 23 × q[0]0 +
2 × q[1]0 + 21 × q[2]0 + 20 × q[3]0 . This is to say that quantum bit q[0]0 is the
2
most significant bit and quantum bit q[3]0 is the least significant bit.
Next, the statement “creg c[4];” on line four of Listing 4.2 is to declare that there
are four classical bits in the program. In the left bottom of Fig. 4.14, four classical
bits are respectively c[0], c[1], c[2] and c[3]. The initial value of each classical bit
is set to zero (0). For the convenience of our explanation, c[k]0 for 0 ≤ k ≤ 3 is to
encode the value 0 of c[k] and c[k]1 for 0 ≤ k ≤ 3 is to encode the value 1 of c[k]. The
corresponding decimal value of the four initial classical bits c[3]0 c[2]0 c[1]0 c[0]0
is 23 × c[3]0 + 22 × c[2]0 + 21 × c[1]0 + 20 × c[0]0 . This implies that classical bit
c[3]0 is the most significant bit and classical bit c[0]0 is the least significant bit.
206 4 Quantum Fourier Transform and Its Applications
Next, the four statements “h q[0];”, “h q[1];” “h q[2];” and “h q[3];” on line five
through eight of Listing 4.2 are to complete four Hadamard gates of the first time
slot
of the quantum circuit in Fig. 4.14. The four statements “h q[0];”, “h q[1];” “h
q[2];” and “h q[3];” take the initial state vector |B0 = |q[0]0 |q[1]0 |q[2]0 |q[3]0
as their input state vector. Since the initial state of each quantum bit is set to state
|0,
1the four statements “hq[0];”,
“h q[1];” “h q[2];” and “h q[3];” actually complete
√ √1 1 √1
1 1 0
2 2 × = 12 = √12 = √12 ( + ) = √12 (|0 + |1).
√1 − √1 0 √ 1 0 1
2 2 2
This indicates that the four statements convert four quantum bits q[0], q[1], q[2] and
q[3] from one state |0 to another state √12 (|0 + |1) (their superposition). Therefore,
the superposition of the four quantum bits q[0], q[1], q[2] and q[3] is ( √12 (|0 + |1))
( √12 (|0 + |1)) ( √12 (|0 + |1)) ( √12 (|0 + |1)). This is to say that we gain the
following new state vector
1 1
√ q[0]0 +q[0]1 √ q[1]0 +q[1]1
|B1 =
2 2
1
√ q[2]0 +q[2]1
2
1
√ q[3]0 +q[3]1 .
2
1 √ 0
to quantum bit q[2] in the second time slot of the quantum
0 e −1×2×π×1/2
circuit in Fig. 4.14. The two statements “u1(2*pi*1/4) q[3];” and “u1(2*pi*1/2)
q[2];” take the new state vector (|B1 ) as their input state vector. The state of
quantum bit q[3] is ( √12 (|q[3]0 + |q[3]1 )) and the state of quantum bit q[2]
is ( √12 (|q[2]0 + |q[2]1 )). Therefore, the statement “u1(2*pi*1/4) q[3];” actu-
1
1 √ 0 √ √1
ally implements × 2 = √ 2
= √12
0 e −1×2×π×1/4 √1 √1 × e −1×2×π×1/4
2 2
1 1 0 1 √
√ = √1 ( + √ )= √1 ( +e −1×2×π×1/4
−1×2×π×1/4 2 0 −1×2×π×1/4 2 0
e e
0 √ √ √
) = √12 (|0 + e −1×2×π×1/4 |1) = √12 (|0 + e −1×π/2 |1) = √12 (|0 + ( −1)
1
√
|1) = √12 (|q[3]0 + ( −1) |q[3]1 ). This is to say that the statement “u1(2*pi*1/4)
√ √ √
q[3];” adds
√ the phase √ e −1×2×π×1/4 = e −1×π/2 = cos(π/2) + −1 × sin(π/2)
= 0 + −1 × 1 = −1 to the coefficient of the state |1 in the superposition
of the quantum bit q[3] and does not change the coefficient of the state |0 in the
superposition of the quantum bit q[3].
Similarly, the statement
1 “u1(2*pi*1/2) q[2];” actually implements
1 √ 0 √ √1 1
× 2 = √ 2 = √12 √
0 e −1×2×π×1/2 √1 √1 × e −1×2×π×1/2 e −1×2×π×1/2
2 2
1 0 1 √ 0
= √1 ( + √ )= √1
+ e (−1×2×π×1/2
) = √12 (|0
2 0 e −1×2×π×1/2 0 2 1
√ √
+e −1×2×π×1/2
|1) = √12 (|0 + e −1×π |1) = √12 (|0 + (−1) |1) = √12 (|q[2]0
+√(−1) |q[2]1 ). This implies that the statement “u1(2*pi*1/2) q[2];” adds the phase
√ √ √
e −1×2×π×1/2 = e −1×π = cos(π) + −1 × sin(π) = −1 + −1 × 0 = −1 to the
coefficient of the state |1 in the superposition of the quantum bit q[2] and does not
change the coefficient of the state |0 in the superposition of the quantum bit q[2].
There is no quantum gate to act on quantum bits q[0] through q[1] in the second
time slot of the quantum circuit in Fig. 4.14, so their current states are not changed.
This indicates that we get the following new state vector
1 1
√ q[0]0 + |q[0]1 √ q[1]0 +q[1]1
|B2 =
2 2
1 √
√ |q[2]0 + e −1×2×π×1/2 |q[2]1
2
1 √
√ q[3]0 + e −1×2×π×1/4 q[3]1
2
1
= √ q[0]0 +q[0]1
2
208 4 Quantum Fourier Transform and Its Applications
1
√ q[1]0 +q[1]1
2
1
√ |q[2]0 + (−1) |q[2]1
2
1 √
√ q[3]0 + ( −1)q[3]1 .
2
In the new state vector (|B2 ), the sixteen amplitudes from state |q[0]0 |q[1] 0
0 0 1 1 1 1 1
|q[2] |q[3] through state |q[0] |q[1] |q[2] |q[3] are subsequently √ ,
√ √ √ √ 24
1 1 1 1 1 1 1
−1 √ , − √ 4 , − −1 √ 4 , √ 4 , −1 √ 4 , − √ 4 , − −1 √ 4 ,
24√ 2 2 √ 2 2 √ 2 2
√1 , −1 √1 , − √1 , − −1 √1 , √1 , −1 √1 , − √1 , and
4
2√ 24 24 24 24 24 24
− −1 √1 4 . This means that in the new state vector (|B2 ), it makes use of the
2
amplitude of each state to encode sixteen outputs to the second given √
oracular func-
1 −1×2×π×0.a3 a4
tion S f : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { 4 × e √ | a3
√ 2
1 −1×2×π×( 21 ×a3 + 41 ×a4 )
and a4 ∈ {0, 1}} = { √ ×e | a3 and a4 ∈ {0, 1}}. Hidden
24
information and hidden patterns stored in the new state vector (|B2 ) are to that the
1
amplitude rotates back to its starting value √ four times.
24
Similarly, in the new state vector (|B2 ), it uses the magnitude √1 4 = (1/4) of
2
the amplitude of each state as the radius of each circle in Fig. 4.13. In Fig. 4.13, the
shaded area of each circle is directly proportional to (1/4)2 , so the shaded area of each
circle is the same. In the new state vector (|B2 ), the sixteen phases from state |q[0]0
|q[1]0 |q[2]0 |q[3]0 through state |q[0]1 |q[1]1 |q[2]1 |q[3]1 are subsequently 0°,
90°, 180°, 270°, 0°, 90°, 180°, 270°, 0°, 90°, 180°, 270°, 0°, 90°, 180°, and 270°.
They subsequently give a 0° rotation, a 90° rotation, a 180° rotation and a 270°
rotation four times to the left first circle through the last circle in Fig. 4.13. In the
new state vector (|B2 ), it applies the darker line drawn in the left first circle through
the last circle in Fig. 4.13 to point out a 0° rotation, a 90° rotation, a 180° rotation
and a 270° rotation four times to the sixteen circles in Fig. 4.13. This indicates that
hidden information and hidden patterns stored in the new state vector (|B2 ) are to
that the phase rotates back to its starting value 0° four times.
Next, the twelve statements from line 11 through line 22 in Listing 4.2 implement the
inverse quantum Fourier transform with four quantum bits. The statement “h q[0];”
4.12 Determine the Frequency and the Period of the Second … 209
on line 11 in Listing 4.2 completes one Hadamard gate in the third time slot of
Fig. 4.14. It takes the new state vector |B2 as its input state vector. The current state
of quantum bit q[0] in |B2 is ( √12 (|q[0]0 + |q[0]1 )), so the statement “h q[0];”
1 1
√ √1 √ 1
on line 11 in Listing 4.2 actually performs 2 2 × 2 = = |0 =
1 1 √1
√ −√
2 2 2
0
|q[0]0 . This is to say that the statement “h q[0];” converts quantum bit q[0] from one
state ( √12 (|q[0]0 + |q[0]1 )) (its superposition) to another state |q[0]0 . Since there
is no quantum gate to act on quantum bits q[1] through q[3] in the third time slot
of the quantum circuit in Fig. 4.14, their current states are not changed. This means
that we acquire the following new state vector
1 1
| B3 = |q[0]0 √ q[1]0 +q[1]1 √ |q[2]0 + (−1)|q[2]1
2 2
1 √
√ |q[3]0 + ( −1)|q[3]1 .
2
1 1
|B4 = |q[0]0 √ q[1]0 +q[1]1 √ q[2]0 + (−1)q[2]1
2 2
1 √
√ |q[3]0 + ( −1)|q[3]1 .
2
1 1
|B5 = |q[0]0 √ q[1]0 +q[1]1 √ q[2]0 + (−1)q[2]1
2 2
1 √
√ q[3]0 + ( −1)q[3]1 .
2
1 1
|B6 = |q[0]0 √ q[1]0 +q[1]1 √ |q[2]0 + (−1)|q[2]1
2 2
4.12 Determine the Frequency and the Period of the Second … 211
1 √
√ q[3]0 + ( −1)q[3]1 .
2
Next, the statement “h q[1];” on line 15 in Listing 4.2 implements one Hadamard
gate in the seventh time slot of Fig. 4.14. It takes the new state vector |B6 as its input
state vector. The current state of quantum bit q[1] in |B6 is ( √12 (|q[1]0 + |q[1]1 )),
1 1
√ √
so the statement “h q[1];” on line 15 in Listing 4.2 actually performs 2 2
√1 − √1
1 2 2
√ 1
× 2 = = |0 = |q[1]0 . This means that the statement “h q[1];” converts
√1 0
2
quantum bit q[1] from one state ( √12 (|q[1]0 + |q[1]1 )) (its superposition) to another
state |q[1]0 . Because there is no quantum gate to act on quantum bits q[0], q[2] and
q[3] in the seventh time slot of the quantum circuit in Fig. 4.14, their current states
are not changed. This is to say that we acquire the following new state vector
1
|B7 = |q[0]0 |q[1]0 √ q[2]0 + (−1)q[2]1
2
1 √
√ |q[3]0 + ( −1) |q[3]1 .
2
1
|B9 = |q[0]0 |q[1]0 √ q[2]0 + (−1)q[2]1
2
1 √
0
√ |q[3] + ( −1) |q[3]1 .
2
Next, the statement “h q[2];” on line 18 in Listing 4.2 completes one Hadamard
gate in the tenth time slot of Fig. 4.14. It takes the new state vector |B9 as its input
state
vector. Since the current state of quantum bit q[2] in |B9 is ( √12 (|q[2]0 +
(−1) |q[2]1
)), the
statement
“h q[2];” on line 18 in Listing 4.2 actually implements
√1 √1 √1 0
2 2 × −12 = = |1 = |q[2]. This means that the statement “h
√1 − √1 √ 1
2 2 2
q[2];” converts quantum bit q[2] from one state ( √12 (|q[2]0 + (−1) |q[2]1 )) (its
superposition) to another state |q[2]1 . Because there is no quantum gate to act on
quantum bits q[0], q[1] and q[3] in the tenth time slot of the quantum circuit in
Fig. 4.14, their current states are not changed. This implies that we get the following
new state vector
4.12 Determine the Frequency and the Period of the Second … 213
1 √
|B10 = |q[0]0 |q[1]0 |q[2]1 √ q[3]0 + ( −1)q[3]1
2
1 √
= |q[0]0 |q[1]0 √ q[2]1 q[3]0 + ( −1)q[2]1 q[3]1 .
2
1 √ √
√ q[2]1 q[3]0 + (− −1)( −1)q[2]1 q[3]1
2
1
= |q[0]0 |q[1]0 √ q[2]1 q[3]0 + q[2]1 q[3]1
2
1
= |q[0]0 |q[1]0 |q[2]1 √ q[3]0 +q[3]1 .
2
Next, the statement “h q[3];” on line 20 in Listing 4.2 performs one Hadamard
gate in the twelfth time slot of the quantum circuit in Fig. 4.14. It takes the new
state vector |B11 as its input state vector. The current state of quantum bit q[3] in
|B11 is ( √12 (|q[3]0 + |q[3]1 )), so the statement “h q[3];” on line 20 in Listing
1 1
√ √1 √ 1
4.2 actually finishes 1
2 2
1 × 2 = = |0 = |[q[3]0 . This implies
√ −√ √1 0
2 2 2
that the statement “h q[3];” converts quantum bit q[3] from one state ( √12 (|q[3]0 +
|q[3]1 )) to another state |q[3]0 . Since there is no quantum gate to act on quantum
bits q[0], q[1] and q[2] in the twelfth time slot of the quantum circuit in Fig. 4.14,
their current states are not changed. This is to say that we gain the following new
state vector
214 4 Quantum Fourier Transform and Its Applications
Next, the statement “swap q[0],q[3];” on line 21 in Listing 4.2 is a swap gate
⎡ ⎤
10 00
⎢00 10⎥
⎣ 0 1 0 0 ⎦ that is to exchange the information contained in the two quantum bits
⎢ ⎥
00 01
q[0] and q[3]. It takes the new state vector |B12 as its input state vector and imple-
ments one swap gate in the thirteenth time slot of the quantum circuit in Fig. 4.14.
Because there is no quantum gate to act on quantum bits q[1] and q[2] in the thirteenth
time slot of the quantum circuit in Fig. 4.14, their current states are not changed. This
means that we obtain the following new state vector
Next, the statement “swap q[1],q[2];” on line 22 in Listing 4.2 is a swap gate
⎡ ⎤
10 00
⎢00 10⎥
⎣ 0 1 0 0 ⎦ that is to exchange the information contained in the two quantum bits
⎢ ⎥
00 01
q[1] and q[2]. It takes the new state vector |B13 as its input state vector and performs
one swap gate in the fourteenth time slot of the quantum circuit in Fig. 4.14. Since
there is no quantum gate to act on quantum bits q[0] and q[3] in the fourteenth time
slot of the quantum circuit in Fig. 4.14, their current states are not changed. This is
to say that we acquire the following new state vector
Quantum bit q[0] is the most significant bit and quantum bit q[3] is the least significant
bit. Classical bit c[3] is the most significant bit and classical bit c[0] is the
Fig. 4.15 The signal frequency that is the number of the period of signals per quantum register
with four quantum bits is 0100 with the probability 1 (100%) for
√
the second given oracular function
S f : {a1 a2 a3 a4 |∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 × e −1×2×π ×0.a3 a4 |a3 and a4 ∈ {0, 1}}
2
√
1 1
−1×2×π × 2 ×a3 + 4 ×a4
= { √1 4 × e |a3 and a4 ∈ {0, 1}}
2
least significant bit. Hence, the four statements “measure q[0] → c[3];”, “measure
q[1] → c[2];”, “measure q[2] → c[1];” and “measure q[3] → c[0];” from line
23 through line 26 in Listing 4.2 are to read out the most significant quantum bit
q[0] through the least significant quantum bit q[3]. They record the outcome of
measurement by means of overwriting the most significant classical bit c[3] through
the least significant classical bit c[0]. They implement the measurement from the
fifteenth time slot through the eighteenth time slot of the quantum circuit in Fig. 4.14.
In the backend that is simulator of Open QASM with thirty-two quantum bits
in IBM’s quantum computer, we make use of the command “run” to execute the
program in Listing 4.2. The measured result appears in Fig. 4.15. From Fig. 4.15,
we acquire the outcome 0100 (c[3] = 0 = q[0] = |0, c[2] = 1 = q[1] = |1, c[1]
= 0 = q[2] = |0 and c[0] = 0 = q[3] = |0) with the probability 1 (100%). This
indicates that with the possibility 1 (100%) we gain that the value of quantum bit q[0]
is |0, the value of quantum bit q[1] is |1, the value of quantum bit q[2] is |0 and the
value of quantum bit q[3] is |0. From the measured outcome 0100 (four) with the
probability 1 (100%), of the second given oracular function S f rotates back
output
to its starting value √1 4 four times and the phase from its output rotates back to
2
its starting value 0° four times. This means that the number of the period per sixteen
outputs is four and the frequency f of S f is equal to four. Therefore, we get that the
period r of S f is (16/4) = 4 so that S f (a1 a2 a3 a4 ) = S f (a1 a2 a3 a4 + 4) to any two
inputs a1 a2 a3 a4 and a1 a2 a3 a4 + 4.
216 4 Quantum Fourier Transform and Its Applications
After the six statements from line 5 through line 10 in Listing 4.2, sampling sixteen
points in Fig. 4.12 encodes sixteen phases of outputs of the second given orac-
ular function that is S f : {a1 a2 a3 a4 | ∀ ad ∈ {0, 1} for 1 ≤ d ≤ 4} → { √1 4 ×
√ √ 2
1 1
e −1×2×π×0.a3 a4 | a3 and a4 ∈ {0, 1}} = { √1 × e −1×2×π×( 2 ×a3 + 4 ×a4 ) | a3 and a4
24
∈ {0, 1}}. Then, after the twelve statements from line 11 through line 22 in Listing
4.2, the quantum state vector with the four quantum bits q[0], q[1], q[2] and q[3]
encodes the frequency of signals and the strength of the frequency of signals. Next,
after the four statements from line 23 through line 26 in Listing 4.2, the measured
outcome in Fig. 4.15 is 0100 with the possibility 100%. We make use of Fig. 4.16
to explain that the inverse Quantum Fourier transform has the same power as the
inverse discrete Fourier transform to find the frequency f and the period r of the
same oracular function S f . We take the horizontal axis of Fig. 4.15 as the new hori-
zontal axis of Fig. 4.16 and take the vertical axis of Fig. 4.15 as the new vertical axis
of Fig. 4.16. The horizontal axis of Fig. 4.15 is to represent the measured outcome
that is the value of the quantum register with four quantum bits and is the frequency
of signals, so the new horizontal axis of Fig. 4.16 is to represent the frequency of
signals. Similarly, because the vertical axis of Fig. 4.15 is to represent the possibility
of gaining each measured outcome that is the strength of the frequency of signals, the
new vertical axis of Fig. 4.16 is to represent the strength of the frequency of signals.
Since the measured outcome in Fig. 4.15 is 0100 (four) with the possibility 100%,
the frequency of signals in Fig. 4.16 is 0100 (four) and the strength of the frequency
of signals is 100%.
From Fig. 4.16, the frequency f of signals to the phase of each output of S f is
equal to 0100 (four). This implies that the signal rotates back to its starting value 0°
Fig. 4.16 After the inverse quantum Fourier transform of sampling sixteen points in Fig. 4.12, the
frequency of signal is the measured outcome of the quantum register with four quantum bits and
its strength is the possibility of reading out the outcome
4.12 Determine the Frequency and the Period of the Second … 217
four times and there are four periods of signals per sixteen time units. This means
that the period r of signals to the phase of each output of S f is equal
to (16/4) = (4).
The magnitude of each output to S f is the same and is equal to √1 4 and the signal
2
(the phase of each output to S f ) rotates back
to its starting value 0° four times, so
its output rotates back to its starting value √1 4 four times. This indicates that the
2
number of the period per sixteen outputs is 0100 (four) and the frequency f of S f
is equal to four. Thus, this gives that the period r of S f is equal to (16/4) = 4. This
means that the inverse quantum Fourier transform and the inverse discrete Fourier
transform have the same power to find the frequency f and the period r of the same
oracular function S f .
4.13 Summary
In this chapter for more details about n complex nth roots of unity and their properties,
the recommended book is (Cormen et al. 2009). For a more detailed description
to the discrete Fourier transform and the inverse discrete Fourier transform, the
recommended books are (Cormen et al. 2009; Nielsen and Chuang 2000; Imre and
Balazs 2007; Lipton and Regan 2014; Silva 2018; Johnston et al. 2019). The two
famous articles (Coppersmith 1964; Shor 1994) gave the original version of the
Quantum Fourier transform and the inverse quantum Fourier transform. A good
illustration for the product state decomposition of the quantum Fourier transform
and the inverse quantum Fourier transform is the two famous articles in [Griffiths
and Niu 1996; Cleve et al. 1998]. A good introduction to the instructions of Open
QASM is the famous article in (Cross 2017).
218 4 Quantum Fourier Transform and Its Applications
4.15 Exercises
4.1 Please write a quantum program to determine the frequency and the period for
outputs of the third given oracular
√
function that is T f : {a1 a2 a3 a4 | ∀ ad ∈ {0,
1 −1×2×π ×0.a2 a3 a4
1} for 1 ≤ d ≤ 4} → { 4 × e
√ | a2 , a3 and a4 ∈ {0, 1}}.
2
4.2 Please write a quantum program to compute the frequency and the period for
outputs of the fourth given oracular
√
function that is F f : {a1 a2 a3 | ∀ ad ∈ {0,
1} for 1 ≤ d ≤ 3} → { √1 3 × e −1×2×π ×0.a3 | a3 ∈ {0, 1}}.
2
4.3 Please write a quantum program to figure out the frequency and the period for
outputs of the fifth given oracular
√
function that is I f : {a1 a2 a3 | ∀ ad ∈ {0, 1}
for 1 ≤ d ≤ 3} → { √1 3 × e −1×2×π ×0.a2 a3 | a2 and a3 ∈ {0, 1}}.
2
References
Cleve, R., Ekert, A., Macciavello, C., Mosca, M.: Quantum algorithms revisited. In: Proceedings
Royal Society London, Ser: A, vol. 454, pp. 339–354 (1998). E-print quant-ph/9708016
Coppersmith, D.: An approximate Fourier transform useful in quantum factoring. IBM research
report. Technical Report RC 19642, December 1994. E-print quant-ph/0201067
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. The
MIT Press. ISBN-13: 978-0262033848, ISBN-10: 9780262033848 (2009)
Cross, A.W., Bishop, L.S., Smolin, J.A., Gambetta, J.M.: Open Quantum Assembly Language
(2017). https://arxiv.org/abs/1707.03429
Griffiths, R.B., Niu, C.S.: Semiclassical Fourier transform for quantum computation. Phys. Rev.
Lett. 76(17), 3228−3231 (1996). E-print quant-ph/9511007
Imre, S., Balazs, F.: Quantum Computation and Communications: An Engineering Approach. John
Wiley & Sons Ltd., UK, 2007, ISBN-10: 047086902X and ISBN-13: 978-0470869024 (2005)
Johnston, E.R., Harrigan, N., Gimeno-Segovia, M.: Programming Quantum Computers: Essential
Algorithms and Code Samples. O’Reilly Media, Inc., ISBN-13: 978-1492039686, ISBN-10:
1492039683 (2019)
Lipton, R.J., Regan, K.W.: Quantum Algorithms via Linear Algebra: A Primer. The MIT Press.
ISBN 978-0-262-02839-4 (2014)
Nielsen, Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge University
Press, New York, NY. ISBN-10: 9781107002173 and ISBN-13: 978-1107002173 (2000)
Shor, P.: Algorithms for quantum computation: discrete logarithms and factoring. In: Proceedings
35th Annual Symposium on Foundations of Computer Science, Santa Fe, pp. 124−134, November
20−22 1994
Silva, V.: Practical Quantum Computing for Developers: Programming Quantum Rigs in the Cloud
using Python, Quantum Assembly Language and IBM Q Experience. Apress, December 13.
ISBN-10: 1484242173 and ISBN-13: 978-1484242179 (2018)
Chapter 5
Order-Finding and Factoring
The inverse quantum Fourier transform and the quantum Fourier transform are the
quantum circuits of implementing the Fourier transform and they can be applied to
solve a variety of interesting questions. In this chapter, we now introduce two of the
most interesting of those questions that are respectively the order-finding problem and
the factoring problem. Miller in 1976 proved that solving the order-finding problem
is equivalent to solve the factoring problem. For the RSA public-key cryptosystem,
People have currently installed more than 400,000,000 copies of its algorithms and
it is the primary cryptosystem used for security on the Internet and World Wide Web.
The security of the RSA public-key cryptosystem is dependent on that the problem
of factoring a big nature number into the production of two large prime numbers is
intractable on a classical computer.
Shor’s order-finding algorithm can solve the problems of order-finding and
factoring exponential faster than any conventional computer. By means of using
Shor’s algorithm to factor a big nature number with 1024 bits into the production of
two prime numbers with 512 bits each, Imre and Ferenc in (Imre and Ferenc 2005)
indicate that the execution time is approximately 0.01 s. This is to say that Shor’s algo-
rithm will make the RSA public-key cryptosystem obsolete once its reliable physical
implementation becomes available on the market. In this chapter, we first introduce a
little background in number theory. Next, we explain how the order-finding problem
implies the ability to factor as well. We also explain how shor’s algorithm solves the
order-finding problem. Next, we describe how to write quantum algorithms to imple-
ment Shor’s algorithm for solving the simplest case in the problems of order-finding
and factoring.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 219
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1_5
220 5 Order-Finding and Factoring
We denote the set Z of integers is {…, −3, −2, −1, 0, 1, 2, 3, …}. We may often
refer to the set of non-negative integers to be {0, 1, 2, 3, …} and the set of positive
integers to be {1, 2, 3, …}. This is to say that 0 (zero) is one element in the set of
non-negative integers and is not one element in the set of positive integers. We may
occasionally say natural numbers that mean positive integers and the set of natural
numbers is to {1, 2, 3, …}.
More formally, given any positive integers w and n, we represent uniquely w in
the following form
w = q × n + r, (5.1)
We assume that N is a composite number with n bits. 1 and N are two trivial factors
of N itself. We also suppose that there is a function β that is {X|0 ≤ X ≤ N} → {X 2
(mod N)}. The domain of the function β is {X|0 ≤ X ≤ N} and its range is {X 2
(mod N)}. If there is an integer 0 ≤ X ≤ N such that β(X) = X 2 = C (mod N), i.e.,
the congruence has a solution, then C is said to be a quadratic congruence (mod N).
Quadratic congruence (mod N) is a NP-complete problem in (Manders and Adleman
1978). If the value of C is equal to one, then four integer solutions for X 2 = 1 (mod
N) are, respectively, b, N − b, 1 and N − 1, where 1 < b < (N/2) and (N/2) < N − b
< N − 1. 1 and N − 1 are trivial solutions and b and N − b are non-trivial solutions.
This is a special case of quadratic congruence (mod N) and it is still a NP-complete
problem. Lemma 5.1 is used to show that we can determine a factor of N if we can
find a non-trivial solution X = ±1 (mod N) to the equation X 2 = 1 (mod N).
Lemma 5.1 We assume that N is a composite number with n bits, and X is a non-
trivial solution to the equation X 2 = 1 (mod N) in the range 0 ≤ X ≤ N, that is, neither
X = 1 (mod N) nor X = N − 1 = -1 (mod N). Then at least one of gcd(N, X − 1)
and gcd(N, X + 1) is a non-trivial factor of N that can be determined using Euclid’s
algorithm with O(n3 ) operations.
We consider one example in which N is equal to 15 and any given function β that
is {X|0 ≤ X ≤ 15} → {X 2 (mod 15)}. The domain of the given function β is {X|0 ≤
X ≤ 15} and its range is {X 2 (mod 15)}. Sixteen outputs of β(X) from the first input
zero through the last input fifteen are subsequently 0, 1, 4, 9, 1, 10, 6, 4, 4, 6, 10,
1, 9, 4, 1 and 0. Four inputs 1, 4, 11 and 14 to satisfy X 2 = 1 (mod 15). Therefore,
four integer solutions for X 2 = 1 (mod 15) are, respectively, 4, 11, 1 and 14. 1 and
14 are trivial solutions. 4 and 11 are non-trivial solutions. Because 42 = 1 (mod 15)
and 112 = 1 (mod 15), it must be that 15 divides 42 − 1 = (4 + 1) × (4 − 1) and
112 − 1 = (11 + 1) × (11−1). Hence, 15 must have a common factor with one or
the other of (4 + 1) and (4 − 1) and 15 must have a common factor with one or the
other of (11 + 1) and (11 − 1). This is to say that using Euclid’s algorithm we may
figure out gcd(15, 5) = 5 and gcd(15, 3) = 3 or gcd(15, 12) = 3 and gcd(15, 10) =
5. This means that 15 has a prime factorization that is to 15 = 5 × 3.
Between the continuum of real numbers and integers, there are many valuable connec-
tions. The theory of continued fractions is one such beautiful connection. If c and d
are integers, then we call (c/d) as the rational fraction or rational number. A finite
simple continued fraction is denoted by a finite collection q[1], q[2], q[3], …, q[i]
of positive integers,
1
(q[1], q[2], q[3], . . . , q[i]) = q[1] + 1
. (5.3)
q[2] + 1
q[3]+···+ q[i]
1
(q[1], q[2], q[3], . . . , q[k]) = q[1] + 1
. (5.4)
q[2] + 1
q[3]+···+ q[k]
Figure 5.2 is the flowchart of the continued fractional algorithm. We can use the
continued fractional algorithm to determine a finite collection q[1], q[2], q[3], …,
q[i] of positive integers in (5.3) for representing continued fraction of a rational
fraction, (c/d). Therefore, applying the right-hand side equivalence in (5.3) we can
describe c/d as
c 1
= q[1] + 1
. (5.5)
d q[2] + q[3]+···+ 1
q[i]
We make use of one example to explain how the continued fractional algorithm
in Fig. 5.2 determines the continued fractional representation of (c/d) if c = 31 and
d = 13 and the corresponding convergent. From the first execution of statement S 0
224 5 Order-Finding and Factoring
31 5 1
=2+ = 2 + 13 . (5.6)
13 13 5
31 1 1
=2+ 3
=2+ 1
. (5.7)
13 2+ 5
2+ 5
3
5.4 Introduction to Continued Fractions 225
31 1 1
=2+ =2+ . (5.8)
13 2 + 1+1 2 2 + 1+1 1
3 3
2
31 1 1
=2+ =2+ . (5.9)
13 2 + 1+ 1 1 2+ 1+
1
1
1+ 21 1+ 21
1
31 1 1
=2+ 1
=2+ 1
. (5.10)
13 2 + 1+ 1 2+ 1
1+
1+ 1 0 1+ 21
2+ 1
31 1
= (q[1] = 2, q[2] = 2, q[3] = 1, q[4] = 1, q[5] = 2) = 2 + .
13 2 + 1+ 1 1
1+ 21
(5.11)
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a rational number (31/13), the first convergent
through the fifth convergent are subsequently (q[1]) = 2, (q[1], q[2]) = 2 + 21 = 25 ,
(q[1], q[2], q[3]) = 2 + 2+1 1 = 37 , (q[1], q[2], q[3], q[4]) = 2 + 2+ 1 1 = 12 5
and
1 1+ 11
1 31
(q[1], q[2], q[3], q[4], q[5]) = 2 + 2+ 1 = 13
.
1+ 1
1+ 21
What resources does the continued fractional algorithm in Fig. 5.2 consume for
obtaining a continued fractional expansion to a rational number c/d > 1, where c and
d are integers of L bits? This is to say that how many values of q[k] for 1 ≤ k ≤ i in
(5.3) must be determined from the continued fractional algorithm in Fig. 5.2. From
statement S 1 and statement S 2 in the continued fractional algorithm of Fig. 5.2, each
quotient q[k] for 1 ≤ k ≤ i is at most L bits long, and the remainder r is at most
L bits long. Thus, we may assume that making use of L bit arithmetic does each
computation. From Fig. 5.2, the divide-and-remainder operation is the heart of the
continued fractional algorithm. At most applying the divide-and-remainder operation
of O(L) times completes the continued fractional algorithm. Because each divide-
and-remainder operation requires O(L 2 ) operations, the total cost of the continued
fractional algorithm is O(L 3 ).
r r
≤ N. That is that neither X 2 = 1 (mod N) nor X 2 = N − 1 = −1 (mod N). Then at
r r
least one of gcd(N, X 2 − 1) and gcd(N, X 2 + 1) is a non-trivial factor of N that can
be determined using Euclid’s algorithm with O(n3 ) operations.
r 2 r 2
Proof Since X r = X 2 = 1 (mod N), it must be that N divides X 2 − 1 =
r r r r
X 2 + 1 × X 2 − 1 . Because X 2 = 1 and X 2 = N − 1, it must be that N does
r r
not divide X 2 + 1 and does not divide X 2 − 1 . This means that N must have a
r r r
common factor with one or the other of X 2 + 1 and X 2 − 1 and 1 < X 2 < N −
r r r r
1. Thus, we get X 2 − 1 < X 2 + 1 < N. From the condition X 2 − 1 < X 2 + 1 <
N, we see that the common factor cannot be N itself. Using Euclid’s algorithm with
r r
O(n3 ) operations we may compute gcd(N, X 2 − 1) and gcd(N, X 2 + 1) and hence
obtain a non-trivial factor of N.
Lemma 5.3 The greatest common divisor of two integers c and d is the least positive
integer that can be written in the form c × u + d × v, where u and v are integers.
Proof Let t = c × u + d × v be the smallest positive integer written in this form. Let
w is the greatest common divisor of c and d. Therefore, w is a divisor to both c and d
and it is a divisor of t. This means that w ≤ t. For completing the proof, we show t ≤
w by demonstrating that t is a divisor of both c and d. The proof is by contradiction.
We assume that t is not a divisor of c. Then c = q × t + r, where the remainder r is
in the range 1 to t − 1. Rearranging this equation c = q × t + r and using t = c × u
+ d × v, we obtain r = c × (1 − q × u) + d × (−q × v) that is a positive integer that
is a linear combination of c and d. Because r is smaller than t, this contradicts the
definition of t as the smallest positive integer written in a linear combination of c and
d. Therefore, we infer that t must divide c. Similarly, by symmetry t must also be a
divisor of d. This means that t ≤ w and w ≤ t. Therefore, we complete the proof.
Lemma 5.4 We assume that integer a divides both integer c and integer d. Then a
divides the greatest common divisor of both c and d.
of 3 is 1 and 3 and the positive divisors of 11 is 1 and 11. We use Lemma 5.5 to
characterize the existence of multiplicative inverse in modular arithmetic applying
co-primality. Simultaneously, we use Lemma 5.6 to show that the order r of X modulo
N satisfies r ≤ N.
Lemma 5.5 Let N be an integer that is greater than 1. An integer X has a multi-
plicative inverse modulo N if and only if the greatest common divisor of X and N is
1.
Lemma 5.6 Let N be an integer greater than 1 and 1 ≤ X ≤ N. X and N are co-prime
and r is the least positive integer such that X r = 1 (mod N). Then r ≤ N.
Proof Consider a sequence of different order values for X: X 0 (mod N), X 1 (mod
N), X 2 (mod N), …, X N −1 (mod N), X N (mod N). Under the modular operation there
can only be N unique values 0, 1, 2, …, N − 1 for X i (mod N) in the above sequence.
If there are more items in the above sequence than N, some X i (mod N) will have
the same value when the modular operation is applied (there are N + 1 items in the
above sequence). For example, let N = 5 and X = 2. Then X 1 = 2 (mod 5) and X 5
= 2 (mod 5).
Hence, among the different items in the above sequence, there are two items that
are equivalent under the modular operation, X n = X m (mod N), where we can assume,
without loss of generality, that n > m and n, m ≤ N. Since from Lemma 5.5 gcd(X, N)
= 1, we know that there exists a multiplicative inverse X −1 of X such that X × X −1
= 1 (mod N). Because the greatest common divisor of X and N is one, the greatest
common divisor of X m and N is equal to one. From Lemma 5.5, gcd(X m , N) = 1,
we know that there exists a multiplicative inverse X −m of X m such that X m × X −m
= 1 (mod N). Next, multiply both sides of the modular operation, X n = X m (mod
N), by X −m to obtain X n × X −m = X m × X −m (mod N) and X n−m = X m−m (mod N)
= X 0 (mod N) = 1 (mod N). From the statements above we have that r = n − m.
Furthermore, as n, m ≤ N and n > m, it follows that r = n − m ≤ N.
5.6 Compute the Order of 2 Modulo 15 and the Prime Factorization for 15 229
We would like to find the prime factorization for N = 15. We need to search for the
nontrivial factor for N = 15. From Lemmas 5.1 and 5.6, we select a number X = 2
so that the greatest common divisor of X = 2 and N = 15 is 1 (one). This is to say
that X = 2 is co-prime to N = 15. From Lemma 5.6, the order r of 2 modulo 15
satisfies r ≤ 15. Because the number of bit representing N = 15 is four bits long, we
also only need to use four bits that represent the value of r.
Determining the order r of 2 modulo 15 is equivalent to determine the period r of
a given oracular function Pf : {r 1 r 2 r 3 r 4 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 4} → {2r1 r2 r3 r4
(mod 15) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 4}. The period r of Pf is to satisfy Pf (r 1 r 2 r 3
r 4 ) = Pf (r 1 r 2 r 3 r 4 + r) to any two inputs (r 1 r 2 r 3 r 4 ) and (r 1 r 2 r 3 r 4 + r). Sixteen
outputs of Pf that takes each input from r10 r20 r30 r40 through r11r21r31r41 are subsequently
1, 2, 4, 8, 1, 2, 4, 8, 1, 2, 4, 8, 1, 2, 4 and 8. The frequency f of Pf is equal to the
number of the period per sixteen outputs and is equal to four. The period r of Pf is
the reciprocal of the frequency f of Pf . Thus, we obtain r = 1f = 16f = 16 4
= 4 and
16
r × f = 4 × 4 = 16.
On the other hand, we think of the input domain of Pf as the time domain and its
output as signals. Computing the order r of 2 modulo 15 is equivalent to determine
the period r and the frequency f of signals in the time domain (the input domain).
Because the output of each input from r10 r20 r30 r40 through r11r21r31r41 is subsequently
1, 2, 4, 8, 1, 2, 4, 8, 1, 2, 4, 8, 1, 2, 4 and 8, we take the sixteen input values as
the corresponding sixteen time units and the sixteen outputs as the sixteen samples
of signals. Each sample encodes an output of Pf . The output can take 1, 2, 4 or 8.
The sixteen input values from r10 r20 r30 r40 through r11r21r31r41 corresponds to sixteen time
units from zero through fifteen.
We use Fig. 5.3 to explain the reason of why computing the order r of 2 modulo
15 is equivalent to determine the period r and the frequency f of signals in the
time domain (the input domain). In Fig. 5.3, the horizontal axis is to represent the
time domain in which it contains the input domain of Pf and the vertical axis is to
represent signals in which it consists of the sixteen outputs of Pf . For convenience
of presentation, we use variable k to represent the decimal value of each binary input
and make use of 2 k mod 15 to represent 2r1 r2 r3 r4 (mod 15).
From Fig. 5.3, hidden patterns and information stored in a given oracular function
Pf are that the signal rotates back to its first signal (output with 1) four times. Its
signal rotates back to its second signal (output with 2) four times. Its signal rotates
back to its third signal (output with 4) four times and its signal rotates back to its
fourth signal (output with 8) four times. This indicates that there are four periods of
signals per sixteen time units and the frequency f of signals is equal to four.
Because in Fig. 5.3 the period r of signals is the reciprocal of the frequency f
of signals, the period r of signals is 14 = 16/4 = 4. The period r = 4 of signals in
16
0 1 0 0
Fig. 5.3 satisfies P f r10 r21r30 r40 = 2r1 r2 r3 r4 (mod 15) = 24 (mod 15) = 16 (mod 15) =
230 5 Order-Finding and Factoring
Fig. 5.3 Sampling sixteen points from sixteen outputs of a given oracular function that is Pf : {r 1
r 2 r 3 r 4 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 4} → {2r1 r2 r3 r4 (mod 15) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 4}
16 0
= 4 + = 4. (5.12)
4 4
Because the value of r is equal to 0, from the first execution of statement S 3 , it
returns to a true. Thus, next, from the first execution of statement S 4 , the answer is
to the continued fractional representation of (16/4)
16
= (q[1] = 4) = 4. (5.13)
4
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a rational
number
(16/4), the first convergent is
16
(q[1]) = 4 = 1 that is the closest to 4 = 4 and is actually equal to 16
4 1
4
. This
16
5.6 Compute the Order of 2 Modulo 15 and the Prime Factorization for 15 231
means that the first convergent (q[1]) = 4 = 41 is equal to the period r = r1 . Hence,
we obtain that the period r is equal to the numerator 4 of the first convergent. Because
the numerator r =4 of the first convergent is less than N =15, the numerator r =4 is
equivalent to that the order r = 4 of 2 modulo 15 satisfies 24 = 1 (mod 15).
We want to search for the prime factorization for N = 21. We need to find the
nontrivial factor for N = 21. From Lemmas 5.1 and 5.6, we select a number X = 2
so that the greatest common divisor of X = 2 and N = 21 is 1 (one). This indicates
that X = 2 is co-prime to N = 21. From Lemma 5.6, the order r of 2 modulo 21
satisfies r ≤ 21. The number of bit representing N = 21 is five bits long, so we only
need to make use of five bits that encode the value of r.
Computing the order r of 2 modulo 21 is equivalent to figure out the period r of a
given oracular function Af : {r 1 r 2 r 3 r 4 r 5 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 5} → {2r1 r2 r3 r4 r5
(mod 21) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 5}. The period r of Af is to satisfy Af (r 1 r 2 r 3 r 4
r 5 ) = Af (r 1 r 2 r 3 r 4 r 5 + r) to any two inputs (r 1 r 2 r 3 r 4 r 5 ) and (r 1 r 2 r 3 r 4 r 5 + r).
Thirty-two outputs of Af that takes each input from r10 r20 r30 r40 r50 through r11r21r31r41r51
are subsequently 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16,
11, 1, 2, 4, 8, 16, 11, 1 and 2. The frequency f of Af is equal to the number of the
period per thirty-two outputs. The period r of Af is the reciprocal of the frequency f
of Af . Hence, we obtain r = r1 = 1f = 32f and r × f = 32 × 1 = 32.
32
On the other hand, we think of the input domain of Af as the time domain and its
output as signals. Figuring out the order r of 2 modulo 21 is equivalent to compute
the period r and the frequency f of signals in the time domain (the input domain).
The output of each input from r10 r20 r30 r40 r50 through r11r21r31r41r51 is subsequently 1, 2, 4,
8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1, 2, 4, 8, 16, 11, 1 and 2.
Therefore, we take the thirty-two input values as the corresponding thirty-two time
units and the thirty-two outputs as the thirty-two samples of signals. Each sample
encodes an output of Af . The output can take 1, 2, 4, 8, 16 or 11. The thirty-two input
values from r10 r20 r30 r40 r50 through r11r21r31r41r51 corresponds to thirty-two time units from
zero through thirty-one.
We apply Fig. 5.4 to explain the reason of why determining the order r of 2
modulo 21 is equivalent to determine the period r and the frequency f of signals in
the time domain (the input domain). In Fig. 5.4, the horizontal axis is to represent the
time domain in which it contains the input domain of Af and the vertical axis is to
represent signals in which it consists of the thirty-two outputs of Af . For convenience
of presentation, we make use of variable k to represent the decimal value of each
binary input and use 2 k mod 21 to represent 2r1 r2 r3 r4 r5 (mod 21).
From Fig. 5.4, hidden patterns and information stored in a given oracular function
Af are that the signal rotates back to its first signal (output with 1) six times. Its signal
232 5 Order-Finding and Factoring
Fig. 5.4 Sampling thirty-two points from thirty-two outputs of a given oracular function that is Af :
{r 1 r 2 r 3 r 4 r 5 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 5} → {2r1 r2 r3 r4 r5 (mod 21) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 5}
rotates back to its second signal (output with 2) six times. Its signal rotates back to its
third signal (output with 4) five times and its signal rotates back to its fourth signal
(output with 8) five times. Its signal rotates back to its fifth signal (output with 16)
five times and its signal rotates
back to its sixth signal (output with 11) five times.
This is to say that there are 5 26 periods of signals per thirty-two time units and the
frequency f of signals is equal to 5 26 .
Since in Fig. 5.4 the period r of signals is the reciprocal of the frequency f of
signals, the period r of signals is 512 = 321 = 32 32 = 6/1 = 6. The period r = 6 of
6 6 6
32 32
0 0 1 1 0
signals in Fig. 5.4 satisfies Af (r 01 r 02 r 13 r 14 r 05 )
= 2r1 r2 r3 r4 r5 (mod 21) = 26 (mod 21)
= 64 (mod 21) = 1 (mod 21), so the period r = 6 of signals in Af is equivalent to
the order r = 6 of 2 modulo 21. The cost to find the order r = 6 of 2 modulo 21 is
to implement thirty-two (25 ) operations of modular exponentiation, 2r1 r2 r3 r4 r5 (mod
21). Because r = 6 is even and is less than 21, from Lemma 5.2, we use Euclid’s
6 6
algorithm to compute gcd(21, 2 2 + 1) and gcd(21, 2 2 − 1). This implies that two
nontrivial factors for N = 21 are respectively 3 and 7. Thus, the prime factorization
for N = 21 is N = 3 × 7.
1 1 32
Because 5 2 = 32 = 32 = 6/1 is a rational number and is an integer, we apply
6 6 6
32 32
the continued fractional algorithm in Fig. 5.2 to determine the continued fractional
representation of (c/d) if c = 6 and d = 1 and the corresponding convergent for
explaining how the continued fractional algorithm works out in real applications.
From the first execution of statement S 0 through statement S 2 , it gets i = 1, q[1]
= c/d = 6/1 = 6 and r = 6 (mod 1) = 0. This is to split (6/1) into its integer and
fractional part and not to invert its fractional part,
6 0
= 6 + = 6. (5.14)
1 1
5.7 Determine the Order of 2 Modulo 21 … 233
6
= (q[1] = 6) = 6. (5.15)
1
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a rational number (6/1), the first convergent is
6 1 1 32 6
(q[1]) = 6 = 1
that is the closest to 5 26
= 32 = 32 = 1
and is actually equal to
6 6
32 32
6
1
This indicates that the first convergent (q[1]) = 6 = 16 is equal to the period r = r1 .
.
Thus, we obtain that the period r is equal to the numerator 6 of the first convergent.
Since the numerator r =6 of the first convergent is less than N =21, the numerator r
=6 is equivalent to that the order r = 6 of 2 modulo 21 satisfies 26 = 1 (mod 21).
We would like to find the prime factorization for N = 35. We need to search for the
nontrivial factor for N = 35. From Lemmas 5.1 and 5.6, we select a number X = 2
so that the greatest common divisor of X = 2 and N = 35 is 1 (one). This implies
that X = 2 is co-prime to N = 35. Because from Lemma 5.6, the order r of 2 modulo
35 satisfies r ≤ 35. The number of bit representing N = 35 is six bits long, we only
need to use six bits that encode the value of r.
Calculating the order r of 2 modulo 35 is equivalent to compute the period r of
a given oracular function Bf : {r 1 r 2 r 3 r 4 r 5 r 6 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6} →
{2r1 r2 r3 r4 r5 r6 (mod 35) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6}. The period r of Bf is to satisfy
Bf (r 1 r 2 r 3 r 4 r 5 r 6 ) = Bf (r 1 r 2 r 3 r 4 r 5 r 6 + r) to any two inputs (r 1 r 2 r 3 r 4 r 5 r 6 )
and (r 1 r 2 r 3 r 4 r 5 r 6 + r). The front twenty-four outputs of Bf that takes each input
from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 are subsequently 1, 2, 4, 8, 16, 32, 29, 23,
11, 22, 9, 18, 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9 and 18. The middle twenty-four
outputs of Bf are respectively 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9, 18, 1, 2, 4, 8, 16,
32, 29, 23, 11, 22, 9 and 18. The last sixteen outputs of Bf are subsequently 1, 2, 4,
8, 16, 32, 29, 23, 11, 22, 9, 18, 1, 2, 4 and 8. The frequency f of Bf is equal to the
number of the period per sixty-four outputs. The period r of Bf is the reciprocal of
the frequency f of Bf . Therefore, we obtain r = r1 = 1f = 64f and r × f = 64 × 1 =
64
64.
On the other hand, we think of the input domain of Bf as the time domain and its
output as signals. Determining the order r of 2 modulo 35 is equivalent to figure out
the period r and the frequency f of signals in the time domain (the input domain). The
front twenty-four outputs of each input from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 are
234 5 Order-Finding and Factoring
Fig. 5.5 Sampling sixty-four points from sixty-four outputs of a given oracular function that is Bf :
{r 1 r 2 r 3 r 4 r 5 r 6 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6} → {2r1 r2 r3 r4 r5 r6 (mod 35) | ∀ r d ∈ {0, 1} for 1 ≤
d ≤ 6}
subsequently 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9, 18, 1, 2, 4, 8, 16, 32, 29, 23, 11, 22,
9 and 18. The middle twenty-four outputs are respectively 1, 2, 4, 8, 16, 32, 29, 23,
11, 22, 9, 18, 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9 and 18. The last sixteen outputs are
subsequently 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9, 18, 1, 2, 4 and 8. Hence, we take the
sixty-four input values as the corresponding sixty-four time units and the sixty-four
outputs as the sixty-four samples of signals. Each sample encodes an output of Bf .
The output can take 1, 2, 4, 8, 16, 32, 29, 23, 11, 22, 9 or 18. The sixty-four input
values from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 corresponds to sixty-four time units
from zero through sixty-three.
We apply Fig. 5.5 to show the reason of why computing the order r of 2 modulo
35 is equivalent to determine the period r and the frequency f of signals in the time
domain (the input domain). In Fig. 5.5, the horizontal axis is to represent the time
domain in which it consists of the input domain of Bf and the vertical axis is to
represent signals in which it includes the sixty-four outputs of Bf . For convenience
of presentation, we use variable k to represent the decimal value of each binary input
and use 2 k mod 35 to represent 2r1 r2 r3 r4 r5 r6 (mod 35).
From Fig. 5.5, hidden patterns and information stored in a given oracular function
Bf are that the signal rotates back to its first signal (output with 1) six times. Its signal
rotates back to its second signal (output with 2) six times. Its signal rotates back to
its third signal (output with 4) six times and its signal rotates back to its fourth signal
(output with 8) six times. Its signal rotates back to its fifth signal (output with 16)
five times and its signal rotates back to its sixth signal (output with 32) five times.
Its signal rotates back to its seventh signal (output with 29) five times and its signal
rotates back to its eighth signal (output with 23) five times. Its signal rotates back
to its ninth signal (output with 11) five times and its signal rotates back to its tenth
signal (output with 22) five times. Its signal rotates back to its eleventh signal (output
with 9) five times and its signal rotates 4back
to its twelfth signal (output with 18) five
times. This indicates that there are 5 12 periods of signals per sixty-four time units
4
and the frequency f of signals is equal to 5 12 .
Since in Fig. 5.5 the period r of signals is the reciprocal of the frequency f of
signals, the period r of signals is 5 14 = 641 = 64 64 = 12/1 = 12. The period r = 12 of
12 12 12
64 64
0 0 1 1 0 0
signals in Fig. 5.5 satisfies Bf (r10 r20 r30 r40 r50 r60 ) = 2r1 r2 r3 r4 r5 r6 (mod 35) = 212 (mod 35)
5.8 Calculate the Order of 2 Modulo 35 and the Prime Factorization for 35 235
12 0
= 12 + = 12. (5.16)
1 1
Since the value of r is equal to 0, from the first execution of statement S 3 , it returns
to a true. Thus, next, from the first execution of statement S 4 , the answer is to the
continued fractional representation of (12/1)
12
= (q[1] = 12) = 12. (5.17)
1
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a
rational number (12/1),the first convergent is
12 1 1 64 12
(q[1]) = 12 = 1
that is the closest to 4
5 12
= 64 = 64 = 1
and is actually equal
12 12
64 64
to 12
1
. This is to say that the first convergent (q[1]) = 12 = 12
1
is equal to the period r
r
= 1 . Therefore, we obtain that the period r is equal to the numerator 12 of the first
convergent. The numerator r =12 of the first convergent is less than N =35, so the
numerator r =12 is equivalent to that the order r = 12 of 2 modulo 35 satisfies 212
= 1 (mod 35).
We would like to search for the prime factorization for N = 33. We need to find the
nontrivial factor for N = 33. From Lemmas 5.1 and 5.6, we select a number X = 5
so that the greatest common divisor of X = 5 and N = 33 is 1 (one). This is to say
that X = 5 is co-prime to N = 33. Since from Lemma 5.6, the order r of 5 modulo
236 5 Order-Finding and Factoring
33 satisfies r ≤ 33. The number of bit representing N = 33 is six bits long, we only
need to use six bits that encode the value of r.
Computing the order r of 5 modulo 33 is equivalent to calculate the period r of
a given oracular function C f : {r 1 r 2 r 3 r 4 r 5 r 6 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6} →
{5r1 r2 r3 r4 r5 r6 (mod 33) | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6}. The period r of C f is to satisfy
C f (r 1 r 2 r 3 r 4 r 5 r 6 ) = C f (r 1 r 2 r 3 r 4 r 5 r 6 + r) to any two inputs (r 1 r 2 r 3 r 4 r 5
r 6 ) and (r 1 r 2 r 3 r 4 r 5 r 6 + r). The front twenty outputs of C f that takes each input
from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 are subsequently 1, 5, 25, 26, 31, 23, 16,
14, 4, 20, 1, 5, 25, 26, 31, 23, 16, 14, 4 and 20. The middle twenty outputs of C f are
respectively 1, 5, 25, 26, 31, 23, 16, 14, 4, 20, 1, 5, 25, 26, 31, 23, 16, 14, 4 and 20.
The last twenty-four outputs of C f are subsequently 1, 5, 25, 26, 31, 23, 16, 14, 4, 20,
1, 5, 25, 26, 31, 23, 16, 14, 4, 20, 1, 5, 25 and 26. The frequency f of C f is equal to
the number of the period per sixty-four outputs. The period r of C f is the reciprocal
of the frequency f of C f . Hence, we get r = r1 = 1f = 64f and r × f = 64 × 1 = 64.
64
On the other hand, we think of the input domain of C f as the time domain and its
output as signals. Calculating the order r of 5 modulo 33 is equivalent to determine
the period r and the frequency f of signals in the time domain (the input domain).
The front twenty outputs of each input from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 are
subsequently 1, 5, 25, 26, 31, 23, 16, 14, 4, 20, 1, 5, 25, 26, 31, 23, 16, 14, 4 and
20. The middle twenty outputs are respectively 1, 5, 25, 26, 31, 23, 16, 14, 4, 20, 1,
5, 25, 26, 31, 23, 16, 14, 4 and 20. The last twenty-four outputs are subsequently 1,
5, 25, 26, 31, 23, 16, 14, 4, 20, 1, 5, 25, 26, 31, 23, 16, 14, 4, 20, 1, 5, 25 and 26.
Therefore, we take the sixty-four input values as the corresponding sixty-four time
units and the sixty-four outputs as the sixty-four samples of signals. Each sample
encodes an output of C f . The output can take 1, 5, 25, 26, 31, 23, 16, 14, 4 or 20.
The sixty-four input values from r10 r20 r30 r40 r50 r60 through r11r21r31r41r51r61 corresponds to
sixty-four time units from zero through sixty-three.
We use Fig. 5.6 to explain the reason of why figuring out the order r of 5 modulo
33 is equivalent to compute the period r and the frequency f of signals in the time
domain (the input domain). In Fig. 5.6, the horizontal axis is to represent the time
domain that is the input domain of C f . The vertical axis is to represent signals that
encode the sixty-four outputs of C f . For convenience of presentation, we make use
Fig. 5.6 Sampling sixty-four points from sixty-four outputs of a given oracular function that is C f :
{r 1 r 2 r 3 r 4 r 5 r 6 | ∀ r d ∈ {0, 1} for 1 ≤ d ≤ 6} → {5r1 r2 r3 r4 r5 r6 (mod 33) | ∀ r d ∈ {0, 1} for 1 ≤
d ≤ 6}
5.9 Determine the Order of 5 Modulo 33 and the Prime … 237
of variable k to represent the decimal value of each binary input and apply 5 k mod
33 to represent 5r1 r2 r3 r4 r5 r6 (mod 33).
From Fig. 5.6, hidden patterns and information stored in a given oracular function
C f are that the signal rotates back to its first signal (output with 1) seven times. Its
signal rotates back to its second signal (output with 5) seven times. Its signal rotates
back to its third signal (output with 25) seven times and its signal rotates back to its
fourth signal (output with 26) seven times. Its signal rotates back to its fifth signal
(output with 31) six times and its signal rotates back to its sixth signal (output with
23) six times. Its signal rotates back to its seventh signal (output with 16) six times
and its signal rotates back to its eighth signal (output with 14) six times. Its signal
rotates back to its ninth signal (output with 4) six times and its signal rotates
4 back to
its tenth signal (output with 20) six times. This implies that there are 6 10 periods
4
of signals per sixty-four time units and the frequency f of signals is equal to 6 10 .
Because in Fig. 5.6 the period r of signals is the reciprocal of the frequency f of
signals, the period r of signals is 6 14 = 641 = 6464 = 10/1 = 10. The period r = 10 of
10 10 10
64 64
0 0 1 0 1 0
signals in Fig. 5.6 satisfies C f (r 01 r 02 r 13 r 04 r 15 r 06 )
= 5r1 r2 r3 r4 r5 r6 (mod 33) = 510 (mod
33) = 9765625 (mod 33) = 1 (mod 33), so the period r = 10 of signals in C f is
equivalent to the order r = 10 of 5 modulo 33. The cost to find the order r = 10 of
5 modulo 33 is to implement sixty-four (26 ) operations of modular exponentiation,
5r1 r2 r3 r4 r5 r6 (mod 33). Since r = 10 is even and is less than 33, from Lemma 5.2,
10 10
we use Euclid’s algorithm to compute gcd(33, 5 2 + 1) and gcd(33, 5 2 − 1). This
indicates that two nontrivial factors for N = 33 are respectively 3 and 11. Therefore,
the primefactorization for N = 33 is N = 3 × 11.
1 1 64
Since 4
6 10
= 64 = 64 = 10/1 is a rational number and is an integer, we use
10 10
64 64
the continued fractional algorithm in Fig. 5.2 to determine the continued fractional
representation of (c/d) if c = 10 and d = 1 and the corresponding convergent for
explaining how the continued fractional algorithm works out in real applications.
From the first execution of statement S 0 through statement S 2 , it obtains i = 1, q[1]
= c/d = 10/1 = 10 and r = 10 (mod 1) = 0. This is to split (10/1) into its integer
and fractional part and not to invert its fractional part,
10 0
= 10 + = 10. (5.18)
1 1
Because the value of r is equal to 0, from the first execution of statement S 3 , it
returns to a true. Therefore, next, from the first execution of statement S 4 , the answer
is to the continued fractional representation of (10/1)
10
= (q[1] = 10) = 10. (5.19)
1
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a rational number (10/1), the first convergent is
238 5 Order-Finding and Factoring
10 1 1 64 10
(q[1]) = 10 = 1
that is the closest to 4
6 10
= 64 = 64 = 1
and is actually equal
10 10
64 64
to 10
1
. This means that the first convergent (q[1]) = 10 = 101
is equal to the period r =
r
1
. Hence, we get that the period r is equal to the numerator 10 of the first convergent.
Because the numerator r =10 of the first convergent is less than N =33, the numerator
r =10 is equivalent to that the order r = 10 of 5 modulo 33 satisfies 510 = 1 (mod
33).
We assume that the set Z={…, −3, −2, −1, 0, 1, 2, 3, …} of integers and the set Y
= {0, 1, 2, 3, …} of natural numbers. The notion d | a (read “d divides a”) means
that a = q × d for some integer q and a is a multiple of d and d is a divisor of a. For
example, 15 is a multiple of 1, 3, 5, and 15 and 1, 3, 5, and 15 are the divisors of 15.
Every integer a is divisible by the trivial divisor 1 and a. Nontrivial divisor of integer
a are also called factors of a. For example, the factors (the nontrivial divisors) of 15
are 3 and 5.
An integer a > 1 whose only divisors are the trivial divisor a and 1 is said to be a
prime number (or, more simply, a prime). The first six prime, in order, are 2, 3, 5,
7, 11 and 13. An integer a > 1 that is not a prime is said to be a composite number
(or, more simply, a composite). For example, 15 is a composite because it has the
factors 3 and 5. The integer 1 is said to be a unit and is neither prime nor composite.
Similarly, the integer 0 and all negative integers are neither prime nor composite.
From (5.1), for given any positive integers w and N, there are unique integers q and
r such that 0 ≤ r < N and w = q × N + r. Integer q is the quotient (result) of
dividing w by N. Integer r is the remainder of dividing w by N and we write r =
w (mod N). Given any integer N, we can partition the integers into those that are
multiples of N and those that are not multiples of N. By classifying the multiples
and the non-multiples of N in light of their remainders when divided by N, we can
obtain the refinement of this partition.
According to their remainders modulo N, the integer can be divided into N
equivalent classes. The equivalent class modulo N including an integer w is
For example, [2]5 = {…, 2, 7, 12, 17, …}. If the remainder of w modulo N is the
same as that of a modulo N, then we can say that writing a ∈ [w]N is the same as
writing a = w (mod N). The set of all such equivalent classes is
Proof For any two elements [c]N and [d]N in ZN , from (5.20) through (5.23), we
obtain that 0 ≤ c ≤ N − 1, 0 ≤ d ≤ N − 1 and [c]N +N [d]N = [c + d]N . If 0 ≤ c
+ d ≤ N − 1, then [c]N +N [d]N = [c + d]N is an element in ZN . If N ≤ c + d ≤
2 × N − 2, then [c]N +N [d]N = [c + d]N = [c + d − N]N . Because 0 ≤ c + d −
N ≤ N − 2, it is one element in ZN . This means that the system (ZN , +N ) is closed.
For any three elements [c]N , [d]N and [e]N in ZN , from (5.23), we obtain ([c]N +
N [d] N ) +N [e]N = ([c + d]N ) +N [e]N = [(c + d) + e]N = [c + (d + e)]N = [c]N
+N ([d + e]N ) = [c]N +N ([d]N ) +N [e]N ). This indicates that the system (ZN , +N )
satisfies the associativity of +N .
For any two elements [c]N and [d]N in ZN , from (5.23), we obtain [c]N +N [d]N =
[c + d]N = [d + c]N = [d]N +N [c]N . This implies that the system (ZN , +N ) satisfies
the commutativity of +N .
The identity element of the system (ZN , +N ) is [0]N because for any element [c]N
in ZN , from (5.23), we have [c]N +N [0]N = [c + 0]N = [c]N = [0 + c]N = [0]N +
N [c]N . The additive inverse of any element [c]N in ZN is [N − c]N because [c]N +
N [N − c]N = [c + N − c]N = [N]N = [0]N . The number of elements in the system
(ZN , +N ) is N, so it is finite. Therefore, from the statements above, we at once infer
that the system (ZN , +N ) is a finite abelian group.
The set Z*N is the set of elements in ZN that are relatively prime to N and is
Proof For any two elements [c]N and [d]N in Z ∗N , from (5.20) through (5.23), we
get that 0 ≤ c ≤ N − 1, 0 ≤ d ≤ N − 1, gcd(c, N) = 1, gcd(d, N) = 1 and [c]N ×N
[d]N = [c × d]N . Because gcd(c, N) = 1 and gcd(d, N) = 1, we have gcd(c × d, N)
= 1. This means that [c]N ×N [d]N = [c × d]N is an element in Z ∗N . Therefore, the
system (Z ∗N , × N ) is closed.
For any three elements [c]N , [d]N and [e]N in Z ∗N , from (5.23), we obtain ([c]N ×
N [d]N ) ×N [e]N = ([c × d]N ) ×N [e]N = [(c × d) × e]N = [c × (d × e)]N = [c]N
×N ([d + e]N ) = [c]N ×N ([d]N ) ×N [e]N ). This is to say that the system (Z ∗N , × N )
satisfies the associativity of ×N .
For any two elements [c]N and [d]N in Z ∗N , from (5.23), we get [c]N ×N [d]N = [c
× d]N = [d × c]N = [d]N ×N [c]N . This indicates that the system (Z ∗N , × N ) satisfies
the commutativity of ×N .
For any element [c]N in Z ∗N , from (5.23), we have [c]N ×N [1]N = [c × 1]N = [c]N
= [1 × c]N = [1]N ×N [c]N . This indicates that the identity element of the system
(Z ∗N , × N ) is [1]N .
Any element [c]N in Z ∗N satisfies gcd(c, N) = 1. Therefore, from Lemma 5.5,
there exists a unique multiplicative inverse [c−1 ]N of [c]N , modulo N, such that [c]N
×N [c−1 ]N = [c × c−1 ]N = [1]N = [c−1 × c]N = [c−1 ]N ×N [c]N . The number
of elements in the system (Z ∗N , × N ) is less than N, so it is finite. Hence, from the
statements above, we at once derive that the system (Z ∗N , × N ) is a finite abelian
group.
The size of Z ∗N is known as Euler’s phi function φ(N) satisfies the following
equation
1
φ(N ) = N × 1− , (5.25)
p|N p
where p runs over all the primes dividing n (including N itself, if N is a prime).
For example, because the prime divisors of 15 are 3 and 5, we obtain φ(15) = 15 ×
(1 − (1/3)) × (1 − (1/5)) = 15 × (2/3) × (4/5) = 8. This is to say that the size of
Z ∗15 is eight (8) and Z ∗15 is equal to {[1]15 , [2]15 , [4]15 , [7]15 , [8]15 , [11]15 , [13]15 ,
[14]15 }. If p is a prime, then p itself is the only prime divisor. Therefore, from (5.25),
we obtain φ(p) = p × (1 − (1/p)) = p − 1. The only integers that are less than pa
and are not co-prime to pa are the multiples of p: p, 2 × p, …, (pa−1−1 ) × p, from
which we infer
Furthermore, if c and d are co-prime, then φ(c × d) satisfies the following equation
On the other hand, when N is a power of an odd prime p, N = pa . It turns out that
Z ∗N = Z a∗ a∗
p is a cyclic group, that is, there is an element h in Z p which generates
Z a∗
p in the sense that any other element y may be written y = h (mod N) = hm (mod
m
a
p ) for some non-negative integer m. We use Lemmas 5.9 and 5.10 to explain the
possibility of finding the even order that are not equal to N − 1 of X modulo N.
Lemma 5.9 We assume that p is an odd prime and 2b is the largest power of 2
dividing φ(pa ). Then with probability exactly one-half 2b divides the order modulo
pa of a randomly chosen element of Z a∗p .
Proof Because p is an odd prime, from (5.26) we obtain that φ(pa ) = pa−1 × (p −
1) is even. Because φ(pa ) is even and 2b divides φ(pa ), we obtain b ≥ 1. Since Za* p
is a cyclic group, there exists an element h in Z a∗ a∗
p which generates Z p in the sense
that any other element X may be written X = hm (mod pa ) for some m in the range 1
through φ(pa ) that is the size of Z a∗ m a
p . Let r the order of h modulo p and consider
two cases. The first case is when m is odd. Since h is co-prime to (pa ) and φ(pa )
φ( pa )
is the size of Z a∗ a
p , φ(p ) is the least value such that h = 1 (mod pa ). Because
m r m r a a
(h ) = h × = 1 (mod p ), we infer that φ(p ) divides (m × r). Since m is odd,
φ(pa ) is even, 2b divides φ(pa ) and φ(pa ) divides (m × r) and 2b divides (m × r),
we infer that 2b divides r. The second case is when m is even. Because h is co-prime
to pa and m is even, we infer that hm /2 modulo pa is co-prime to pa . Therefore, we
a a m/2
have h m×φ( p )/2 = h φ( p ) = (1)m /2 = 1 (mod pa ). Because r is the order of hm
modulo p that is the least value such that (hm )r = hm × r = 1 (mod pa ), we infer
a
that r divides (φ(pa )/2) and r is less than 2b that is the largest power of 2 dividing
φ(pa ). Thus, we infer that 2b does not divide r.
Because the value of m is in the range 1 through φ(pa ) that is even and is the size
of Z a∗ a∗
p , we may partition Z p into two sets of equal size. The first set of equal size
is those that may be written hm (mod pa ) with that m is odd, for which 2b divides r
that is the order of hm modulo pa . The second set of equal size is those that may be
written hm (mod pa ) with that m is even, for which 2b does not divide r that is the
order of hm modulo pa . Therefore, with probability (1/2) the integer 2b divides the
order r of a randomly chosen element Z a∗ p , and with probability (1/2) it does not.
Lemma 5.10 We assume that N = p1a1 × ··· × pmam is the prime factorization of an
odd composite positive integer. Let X be chosen uniformly at random from Z ∗N and
let r be the order of X modulo N. Then P(r is even and X r/2 = − 1 (mod N)) ≥ 1 −
(1/2 m ).
Proof We show that P(r is odd or X r/2 = − 1 (mod N)) ≤ 1/2 m . According to the
Chinese remainder theorem, selecting X uniformly at random from Z ∗N is equivalent
to selecting X k independently and uniformly at random from Z ∗pak , and satisfying
k
242 5 Order-Finding and Factoring
that X = X k (mod pkak ) for 1 ≤ k ≤ m. Let r k be the order of X k modulo pkak . Let
2bk be the largest power of 2 dividing r k and 2b is the largest power of 2 dividing r.
Because X is co-prime to N ( p1a1 × ··· × pmam ), φ(N) = φ( p1a1 × ··· × pmam ) is the size
a1 am
of Z ∗N that is the least value such that X φ(N ) = X φ ( p1 ×···× pm ) = 1 (mod N). Since r
is the order of X modulo N that is the least value such that X r = 1 (mod N), we have
r = φ( p1a1 × · · · × pmam ) = φ( p1a1 ) ×··· × φ( pmam ). Because X k is co-prime to ( pkak )
a
φ( p k )
for 1 ≤ k ≤ m, φ( pkak ) is the size of Z ∗pak that is the least value such that X k k =
k
1 (mod ( pkak )). Since r k is the order of X k modulo ( pkak ) that is the least value such
that X rk = 1 (mod ( pkak )), we have r k = φ( pkak ) for 1 ≤ k ≤ m. Because r = φ( p1a1 )
× … × φ( pmam ) and r k = φ( pkak ) for 1 ≤ k ≤ m, we infer that r k divides r for 1 ≤ k
≤ m. We will show that to have r odd or X r/2 = - 1 (mod N) it is necessary that bk
takes the same value for 1 ≤ k ≤ m. The result then follows, as from Lemma 5.9 the
probability of this occurring is at most (1/2) × (1/2) × ··· × (1 /2) = 1/2m .
We consider the first case is when r is odd. Because r k divides r for 1 ≤ k ≤ m,
we infer r k is odd. Because 2bk divides r k for 1 ≤ k ≤ m, we obtain bk = 0 for 1 ≤ k
≤ m. The second case is when r is even and X r/2 = −1 (mod N). This is to say that
X r/2 = N − 1 = p1a1 × ··· × pmam − 1. Therefore, we have X r/2 = N − 1 = p1a1 ×
··· × pmam − 1 = - 1 (mod ( pkak )). So we obtain that r k does not divide (r/2). Because
r k divides r for 1 ≤ k ≤ m, we must have bk = b for 1 ≤ k ≤ m. Since P(r is even
and X r/2 = − 1 (mod N)) + P(r is odd or X r/2 = −1 (mod N)) = 1 and P(r is odd
or X r/2 = −1 (mod N)) ≤ 1 / 2 m , we have P(r is even and X r/2 = − 1 (mod N)) ≥
1 − (1 / 2 m ).
A consumer wants to buy something on the internet. He would like to transmit his
credit card number over the internet in such a way that only the company offering
the products that he is buying can receive the number. A cryptographic protocol or
a cryptosystem on the internet can achieve such private communication. Effective
cryptosystems make it easy for two parties who want to communicate each other,
but make it very difficult for the eavesdropper to eavesdrop on the content of the
conversation.
A particularly important class of cryptosystems are the public key cryptosystems.
In a public key cryptosystem, Mary wants to send messages to her friends and to
receive messages sent by her friends. She must first generate two cryptographic keys.
One is a public key, P, and the other is a secret key, S. After Mary has generated her
keys, she announces or publishes the public key so that anybody can gain access to
the public key.
John is Mary’s good friend. He would like to send a private message to Mary.
Therefore, John first gets a copy of Mary’s public key P. Then, he encrypts the private
message he wants to send Mary, making use of Mary’s public key P to complete the
5.11 Public Key Cryptography and the RSA Cryptosystem 243
encryption. Because the public key and the encoded message is the only information
available to an eavesdropper, it will be impossible for the eavesdropper to recover the
message. However, Mary has the secret key S that is not available to an eavesdropper.
She uses the secret key S to decrypt the encrypted message and obtains the original
message. This transformation known as decryption is inverse to encryption, allowing
Mary to recover John’s private message.
The most widely used of public key cryptosystems is the RSA cryptosystem,
named RSA for the initials of its creators, Rivest, Shamir, and Adleman. The
presumed security of the RSA cryptosystem is based on the apparent difficulty of
factoring on a digital computer. Now Mary wishes to generate public and secret keys
for use with the RSA cryptosystem. She makes use of the following procedure to
generate them:
(1) Choose two large prime numbers, p and q.
(2) Calculate the product N = p × q.
(3) Choose at random a small odd integer, e, which is relatively prime to φ(N) =
(p − 1) × (q − 1).
(4) Calculate d, the multiplicative inverse of e, modulo φ(N).
(5) The public key is the pair P = (e, N).
(6) The secret key is the pair S = (d, N).
Now John uses the public key (e, N) to encrypt a message M to send to Mary. We
assume that the message M has only logN2 bits, as longer messages may be encrypted
by means of breaking M up into blocks of at most logN2 bits and then encrypting the
blocks separately. The encryption procedure for a single block is to calculate:
E(M) is the encrypted version of the message M, which John sends to Mary. Mary
is able to decrypt quickly the message applying her secret key S = (d, N), simply by
raising the encrypted message to the dth power:
How can the RSA cryptosystem be broken? The answer is to that if we can effi-
ciently factor a big composite number N into the production of two big prime numbers
p and q. Then, we can extract p and q. This means that we can efficiently calculate
φ(N) = (p − 1) × (q − 1). Next, we can efficiently compute d, the multiplicative
inverse of e, modulo φ(N). Therefore, we can completely determine the secret key
(d, N). So, if factoring large numbers were easy then breaking the RSA cryptosystem
would be easy.
244 5 Order-Finding and Factoring
Fig. 5.7 Circuit Representation of a CSWAP gate with three quantum bits
5.12 Implementing The Controlled-Swap Gate … 245
Fig. 5.8 Decomposing CCNOT gate into six CNOT gates and nine gates of one bit
Fig. 5.9 The corresponding quantum circuit of the program in Listing 5.1
246 5 Order-Finding and Factoring
q[2] to subsequently encode the first target bit |S 1 , the second target bit |S 2 and the
controlled bit |C 1 .
Listing 5.1 The Program of Implementing a CSWAP Gate of Three Quantum Bits.
Then, the statement “creg c[3];” on instruction number four in the first line of
Listing 5.1 is to declare that there are three classical bits in the program. In the left
bottom of Fig. 5.9, three classical bits are respectively c[0], c[1] and c[2]. The initial
value of each classical bit is set to 0. Classical bit c[2] is the most significant bit and
classical bit c[0] is the least significant bit.
Next, the two statements “x q[0];” and “x q[2];” on line number five through line
number six in the second line of Listing 5.1 implement two NOT gates to quantum
bit q[0] and quantum bit q[2] in the
first time
slot of quantum circuit in Fig. 5.9.
the
01 1 0
They both actually complete × = = |1 . This indicates that
10 0 1
converting q[0] from one state |0 to another state |1 and converting q[2] from one
state |0 to another state |1 are completed. Since in the first time slot of the quantum
circuit in Fig. 5.9 there is no quantum gate to act on quantum bit q[1], its state |0 is
not changed. Therefore, we have the following new state vector
|1 = q[2]1 q[1]0 q[0]1 .
In the state vector |1 = |q[2]1 |q[1]0 |q[0]1 , quantum bit |q[2]1 is the input
state |1 of the controlled bit |C 1 in the CSWAP gate in Fig. 5.7. Quantum bit
|q[1]0 is the input state |0 of the target bit |S 2 in the CSWAP gate in Fig. 5.7.
Quantum bit |q[0]1 is the input state |1 of the target bit |S 1 in the CSWAP gate in
Fig. 5.7. Next, the statement “barrier q[0], q[1], q[2];” on line number seven in the
sixth line of Listing 5.1 implements one barrier instruction to prevent optimization
from reordering gates across its source line in the second time slot of the quantum
circuit in Fig. 5.9. Next, from instruction number eight through instruction number
twenty-two in Listing 5.1, the fifteen statements are “h q[1];” “cx q[0], q[1];”, “tdg
q[1];”, “cx q[2], q[1];”, “t q[1];”, “cx q[0], q[1];”, “tdg q[1];”, “cx q[2], q[1];”, “t
q[0];”, “t q[1];”, “h q[1];”, “cx q[2], q[0];”, “tdg q[0];”, “t q[2];” and “cx q[2], q[0];”.
They take the state vector |1 = |q[2]1 |q[1]0 |q[0]1 as their input and implement
the first CCNOT gate with two controlled bits q[2] and q[0] and one target bit q[1]
from the third time slot through the fifteen time slot of Fig. 5.9. Because the two
controlled bits q[2] and q[0] are both state |1 , the state |0 of the target bit q[1] is
converted into state |1 . Therefore, we have the following new state vector
|15 = q[2]1 q[1]1 q[0]1 .
248 5 Order-Finding and Factoring
Next, the statement “barrier q[0], q[1], q[2];” on instruction number twenty-
three in Listing 5.1 implements one barrier instruction to prevent optimization from
reordering gates across its source line in the sixteenth time slot of the quantum circuit
in Fig. 5.9. Next, from instruction number twenty-four through instruction number
thirty-eight in Listing 5.1, the fifteen statements are “h q[0];”, “cx q[1], q[0];”, “tdg
q[0];”, “cx q[2], q[0];”, “t q[0];”, “cx q[1], q[0];”, “tdg q[0];”, “cx q[2], q[0];”, “t
q[1];”, “t q[0];”, “h q[0];”, “cx q[2], q[1];”, “tdg q[1];”, “t q[2];” and “cx q[2], q[1];”.
They take the state vector |15 = |q[2]1 |q[1]1 |q[0]1 as their input and implement
the second CCNOT gate with two controlled bits q[2] and q[1] and one target bit
q[0] from the seventeenth time slot through the twenty-eighth time slot of Fig. 5.9.
Since the two controlled bits q[2] and q[1] are both state |1 , the state |1 of the target
bit q[0] is converted into state |0 . Thus, we obtain the following new state vector
|28 = q[2]1 q[1]1 q[0]0 .
Next, the statement “barrier q[0], q[1], q[2];” on instruction number thirty-nine
in Listing 5.1 implements one barrier instruction to prevent optimization from
reordering gates across its source line in the thirty-ninth time slot of the quantum
circuit in Fig. 5.9. Next, from instruction number forty through instruction number
fifty-four in Listing 5.1, the fifteen statements are “h q[1];”, “cx q[0], q[1];”, “tdg
q[1];”, “cx q[2], q[1];”, “t q[1];”, “cx q[0], q[1];”, “tdg q[1];”, “cx q[2], q[1];”, “t
q[0];”, “t q[1];”, “h q[1];”, “cx q[2], q[0];”, “tdg q[0];”, “t q[2];” and “cx q[2], q[0];”.
They take the state vector |28 = |q[2]1 |q[1]1 |q[0]0 as their input and implement
the third CCNOT gate with two controlled bits q[2] and q[0] and one target bit q[1]
from the thirtieth time slot through the forty-second time slot of Fig. 5.9. Because
the first controlled bit q[2] is state |1 and the second controlled bit q[0] is state |0
, the state |1 of the target bit q[1] is not changed. Hence, we get the following new
state vector
|42 = q[2]1 q[1]1 q[0]0 .
Next, the three statements “measure q[0] -> c[0];”, “measure q[1] -> c[1];” and
“measure q[2] -> c[2];” from instruction number fifty-five through instruction number
in Listing 5.1 is to measure the first quantum bit q[0], the second quantum bit q[1] and
the third quantum bit q[2]. They record the measurement outcome by overwriting the
first classical bit c[0], the second classical bit c[1] and the third classical bit c[2]. In
the backend simulator with thirty-two quantum bits in IBM’s quantum computers,
we use the command “run” to execute the program in Listing 5.1. The measured
result appears in Fig. 5.10. From Fig. 5.10, we obtain the answer 110 (c[2] = 1 =
q[2] = |1 , c[1] = 1 = q[1] = |1 and c[0] = 0 = q[0] = |0 ) with the probability
100%. Because the input state of the target bit |S 1 is state |1 , the input state of the
target bit |S 2 is state |0 and the input state of the controlled bit |C 1 is |1 in the
CSWAP gate in Fig. 5.7, the information contained in the two target bits |S 1 and
|S 2 are exchanged. Therefore, we have the final state of the target bit |S 1 encoded
5.12 Implementing The Controlled-Swap Gate … 249
by |q[0]0 is state |0 and the final state of the target bit |S 2 encoded by |q[1]1 is
state |1 with the probability 100%.
For positive integers X and N with that the value of X is less than the value of N
and the greatest common factor for them is one, the order (the period) of X modulo
N is to the least positive integer r such that X r = 1 (mod N). The order-finding
problem is to compute the order for some given X and N. On a digital computer, no
algorithm known solves the problem with the number of the bit of specifying N that
is L to be greater than or equal to log2 (N), by means of using resources polynomial
in the O(L) bits needed to specify the problem. In this section, we explain how Shor’s
order-finding algorithm is an efficient quantum algorithm to order finding.
Quantum circuit to Shor’s order-finding algorithm is schematically
depicted in
Fig. 5.11. The first quantum register of n quantum bits is ⊗nk=1 pk0 and the initial
state of each quantum bit is the |0 state. Quantum bit |p01 is the most significant
bit and quantum bit |p0n is the least significant bit. Because the order of X modulo
N is less than or equal to N, n is greater than or equal to log2 (N). Its decimal value
is equal to p1 × 2n−1 + p2 × 2n−2 + p3 × 2n−3 + ··· + pn × 2n−n . The second
quantum register of L quantum bits is ((⊗ L−1 0 1
y=1 w y ) ⊗ (|wL )). The initial state of
each quantum bit in the front (L − 1) quantum bits is the |0 state. The initial state of
the least significant quantum bit (|w1L ) is the |1 state. Quantum bit |w01 is the most
significant bit and quantum bit |w1L is the least significant bit. Its decimal value is
equal to w1 × 2L−1 + w2 × 2L−2 + w3 × 2L− 3 + ··· + pL × 2L−L . From Fig. 5.11,
the initial state vector is
0 1
|ϕ0 = ⊗nk=1 pk0 ⊗ ⊗ L−1
y=1 w y
⊗ w L . (5.31)
From Fig. 5.11, the initial state vector |ϕ 0 in (5.31) is followed by n Hadamard
gates on the first (upper) quantum register. This gives that the new state vector is
250 5 Order-Finding and Factoring
1 0 1
|ϕ1 = √ n ⊗nk=1 pk0 + pk1 ⊗ ⊗ L−1 y=1 w y
⊗ w L
2
⎛n ⎞
2 −1
1 ⎝
|P ⊗ ⊗ L−1
0 ⊗ w1 ⎠.
=√ y=1 w y L (5.32)
2n p=0
Next, from Fig. 5.11, the new state vector |ϕ 1 in (5.32) is followed by a quantum
n−1 n−2 n−n
gate |X P mod N = | X p1 ×2 + p2 ×2 +···+ pn ×2 mod N > operating on both quantum
registers. This gives that the new state vector is
⎛ n
⎞
2 −1
1 p
|ϕ2 = √ ⎝ |P X mod N ⎠. (5.33)
2n p=0
We
√
call (5.38) as the ideal case. If the argument of the absolute value operator
2×π
is e− −1× 2n ×i×r = 1, then (i × r /2n ) is not an integer and we can rewrite P(i) in
(5.37) as follows
√
⎛ 2 ⎞
r −1 − −1× 2×π
1 1 − e 2n ×i×r ×(1+Y P )
P(i) = ×⎝ . (5.39)
⎠
√
22×n − −1× 2×π
1 − e 2n ×i×r
P=0
√ 2
We call (5.39) as the practice case. Because 1 − e −1×θ = 4 × sin2 (θ /2) and
sin(-θ /2) = −sin(θ /2) and sin2 (−θ /2) = sin(−θ /2) × sin(−θ /2) = (-sin(θ /2)) ×
(−sin(θ /2)) = sin2 (θ /2), we can rewrite P(i) in (5.39) as follows
⎛ ⎞
r −1 4 × sin2 −2×π×i×r ×(Y P +1) 1
1 2n
× 2
P(i) = ×⎝ −2×π×i×r 1
⎠
4 × sin2
22×n P=0 2n
× 2
5.13 Shor’s Order-Finding Algorithm 253
⎛ ⎞
r −1 sin2 π×i×r ×(Y P +1)
1 2n
= ×⎝ π×i×r ⎠. (5.40)
22×n P=0
sin2 2n
We want to complete the prime factorization for N = 15. We need to find the nontrivial
factor for N = 15. From Lemmas 5.1 and 5.6, we select a number X = 2 so that the
greatest common divisor of X = 2 and N = 15 is 1 (one). This indicates that X = 2
is co-prime to N = 15. From Lemma 5.6, the order r of 2 modulo 15 satisfies r ≤
15. Since the number of bit representing N = 15 is four bits long, we also only need
to make use of four bits that represent the value of r. If the value of r is an even,
then the first nontrivial factor for N = 15 is equal to gcd(2r /2 + 1, N) and the second
nontrivial factor for N = 15 is equal to gcd(2r /2 − 1, N).
Computing the order r of 2 modulo 15 is equivalent to calculate the period r of a
given oracular function Of : {p1 p2 p3 p4 | ∀ pd ∈ {0, 1} for 1 ≤ d ≤ 4} → {2 p1 p2 p3 p4
(mod 15) | ∀ pd ∈ {0, 1} for 1 ≤ d ≤ 4}. An input variable p1 p2 p3 p4 is four
bits long. Bit p1 is the most significant bit and bit p4 is the least significant bit. The
corresponding decimal value is equal to p1 × 24−1 + p2 × 24−2 + p3 × 24−3 + p4 ×
24−4 = P. The period r of Of is to satisfy Of (p1 p2 p3 p4 ) = Of (p1 p2 p3 p4 + r) to
any two inputs (p1 p2 p3 p4 ) and (p1 p2 p3 p4 + r).
For implementing the operation of modular exponentiation, 2 p1 p2 p3 p4 (mod 15),
we assume that an auxiliary variable w1 w2 w3 w4 is four bits long. Bit w1 is the most
significant bit and bit w4 is the least significant bit. The corresponding decimal value
is equal to w1 × 24−1 + w2 × 24−2 + w3 × 24−3 + w4 × 24−4 = W. The initial value
of each bit in the front three bits is zero (0). The initial value of the least significant
bit w4 is one (1).
The order of (X = 2) modulo (N = 15) is to the least positive integer r such that 2r
= 1 (mod 15). Because r ≤ (N = 15) and the number of bits representing (N = 15)
is four bits long, the number of bits representing r is four bits long. Therefore, an
input variable p1 p2 p3 p4 is four bits long. Bit p1 is the most significant bit and bit
p4 is the least significant bit. The corresponding decimal value is equal to p1 × 24−1
256 5 Order-Finding and Factoring
+ p2 × 24−2 + p3 × 24−3 + p4 × 24−4 = P. This is to say that the range of the value
to P is from zero through fifteen. If P is to the least positive integer such that 2P =
1 (mod 15), then the value of r is equal to the value of P. We use pd0 to represent the
value of pd to be zero for 1 ≤ d ≤ 4 and apply pd1 to represent the value of pd to be
one for 1 ≤ d ≤ 4.
Because the remainder for 2P (mod 15) to 0 ≤ P ≤ 15 is from zero through
fourteen, we assume that an auxiliary variable w1 w2 w3 w4 is four bits long and we
use it to store the result of computing 2P (mod 15) to 0 ≤ P ≤ 15. We use wd0 to
represent the value of wd to be zero for 1 ≤ d ≤ 4 and apply wd1 to represent the value
of wd to be one for 1 ≤ d ≤ 4. Bit w1 is the most significant bit and bit w4 is the least
significant bit. The corresponding decimal value is equal to w1 × 24−1 + w2 × 24−2
+ w3 × 24−3 + w4 × 24−4 = W. The initial value of each bit in the front three bits is
zero. The initial value of the least significant bit w4 is one. This is to say that W =
w01 × 24−1 + w02 × 24−2 + w03 × 24−3 + w14 × 24−4 = 1.
Computing the order r of 2 modulo 15 is equivalent to calculate the period r of a
given oracular function Of : {p1 p2 p3 p4 | ∀ pd ∈ {0, 1} for 1 ≤ d ≤ 4} → {2 p1 p2 p3 p4
(mod 15) | ∀ pd ∈ {0, 1} for 1 ≤ d ≤ 4}. The period r of Of is to satisfy Of (p1 p2
p3 p4 ) = Of (p1 p2 p3 p4 + r) to any two inputs (p1 p2 p3 p4 ) and (p1 p2 p3 p4 + r).
The first stage of computing Of (p1 p2 p3 p4 ) = 2 p1 p2 p3 p4 (mod 15) is to use modular
multiplication to compute 22 (mod 15), by squaring 2 (mod 15). We get the following
result
22 (mod 15) = (2(mod 15))2 = (2(mod 15)) ×15 (2(mod 15)) = 4(mod 15) = 4.
(5.42)
Next, it computes 24 (mod 15) by squaring 22 (mod 15) and we get the following
result
2
24 (mod 15) = 22 (mod 15) = 22 (mod 15)
Then, it computes 28 (mod 15) by squaring 24 (mod 15) and we get the following
result
2
28 (mod 15) = 24 (mod 15) = 24 (mod 15)
3 2 1 0
3
2 P (mod 15) = 2 p1 ×2 + p2 ×2 + p3 ×2 + p4 ×2 (mod 15) = 2 p1 ×2 (mod 15) ×15
0
2
1
2 p2 ×2 (mod 15) ×15 2 p3 ×2 (mod 15) ×15 2 p4 ×2 (mod 15) . (5.45)
5.14 Quantum Circuits of Factoring 15 257
1 3
If the value of bit p1 is equal to one (1), then (2 p1 ×2 (mod 15)) = (28 (mod 15))
0 3 3
= 1. Otherwise, (2 p1 ×2 (mod 15)) = (20 (mod 15)) = 1. This is to say that (2 p1 ×2
p21 ×22
(mod 15)) = 1. Next, if the value of bit p2 is equal to one (1), then (2 (mod
0 2
15)) = (24 (mod 15)) = 1. Otherwise, (2 p2 ×2 (mod 15)) = (20 (mod 15)) = 1. This
2
indicates that (2 p2 ×2 (mod 15)) = 1. Therefore, we can rewrite the equation in (5.45)
as follows
1
0
2 P (mod 15) = 2 p3 ×2 (mod 15) ×15 2 p4 ×2 (mod 15) . (5.46)
According to the equation in (5.46), sixteen outputs of Of that takes each input
from p10 p20 p30 p40 through p11 p21 p31 p41 are subsequently 1, 2, 4, 8, 1, 2, 4, 8, 1, 2, 4,
8, 1, 2, 4 and 8. The binary values to one (1), two (2), four (4) and eight (8) are
subsequently w10 w20 w30 w41 , w10 w20 w31 w40 , w10 w21 w30 w40 and w11 w20 w30 w40 . The frequency f
of Of is equal to the number of the period per sixteen outputs. This gives that r × f
= 16. Hidden patterns and information stored in a given oracular function Of are to
that its output rotates back to its starting value (1) four times. This implies that the
number of the period per sixteen outputs is four and the frequency f of Of is equal to
four. The period r of Of is the reciprocal of the frequency f of Of . Thus, we obtain
r = 1f = 16f = 16 4
= 4 and r × f = 4 × 4 = 16. Therefore, the order r of (X = 2)
16
modulo (N = 15) is equal to four.
computing 2P requires completing left bit shift of P times on any binary auxiliary
variable.
For example, in our example we use bits p3 and p4 as the controlled bits. If the
value of the controlled bit p4 is equal to one (1), then we use left bit shift of one time
1 0
on the binary auxiliary variable w10 w20 w30 w41 to implement the instruction “(2 p4 ×2
1
(mod 15)) = (2 (mod 15)) = 2”. Left bit shift of one time is to that it exchanges
each bit wk for 1 ≤ k ≤ 4 with the next highest weighted position. On the execution
of the first time, it exchanges bit w10 with bit w20 and the result is w20 w10 w30 w41 . Next,
on the execution of the second time, it exchanges bit w10 with bit w30 and the result is
w20 w10 w30 w41 . Next, on the execution of the third time, it exchanges bit w10 with bit w41
and the result is w20 w30 w41 w10 .
The corresponding decimal value of w20 w30 w41 w10 is two (2). This means that it
1 0
implement the instruction “(2 p4 ×2 (mod 15)) = (21 (mod 15)) = 2”. We can use
three CSWAP gates to implement them. Similarly, if the value of the controlled bit p3
is equal to one (1), then we complete a shift by two bits to implement the instruction
1 1
“(2 p3 ×2 (mod 15)) = (22 (mod 15)) = 4”. A shift by two bits is to that it exchanges
the bit at the weighted position (23 ) with another bit at the weighted position (21 ) and
exchanges at the weighted position (22 ) with another bit at the weighted position (20 ).
We can make use of two CSWAP gates to implement them. This reduction makes us
not to implement multiplication circuits and modular circuits.
We use the quantum circuit in Fig. 5.13 to find the order r of (X = 2) modulo (N
= 15). The first (upper) quantum register has four quantum bits. Bit |p1 is the most
significant bit and bit |p4 is the least significant bit. The corresponding decimal value
is equal to p1 × 24−1 + p2 × 24−2 + p3 × 24−3 + p4 × 24−4 = P. The initial state of
each quantum bit |pd for 1 ≤ d ≤ 4 is set to state |0 . The second (lower) quantum
register has four quantum bits. Bit |w1 is the most significant bit and bit |w4 is the
least significant bit. The corresponding decimal value is equal to w1 × 24−1 + w2 ×
24−2 + w3 × 24−3 + w4 × 24−4 = W. The initial state of the front three quantum bits
|wd for 1 ≤ d ≤ 3 is set to state |0 . The initial state of the least significant quantum
bits |wd for 1 ≤ d ≤ 3 is set to state |1 .
In Listing 5.2, the program is in the backend that is simulator of Open QASM
with thirty-two quantum bits in IBM’s quantum computer. The program is to find the
order r of (X = 2) modulo (N = 15). Figure 5.14 is the corresponding quantum circuit
of the program in Listing 5.2 and is to implement the quantum circuit of finding the
order r of (X = 2) modulo (N = 15) in Fig. 5.13.
The statement “OPENQASM 2.0;” on line one of Listing 5.2 is to indicate that the
program is written with version 2.0 of Open QASM. Then, the statement “include
“qelib1.inc”;” on line two of Listing 5.2 is to continue parsing the file “qelib1.inc” as
5.14 Quantum Circuits of Factoring 15 259
Fig. 5.14 Implementing quantum circuits of finding the order r of (X = 2) modulo (N = 15) in
Fig. 5.13
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current working directory.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[8];
4. creg c[4];
5. x q[7];
Next, the statement “qreg q[8];” on line three of Listing 5.2 is to declare that in
the program there are eight quantum bits. In the left top of Fig. 5.14, eight quantum
260 5 Order-Finding and Factoring
bits are subsequently q[0], q[1], q[2], q[3], q[4], q[5], q[6] and q[7]. The initial value
of each quantum bit is set to state |0 . We use four quantum bits q[0], q[1], q[2] and
q[3] to respectively encode four quantum bits |p1 , |p2 , |p3 and |p4 in Fig. 5.13.
We apply four quantum bits q[4], q[5], q[6] and q[7] to respectively encode four
quantum bits |w1 , |w2 , |w3 and |w4 in Fig. 5.13.
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 7 is to represent the
value 0 of q[k] and q[k]1 for 0 ≤ k ≤ 7 is to represent the value 1 of q[k]. Next,
the statement “creg c[4];” on line four of Listing 5.2 is to declare that there are four
classical bits in the program. In the left bottom of Fig. 5.14, four classical bits are
subsequently c[0], c[1], c[2] and c[3]. The initial value of each classical bit is set to
zero (0). For the convenience of our explanation, c[k]0 for 0 ≤ k ≤ 3 is to represent
the value 0 of c[k] and c[k]1 for 0 ≤ k ≤ 3 is to represent the value 1 of c[k]. The
corresponding decimal value of the four initial classical bits c[3]0 c[2]0 c[1]0 c[0]0
is 23 × c[3]0 + 22 × c[2]0 + 21 × c[1]0 + 20 × c[0]0 . This indicates that classical
bit c[3]0 is the most significant bit and classical bit c[0]0 is the least significant bit.
Next, the statement “x q[7];” on line five of Listing 5.2 is to convert the state |0 of
quantum bit |q[7] into the state |1 . For the convenience of our explanation, an initial
state vector of finding the order r of (X = 2) modulo (N = 15) is
|0 = q[0]0 q[1]0 q[2]0 q[3]0 q[4]0 q[5]0 q[6]0 q[7]1 .
(5.47)
In the initial state vector |0 in (5.47), quantum bits |q[0]0 |q[1]0 |q[2]0 |q[3]0
encode four quantum bits | p10 , | p20 , | p30 and | p40 of the first register in Fig. 5.13
and are the precision register. We use the precision register to represent the values of
P that we shall pass to modular exponentiation 2P (mod 15). We shall make use of
quantum superposition to evaluate modular exponentiation 2P (mod 15) for multiple
values of P in parallel, so we use four statements “h q[0];”, “h q[1];”, “h q[2];” and
“h q[3];” from line six through line nine in Listing 5.2 to place the precision register
into
a superposition of all possible values. Therefore, we have the following new state
vector
1 1 1
|1 = √ q[0]0 + q[0]1 √ q[1]0 + q[1]1 √ q[2]0 + q[2]1
2 2 2
1
√ q[3]0 + q[3]1 q[4]0 q[5]0 q[6]0 q[7]1
2
⎛4 ⎞
2 −1
1 ⎝
|P ⎠ q[4]0 q[5]0 q[6]0 q[7]1 .
=√ (5.48)
2 4
p=0
This way makes each state (|P |q[4]0 |q[5]0 |q[6]0 |q[7]1 ) in the new state
vector (|1 ) in (5.48) to be ready to be treated as a separate input to a parallel
computation.
In the initial state vector |0 in (5.47), quantum bits |q[4]0 |q[5]0 |q[6]0 |q[7]1
encode four quantum bits |w01 , |w02 , |w03 and |w14 of the second register in Fig. 5.13
and are the auxiliary register. We now would like to complete modular exponentiation
1 0
2P (mod 15) = (2 p3 ×2 (mod 15)) ×15 (2 p4 ×2 (mod 15)) in (5.46) on the superposition
of inputs we have within the precision register, and we shall apply the auxiliary
register to hold and to store the results. We use three CSWAP gates to implement
0
the instruction (2 p4 ×2 (mod 15)) that is conditional multiply-by-2. Three statements
“ccx q[3], q[4], q[5];”, “ccx q[3], q[5], q[4];” and “ccx q[3], q[4], q[5];” from line
ten through line twelve in Listing 5.2 are three CCNOT gates to implement the first
CSWAP gate in Fig. 5.13. In the first CSWAP gate, quantum bit q[3] is its controlled
bit and quantum bits q[4] and q[5] are its target bits. In the CCNOT instruction, the
first operand and the second operand are its two controlled bits and the third operand
is its target bit.
The three CCNOT gates exchange the quantum bit at the weighted position (23 )
of the work register with the quantum bit at the weighted position (22 ) of the work
register. This means that the new state vector is
1
|2 = √ q[0]0 + q[0]1 q[1]0 + q[1]1 q[2]0 + q[2]1 q[3]0 q[4]0
4
2 0
q[5] q[6]0 q[7]1 + q[3]1 q[5]0 q[4]0 q[6]0 q[7]1 .
(5.49)
Next, in the second CSWAP gate in Fig. 5.13, quantum bit q[3] is its controlled bit
and quantum bits q[5] and q[6] are its target bits. Three CCNOT gates “ccx q[3], q[5],
q[6];”, “ccx q[3], q[6], q[5];” and “ccx q[3], q[5], q[6];” from line thirteen through
line fifteen in Listing 5.2 are to implement the second CSWAP gate in Fig. 5.13. This
indicates that the new state vector is
Then, in the third CSWAP gate in Fig. 5.13, quantum bit q[3] is its controlled bit
and quantum bits q[6] and q[7] are its target bits. Three CCNOT gates “ccx q[3], q[6],
q[7];”, “ccx q[3], q[7], q[6];” and “ccx q[3], q[6], q[7];” from line sixteen through
line eighteen in Listing 5.2 are to implement the third CSWAP gate in Fig. 5.13. This
implies that the new state vector is
1
|4 = √ q[0]0 + q[0]1 q[1]0 + q[1]1 q[2]0 + q[2]1 q[3]0 q[4]0
4
20
q[5] q[6]0 q[7]1 + q[3]1 q[5]0 q[6]0 q[7]1 q[4]0 .
(5.51)
In the new state vector |4 in (5.51), if the least significant quantum bit q[3] in
the precision register is the |1 state, then the value of its work register is two (2).
Otherwise, the value of its work register is not changed and is still one (1).
5.14 Quantum Circuits of Factoring 15 263
Because the work register in the new state vector |4 in (5.51) holds and stores
0
the result of computing (2 p4 ×2 (mod 15)), next we want to complete the instruction
1
(2 p3 ×2 (mod 15)). If the value of quantum bit |q[2] that encodes quantum bit |p3 in
1
the precision register is one (1), then that indicates to complete the instruction (2 p3 ×2
(mod 15)) to require another two multiplications by two (2) on the work register.
1
We use two CSWAP gates to implement the instruction (2 p3 ×2 (mod 15)) that is
conditional multiply-by-4. In Fig. 5.13, the fourth CSWAP gate is to exchange the
quantum bit at the weighted position (23 ) of the work register with another quantum
bit at the weighted position (21 ) of the work register if the value of the quantum bit
at the weighted position (21 ) of the precision register is state |1 . Next, in Fig. 5.13,
the fifth CSWAP gate is to exchange the quantum bit at the weighted position (22 ) of
the work register with another quantum bit at the weighted position (20 ) of the work
register if the value of the quantum bit at the weighted position (21 ) of the precision
register is state |1 .
Three statements “ccx q[2], q[4], q[6];”, “ccx q[2], q[6], q[4];” and “ccx q[2], q[4],
q[6];” from line nineteen through line twenty-one in Listing 5.2 are three CCNOT
gates to implement the fourth CSWAP gate in Fig. 5.13. In the fourth CSWAP gate,
quantum bit q[2] is its controlled bit and quantum bits q[4] and q[6] are its target
bits. In the CCNOT instruction, the first operand and the second operand are its two
controlled bits and the third operand is its target bit.
The three CCNOT gates exchange the quantum bit at the weighted position (23 )
of the work register with the quantum bit at the weighted position (21 ) of the work
register. This means that the new state vector is
Next, in the fifth CSWAP gate in Fig. 5.13, quantum bit q[2] is its controlled
bit and quantum bits q[5] and q[7] are its target bits. Three CCNOT gates “ccx
q[2],q[5],q[7];”, “ccx q[2],q[7],q[5];” and “ccx q[2],q[5],q[7];” from line twenty-
two through line twenty-four in Listing 5.2 are to implement the fifth CSWAP gate
in Fig. 5.13. This means that the new state vector is
In the new state vector |6 in (5.53), it shows how we have now managed to
1 0
compute 2P (mod 15) = (2 p3 ×2 (mod 15)) ×15 (2 p4 ×2 (mod 15)) in (5.46) on every
value of P from the precision register in superposition.
34. h q[3];
35. swap q[0],q[3];
36. swap q[1],q[2];
1 1
q[0]1 q[1]0 q[2]0 q[3]0 + √ q[0]1 q[1]1 q[2]0 q[3]0
+√
2 2 2 2
q[4]0 q[5]0 q[6]0 q[7]1 + q[5]0 q[6]0 q[7]1 q[4]0 + q[6]0 q[7]1
q[4]0 q[5]0 + q[7]1 q[4]0 q[5]0 q[6]0 .
(5.54)
Finally, four statements “measure q[0] -> c[3];”, “measure q[1] -> c[2];”, “measure
q[2] -> c[1];” and “measure q[3] -> c[0];” from line thirty-seven through line forty in
Listing 5.2 implement the measurement on the precision register in Fig. 5.13. They
measure four quantum bits q[0], q[1], q[2] and q[3] of the precision register. They
record the measurement outcome by overwriting four classical bits c[3], c[2], c[1]
and c[0].
Fig. 5.15 A computational basis state 0000 has the probability 24.512% (0.24512), a computational
basis state 0100 has the probability 23.145% (0.23145), a computational basis state 1000 has the
probability 27.148% (0.27148) and a computational basis state 1100 has the probability 25.195%
(0.25195)
16 0
= 4 + = 4. (5.55)
4 4
Since the value of r is equal to 0, from the first execution of statement S 3 , it returns
to a true. Thus, next, from the first execution of statement S 4 , the answer is to the
continued fractional representation of (16 /4)
16
= (q[1] = 4) = 4. (5.56)
4
Next, from the first execution of Statement S 5 , it terminates the execution of the
continued fractional algorithm. For a rational
number (16 /4), the first convergent is
(q[1]) = 4 = 41 and is the closest one to 16
4
with numerator less than 15. Therefore,
we check 24 (mod 15) which equals one (1) and we find the order r to be four (4).
Because the order r is even, from Lemma 5.2, we use Euclid’s algorithm to compute
5.14 Quantum Circuits of Factoring 15 267
4 4
gcd(15, 2 2 + 1) and gcd(15, 2 2 − 1). This implies that two nontrivial factors for N
= 15 are respectively 5 and 3. Therefore, the prime factorization for N = 15 is N =
5 × 3.
In Fig. 5.11, the precision register (the first register or the upper register) with n
quantum bits represents the order r of X modulo N. Because the value of r is less
than or equal to the value of N, the value of n is the smallest integer greater than
or equal to logN2 and we can write it as n = log2N . In Fig. 5.11, the work register
(the second register or the lower register) with L quantum bits is to store the result
of computing X P (mod N) for 0 ≤ p ≤ 2n − 1. Because the result of computing X P
(mod N) for 0 ≤ p ≤ 2n − 1 is less than the value of N, the value of L isthe smallest
integer greater than or equal to logN2 and we can write it as L = log2N . The value
of X is less than the value of N, so we can use L bits to represent the value of X and
the value of N.
In Fig. 5.11, in parallel model, it simultaneously computes each modular expo-
nentiation X P (mod N) to 0 ≤ P ≤ 2n − 1. In Fig. 5.11, it applies a quantum gate |X P
n−1 n−2 n−n
mod N = | X p1 ×2 + p2 ×2 +···+ pn ×2 mod N > operating on the precision register
and the work register. The method to calculate the modular exponentiation X P (mod
N) contains two stages. The first stage uses modular multiplication to compute X 2
(mod N), by squaring X (mod N). Then, it figures out X 4 (mod N) by squaring X 2
(mod N). Then, it calculates X 8 (mod N) by squaring X 4 (mod N). In this way, it
2k
N out X (mod
continues to figure N) for all k up to (n − 1).
Since n = log2 and L = log2N , we apply n = O(L). Therefore, in the first stage
it completes a total of (n − 1) = O(L) squaring operations. A squaring operation
consists of a multiplication instruction and a modular instruction. Multiplicand and
multiplier in the multiplication instruction are both X and they are L bits long. Product
in the multiplication instruction is (2 × L) bits long and it is dividend in the modular
instruction. Divisor in the modular instruction is N and it is n = O(L) bits long. The
number of the auxiliary carry bit in the multiplication instruction is (2 × L + 1) bits
and the number of the auxiliary borrow bit in the modular instruction is (2 × L + 1)
bits.
Because the cost of the circuit to implement one multiplication instruction is O(L 2 )
digital logic gates and the cost of the circuit to implement one modular instruction is
O(L 2 ) digital logic gates, the cost of the quantum circuit to implement one squaring
operation is O(L 2 ) quantum gates. Therefore, the cost to complete (n − 1) = O(L)
squaring operations is O(L 3 ) quantum gates and a total cost of implementing the first
stage is O(L 3 ) quantum gates.
n−1
Next, the second stage of the method is to complete X P (mod N) = (X p1 ×2
p2 ×2n−2 pn ×2n−n
(mod N)) ×N (X (mod N)) ×N ··· ×N (X (mod N)). It implements (n
268 5 Order-Finding and Factoring
5.16 Summary
In this chapter for more details about an introduction of fundamental and advanced
knowledge of number theory, the recommended books are (Hardy and Wright 1979;
Nielsen and Chuang 2000; Imre and Balazs 2005; Lipton and Regan 2014; Silva
2018; Johnston et al. 2019). For a more detailed description to Shor’s order-finding
algorithm, the recommended article and books are (Shor 1994; Nielsen and Chuang
2000; Imre and Balazs 2005; Lipton and Regan 2014; Silva 2018; Johnston et al.
2019). A good introduction to the instructions of Open QASM is the famous article
in (Cross et al. 2017).
5.18 Exercises
5.1 Let c and d be integer, and let r be the remainder when c is divided by d. Then
provided r = 0, please prove the equation gcd(c, d) = gcd(d, r).
5.2 (Chinese remainder theorem) we assume that y1 , …, yn are positive integers
such that any pair yi and yj (i = j) are co-prime. Then the system of equations
z = c1 (mod y1 )
z = c2 (mod y2 )
···
z = cn (mod yn )
has a solution. Moreover, any two solutions to this system of equations are equal
modulo Y =y1 y2 … yn . Please prove Chinese remainder theorem.
5.3 We assume that p andk are integers and p is a prime in the range 1 to p − 1.
p
Then prime p divides . Please prove it.
k
5.4 (Fermat’s little theorem) we assume that p is a prime, and a is an integer. Then
ap = a (mod p). If integer a is not divisible by prime p then ap−1 = 1 (mod p).
Please prove them.
5.5 The Euler function ϕ(n) is defined to be the number of positive integers which
are less than n and are co-prime to n. We assume that a is co-prime to n. Then
aϕ (n) = 1 (mod n).
5.6 If (i /2n ) is a rational fraction and z and r are positive integers that satisfy |(z
/r) − (i /2n )| ≤ (1 /(2 × r 2 )), then (z /r) is a convergent of the continued fraction
of (i /2n ).
√ 2
5.7 Prove that 1 − e −1×θ = 4 × sin2 (θ /2).
270 5 Order-Finding and Factoring
References
Cross, A.W., Bishop, L.S., Smolin, J.A., Gambetta, J.M.: Open quantum assembly language. 2017.
https://arxiv.org/abs/1707.03429
Hardy, G.H., Wright, E.M.: An Introduction to the Theory of Numbers, 5th edn. Oxford University
Press, New York (1979). ISBN: 0198531702
Imre, S., Balazs, F.: Quantum Computation and Communications: An Engineering Approach. Wiley,
UK (2007). ISBN-10: 047086902X and ISBN-13: 978-0470869024, 2005
Johnston, E.R., Harrigan, N., Gimeno-Segovia, M.: Programming Quantum Computers: essen-
tial algorithms and code samples. O’Reilly Media, Inc., ISBN-13: 978-1492039686, ISBN-10:
1492039683, 2019
Lipton, R.J., Regan, K.W.: Quantum Algorithms via Linear Algebra: A Primer. The MIT Press
(2014). ISBN 978-0-262-02839-4
Manders, K.L., Adleman, L.M.: NP-complete decision problems for binary quadratics. J. Comput.
Syst. Sci. 16(2), 168–184 (1978)
Nielsen, M.A., Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge Univer-
sity Press, New York, NY, 2000, ISBN-10: 9781107002173 and ISBN-13: 978-1107002173
Shor, P.: Algorithms for quantum computation: discrete logarithms and factoring. In: Proceedings
35th Annual Symposium on Foundations of Computer Science, Santa Fe, pp. 124–134, November
20-22 1994
Silva, V.: Practical Quantum Computing for Developers: Programming Quantum Rigs in the Cloud
using Python, Quantum Assembly Language and IBM Q Experience. Apress, December 13,
2018, ISBN-10: 1484242173 and ISBN-13: 978-1484242179
Chapter 6
Phase Estimation and Its Applications
A decision problem is a problem in which it has only two possible outputs (yes or
no) on any input of n bits. An output “yes” in the decision problem is to the number
of solutions not to be zero and another output “no” in the decision problem is to
the number of solutions to be zero. An example of a decision problem is deciding
whether a given Boolean formula, F(x 1 , x 2 ) = x 1 ∧ x 2 , has solutions that satisfy
F(x 1 , x 2 ) to have a true value or not, where the value of two Boolean variables x 1 and
x 2 is either true (1) or false (0) and “∧” is the AND operation of two operands. For
the convenience of the presentation, Boolean variable x 1 0 is to represent the value
0 (zero) of Boolean variable x 1 and Boolean variable x 1 1 is to represent the value 1
(one) of Boolean variable x 1 . Boolean variable x 2 0 is to represent the value 0 (zero)
of Boolean variable x 2 and Boolean variable x 2 1 is to represent the value 1 (one) of
Boolean variable x 2 .
A decision procedure is in the form of an algorithm to solve a decision problem.
A decision procedure for the decision problem “a given Boolean formula, F(x 1 , x 2 )
= x 1 ∧ x 2 , does it have solutions that satisfy F(x 1 , x 2 ) to have a true value?” would
implement x 1 ∧ x 2 (the AND operation of two operands) of four times according to
four different inputs x 1 0 x 2 0 , x 1 0 x 2 1 , x 1 1 x 2 0 and x 1 1 x 2 1 . After it executes each AND
operation, it finds the fourth input x 1 1 x 2 1 that satisfies F(x 1 , x 2 ) to have a true value.
Finally, it gives an output “yes” to the decision problem. This implies that the number
of solutions is not equal to zero. If time complexity of a decision procedure to solve
a decision problem with the input of n bits is O(2n ), then the decision problem is a
NP-Complete problem.
We assume that a (2√n × 2n ) unitary matrix (operator) U has √
a (2n × 1) eigenvector
|u with eigenvalue e −1×2×π×θ such that U × |u = e −1×2×π×θ × |u, where
the value of θ is unknown and is real. The purpose of the phase estimate algorithm
is to estimate the value of θ. Deciding whether there exist solutions for a problem
with the input of n bits is equivalent to estimate the value of θ. In this chapter, we
first describe how the phase estimate algorithm works on quantum computers and
various kinds of real applications. We illustrate how to write quantum programs to
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 271
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1_6
272 6 Phase Estimation and Its Applications
compute and estimate the value of θ to that any given a (2n × 2n ) unitary matrix
√
(operator) U has a (2n × 1) eigenvector |u with eigenvalue e −1×2×π×θ . Next,
we explain the reason of why deciding whether there exist solutions for a problem
with the input of n bits is equivalent to estimate the value of θ. We also explain how
the quantum-counting algorithm determines the number of solutions for a decision
problem with the input of n bits. Next, we introduce how to write quantum algorithms
to implement the quantum-counting algorithm that is a real application of the phase
estimate algorithm for computing the number of solutions for various kinds of real
applications with the input of n bits.
We use the quantum circuit shown in Fig. 6.1 to implement the phase estimation
algorithm.
1 0 It uses two quantum registers. At the left top in Fig. 6.1, the first 0register
⊗k=t yk contains t quantum bits initially in the state |0. Quantum bit |yt is the
most significant bit. Quantum bit |y1 0 is the least significant bit. The corresponding
decimal value of the first register is (|yt 0 × 2t −1 ) + · · · + (|y2 0 × 22−1 ) + (|y1 0 ×
21−1 ). How we select t that is dependent on two things. The first thing is to that the
number of bits of accuracy we wish to have in our estimation for the value of θ. The
second thing is to that with what probability we wish the phase estimation algorithm
bits initially in the state |0. Quantum bit |u1 0 is the most significant bit. Quantum
bit |un 0 is the least significant bit. The corresponding decimal value of the second
register is (|u1 0 × 2n−1 ) + (|u2 0 × 2n−2 ) + · · · + (|yn 0 × 2n−n ). How we select n
that is dependent on a thing. The thing is to the size of the input for various kinds of
real applications. This means that we select n that actually is to the number of bits
of input for a problem. For the convenience of the presentation, the following initial
state vector is
|ϕ0 = ⊗1k=t yk0 ⊗ ⊗nj=1 u 0j .
(6.1)
1 1
|ϕ1 = √ ⊗1k=t yk0 + yk1 ⊗ √ ⊗nj=1 u 0j + u 1j
2t 2n
1
√ ⊗1k=t yk0 + yk1
= ⊗ (|u). (6.2)
2t
1 1 √ 0
|ϕ2 = √ ⊗2k=t yk0 + y
k ⊗ y10 |u + e −1×2×π×θ×2 y11 |u
2t
1 1 √ 0
= √ ⊗2k=t yk0 + y
k ⊗ y10 + e −1×2×π×θ×2 y11 ⊗ ( |u ).
2t
(6.4)
√ 0
Altering the phase of the state |y1 1 is from one (1) to become e −1×2×π×θ×2 .
We call it as phase kickback.
Next, in the new state vector |ϕ 2 in (6.4), a superposition √12 y20 + y21 at
the weighted position 21 is the controlled quantum bit of implementing controlled-
1
U 2 operations on the superposition of the second register that is the state (|u). This
means that the following new state vector is
1 √ 1
|ϕ3 = √ ⊗3k=t yk0 + yk1 ⊗ y20 + e −1×2×π×θ×2 y21
2t
√ 0
⊗ y10 + e −1×2×π×θ×2 y11 ⊗ (|u). (6.5)
6.1 Phase Estimation 275
1
√Because of phase
1
kickback, the phase of the state |y2 is from one (1) to become
e −1×2×π×θ×2 .
Next, in the new state vector |ϕ 3 in (6.5), a superposition √12 y30 + y31 at
2 1 0 1
the weighted position 2 through a superposition √2 yt + yt at the weighted
2
position 2t −1 are the controlled quantum bits of implementing controlled-U 2 opera-
t−1
tions through controlled-U 2 operations on the superposition of the second register
that is the state (|u). This gives that the following new state vector is
1 0 √ t−1
√
t−2 1
yt + e −1×2×π ×θ ×2 yt1 ⊗ yt−1
0
|ϕ4 = √ + e −1×2×π ×θ ×2 yt−1
2 t
√
1
√
0
⊗ · · · ⊗ y20 + e −1×2×π ×θ ×2 y21 ⊗ y10 + e −1×2×π ×θ ×2 y11 ⊗ (|u)
⎛ ⎛t ⎞⎞
2 −1 √
1
= ⎝√ ⎝ e −1×2×π ×θ ×Y |Y ⎠⎠ ⊗ (|u). (6.6)
2t Y =0
t
Because of phase kickback,
√ the phase of the state |Y for 0 ≤ Y ≤ 2 − 1 is
−1×2×π×θ×Y
from one (1) to become e . From this description above, the second
quantum register stays in the state (|u) through the computation.
Next, in Fig. 6.1, the circuit implements the inverse Quantum Fourier transform
on the superposition of the first register. It takes the new state vector (|ϕ 4 ) in (6.6) as
its input state vector. The output state of the inverse Quantum Fourier transform
on the superposition of the first register is
2t −1 2t −1
1 √ 1 √
− −1×2×π× 2it ×Y
|ϕ5 = √ e −1×2×π×θ×Y √ e |i ⊗ (|u)
2 t 2t
Y =0 i=0
2t −1 2t −1
1 √
−1×2×π×Y ×(θ− 2it ) |i
= e ⊗ (|u)
2t Y =0 i=0
2t −1 2t −1
1 √−1×2×π×(θ− it ) Y
= e 2 |i ⊗ (|u). (6.7)
i=0 Y =0
2t
276 6 Phase Estimation and Its Applications
From this description above, the second quantum register still stays in the state (|u)
through the computation. From the new state vector (|ϕ 5 ) in (6.7), the probability
amplitude of |i is
2t −1
1 √ i
Y
φi = t × e −1×2×π×(θ− 2t ) . (6.8)
2 Y =0
The probability
√amplitude of i|iis simply the sum of a geometrical sequence with
quotient q = e −1×2×π×(θ− 2t ) . On one hand if the value of θ may be expressed in
t bits in the first quantum register, as θ = 0.yt yt −1 … y2 y1 = (yt yt −1 … y2 y1 /2t ). Then
the value of θ actually is equal to (i/2t ) for 0 √≤ i ≤ 2t − 1 and is an√integer multiple
of (1/2t ). This gives that the quotient q is e −1×2×π× ( 2it − 2it ) = e −1×2×π×0 = 1,
t
t
2 −1 Y 2 −1
the probability amplitude of |i is 21t × 1
= 21t × 2t =
Y =0 1 = 2t
× Y =0 1
1 and any other probability amplitudes disappear. This is the ideal case of phase
estimation. Finally, in Fig. 6.1, after a measurement on the output state of the inverse
quantum Fourier transform to the superposition of the first register is completed,
we obtain the computational basis state |iwith the successful probability 1 (100%).
t
This indicates that the value of θ is equal to (i/2 √) with the i successful
probability
1 (100%). Therefore, we obtain the eigenvalue e −1×2×π× 2t with the successful
probability 1 (100%).
On the other hand if the value of θ may not be expressed in t bits in the first quantum
register. This is√
to say that θ = 0.yt yt −1 … y2 y1 = (yt yt −1 … y2 y1 /2t ). Then the
−1×2×π×(θ− 2it )
quotient q is e = 1 and we can rewrite the probability amplitude of
|i in (6.8) as follows
√ i
2t
1 1−q 2t
1 1 − e −1×2×π×(θ− 2t )
φi = × = t × √ i
1 − e −1×2×π×(θ− 2t )
2 t 1−q 2
√
1 − e −1×2×π×(2 ×θ−i )
t
1
= t × √ i
. (6.9)
2 1 − e −1×2×π×(θ− 2t )
6.1 Phase Estimation 277
This gives another good explanation of uncertainty and thus appearing inaccuracy
when measuring the output of the inverse quantum Fourier transform in Fig. 6.1.
The probability of measuring a suitable state |i on the first register in Fig. 6.1 is
√ 2
−1×2×π×(2t ×θ−i )
− e
1 1
|φi |2 = 2×t √ . (6.10)
2 i 2
1 − e −1×2×π×(θ− 2t )
√ 2
Because 1 − e −1×γ = 4 sin2 (γ /2), we can rewrite |φ i |2 in (6.10) as follows
2×π×(2t ×θ−i )
1 4 × sin2 2
|φi |2 = × 2×π× θ− i
22×t ( 2t )
4 × sin2 2
2 2×π×(2 ×θ−i )
t
1 sin 2
= 2×t × 2×π× θ− i . (6.11)
2 ( 2t )
sin2 2
This is the practical case of phase estimation. Finally, in Fig. 6.1, after a measure-
ment on the output state of the inverse quantum Fourier transform to the superpo-
sition of the first register
⎛ is completed, we obtain
⎞ the computational basis state |i
2×π×(2t ×θ −i )
⎜ 1 sin2 2
with the probability ⎝ 22×t × ⎠. Because (i/2t ) = (yt yt −1 … y2 y1 /2t )
⎟
(
2×π× θ − it )
sin2 2
2
yt −1 … y2 y1 , (i/2t ) is an
= 0.yt ⎛ ⎞ estimated value to the value of θ with the prob-
2×π×(2t ×θ −i )
⎜ 1 sin2 2
ability ⎝ 22×t × ⎠. Hence, we only obtain an estimated eigenvalue
⎟
(
2×π× θ − it )
2 2
sin 2
⎛ ⎞
2×π×(2t ×θ −i )
√ i
sin2
⎜ 1
e −1×2×π× 2t with the probability ⎝ 22×t
2
× ⎠.
⎟
sin 2 (
2×π× θ − it
2 )
2
This is to say that if more than one |φ i |2 differs from zero then there is a
nonzero probability of receiving different estimated phases (eigenvalues) after the
measurement when repeating to execute the circuit of phase estimation in Fig. 6.1.
1
P(|i − Y | > ε) ≤ . (6.12)
2 × (ε − 1)
1
P(|i − Y | ≤ ε) = 1 − P(|i − Y | > ε) = 1 −
2 × (ε − 1)
1 1
=1− =1− . (6.13)
2 × (2t−n − 1 − 1) 2 × (2t−n − 2)
Therefore to successfully obtain the value of the phase θ accurate to t bits with
1
probability of success at least 1 − α = 1− 2×(2t−n −2)
, we select
t = n + log2 (2 + (1/(2 × α))) . (6.14)
1
Because α = 2×(2t−n −2)
, we obtain α × (2 × (2t −n − 2)) = 1. This is to say that
2 − 2 = (1/(2 × α)) and 2t −n = (1/(2 × α)) + 2 and log2 (2t −n ) = log2 (2 + (1/(2
t −n
We use the circuit in Fig. 6.2 to compute eigenvalue of a (22 × 22 ) unitary matrix U
with a (22 × 1) eigenvector |u. It makes use of two quantum registers. At the left
Fig. 6.2 Quantum circuit for calculating eigenvalue of a (22 × 22 ) unitary matrix U with a (22 ×
1) eigenvector |u
280 6 Phase Estimation and Its Applications
top in Fig. 6.2, the first register ⊗1k=4 yk0 contains four quantum bits initially in
the state |0. Quantum bit |y4 0 is the most significant bit. Quantum bit |y1 0 is the
least significant bit. The corresponding decimal value of the first register is (|y4 0 ×
24−1 ) + (|y3 0 × 23−1 ) + (|y2 0 × 22
−1
) + (|y1 0 × 21 −1 ). At the left bottom in
Fig. 6.2, the second register ⊗2j=1 u 0j contains two quantum bits initially in the
state |0. Quantum bit |u1 0 is the most significant bit. Quantum bit |u2 0 is the least
significant bit. The corresponding decimal value of the second register is (|u1 0 ×
22−1 ) + (|u2 0 × 22−2 ). For the convenience of the presentation, the following initial
state vector is
|ϕ0 = ⊗1k=4 yk0 ⊗ ⊗2j=1 u 0j .
(6.15)
In Listing 6.1, the program is in the backend that is simulator of Open QASM with
thirty-two quantum bits in IBM’s quantum computer. The program is to compute
eigenvalue of a (22 × 22 ) unitary matrix U with a (22 × 1) eigenvector |u in phase
estimation. Figure 6.3 is the corresponding quantum circuit of the program in Listing
6.1 and is to implement the quantum circuit of Fig. 6.2 to compute eigenvalue of a
(22 × 22 ) unitary matrix U with a (22 × 1) eigenvector |u in phase estimation.
Fig. 6.3 Implementing quantum circuits of Fig. 6.2 to compute eigenvalue of a (22 × 22 ) unitary
matrix U with a (22 × 1) eigenvector |u in phase estimation
6.2 Computing Eigenvalue of a (22 × 22 ) Unitary Matrix U with … 281
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[6];
4. creg c[4];
The statement “OPENQASM 2.0;” on line one of Listing 6.1 is to point out that
the program is written with version 2.0 of Open QASM. Next, the statement “include
“qelib1.inc”;” on line two of Listing 6.1 is to continue parsing the file “qelib1.inc” as
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current working directory.
Then, the statement “qreg q[6];” on line three of Listing 6.1 is to declare that
in the program there are six quantum bits. In the left top of Fig. 6.3, six quantum
bits are subsequently q[0], q[1], q[2], q[3], q[4] and q[5]. The initial value of each
quantum bit is set to state |0. We make use of four quantum bits q[0], q[1], q[2] and
q[3] to respectively encode four quantum bits |y4 , |y3 , |y2 and |y1 in Fig. 6.2. We
use two quantum bits q[4] and q[5] to respectively encode two quantum bits |u1 and
|u2 in Fig. 6.2. For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 5 is to
represent the value 0 of q[k] and q[k]1 for 0 ≤ k ≤ 5 is to represent the value 1 of
q[k]. Because quantum bit |y4 0 is the most significant bit and quantum bit |y1 0 is
the least significant bit, quantum bit |q[0]0 is the most significant bit and quantum
bit |q[3]0 is the least significant bit. The corresponding decimal value of the first
register in Fig. 6.3 is (|q[0]0 × 24−1 ) + (|q[1]0 × 23−1 ) + (|q[2]0 × 22−1 ) + (|q[3]0
× 21−1 ).
Next, the statement “creg c[4];” on line four of Listing 6.1 is to declare that there
are four classical bits in the program. In the left bottom of Fig. 6.3, four classical
bits are subsequently c[0], c[1], c[2] and c[3]. The initial value of each classical bit
is set to zero (0). For the convenience of our explanation, c[k]0 for 0 ≤ k ≤ 3 is to
represent the value 0 of c[k] and c[k]1 for 0 ≤ k ≤ 3 is to represent the value 1 of
c[k]. The corresponding decimal value of the four initial classical bits c[3]0 c[2]0
c[1]0 c[0]0 is 23 × c[3]0 + 22 × c[2]0 + 21 × c[1]0 + 20 × c[0]0 . This indicates
that classical bit c[3]0 is the most significant bit and classical bit c[0]0 is the least
significant bit. For the convenience of
our
explanation, we can rewrite the initial state
vector |ϕ0 = ⊗1k=4 yk0 ⊗ ⊗2j=1 u 0j in (6.15) in Fig. 6.2 as follows
|ϕ0 =q[0]0 q[1]0 q[2]0 q[3]0 q[4]0 q[5]0 .
(6.16)
282 6 Phase Estimation and Its Applications
0 1
A superposition of the first register is √1 ⊗1
k=4 yk + yk =
24
√1 ⊗3 0 1
a=0 q[a] + q[a]
. Another superposition of the second register
24
1
= √1 2 ⊗5b=4 q[b]0 + q[b]1
2 0 1
is |u = √ 2 ⊗ j=1 u j + u j . This is
2 2
to
say that the superposition
of the second register begins in the new state vector
|u = √1 2 ⊗2j=1 u 0j + u 1j = √1 2 ⊗5b=4 q[b]0 + q[b]1
and contains
2 2
two quantum bits as is necessary to store (|u). The new state vector (|u) is an
eigenstate (eigenvector) of U. Therefore, this gives that the following new state
vector is
1 1
|ϕ1 = √ ⊗1k=4 yk0 + yk1 ⊗ √ ⊗2j=1 u 0j + u 1j
2 4 2 2
1 1 0 1
= √ ⊗k=4 yk + yk ⊗ (|u)
24
1 3 1
q[a]0 + q[a]1 ⊗ √ ⊗5b=4 q[b]0 + q[b]1
= √ ⊗a=0
2 4 2 2
1 3
√ ⊗a=0 q[a]0 + q[a]1
= ⊗ (|u). (6.17)
24
6.2 Computing Eigenvalue of a (22 × 22 ) Unitary Matrix U with … 283
In the new state vector |ϕ 1 in (6.17), each quantum bit in the first register
1 is0currently
in its superposition. 3 The value of the first register is
from state ⊗k=4 yk (zero)
0
1
encoded 1
3 by 1state ⊗a=0 q[a] through state ⊗k=4 yk (fifteen) encoded by state
⊗a=0 q[a] . The circuit of Fig. 6.2 can precisely estimate sixteen phases. This is
to say that the first register with four quantum bits can precisely represent sixteen
phases. Sixteen phases are subsequently (0/24 ), (1/24 ), (2/24 ), (3/24 ), (4/24 ), (5/24 ),
(6/24 ), (7/24 ), (8/24 ), (9/24 ), (10/24 ), (11/24 ), (12/24 ), (13/24 ), (14/24 ) and (15/24 ).
The corresponding sixteen phase angles are subsequently (2 × π × 0/24 ), (2 × π ×
1/24 ), (2 × π × 2/24 ), (2 × π × 3/24 ), (2 × π × 4/24 ), (2 × π × 5/24 ), (2 × π ×
6/24 ), (2 × π × 7/24 ), (2 × π × 8/24 ), (2 × π × 9/24 ), (2 × π × 10/24 ), (2 × π ×
11/24 ), (2 × π × 12/24 ), (2 × π × 13/24 ), (2 × π × 14/24 ) and (2 × π × 15/24 ).
Say that we are trying to determine an eigenvalue of 90°. This is to say that the
effect
of one application of unitary√operator U on itseigenvector (eigenstate) (|u)
√ 4
is U × |ue −1×2×π×θ
×|u = e −1×2×π× 24 × |u . So, the effect of repeated
application of unitary operator U on its eigenvector (eigenstate) (|u) is
√ √
−1×2×π× 244 ×a
U a |u = e −1×2×π×θ×a
|u = e × |u. (6.18)
0 1
A superposition √1 y + y that is encoded by √12 q[3]0 + q[3]1 at
2 1 1
the weighted position 20 is the controlled quantum bit of implementing controlled-
0
U 2 operations on thesuperposition of the second register that
isthe state (|u). Simi-
larly, a superposition √12 y20 + y21 that is encoded by √12 q[2]0 + q[2]1 at
the weighted position 21 is the controlled quantum bit of implementing controlled-
1
U 2 operations on
thesuperposition of the second register
that is the state (|u). Next,
1 0
1 1
0
1
a superposition √2 y3 + y3
that is encoded by √2 q[1] + q[1]
at the
2
weighted position 22 is the controlled quantum bit of implementing controlled-U 2
operations on the superposition of the second register that
isthe state (|u). Next,
1
1 0 1 0
1
a superposition √
2
y4 + y4 that is encoded by √
2
q[0] + q[0] at
the weighted position 23 is the controlled quantum bit of implementing controlled-
3
U 2 operations on the superposition of the second register that is the state (|u).
The four statements from line eleven through line fourteen in Listing 6.1 are
“u1(2 * pi * 4/16 * 1) q[3];”, “u1(2 * pi * 4/16 * 2) q[2];”, “u1(2 * pi * 4/16 * 4)
q[1];” and “u1(2 * pi * 4/16 * 8) q[0];”. They take the new state vector (|ϕ 1 ) in
(6.17) as their input state vector and implement each controlled-U operation on
the superposition of the second register in the second time slot of Fig. 6.3 and
in the second stage of Fig. 6.2. They alert the phase of the state |y1 1 (|q[3]1 ) is
284 6 Phase Estimation and Its Applications
√ 4 0
√ 4
from one (1) to become e −1×2×π× 16 ×2 = e −1×2×π× 16 ×1 . They alert the
√ 4 1
phase of the state |y2 1 (|q[2]1 ) is from one (1) to become e −1×2×π× 16 ×2 =
√ 4
e −1×2×π× 16 ×2 . They alert the phase of the state |y3 1 (|q[1]1 ) is from one (1)
√ 4 2
√ 4
to become e −1×2×π× 16 ×2 = e −1×2×π× 16 ×4 and alert the phase of the state
√ 4 3
√ 4
|y4 1 (|q[0]1 ) is from one (1) to become e −1×2×π× 16 ×2 = e −1×2×π× 16 ×8 .
This gives that the following new state vector is
1 0 √ 4 3 √ 4 2
|ϕ2 = √ y4 + e −1×2×π× 16 ×2 y41 ⊗ y30 + e −1×2×π× 16 ×2 y31
24
√ 4 1 √ 4 0
⊗ y20 + e −1×2×π× 16 ×2 y21 ⊗ y10 + e −1×2×π× 16 ×2 y11 ⊗ (|u)
1 0
√ 4 √ 4
= √ y4 + e −1×2×π× 16 ×8 y41 ⊗ y30 + e −1×2×π× 16 ×4 y31
24
√ 4 √ 4
⊗ y20 + e −1×2×π× 16 ×2 y21 ⊗ y10 + e −1×2×π× 16 ×1 y11 ⊗ (|u)
1 √ 4
q[0]0 + e −1×2×π× 16 ×8 q[0]1
= √
24
√ 4
⊗ q[0]1 + e −1×2×π× 16 ×4 q[0]1
√ 4
⊗ q[2]0 + e −1×2×π× 16 ×2 q[2]1
√ 4
⊗ q[3]0 + e −1×2×π× 16 ×1 q[3]1
⊗ (|u)
⎛ ⎛4 ⎞⎞
2 −1 √
1 4
= ⎝√ ⎝ e −1×2×π× 16 ×Y |Y ⎠⎠ ⊗ (|u). (6.19)
24 Y =0
From this description above, the second quantum register stays in the state (|u)
through the computation. Because of phase √kickback, 4the phase of the state |Y for 0
≤ Y ≤ 24 − 1 is from one (1) to become e −1×2×π× 16 ×Y . In the state vector (|ϕ 2 )
in (6.19), it contains sixteen phase angles from state |0 through state |15. The front
6.2 Computing Eigenvalue of a (22 × 22 ) Unitary Matrix U with … 285
eight phase angles are (90° × 0 = 0°), (90° × 1 = 90°), (90° × 2 = 180°), (90° × 3
= 270°), (90° × 4 = 360° = 0°), (90° × 5 = 450° = 90°), (90° × 6 = 540° = 180°)
and (90° × 7 = 630° = 270°). The last eight phase angles are (90° × 8 = 720° =
0°), (90° × 9 = 810° = 90°), (90° × 10 = 900° = 180°), (90° × 11 = 990° = 270°),
(90° × 12 = 1080° = 0°), (90° × 13 = 1170° = 90°), (90° × 14 = 1260° = 180°)
and (90° × 15 = 1350° = 270°). The phase angle rotates back to its starting value
0° four times.
Hidden patterns and information stored in the state vector (|ϕ 2 ) in (6.19) are to
that its phase angle rotates back to its starting value 0° four times. This implies that
the number of the period per sixteen phase angles is four and the frequency is equal
to four (16/4). The twelve statements from line fifteen through line twenty-six in
Listing 6.1 implement each quantum operation from the third time slot through the
fourteenth time slot in Fig. 6.3. They actually implement each quantum operation
of completing an inverse quantum Fourier transform on the superposition of the
first register in Fig. 6.2. They take the state vector (|ϕ 2 ) in (6.19) as their input state
286 6 Phase Estimation and Its Applications
vector. Because the inverse quantum Fourier transform effectively transforms the
state of the first register into a superposition of the periodic signal’s component
frequencies, they produce the following state vector
⎛ ⎞
24 −1 √ 24 −1 √
1 4 1 i
|ϕ3 = ⎝ √ e −1×2×π× 24 ×Y √ e− −1×2×π× 24 ×Y |i ⎠ ⊗ (|u)
2 4 2 4
Y =0 i=0
⎛ ⎛4 4 ⎞⎞
2 −1 2 −1 √
1
4 i
−1×2×π×Y × 24 − 24
= ⎝ 4⎝ e |i ⎠⎠ ⊗ (|u)
2 Y =0 i=0
⎛4 4 ⎞
2 −1 2 −1 √ Y
1
4 i
−1×2×π× 24 − 24
=⎝ 4
e |i ⎠ ⊗ (|u). (6.20)
i=0 Y =0
2
Finally, the four statements “measure q[0] → c[3];”, “measure q[1] → c[2];”, “mea-
sure q[2] → c[1];” and “measure q[3] → c[0];” from line twenty-seven through line
thirty in Listing 6.1 implement a measurement. They measure the output state of the
inverse quantum Fourier transform to the superposition of the first register in Fig. 6.3
and in Fig. 6.2. This is to say that they measure four quantum bits q[0], q[1], q[2]
and q[3] of the first register and record the measurement outcome by overwriting
four classical bits c[3], c[2], c[1] and c[0].
Fig. 6.4 A computational basis state 0100 has the probability 100%
√ 4
unitary matrix U with a (22 × 1) eigenvector |u is equal to e −1×2×π× 24 with the
probability 100%.
A decision problem is a problem in which it has only two possible outputs (yes or
no) on any input of n bits. An output “yes” in the decision problem is to the number
of solutions not to be zero and another output “no” in the decision problem is to the
number of solutions to be zero. Solving a decision problem with any input of n bits
is equivalent to solve one interesting problem with any input of n bits that is to from
an unsorted database including 2n items with each item has n bits how many items
satisfy any given condition and we would like to find the number of solutions. If the
number of solutions is not equal to zero, then there is an output “yes” in the decision
problem with any input of n bits. Otherwise, there is an output “no” in the decision
problem with any input of n bits.
A common formulation of a decision problem with any input of n bits is as follows.
For any given oracular function Of : {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤
n} → {0, 1}, its domain is {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n} and
its range is {0, 1}. The decision problem with any input of n bits is asking to how
many elements from its domain satisfy the condition Of (u1 u2 … un−1 un ) = 1. If the
number of elements from its domain that satisfy Of (u1 u2 … un−1 un ) to have a true
value (1) is not equal to zero, then an output is “yes” to the decision problem with
any input of n bits. Otherwise, an output is “no” for the decision problem with any
input of n bits.
288 6 Phase Estimation and Its Applications
A tree is a finite set of one or more nodes such that there is a specially designated
node called the root and the remaining nodes are partitioned into v ≥ 0 disjoint sets
T 1 , …, T v , where each of these sets is a tree. T 1 , …, T v are called the subtrees of the
root. A binary tree is a finite set of nodes that is either empty or contains a root and
two disjoint binary trees called the left subtree and the right subtree.
For any given oracular function Of : {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤
n} → {0, 1}, its domain is {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n} and its
range is {0, 1}. A decision problem with any input of n bits is asking to how many
elements from its domain satisfy the condition Of (u1 u2 … un−1 un ) to have a true
value (1). We make use of a binary tree in Fig. 6.5 to represent the structure of the
domain that is {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n}. In the binary tree
in Fig. 6.5, a node stands for a bit of one element in {u1 u2 … un−1 un | ∀ uj ∈ {0,
1} for 1 ≤ j ≤ n}. The root of the binary tree in Fig. 6.5 is u1 . The value of the left
branch of each node represents that the value of the corresponding bit is equal to
zero (0) and the value of the right branch of each node stands for that the value of
the corresponding bit is equal to one (1). Since the value of the left branch of each
node is less than the value of the right branch of each node, we regard the binary tree
in Fig. 6.5 as a binary search tree.
The binary search tree in Fig. 6.5 includes 2n subtrees and each subtree encodes
one element in {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n}. For example, the first
subtree (u1 )--0 --(u2 )--0 -- … (un−1 )--0 --(un )--0 -- encodes the first element {u1 0 u2 0
Fig. 6.5 A binary search tree for representing the domain of a decision problem with any input of
n bits
6.3 Quantum Counting to a Decision Problem with Any Input of n … 289
… un−1 0 un 0 }. The second subtree (u1 )--0 --(u2 )--0 -- … (un–1 )--0 --(un )--1 -- encodes
the second element {u1 0 u2 0 … un−1 0 un 1 }. The last subtree (u1 )--1 --(u2 )--1 -- …
(un−1 )--1 --(un )--1 -- encodes the last element {u1 1 u2 1 … un−1 1 un 1 }.
Figure 6.6 is flowchart of solving a decision problem with any input of n bits. On
the execution of the first statement, S 1 , it sets the initial value of u1 u2 … un−1 un
to zero (0). Next, on the execution of the second statement, S 2 , it judges whether
Of (u1 u2 … un−1 un ) has a true value (1) or not. If it returns a true value, then on the
execution of the third statement, S 3 , it generates that an output is “yes”. Next, on the
execution of the fourth statement, S 4 , it executes one “End” instruction to terminate
Fig. 6.6 Logical flowchart of solving a decision problem with any input of n bits
290 6 Phase Estimation and Its Applications
the processing of solving a decision problem with any input of n bits. Otherwise, on
the execution the fifth statement, S 5 , it increases the value of u1 u2 … un−1 un . Next,
on the execution of the sixth statement, S 6 , it judges whether the value of u1 u2 …
un−1 un is greater than 2n or not. If it returns a true value, then on the execution of the
seventh statement, S 7 , it produces that an output is “no”. Next, on the execution of the
eighth statement, S 8 , it executes one “End” instruction to terminate the processing of
solving a decision problem with any input of n bits. Otherwise, it goes to statement
S 2 and continues to execute statement S 2 .
Binary search trees in Fig. 6.5 encode {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤
n} that is the domain of a decision
problem
with any input of n bits. We assume that
an initial state vector (|φ0 ) is ⊗nj=1 u 0j . We begin to make use of a Hadamard
transform ⊗nj=1 H on the initial state vector (|φ0 ) that is the register ⊗nj=1 u 0j .
A superposition of the register is
1
|φ1 = √ ⊗nj=1 u 0j + u 1j . (6.21)
2n
Fig. 6.7 Geometrical interpretation of solving a decision problem with any input of n bits in a
two-dimensional Hilbert space spanned by ( |ϕ ) and ( |λ )
Because the inner product of |ϕ and |λ is equal to zero and the length of |ϕ
and |λ is equal to one, |ϕ and |λ form an orthonormal basis of a two-dimensional
Hilbert space which is depicted in Fig. 6.7. In Fig. 6.7, Point D is the original point
of the two-dimensional Hilbert space and its coordinate is (0, 0).
The state vector ( |φ1 ) in (6.21) can be expressed as a linear combination of ( |ϕ )
and ( |λ ) in a two-dimensional Hilbert space of Fig. 6.7 in the following way
⎛
1 ⎝
|φ1 = √ |u 1 u 2 · · · u n
N O f (u 1 u 2 ···u n )=0
⎞
+ |u 1 u 2 · · · u n ⎠
O f (u 1 u 2 ···u n )=1
√ √
N−S S
= √ |ϕ + √ |λ . (6.24)
N N
From
√
(6.24),
√
coordinate of ( |φ1 ) in a two-dimensional Hilbert space of Fig. 6.7
N −S √ S
is √ , and is strictly related to the angle between ( |φ1 ) and (|ϕ ) denoted
θ N N
by 2 which is depicted in Fig. 6.7. Point B is coordinate point of ( |φ1 ).
In the quantum search algorithm introduced in the third Chapter, the Oracle O
multiplies the probability amplitude of the answer(s) by −1 and leaves any other
amplitude unchanged. We use the Oracle O to operate on the state vector ( |φ1 ) in
(6.21) and obtain the new state vector |φ2 = O( |φ1 ) that can be expressed as a
292 6 Phase Estimation and Its Applications
√From (6.25),
√
coordinate of ( |φ2 )in a two-dimensional Hilbert space of Fig. 6.7 is
N −S S
√
N
, − √ N and is depicted in Fig. 6.7. Point C is coordinate point of ( |φ2 ). The
angle between ( |φ2 ) and ( |ϕ ) is actually equal to θ2 that is depicted in Fig. 6.7.
From
√
(6.26), coordinate of ( |φ3 ) in a two-dimensional Hilbert space of Fig. 6.7
√ S 3×N −4×S
is √NN−S × N −4×S
N
, √ ×
N N
and is depicted in Fig. 6.7. Point E is coor-
dinate point of ( |φ3 ). The angle between ( |φ3 ) and ( |φ1 ) is actually equal to (θ )
that is depicted in Fig. 6.7. The unitary operator U (the inversion about the average)
in Fig. 6.7 reflects its input state ( |φ2 ) over ( |φ1 ) to ( |φ3 ) in the two-dimensional
geometrical interpretation of Fig. 6.7. In Fig. 6.7, point F is the intersection of line
EC and line D B in which they are vertical each other and point H is the intersection
of line E H and axis |ϕ in which they are vertical each other.
6.3 Quantum Counting to a Decision Problem with Any Input of n … 293
From
Fig. 6.7,we can figure out the projection of |φ1 onto axis |ϕ that is sin(θ/2) =
S
N
/1 = NS . The value of S is to the number of solutions that is how many
elements in the domain {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n} satisfy Of (u1
u2 … un−1 un ) to have a true value. Because S = (sin(θ /2))2 × N and the value of
N is known, if we can determine the value of θ, then we can compute the value of S
that is the number of solutions. If the value of S is not equal to zero, then an output
is “yes” to a decision problem with any input of n bits. Otherwise, an output is “no”
to the decision problem with any input of n bits.
Figure 6.8 is quantum-counting circuits that are a real application of phase esti-
mation.
√ In
Fig. 6.8, if an eigenvalue generated from controlled Grover operations are
−1×θ
e , then we use controlled Grover operations followed by inverse quantum
Fourier transform to find the best approximation of t bits to the value of θ. Other-
wise, we use controlled Grover operations followed by quantum Fourier transform
to find the best approximation of t bits to the value of θ. In Fig. 6.8, a superposition
of the second register is the state vector |u . The state vector |u is a superposition
of ( |ϕ ) in (6.22) and ( |λ ) in (6.23). Because |V1 and |V2 in (6.29) form an
orthonormal basis of the space spanned by ( |ϕ ) in (6.22) and ( |λ ) in (6.23), the
Fig. 6.8 Quantum counting circuits to calculate the number of solutions to a decision problem with
the input of n bits
6.3 Quantum Counting to a Decision Problem with Any Input of n … 295
state vector |u in Fig. 6.8 can be expressed as a linear combination of |V1 and |V2
in (6.29).
We assume that graph G has is a set V of vertices and a set E of edges. We also
suppose that V is {v1 , …, vn } in which each element vj for 1 ≤ j ≤ n is a vertex in
graph G. We assume that E is {(va , vb )| va ∈ V and vb ∈ V }. We use G = (V, E) to
represent it. We assume that |V| is the number of vertices in V and |E| is the number
of edges in E. We also suppose that |V| is equal to n and |E| is equal to m. The value
of m is at most equal to ((n × (n − 1))/2). For graph G = (V, E), its complementary
graph is G = V, E in which each edge in Eis out of E. This is to say that E is {(vc ,
vd )| vc ∈ V and vd ∈ V and (vc , vd ) ∈ / E}. We assume that E is the number of edges
in E. The number of edges in E is (((n × (n − 1))/2) − m). An independent-set of
graph G with n vertices and m edges is a subset V 1 ⊆ V of vertices such that for all
vc , vd ∈ V 1 , the edge (vc , vd ) is not in E. The independent-set problem of graph G
with n vertices and m edges is to find a maximum-sized independent set in G.
Consider that in Fig. 6.9, a graph G1 contains two vertices {v1 , v2 } and one edge
{(v1 , v2 )} and its complementary graph G 1 includes the same vertices and zero edge.
This is an example of a decision problem that is deciding whether a graph G1 in
Fig. 6.9 has a maximum-sized independent set or not. All of the subsets of vertex are
{} that is an empty set, {v1 }, {v2 } and {v1 , v2 }. Because in {v1 , v2 }, the edge (v1 , v2 )
is one edge of graph G1 , {v1 , v2 } does not satisfy definition of an independent set. For
other three subsets of vertex that are {} that is an empty set, {v1 } and {v2 }, there is no
edge in them to connect to other distinct vertex. Therefore, they satisfy definition of
an independent set. So, all of the independent sets in graph G1 are {} that is an empty
set, {v1 } and {v2 }. Since the number of vertex in them are subsequently zero, one
and one, the maximum-sized independent set for graph G1 is {v1 } and {v2 }. Finally,
for the decision problem “a graph G1 in Fig. 6.9, does it have a maximum-sized
independent set?” it gives an output “yes”.
For any graph G with n vertices and m edges, all possible independent sets are 2n
possible choices consisting of legal and illegal independent sets in G. Each possible
choice corresponds to a subset of vertices in G. Hence, we assume that Y is a set of 2n
Fig. 6.10 Quantum circuit for deciding whether a graph G1 with two vertices and one edge in
Fig. 6.9 has a maximum-sized independent set or not
possible choices and Y is equal to {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j ≤ n}. This
indicates that the length of each element in Y is n bits and each element represents
one of 2n possible choices. For the sake of presentation, we suppose that uj 0 is that
the value of uj is zero and uj 1 is that the value of uj is one. If an element u1 u2 …
un−1 un in Y is a legal independent set and the value of uj for 1 ≤ j ≤ n is one, then
uj 1 represents that the jth vertex is within the legal independent set. If an element
u1 u2 … un−1 un in Y is a legal independent set and the value of uj for 1 ≤ j ≤ n is
zero, then uj 0 represents that the jth vertex is not within the legalindependent
set. We
1 n 0 1
use superposition of a register with n quantum bits 2n ⊗ j=1 u j + u j
√ to
encode a set of 2n possible choices, Y = {u1 u2 … un−1 un | ∀ uj ∈ {0, 1} for 1 ≤ j
≤ n}.
Deciding whether a graph G1 with two vertices and one edge in Fig. 6.9 has
a maximum-sized independent set or not is equivalent to compute the number of
solution to the same problem. Therefore, we make use of the circuit in Fig. 6.10 to
determine the number of solution to the independent set problem in a graph G1 with
two vertices and one edge in Fig. 6.9. Ituses two quantum registers. At the left top
in Fig. 6.10, the first register ⊗1k=4 yk0 includes four quantum bits initially in the
state |0. Quantum bit |y4 0 is the most significant bit. Quantum bit |y1 0 is the least
significant bit. The corresponding decimal value of the first register is (|y4 0 × 24−1 )
+ (|y3 0 × 23−1 ) + (|y2 0 × 2
2−1
) + (|y1 0 × 21−1 ). At the left bottom in Fig. 6.10,
the second register ⊗2j=1 u 0j contains two quantum bits initially in the state |0.
Quantum bit |u1 encodes the first vertex v1 in graph G1 in Fig. 6.9 and is the most
significant bit. Quantum bit |u2 encodes the second vertex v2 in graph G1 in Fig. 6.9
and is the least significant bit. Quantum bits |u1 1 |u2 1 encodes {v1 , v2 } that is a
subset of two vertices. Quantum bits |u1 1 |u2 0 encodes {v1 } that is a subset of one
vertex. Quantum bits |u1 0 |u2 1 encodes {v2 } that is a subset of one vertex. Quantum
bits |u1 0 |u2 0 encodes {} that is an empty subset without any vertex. Of course, the
corresponding decimal value of the second register is (|u1 0 × 22−1 ) + (|u2 0 ×
22−2 ). For the convenience of the presentation, the following initial state vector is
6.4 Determine the Number of Solutions to the Independent-Set … 297
|ϕ0 = ⊗1k=4 yk0 ⊗ ⊗2j=1 u 0j .
(6.30)
In Listing 6.2, the program is in the backend that is simulator of Open QASM with
thirty-two quantum bits in IBM’s quantum computer. The program is to calculate the
number of solutions to the independent-set problem in graph G1 with two vertices and
one edge in Fig. 6.9. Figure 6.11 is the corresponding quantum circuit of the program
in Listing 6.2 and is to implement the quantum circuit of Fig. 6.10 to calculate the
number of solutions to the independent-set problem in graph G1 with two vertices
and one edge in Fig. 6.9.
Listing 6.2 The program of computing the number of solutions to the independent-
set problem in graph G1 with two vertices and one edge in Fig. 6.9.
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[6];
4. creg c[4];
The statement “OPENQASM 2.0;” on line one of Listing 6.2 is to indicate that the
program is written with version 2.0 of Open QASM. Then, the statement “include
“qelib1.inc”;” on line two of Listing 6.2 is to continue parsing the file “qelib1.inc” as
if the contents of the file were pasted at the location of the include statement, where
the file “qelib1.inc” is Quantum Experience (QE) Standard Header and the path
is specified relative to the current working directory.
Fig. 6.11 Implementing quantum circuits of Fig. 6.10 to compute the number of solutions to the
independent-set problem in graph G1 with two vertices and one edge in Fig. 6.9
298 6 Phase Estimation and Its Applications
Next, the statement “qreg q[6];” on line three of Listing 6.2 is to declare that in the
program there are six quantum bits. In the left top of Fig. 6.11, six quantum bits are
respectively q[0], q[1], q[2], q[3], q[4] and q[5]. The initial value of each quantum bit
is set to state |0. We use four quantum bits q[0], q[1], q[2] and q[3] to subsequently
encode four quantum bits |y4 , |y3 , |y2 and |y1 in Fig. 6.10. We apply two quantum
bits q[4] and q[5] to respectively encode two quantum bits |u1 and |u2 in Fig. 6.10.
For the convenience of our explanation, q[k]0 for 0 ≤ k ≤ 5 is to represent the value
0 of q[k] and q[k]1 for 0 ≤ k ≤ 5 is to represent the value 1 of q[k]. Since quantum
bit |y4 0 is the most significant bit and quantum bit |y1 0 is the least significant bit,
quantum bit |q[0]0 is the most significant bit and quantum bit |q[3]0 is the least
significant bit. The corresponding decimal value of the first register in Fig. 6.11 is
(|q[0]0 × 24−1 ) + (|q[1]0 × 23−1 ) + (|q[2]0 × 22−1 ) + (|q[3]0 × 21−1 ).
Then, the statement “creg c[4];” on line four of Listing 6.2 is to declare that there
are four classical bits in the program. In the left bottom of Fig. 6.11, four classical
bits are respectively c[0], c[1], c[2] and c[3]. The initial value of each classical bit
is set to zero (0). For the convenience of our explanation, c[k]0 for 0 ≤ k ≤ 3 is to
represent the value 0 of c[k] and c[k]1 for 0 ≤ k ≤ 3 is to represent the value 1 of
c[k]. The corresponding decimal value of the four initial classical bits c[3]0 c[2]0
c[1]0 c[0]0 is 23 × c[3]0 + 22 × c[2]0 + 21 × c[1]0 + 20 × c[0]0 . This is to say
that classical bit c[3]0 is the most significant bit and classical bit c[0]0 is the least
significant bit. For the convenience of our explanation, we can rewrite the initial state
1 0 2 0
vector |ϕ0 = ⊗k=4 yk ⊗ ⊗ j=1 u j in (6.30) in Fig. 6.10 as follows
|ϕ0 = ⊗1k=4 yk0 ⊗ ⊗2j=1 u 0j = q[0]0 q[1]0 q[2]0 q[3]0 q[4]0 q[5]0 .
(6.31)
0 1
A superposition of the first register is √1 ⊗1 y + y =
2 4 k=4 k k
√1 ⊗3 0 1
a=0 q[a] + q[a]
. Another superposition of the second register
24
is |u = √1 2 ⊗2j=1 u 0j + u 1j = √1 2 ⊗5b=4 q[b]0 + q[b]1
. This
2 2
impliesthat the superposition
of the second register begins in the new state
1 2 0 1 1
5 0
1
vector |u = √ 2 ⊗ j=1 u j + u j = √ 2 ⊗b=4 q[b] + q[b] and
2 2
contains two quantum bitsas is necessary to store ( |u ). In superposition
of the
1
second register ( |u ), state u 11 u 12 that is encoded by state q[4]1 q[5]
1 with
the amplitude (1/2) encodes {v 1 , v 2 } that is a subset of two vertices. State u u 0
1 2
that is encoded by state q[4]1 q[5] 0 with the amplitude (1/2) encodes
{v } that
1 1
is a subset of one vertex. State u 01 u 12 that is encoded by state q[4]0 q[5]
with the amplitude (1/2) encodes {v2 } that is a subset of one vertex. State u 01 u 02
that is encoded by state q[4]0 q[5]0 with the amplitude (1/2) encodes {} that is
an empty subset without vertex. The new state vector ( |u ) is an eigenstate (eigen-
vector) of G that is the Grover operator and is a unitary operator. Thus, this gives
that the following new state vector is
1 1
|ϕ1 = √ ⊗1k=4 yk0 + yk1 ⊗ √ ⊗2j=1 u 0j + u 1j
2 4 2 2
1 1 0 1
= √ ⊗k=4 yk + yk ⊗ (|u)
24
1 3 1
q[a]0 + q[a]1 ⊗ √ ⊗5b=4 q[b]0 + q[b]1
= √ ⊗a=0
2 4 2 2
1 3
√ ⊗a=0 q[a]0 + q[a]1
= ⊗ (|u). (6.32)
24
300 6 Phase Estimation and Its Applications
In the new state vector |ϕ 1 in (6.32), each quantum bit in the first register 1 is0currently
in its superposition. 3 The value of the first register is from state ⊗k=4 yk (zero)
0
1
1
encoded ⊗a=0 q[a] through state ⊗k=4 yk (fifteen) encoded by state
by 1state
3
⊗a=0 q[a] with that the amplitude of each state is (1/4). The circuit of Fig. 6.10
can precisely estimate sixteen phases. This indicates that the first register with four
quantum bits can precisely represent sixteen phases. Sixteen phases are respec-
tively (0/24 ), (1/24 ), (2/24 ), (3/24 ), (4/24 ), (5/24 ), (6/24 ), (7/24 ), (8/24 ), (9/24 ), (10/24 ),
(11/24 ), (12/24 ), (13/24 ), (14/24 ) and (15/24 ). The corresponding sixteen phase angles
are respectively (2 × π × 0/24 ), (2 × π × 1/24 ), (2 × π × 2/24 ), (2 × π × 3/24 ), (2
× π × 4/24 ), (2 × π × 5/24 ), (2 × π × 6/24 ), (2 × π × 7/24 ), (2 × π × 8/24 ), (2 ×
π × 9/24 ), (2 × π × 10/24 ), (2 × π × 11/24 ), (2 × π × 12/24 ), (2 × π × 13/24 ), (2
× π × 14/24 ) and (2 × π × 15/24 ).
Say that we are trying to compute an eigenvalue of 90°. The number of solutions
for the independent-set problem in a graph G1 with two vertices and one edge in
Fig. 6.9 is S = N × (sin(θ /2))2 = 4 × (sin(90°/2))2 = 4 × (1/2) = 2. This gives that
the answer is two for determining the number of solutions for the independent-set
problem in a graph G1 with two vertices and one edge in Fig. 6.9. Therefore, the
effect of one application √
of the Grover operator √
G on its eigenvector (eigenstate)
4
( |u ) is (G × |u = e± −1×2×π×θ × |u = e± −1×2×π× 24 × |u). So, the effect of
repeated application of the Grover operator G on its eigenvector (eigenstate) ( |u )
is
√ √
−1×2×π× 244 ×a
G a |u = e± −1×2×π×θ×a
|u = e± × |u. (6.33)
0 1
A superposition √1 y + y that is encoded by √12 q[3]0 + q[3]1
2 1 1
at the weighted position 20 is the controlled quantum bit of implementing
0
controlled-G 2 operations on the superposition of the second register that is
1 0 1
the state (|u). Similarly, a superposition √2 y2 + y2 that is encoded by
√1 q[2]0 + q[2]1 nat the weighted position 21 is the controlled quantum bit of
2
1
implementing controlled-G 2 operations on the superposition of the second register
1
1 0
that is the state (|u). Then, a superposition √
2
y3 + y3
that is encoded by
1 0
√ q[1] + q[1] 1 2
2
at the weighted position 2 is the controlled quantum bit of
2
implementing controlled-G 2 noperations on the superposition of the second register
that is the state (|u). Next, a superposition √12 y40 + y41 that is encoded by
6.4 Determine the Number of Solutions to the Independent-Set … 301
√1 q[0]0 + q[0]1 at the weighted position 23 is the controlled quantum bit of
2
3
implementing controlled-G 2 operations on the superposition of the second register
that is the state (|u). √ √
The Grover operator G has two eigenvalues e −1×2×π×θ and e− −1×2×π×θ .
√ √ 4
We assume that it generates the eigenvalue e −1×2×π×θ = e −1×2×π× 24 .
The four statements from line eleven through line fourteen in Listing 6.2 are
“u1(2 * pi * 4/16 * 1) q[3];”, “u1(2 * pi * 4/16 * 2) q[2];”, “u1(2 * pi * 4/16 * 4)
q[1];” and “u1(2 * pi * 4/16 * 8) q[0];”.
They take the new state vector (|ϕ 1 ) in (6.32) as their input state vector and imple-
ment each controlled-G operation on the superposition of the second register in the
second time slot of Fig. 6.11 and in the second stage of Fig. 6.10. They alertthe
√ 4 0
phase of the state |y1 1 (|q[3]1 ) is from one (1) to become e −1×2×π× 16 ×2 =
√ 4
e −1×2×π× 16 ×1 . They alert the phase of the state |y2 1 (|q[2]1 ) is from one
√ 4 1
√ 4
(1) to become e −1×2×π× 16 ×2 = e −1×2×π× 16 ×2 . They alert the phase
√ 4 2
of the state |y3 1 (|q[1]1 ) is from one (1) to become e −1×2×π× 16 ×2 =
√ 4
e −1×2×π× 16 ×4 nnand alert the phase of the state |y4 1 (|q[0]1 ) is from one (1)
√ 4 3
√ 4
to become e −1×2×π× 16 ×2 = e −1×2×π× 16 ×8 . This gives that the following
new state vector is
1 0 √ 4 3 √ 4 2
|ϕ2 = √ y4 + e −1×2×π× 16 ×2 y41 ⊗ y30 + e −1×2×π× 16 ×2 y31
24
√ 4 1 √ 4 0
⊗ y20 + e −1×2×π× 16 ×2 y21 ⊗ y10 + e −1×2×π× 16 ×2 y11 ⊗ (|u)
1 0
√ 4 √ 4
= √ y4 + e −1×2×π× 16 ×8 y41 ⊗ y30 + e −1×2×π× 16 ×4 y31
2 4
√ 4 √ 4
⊗ y20 + e −1×2×π× 16 ×2 y21 ⊗ y10 + e −1×2×π× 16 ×1 y11
⊗ (|u)
1 √ 4
q[0]0 + e −1×2×π× 16 ×8 q[0]1
= √
24
302 6 Phase Estimation and Its Applications
√ 4
⊗ q[1]0 + e −1×2×π× 16 ×4 q[1]1
√ 4
⊗ q[2]0 + e −1×2×π× 16 ×2 q[2]1
√ 4
⊗ q[3]0 + e −1×2×π× 16 ×1 q[3]1
⊗ (|u)
⎛ ⎛4 ⎞⎞
2 −1 √
1 4
= ⎝√ ⎝ e −1×2×π× 16 ×Y |Y ⎠⎠ ⊗ (|u). (6.34)
24 Y =0
From this description above, the second quantum register stays in the state (|u)
through the computation. Because of phase √kickback, 4the phase of the state |Y for 0
4
≤ Y ≤ 2 − 1 is from one (1) to become e −1×2×π× 16 ×Y . In the state vector (|ϕ 2 )
in (6.34), it includes sixteen phase angles from state |0 through state |15. The front
eight phase angles are (90° × 0 = 0°), (90° × 1 = 90°), (90° × 2 = 180°), (90° × 3
= 270°), (90° × 4 = 360° = 0°), (90° × 5 = 450° = 90°), (90° × 6 = 540° = 180°)
and (90° × 7 = 630° = 270°). The last eight phase angles are (90° × 8 = 720° =
0°), (90° × 9 = 810° = 90°), (90° × 10 = 900° = 180°), (90° × 11 = 990° = 270°),
(90° × 12 = 1080° = 0°), (90° × 13 = 1170° = 90°), (90° × 14 = 1260° = 180°)
and (90° × 15 = 1350° = 270°). The phase angle rotates back to its starting value
0° four times.
22. h q[2];
23. cu1(−2 * pi * 1/4) q[3], q[2];
24. h q[3];
25. swap q[0], q[3];
26. swap q[1], q[2];
Hidden patterns and information stored in the state vector (|ϕ 2 ) in (6.34) are to
that its phase angle rotates back to its starting value 0° four times. This is to say
that the number of the period per sixteen phase angles is four and the frequency is
equal to four (16/4). The twelve statements from line fifteen through line twenty-six
in Listing 6.2 complete each quantum operation from the third time slot through the
fourteenth time slot in Fig. 6.11. They actually implement each quantum operation
of performing an inverse quantum Fourier transform on the superposition of the
first register in Fig. 6.10. They take the state vector (|ϕ 2 ) in (6.34) as their input
state vector. Since the inverse quantum Fourier transform effectively transforms
the state of the first register into a superposition of the periodic signal’s component
frequencies, they generate the following state vector
⎛ ⎞
24 −1 24 −1
1 √ 4 1 √
− −1×2×π× 2i4 ×Y
|ϕ3 = ⎝ √ e −1×2×π× 24 ×Y √ e |i ⎠ ⊗ (|u)
Y =0 24 24 i=0
⎛ ⎛4 4 ⎞⎞
2 −1 2 −1 √
1
4 i
−1×2×π×Y × 24 − 24
= ⎝√ ⎝ e |i ⎠⎠ ⊗ (|u)
24 Y =0 i=0
⎛4 4 ⎞
2 −1 2 −1 √ Y
1
4 i
−1×2×π× 24 − 24
=⎝ √ e |i ⎠ ⊗ (|u). (6.35)
2 4
i=0 Y =0
Finally, the four statements “measure q[0] → c[3];”, “measure q[1] → c[2];”, “mea-
sure q[2] → c[1];” and “measure q[3] → c[0];” from line twenty-seven through line
thirty in Listing 6.2 implement a measurement. They measure the output state of the
inverse quantum Fourier transform to the superposition of the first register in Fig. 6.11
and in Fig. 6.10. This is to say that they measure four quantum bits q[0], q[1], q[2]
304 6 Phase Estimation and Its Applications
Fig. 6.12 A computational basis state 0100 has the probability 100%
and q[3] of the first register and record the measurement outcome by overwriting
four classical bits c[3], c[2], c[1] and c[0].
6.5 Summary
In this chapter for more details about an introduction of the phase estimation algo-
rithm, the recommended books are Nielsen and Chuang (2000), Imre and Balazs
(2005), Lipton and Regan (2014), Silva (2018), Johnston et al. (2019). For a more
detailed description to binary search trees, the recommended book is Horowitz et al.
(2003). For a more detailed introduction to the discrete Fourier transform and the
inverse discrete Fourier transform, the recommended books are Cormen et al. (2009),
Nielsen and Chuang (2000), Imre and Balazs (2005), Lipton and Regan (2014), Silva
(2018), Johnston et al. (2019). The two famous articles (Coppersmith 1994; Shor
1994) gave the original version of the Quantum Fourier transform and the inverse
quantum Fourier transform. A good illustration for the product state decomposition of
the quantum Fourier transform and the inverse quantum Fourier transform is the two
famous articles in Griffiths and Niu (1996), Cleve et al. (1998). For a more detailed
description to the quantum-counting algorithm, the recommended article and books
are Brassard et al. (1998), Nielsen and Chuang (2000), Imre and Balazs (2005),
Lipton and Regan (2014), Silva (2018), Johnston et al. (2019). A good introduction
to the instructions of Open QASM is the famous article in Cross et al. (2017).
6.7 Exercises
6.1 Prove that the transformation of the Oracle is O = I2n ,2n − 2 × |x0 x0 |, where
x 0 is one element in the domain of the Oracle and x 0 satisfies O(x 0 ) = 1.
6.2 Determine the matrix of the Oracle that is O = I22 ,22 − 2 × |x0 x0 |, where x 0
= 2 and x 0 satisfies O(x 0 ) = 1.
306 6 Phase Estimation and Its Applications
6.3 Show that the unitary operator U (inversion about the average) is equivalent to
reflect its input state |φ2 over |φ1 to |φ3 that is a reflection about |φ1 in
the two-dimensional geometrical interpretation of Fig. 6.7.
6.4 Compute the matrix of the Grover operator G in the basis of ( |ϕ ) and ( |λ ) in
Fig. 6.7.
6.5 Calculate the eigenvalues and corresponding eigenvectors of the Grover
operator G in the basis of ( |ϕ ) and ( |λ ) in Fig. 6.7.
References
Brassard, G., Hoyer, P., Tapp, A.: Quantum counting. In: The 25th International Colloquium on
Automata, Languages, and Programming (ICALP), LNCS 1443, pp. 820–831 (1998)
Cleve, R., Ekert, A., Macciavello, C., Mosca, M.: Quantum algorithms revisited. Proc. Roy. Soc.
Lond. Ser. A 454, 339–354 (1998). E-print quant-ph/9708016
Coppersmith, D.: An approximate Fourier transform useful in quantum factoring. IBM Research
Report, Technical Report RC 19642, December 1994. E-print quant-ph/0201067
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. The
MIT Press, Cambridge (2009). ISBN-13: 978-0262033848, ISBN-10: 9780262033848
Cross, A.W., Bishop, L.S., Smolin, J.A., Gambetta, J.M.: Open quantum assembly language. 2017.
https://arxiv.org/abs/1707.03429
Griffiths, R.B., Niu, C.S.: Semiclassical Fourier transform for quantum computation. Phys. Rev.
Lett. 76(17), 3228−3231 (1996). E-print quant-ph/9511007
Horowitz, E., Sahni, S., Anderson-Freed, S.: Fundamentals of Data Structures in C, 12th edn.
Computer Science Press, New York (2003). ISBN 0-7167-8250-2
Imre, S., Balazs, F.: Quantum Computation and Communications: An Engineering Approach. Wiley,
UK (2007). ISBN-10: 047086902X, ISBN-13: 978-0470869024, 2005
Johnston, E.R., Harrigan, N., Gimeno-Segovia, M.: Programming Quantum Computers: Essential
Algorithms and Code Samples. O’Reilly Media, Inc. (2019). ISBN-13: 978-1492039686, ISBN-
10: 1492039683
Lipton, R.J., Regan, K.W.: Quantum Algorithms via Linear Algebra: A Primer. The MIT Press,
Cambridge (2014). ISBN 978-0-262-02839-4
Nielsen, M.A., Chuang, I.L.: Quantum Computation and Quantum Information. Cambridge
University Press, New York, NY (2000). ISBN-10: 9781107002173, ISBN-13: 978-1107002173
Shor, P.: Algorithms for quantum computation: discrete logarithms and factoring. In: Proceedings
35th Annual Symposium on Foundations of Computer Science, Santa Fe, pp. 124−134, 20–22
Nov 1994
Silva, V.: Practical Quantum Computing for Developers: Programming Quantum Rigs in the Cloud
using Python, Quantum Assembly Language and IBM Q Experience. Apress, New York, 13 Dec
2018. ISBN-10: 1484242173, ISBN-13: 978-1484242179
Answer Key
Solutions to Chapter 1
1.1 The problem at hand is to find the parameters based on the following equality:
In the following we assume the interval [0, π ]. Since cos θ2 = 0, θ2 must equal
π
2
, and thus θ = π . Since sin π2 = 1, eiφ = 1 as well, and hence iφ = 0
and therefore also φ = 0. Again, since sin π2 = 1, −eiλ = 1, and hence by
squaring both sides we have ei2λ = 1 and from this λ = 0.
1.2 Along the same lines as in Exercise 1.1
cos θ −eiλ sin θ √1 √1
U 3(θ, φ, λ) = iφ 2 θ i(λ+φ) 2 θ = 2 2 =H
e sin 2 e cos 2 √1 − √12
2
With cos θ2 = 1 it follows that θ = 0. Since cos 02 = 1, it must be the case that
ei(λ+φ) = −1. It must therefore be the case that λ + φ = π . With sin 20 = 0
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 307
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1
308 Answer Key
fore, the parameter θ = 0, while for the remaining two parameters there are
multiple values for which holds that ei(λ+φ) = 1+i√ . To obtain the parameters
2
we use trigonometric identities
This corresponds to
Thus we have λ + φ = π4 .
1.8 The reasoning regarding the T + gate is similar to that for the T gate. Instead of
1+i
√ , the T + has its conjugate 1−i
√ . The parameter θ = 0. Using trigonometric
2 2
identities
1 i
cos(λ + φ) + i sin(λ + φ) = √ − √
2 2
Answer Key 309
eiλ
cos θ −eiλ sin θ √1 − √
U 3(θ, φ, λ) = iφ 2 θ i(λ+φ) 2 θ = 2 2 = U 2(φ, λ)
e sin 2 e cos 2 eiφ ei(λ+φ)
√ √
2 2
Solutions to Chapter 2
2.1 To obtain the required operation, 3 quantum bits and 3 classical bits are needed.
In the following circuit, q[0] represents the x operand, q[1] represents the y
operand, while q[2] is the additional bit in the OR gate introduced in the material
of this chapter. First, set the two operands into a superposition by applying to
each a Hadamard gate H. As the OR gate requires that the auxialiary quantum
bit be equal 1, apply the NOT gate to it. The OR gate would require a total
of 4 NOT gates. It can however be simplified due to the fact that one of the
operands, namely x, is negated. The equivalence of the two subcircuits can be
easily checked by writing down truth tables for each of them. Note that the
output is presented in the order c[2], c[1], c[0].
The circuit:
310 Answer Key
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
7. x q[1];
8. x q[2];
9. ccx q[0], q[1], q[2];
10. x q[1];
11. measure q[0] → c[0];
12. measure q[1] → c[1];
13. measure q[2] → c[2];
The output:
2.2 To obtain the required operation, 3 quantum bits and 3 classical bits are needed.
q[0] represents the x operand, q[1] represents y, while q[2] is the additional
bit in the OR gate introduced in the material of this chapter. First, set the two
operands into a superposition by applying to each a Hadamard gate H. As the
Answer Key 311
OR gate requires that the auxiliary quantum bit be equal 1, apply the Pauli X
gate to it. The OR gate would require a total of 4 X gates. It can however be
simplified due to the fact that one of the operands, namely y, is negated. Note
that the output is presented in the order c[2], c[1], c[0].
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
7. x q[2];
8. x q[0];
9. ccx q[0], q[1], q[2];
10. x q[0];
11. measure q[0] → c[0];
12. measure q[1] → c[1];
13. measure q[2] → c[2];
14. measure q[0] → c[0];
15. measure q[1] → c[1];
16. measure q[2] → c[2];
312 Answer Key
The output:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
7. x q[1];
Answer Key 313
The output:
while q[4] is the auxiliary bit for the OR operation mentioned above. Note that
the output is presented in the order c[2], c[1], c[0].
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
7. x q[0];
8. ccx q[0], q[1], q[2];
9. x q[0];
10. barrier q[0], q[1], q[2], q[3], q[4];
11. x q[0];
12. x q[1];
13. ccx q[0], q[1], q[3];
14. x q[0];
15. x q[1];
16. barrier q[0], q[1], q[2], q[3], q[4];
17. x q[2];
18. x q[3];
19. x q[4];
20. ccx q[2], q[3], q[4];
21. x q[2];
22. x q[3];
23. barrier q[0], q[1], q[2], q[3], q[4];
24. measure q[0] → c[0];
25. measure q[1] → c[1];
26. measure q[4] → c[2];
Answer Key 315
The output:
2.5 To obtain the required operation, 3 quantum bits and 3 classical bits are needed.
q[0] represents the x operand, q[1] represents y, while q[2] is the additional bit
in the AND gate introduced in the material of this chapter. First, set the two
operands into a superposition by applying to each a Hadamard gate H. The two
X gates are to account for the fact that operand x is negated. Note that the output
is presented in the order c[2], c[1], c[0].
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
7. x q[0];
8. ccx q[0], q[1], q[2];
9. x q[0];
10. measure q[0] → c[0];
316 Answer Key
The output:
2.6 To obtain the required operation, 3 quantum bits and 3 classical bits are needed.
q[0] represents the x operand, q[1] represents y, while q[2] is the additional bit
in the AND gate introduced in the material of this chapter. First, set the two
operands into a superposition by applying to each a Hadamard gate H. The two
X gates are to account for the fact that operand y is negated. Note that the output
is presented in the order c[2], c[1], c[0].
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[3];
5. h q[0];
6. h q[1];
Answer Key 317
7. x q[1];
8. ccx q[0], q[1], q[2];
9. x q[1];
10. measure q[0] → c[0];
11. measure q[1] → c[1];
12. measure q[2] → c[2];
The output:
Solutions to Chapter 3
3.1 The circuit for the satisfiability problem specified by the oracle F(x1 , x2 ) =
x1 ∧ x2 is very similar to the one given for F(x1 , x2 ) = x1 ∧ x2 in this chapter
(see also the answer to Exercise 3.4). The difference is that the input x1 is negated
(that is q[3]). q[0] is a workspace qubit, q[1] is not used, q[2] represents s2 , q[3]
is for x1 , while q[4] is for x2 . We introduce barriers to separate different parts
of the code. Everything up to the first barrier represents the initial encoding.
q[0] represents the state |− = √12 (|0 − |1), as in the material, while q[3] and
q[4] are set into a uniform superposition. The oracle that negates the amplitude
of the qubit for which the condition x1 ∧ x2 = 1 is fulfilled is located between
the first and the second barrier. To represent the fact that the value of x1 is
negated, the NOT gate is used on the qubit representing x1 . After the oracle
has completed its task, another NOT gate is applied to x1 to uncompute. The
circuit part between the second and the third barriers has been introduced in the
material of this chapter and encodes the Grover diffusion operator.
The circuit:
318 Answer Key
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[2];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
9. barrier q[0], q[1], q[2], q[3], q[4];
10. x q[3];
11. ccx q[4], q[3], q[2];
12. cx q[2], q[0];
13. ccx q[4], q[3], q[2];
14. x q[3];
15. barrier q[0], q[1], q[2], q[3], q[4];
16. h q[3];
17. h q[4];
18. x q[3];
19. x q[4];
20. h q[4];
21. cx q[3], q[4];
22. x q[3];
23. h q[4];
24. u3(2 * pi, 0, 0) q[3];
25. x q[4];
26. h q[3];
27. h q[4];
28. barrier q[0], q[1], q[2], q[3], q[4];
29. measure q[4] → c[0];
30. measure q[3] → c[1];
Answer Key 319
The output:
3.2 The circuit for the satisfiability problem specified by the oracle F(x1 , x2 ) =
x1 ∧ x2 is very similar to the one given for F(x1 , x2 ) = x1 ∧ x2 in Exercise
3.1. The difference is that the input x2 is negated (that is q[2]) instead of x1 .
q[0] is a workspace qubit, q[1] is not used, q[2] represents s2 , q[3] is for x1 ,
while q[4] is for x2 . We introduce barriers to separate different parts of the code.
Everything up to the first barrier represents the initial encoding. q[0] represents
the state |− = √12 (|0 − |1), as in the material, while q[3] and q[4] are set
into a uniform superposition. The oracle that negates the amplitude of the qubit
for which the condition x1 ∧ x2 = 1 is fulfilled is located between the first and
the second barrier. To represent the fact that the value of x2 is negated, the NOT
gate is used on the qubit representing x2 . After the oracle has completed its task,
another NOT gate is applied to x2 to uncompute. The circuit part between the
second and the third barriers has been introduced in the material of this chapter
and encodes the Grover diffusion operator.
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
320 Answer Key
4. creg c[2];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
9. barrier q[0], q[1], q[2], q[3], q[4];
10. x q[4];
11. ccx q[4], q[3], q[2];
12. cx q[2], q[0];
13. ccx q[4], q[3], q[2];
14. x q[4];
15. barrier q[0], q[1], q[2], q[3], q[4];
16. h q[3];
17. h q[4];
18. x q[3];
19. x q[4];
20. h q[4];
21. cx q[3], q[4];
22. x q[3];
23. h q[4];
24. u3(2 * pi, 0, 0) q[3];
25. x q[4];
26. h q[3];
27. h q[4];
28. barrier q[0], q[1], q[2], q[3], q[4];
29. measure q[4] → c[0];
30. measure q[3] → c[1];
The output:
3.3 In theory, the circuit for the satisfiability problem specified by the oracle
F(x1 , x2 ) = x1 ∧ x2 should be very similar to the one given for F(x1 , x2 ) =
Answer Key 321
x1 ∧ x2 in this chapter (see also the answer to Exercise 3.4). The difference
would be that the quantum bit q[2] is negated in order to represent the NAND
gate. q[0] is a workspace qubit, q[1] is not used, q[2] represents s2 , q[3] is
for x1 , while q[4] is for x2 . We introduce barriers to separate different parts of
the code. Everything up to the first barrier represents the initial encoding. q[0]
represents the state |− = √12 (|0 − |1), as in the material, while q[3] and q[4]
are set into a uniform superposition. The oracle that negates the amplitude of
the qubit for which the condition x1 ∧ x2 = 1 is fulfilled is located between
the first and the second barrier. As mentioned above, the NOT gate is used on
the qubit representing s2 . After the oracle has completed its task, another NOT
gate is applied to s2 to uncompute. The circuit part between the second and the
third barriers has been introduced in the material of this chapter and encodes the
Grover diffusion operator. After executing the code we notice that the obtained
output is incorrect. In fact it shows the state |11 with probability 1. The correct
answer should have included equal probabilities for three states: |00, |01, and
|10. These three states are the correct outcomes for the NAND gate. The reason
we obtained the wrong answer is that Grover’s algorithm imposes a limitation
on the ratio between the number of answers (A) and the number of all possible
outcomes (O). This limitation is OA < 21 . In this exercise, this ratio is 43 > 21
however.
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[2];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
9. barrier q[0], q[1], q[2], q[3], q[4];
10. x q[2];
11. ccx q[4], q[3], q[2];
12. cx q[2], q[0];
322 Answer Key
The output:
3.4 The circuit for the satisfiability problem specified by the oracle F(x1 , x2 ) =
x1 ∧ x2 which was given in the material of this chapter, can be simplified by
replacing the twelve gates used to represent the CCNOT operation with the
CCNOT gate itself (in code: ccx). As the procedure of deriving the circuit has
been described in detail in the material, here we only present the corresponding
circuit, the code and its output. As before, q[0] is a workspace qubit, q[1] is
not used, q[2] represents s2 , q[3] is for x1 , while q[4] is for x2 . We introduce
barriers to separate different parts of the code. Everything up to the first barrier
represents the initial encoding. q[0] represents the state |− = √12 (|0 − |1),
as in the material, while q[3] and q[4] are set into a uniform superposition. The
oracle that negates the amplitude of the qubit for which the condition x1 ∧x2 = 1
is fulfilled is located between the first and the second barrier. The circuit part
Answer Key 323
between the second and the third barriers has been introduced in the material
of this chapter and encodes the Grover diffusion operator.
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[5];
4. creg c[2];
5. x q[0];
6. h q[3];
7. h q[4];
8. h q[0];
9. barrier q[0], q[1], q[2], q[3], q[4];
10. ccx q[4], q[3], q[2];
11. cx q[2], q[0];
12. ccx q[4], q[3], q[2];
13. barrier q[0], q[1], q[2], q[3], q[4];
14. h q[3];
15. h q[4];
16. x q[3];
17. x q[4];
18. h q[4];
19. cx q[3], q[4];
20. x q[3];
21. h q[4];
22. u3(2 * pi, 0, 0) q[3];
23. x q[4];
24. h q[3];
25. h q[4];
26. barrier q[0], q[1], q[2], q[3], q[4];
27. measure q[4] → c[1];
28. measure q[3] → c[0];
324 Answer Key
The output:
Solutions to Chapter 4
a2 a3 a4
1 2πi + +
a1 a2 a3 a4 4e
2 4 8
0/1 0 0 0 1/4
√ √
0/1 0 0 1 2/8 + i 2/8
0/1 0 1 0 i/4
√ √
0/1 0 1 1 − 2/8 + i 2/8
0/1 1 0 0 −1/4
√ √
0/1 1 0 1 − 2/8 − i 2/8
0/1 1 1 0 −i/4
√ √
0/1 1 1 1 2/8 − i 2/8
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[4];
4. creg c[4];
5. h q[0];
6. h q[1];
7. h q[2];
8. h q[3];
9. u1(pi) q[1];
10. u1(pi/2) q[2];
11. u1(pi/4) q[3];
12. barrier q[0], q[1], q[2], q[3];
13. h q[0];
14. cu1(−pi/2) q[1], q[0];
15. cu1(−pi/4) q[2], q[0];
16. cu1(−pi/8) q[3], q[0];
17. h q[1];
18. cu1(−pi/2) q[2], q[1];
19. cu1(−pi/4) q[3], q[1];
20. h q[2];
21. cu1(−pi/2) q[3], q[2];
22. h q[3];
23. swap q[0], q[3];
24. swap q[1], q[2];
25. measure q[0] → c[3];
26. measure q[1] → c[2];
27. measure q[2] → c[1];
28. measure q[3] → c[0];
326 Answer Key
The output:
a3
4.2 The mapping this oracle performs is a1 a2 a3 → 1
√
2 2
e2πi×0.a3 = 1
√
2 2
e2πi ( 2 ) =
1
√
2 2
eiπa3 . The following table shows the particular results. Input 0/1 indicates
that the respective value is either 0 or 1.
1 iπa3
a1 a2 a3 √ e
2 2
√
00/01/10/11 0 1/2 2
√
00/01/10/11 1 −1/2 2
The oracle oscillates between two values corresponding to rotations by 180◦ . The
frequency is 4, whereby the period equals 8/4 = 2. Our solution follows closely
that already introduced in the material of this chapter. Quantum bits q[0], q[1],
and q[2] correspond to a1 , a2 , a3 , respectively. 3 classical registers are needed to
encode the output. The first column indicates that all 3 quantum bits are set into a
uniform superposition by applying to them the Hadamard gate. In the next column, the
respective rotations are encoded. In accordance with the mapping formula, quantum
bit q[2] is rotated by 180◦ . These operations are delineated by the barrier. After the
barrier, the inverse quantum Fourier transform is applied. This transformation is the
same (starting in row 10 of the code) as in the chapter material, but applied to 3
quantum bits instead of 4. What changes is the application of the U1 gate to the qubit
q[2] corresponding to a3 . Moreover, the swapping operation takes place between the
most significant digit and the least significant digit only.
The circuit:
Answer Key 327
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[3];
4. creg c[3];
5. h q[0];
6. h q[1];
7. h q[2];
8. u1(pi) q[2];
9. barrier q[0], q[1], q[2];
10. h q[0];
11. cu1(−pi/2) q[1], q[0];
12. cu1(−pi/4) q[2], q[0];
13. h q[1];
14. cu1(−pi/2) q[2], q[1];
15. h q[2];
16. swap q[0], q[2];
17. measure q[0] → c[2];
18. measure q[1] → c[1];
19. measure q[2] → c[0];
The output:
a2 a3
1 2πi +
a1 a2 a3 √ e 2 4
2 2
√
0/1 0 0 1/2 2
√
0/1 0 1 i/2 2
(continued)
328 Answer Key
(continued)
a2 a3
1 2πi +
a1 a2 a3 √ e 2 4
2 2
√
0/1 1 0 −1/2 2
√
0/1 1 1 −i/2 2
The oracle oscillates between four values corresponding to rotations by 90◦ . The
frequency is 2, whereby the period equals 8/2 = 4. Our solution follows closely that
already introduced in the material of this chapter. Quantum bits q[0], q[1], and q[2]
correspond to a1 , a2 , a3 , respectively. 3 classical registers are needed to encode the
output. The first column indicates that all 3 quantum bits are set into a uniform super-
position by applying to them the Hadamard gate. In the next column, the respective
rotations are encoded. In accordance with the mapping formula, quantum bit q[1]
is rotated by 180◦ , while q[2] is rotated by 90◦ . These operations are delineated by
the barrier. After the barrier, the inverse quantum Fourier transform is applied. This
transformation is the same (starting in row 11 of the code) as in the chapter material,
but applied to 3 quantum bits instead of 4. What changes is the application of the U1
gate to the qubits q[1] and q[2] corresponding to a2 and a3 , respectively. Moreover,
the swapping operation takes place between the most significant digit and the least
significant digit only.
The circuit:
The code:
1. OPENQASM 2.0;
2. include “qelib1.inc”;
3. qreg q[3];
4. creg c[3];
5. h q[0];
6. h q[1];
7. h q[2];
8. u1(pi) q[1];
9. u1(pi/2) q[2];
10. barrier q[0], q[1], q[2];
11. h q[0];
12. cu1(−pi/2) q[1], q[0];
Answer Key 329
The output:
Solutions to Chapter 5
z = ci K i Yi = ci (1 − ki yi ) = ci (mod yi )
Moreover, any two solutions are equal modulo Y. To prove that let z 1 and z 2
be two solutions to the given system of equations. Since y1 , y2 , . . . , yn are
pairwise coprime, it follows that y1 |(z 1 − z 2 ), y2 |(z 1 − z 2 ), . . . yn |(z 1 − z 2 ).
Thus, y1 y2 . . . yn |(z 1 − z 2 ), or, equivalently
z 1 = z 2 (mod y1 y2 . . . yn )
p p! p( p − 1) · · · ( p − k + 1)( p − k)!
= =
k k!( p − k)! k!( p − k)!
p( p − 1) · · · ( p − k + 1)
=
k!
p( p−1)···( p−k+1)
As binomial coefficients represent integers, k!
is an integer. From
p
the above equation it can be verified that is a multiple of p. Hence, what
k
must be shown is that ( p−1)···(k!p−k+1) is an integer. To that end we use Euclid’s
lemma. Euclid’s lemma states that if a prime n divides the product ab, where both
a and b are integers, then n must divide at least one of them. Since p( p−1)···( k!
p−k+1)
a · 2a · 3a · · · ( p − 1)a = 1 · 2 · 3 · · · ( p − 1)(mod p)
Answer Key 331
This corresponds to
and further to
a p−1 = 1(mod p)
a · a p−1 = a(mod p)
i i
z −r n
≤ pn − q n n (1)
2 2
pn pn+1 u z
=
qn qn+1 v r
pn+1 pn (−1)n
− =
qn+1 qn qn qn+1
332 Answer Key
r = uqn + vqn+1
holds that uv ≤ 0. u and v cannot be both positive or both negative because this
would imply that |r | > |qn+1 | which contradicts our assumption that qn < r <
qn+1 .
With (2) we have
i i
z −r n
= (upn + vpn+1 ) − (uqn + vqn+1 ) n
2 2
i i
= u pn − qn n + v pn+1 − qn+1 n
2 2
Since even convergents are increasing and odd convergents are decreasing with
i
2n
lying in between them, and given that uv ≤ 0, it must be that either (1)
u pn − qn 2in and v pn+1 − qn+1 2in have the same sign, or (2) one of them is
zero. Therefore,
i i i
z −r = u pn − q n n + v pn+1 − qn+1
2n 2 2n
For
i i
z −r < pn − q n n
2n 2
i i
z −r ≥ pn − q n n
2n 2
Then
i 1
pn − q n <
2n 2r
z pn z i pn i 1 1 1 1 1
− ≤ − n + − n < 2+ ≤ + =
r qn r 2 qn 2 2r qn qn+1 2rqn 2qn r rqn
(3)
However
z pn zqn − r pn
− = (4)
r qn rqn
|zqn − r pn | ≤ 1
and because we assumed that z and r are positive integers and therefore the
nominator in (4) has to be an integer, we have
|zqn − r pn | = 1
θ
2 − 2 cos(2δ) = 2 − 2 1 − 2 sin2 δ = 4 sin2 δ = 4 sin2
2
Solutions to Chapter 6
6.1 Let |x0 be the only solution of a given search problem. Then, recalling that the
inner product x|x = 1, we have
6.2 For |x0 = |2 = |10 the matrix has dimensions 4x4 and can be derived as
follows:
⎡ ⎤ ⎡ ⎤ ⎡ ⎤
1 0 0 0 0 10 00
⎢0 1 0 0⎥⎥ ⎢ 0 ⎥ ⎢01 00 ⎥
⎢
⎣0 − 2⎢ ⎥ 00 10 =⎢ ⎥
0 1 0⎦ ⎣1 ⎦ ⎣00 −1 0 ⎦
0 0 0 1 0 00 0 1
334 Answer Key
Thus U has reflected vector |φ2 about |φ1 as shown in Fig. 6.7.
6.4 The matrix of G is
cos θ − sin θ
G=
sin θ cos θ
θ S N −S
From the state |φ1 we have that sin 2
= N
and cos θ2 =
From N
.
the state |φ3 we have that sin 3θ2 = 3NN−4S NS while cos 3θ2 = N −4S
N
N −S
N
.
The elements of Grovers operator can then be obtained from the following
trigonometric identities
√
3θ θ 3θ θ 3θ θ 2 S(N − S)
sin θ = sin − = sin cos − cos sin =
2 2 2 2 2 2 N
3θ θ 3θ θ 3θ θ N − 2S
cos θ = cos − = cos cos + sin sin =
2 2 2 2 2 2 N
G|ψ = λ|ψ
|G − λI | = 0
to be
Answer Key 335
√ √
N −2S
−2 S(N −S) N −2S
−2 S(N −S)
√ N N λ0 N
−λ N
2 S(N −S) N −2S
− = √
2 S(N −S) N −2S
N N
0λ N N
−λ
2 √ 2
N − 2S 2 S(N − S)
= −λ +
N N
2N − 4S
= λ2 − λ+1=0
N
The two eigenvalues are therefore (the complex value is due to the fact that
N ≤ S)
√
N − 2S + 2i S(N − S)
λ1 =
N
and
√
N − 2S − 2i S(N − S)
λ2 =
N
The corresponding eigenvectors are calculated as follows. For λ1
√ √ √
2 S(N −S)
N −2S
− 2i√S(N −S)
− 2 √S(N −S)
N
− λ 1 − N N N ψ1,1
√
2 S(N −S) N −2S
|ψ1 = 2 S(N −S) =0
N N
− λ1 N
− 2i S(N
N
−S) ψ1,2
√1
i
and hence ψ1,1 = iψ1,2 . Therefore the eigenvector is 2
. For λ2
1
√ √ √
N −2S
− λ2 − 2 S(N −S) 2i S(N −S)
− 2√ S(N −S)
N N √ N N ψ1,1
√
2 S(N −S) N −2S
|ψ1 = 2 S(N −S) 2i S(N −S) =0
N N
− λ2 N N
ψ1,2
√1
−i
and hence ψ1,1 = −iψ1,2 . This corresponds to the eigenvector 2
.
1
Index
A properties, 166–167
Abelian group, 165–166, 239–240 Complex number, 163–164, 166
Additive group moduloN, 239 Computational basis state, 2, 4, 169, 172,
Amplitude, 2–4, 8, 11, 14, 17, 23, 26, 29, 32, 179–180, 250, 265, 276, 286, 287,
35, 40, 47 290, 304
AND operation, 53, 73–77 Computational basis state vectors
2n -dimensional, 4
four-dimensional, 2–3
B two-dimensional, 2
Binary search trees, 288, 290, 305
Continued fractions, 223–226
Binary trees, 290–291
Controlled-controlled-NOT gate, The, 60–
Bio-molecular computers, 1
61
Boolean algebras, 53
Boolean formula, 110–111, 113, 131–133, Controlled-G operations, 300, 301
137, 161–162 Controlled-Rk, c operation, 174–176
Boolean variable, 110–117, 119, 130–134, Controlled-Rk,c operation, 175–177
137–145, 147–149, 157, 160–162 Controlled-U operations, 273, 275, 279, 283,
285
Copy machine of one bit, 37–38
C
CCNOT gate, The, 60–65, 72
Classical bit, 1–7, 9–10, 13, 15, 18, 21–22,
24, 27–28, 30–31, 33–34, 38–39, 42, D
45–46, 49–50 Data dependence analysis, 113–114, 142–
Classical computers, 2, 3–4 143
Classical register, 5 Decision problem
Clause, 110–114, 131–134, 137–139, 160,
an example, 271
161
flowchart, 289
Column vector
2n -dimensional, 4 geometrical interpretation, 290, 293
four-dimensional, 2–3 Declaration
two-dimensional, 2 multiple quantum bits, 5, 7
Comment, 5 single quantum bit, 7, 9
Complex nth root of unity Dirac notation, The, 1
definition, 163 Discrete Fourier transform, 163, 167–169,
principal nth root of unity, 164 178
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2021 337
W.-L. Chang and A. V. Vasilakos, Fundamentals of Quantum Programming in IBM’s
Quantum Computers, Studies in Big Data 81,
https://doi.org/10.1007/978-3-030-63583-1
338 Index
E H
Eigenstate, 273, 275, 283, 285, 300, 302 Hilbert space, The
Eigenvalue, 271, 274, 276–277, 279–286, 2n -dimensional, 4
293, 300–301 four-dimensional, 2–3
Eigenvector, 271, 273, 275, 283, 285, 300, two-dimensional, 2
302
Equivalent class moduloN, 238
Euclid’s algorithm, 221–223, 227, 230, 232, I
235, 237, 266, 268 IBM’s quantum computer
Euler’s phi function, 240 simulator with 32 quantum bits, 245, 248,
Even order ofX ModuloN, 238–242 258, 265
Exclusive-NOR (XNOR) operation, 53, 99– the backend ibmqx4, 6–7, 9–10, 12–13,
105 15–17, 19–20, 22–23, 25–26, 28, 30–
Exclusive-OR (XOR) operation, 53, 95–99 31, 33–34, 37, 39, 41–42, 44, 46, 48,
Exponential of a complex number, 163 50–51
Independent-set problem, The
complementary graph G, 295–296
F graph G, 295–296
Factoring, 226, 227 an independent-set, 295–304
Factoring, 219, 226–228, 243, 253–267 a maximum-sized independent set, 295–
Factoring 15 304
conditional multiply-by-2 for 2P (mod
Inverse discrete Fourier transform, 167–169
15), 261–262
Inverse quantum Fourier transform, 179–
conditional multiply-by-4 for 2P (mod
184, 189, 192, 199, 200, 204, 208,
15), 263–264
216
flowchart, 253, 255
initialize quantum registers, 258–260
inverse quantum Fourier transform, 264–
265 M
modular exponentiation 2P (mod 15), Matrix Rk, c , 173–174
257–258 Matrix Rk,c , 173–174
modular exponentiation X P (mod N), Matrix swap, 175
255 Matrix swap, 175
order r of (X= 2) modulo (N= 15), The, Maximal clique problem, The
255–257 clique, 136, 139
quantum superposition to 2P (mod 15), complementary graph, 134
260–261 data dependence analysis, 142–143
read the quantum result, 265–267 graphG, 134
Fast Fourier transform, 163 Grover’s iteration, 159
implement solution space, 144–147
implement the Grover diffusion operator,
G 153–155
Group implement the Oracle, 149–152
associativity, 165 maximum-sized clique, 136
closure, 165 number of vertex, The, 139–142
commutative law, 165–166 Oracle, The, 147–149
identity, 165 quadratic speed-up, 160
inverse, 165–166 quantum search algorithm, 155–160
Grover diffusion operator, The, 123–130, recognize cliques, 136–139
134, 153–155, 159–160 solution space, 143–144
Grover operator, The Measurement
eigenstate, 299 multiple quantum bits, 5, 7
eigenvalue, 293 single quantum bit, 7, 9
matrix, 293 Modern algebra, 53
Index 339