Undergraduate Computational Physics Projects On Quantum Computing
Undergraduate Computational Physics Projects On Quantum Computing
D. Candela
Quantum computing
American Journal of Physics 66, 956 (1998); https://doi.org/10.1119/1.19005
QUANTUM MEASUREMENTS
American Journal of Physics 85, 5 (2017); https://doi.org/10.1119/1.4967925
At the University of Massachusetts, these projects have Many interesting projects can be carried out with N ¼ 3
been carried out by students in a junior-level quantum qubits, and the earlier projects all use N ¼ 3. The 8-element
mechanics course for physics majors. Some of the students state vectors and 8 8 matrices can be written out explicitly,
in this course took an enrichment module on computational leaving some of the thornier computational issues to the later
quantum mechanics. In this module students wrote their own projects. Using N ¼ 3, students can explore one-qubit gates
programs, using their programming languages of preference.
At weekly meetings algorithms were discussed and students
presented results and discussed difficulties they were
encountering. Before working on the quantum computing
projects described here, the students had written programs to
simulate the wave mechanics of a particle in one dimension.2
The prior programming experience of the students varied
widely. All students had taken a required one-semester Fig. 1. Example of a quantum gate array computation. Four qubits are used
course on computational physics using MATLAB. Some (N ¼ 4), shown by the four horizontal lines. The diagram is read from left to
students had no programming experience beyond this right. First the qubit register is initialized to the state jWi ¼ j000bi. (Here
one required course, while others were experienced pro- the quantum state jbi is varied to provide input data to the calculation. For
grammers. The programming platforms chosen by the many other quantum calculations, the initialization state jWi is fixed.) Next
a sequence of quantum gate operations is carried out, denoted by the various
students were, in order of decreasing popularity: Python symbols between the left and right sides of the diagram. Finally the qubits
(with NumPy), MATLAB, Cþþ, and Mathematica. Their are measured, as indicated by the voltmeter symbols on the right side. This
choices were driven mainly by the availability of peer help quantum computer was built by Cai et al., Ref. 6, using photons as the qubits
from other students. and used to solve a system of linear equations.
688 Am. J. Phys. 83 (8), August 2015 http://aapt.org/ajp C 2015 American Association of Physics Teachers
V 688
like the Hadamard gate and the phase-shift gate and two- j0i ¼ j"i; j1i ¼ j#i: (2)
qubit gates like the CNOT. Also, Grover’s quantum search pffiffiffi
can be fully implemented with N ¼ 3. A student completing The significance ffi a superposition state like ðj"i þ j#iÞ= 2
pffiffiof
the N ¼ 3 projects should have learned quite a lot about or ðj"i þ ij#iÞ= 2 is less obvious.
quantum computing, multiparticle quantum states, and quan- An N-qubit register is N of these 2-state systems, consid-
tum measurement. ered together to be one quantum system. Quantum mechan-
The projects with N > 3 are more challenging. As N ics tells us that a system of N particles should be described
increases the matrices become too large to enter manually by a single joint quantum state jWi. For example, a 3-qubit
and must be computer-generated. In this way, an inexpensive register has 2N ¼ 8 basis states,
computer can handle up to about N ¼ 12 qubits. If the addi-
tional step is taken of storing the matrices in sparse form, it j000i ¼ j"ij"ij"i
is possible to reach N 20. j001i ¼ j"ij"ij#i
The last project described is Shor’s factoring algorithm.
j010i ¼ j"ij#ij"i (3)
Shor’s algorithm is much more complicated to understand
and program than Grover’s algorithm. To help students over ..
.
this hurdle, the smallest possible Shor’s-algorithm computa-
tion (using N ¼ 7 to factor 15) is described in great detail. j111i ¼ j#ij#ij#i:
Finally, information is provided so ambitious students can
try Shor’s algorithm with more qubits, enabling factoring of In the basis state j001i, qubits 1 and 2 have Sz ¼ þh=2,
numbers up to about 100. while qubit 3 has Sz ¼ h=2. The general quantum state of a
3-qubit register is a superposition of the eight basis states,
D. Computer projects, programming hints, and exercises jWi ¼ aj000i þ bj001i þ þ gj110i þ hj111i; (4)
This article is structured as eight programming projects,
each preceded by explanatory material. The projects build on where the complex amplitudes satisfy jaj2 þ jbj2 þ þ
each other, with the exception of Project 6 (sparse matrices) jhj2 ¼ 1 for normalization.20 The quantum state will be
which is not required for the later projects. The Appendix stored in the computer as a column vector21 of eight complex
has programming hints based on questions that arose when numbers:
the projects were tried. A few exercises intended for students 2 3
a
are also given. They are not difficult, but they require key 6b7
conceptual points to be absorbed. 6 7
6c7
6 7
6d7
E. Sources for further information jWi ¼ 6 7
6 e 7: (5)
6 7
A comprehensive source on all aspects of quantum infor- 6f 7
6 7
mation is the classic text by Nielsen and Chuang.8 A number 4g5
of shorter texts intended for science and engineering students h
have appeared.9–14 Among the most convenient resources for
students carrying out a short computational module are the Exercise: Compute the largest N for which the quantum
Wikipedia articles on quantum computing,15 Grover’s algo- state of an N-qubit register can be stored on a 2014-era lap-
rithm,16 and Shor’s algorithm.17 All students should learn top computer with 4 GB ¼ 4 109 bytes of RAM. Assume
about the difficulties of building physical quantum com- each complex amplitude a; b… requires 16 bytes of storage.
puters; Ref. 18 is an accessible article current as of late How does the amount of memory necessary change if the
2014. number of qubits is doubled?
II. THE N-QUBIT REGISTER III. THE QUANTUM GATE ARRAY COMPUTER
The bit is the smallest unit of classical information, with The type of quantum computation that will be simulated
two possible values (0 or 1). In a classical computer, a physi- in these projects is shown in Fig. 1. First the N-qubit register
cal system like a tiny capacitor is used to store each bit, with is initialized to a definite quantum state. Next, a series of
different values of the charge on the capacitor used to repre- quantum gate operations is applied to the register. Typically,
sent 0 and 1. For a quantum computer, the bit is replaced by each gate operates on only a few (1–3) of the qubits. Finally,
the qubit, stored by a quantum system with two basis vectors the qubits are measured. To simulate a quantum computer,
in its Hilbert space. An example would be a spin-1/2 particle it is necessary to carry out each of the three stages (initializa-
like an electron, when only the spin can change.19 The gen- tion, quantum gate operations, and measurements) on
eral quantum state of a spin-1/2 system is a quantum state represented in the computer in the form of
Eq. (5).
jWi ¼ aj"i þ bj#i; (1) The initialization step is simple. Most often the N-qubit
register will be initialized to one of its 2N basis states, with
where j"i and j#i are the states with Sz ¼ 6h=2. The all N qubits in states of definite Sz. This is represented by
complex amplitudes a and b obey the normalization con- having one of the 2N amplitudes a; b; c… equal to 1, with the
dition jaj2 þ jbj2 ¼ 1. The Sz states are used to represent rest of the amplitudes equal to zero. For example, the initial
0 and 1: state might be chosen to be
689 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 689
2 3
0 the quantum-mechanical law of Eq. (8). The result will
607 always be one of the eight basis states j000i…j111i and
6 7
607 should be printed out in this form (see Appendix for
6 7
617 hints). For example, a result of j101i shows that the first
jWi ¼ j011i ¼ 6 7
6 0 7: (6) and third qubits were measured to be 1, while the second
6 7 qubit was measured to be zero.
607
6 7 (3) Repeat step 2 many times to see how variable the results
405
are.22,23 It is helpful to make the program list what the
0
most frequent results are.
Another type of state encountered in quantum computing is Once the program is working, try the following:
the “cat state”
• Set the initial state jWi to one of the basis states, for exam-
2 pffiffiffi 3 ple, j011i [Eq. (6)]. With this initial state, every measure-
1= 2
6 7 ment should give the result j011i.
6 0 7 • Set the initial state to the cat state, Eq. (7). Now, at ran-
6 7
6 0 7 dom, either all of the qubits should be 0 or all of the qubits
6 7
1 6 7 should be 1. Thus, each qubit is equally likely to be meas-
6 0 7
jWi ¼ pffiffiffi ðj000i þ j111iÞ ¼ 6 7: (7) ured 0 or 1, but all three qubits are always measured to
2 6 0 7
6 7 have the same value.
6 0 7
6 7 • Set the initial state to an equal superposition of all 2N basis
6 7
4 0 5 states,
pffiffiffi
1= 2
2 3
1
This highly nonclassical state is named for Schr€odinger’s 617
6 7
cat, because it is an equal superposition of two very different 617
classical states. 6 7
1 6 17
jWi ¼ pffiffiffi 6 7: (9)
6
8617 7
IV. MEASUREMENTS OF THE N-QUBIT REGISTER 617
6 7
Usually one measures Sz for each of the N qubits. Each 415
measurement gives Sz ¼ 6h=2. Therefore, one is sure to find 1
the register to be in one of the 2N basis states of Eq. (3), no
matter what the state jWi was before the measurement. The This type of state will prove important later for Grover’s
probability of measuring each basis state is given by the algorithm (Sec. VI) and Shor’s algorithm (Sec. IX). With
square magnitude of the corresponding amplitude, for this state the measured value for each qubit is both random
example and uncorrelated with the other qubits. Thus, all possible
results from j000i to j111i should occur, each with equal fre-
Pðj110iÞ ¼ jh110jWij2 ¼ jgj2 : (8) quency to within statistical fluctuations.
690 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 690
• The operations carried out by the quantum gates must not Usually h has a specified value like p=4 or p, and the symbol
collapse the state of the system the way a measurement for this gate is a box with h in it. Consider the result of
would. applying a phase-shift gate to a superposition of the basis
states:
A physical system that is not measured or otherwise dis-
turbed obeys Schr€odinger’s equation,
a a
If jWi ¼ ; then R^h jWi ¼ ih : (17)
b e b
djWi ^
i
h ¼ HjWi: (10)
dt This shows that the probabilities jaj2 and jbj2 for finding the
In a physical quantum computer,6,24 the Hamiltonian H ^ qubit in the two basis states are unchanged by the phase-shift
must vary with time so that Eq. (10) results in the desired gate, but the relative phase of the two amplitudes is changed.
quantum gate operations. Applying Schr€odinger’s equation Although a quantum phase cannot be measured directly, the
over a time period is always equivalent to applying a unitary phase shift in Eq. (17) can have an effect if further quantum
operator to jWi. This gives the following important rule: gate operations are applied to the qubit before it is measured.
Every quantum gate operation is carried out by applying
some unitary operator to the state vector,25
C. Applying a gate to the N-qubit register
^
jWi ( UjWi; (11) The N-qubit register has 2N basis states, so a quantum gate
† † is represented by a 2N 2N unitary matrix. How is this larger
where U^ U^ ¼ U^ U^ ¼ I^ (the definition of “unitary”) and I^ is matrix computed from the 2 2 matrix of Eq. (12) or Eq.
the identity operator. (16)? The answer is written formally as a tensor product,
denoted by . With N ¼ 3 qubits the operation that applies a
A. The Hadamard gate Hadamard gate to qubit 1 is
One of the most commonly used quantum gates is the ð1Þ
H^ ¼ H^ I^ I:
^ (18)
Hadamard gate, symbolized by a box with an H in it. There
are four Hadamard gates in Fig. 1. A Hadamard gate acts on ð1Þ
The superscript on H^ indicates that the gate operates on
a single qubit and is described by the matrix qubit 1. It is computed as the tensor product of the
Hadamard operator with two copies of the identity operator.
1 1 1
H^ ¼ pffiffiffi : (12) Conceptually, the identity operators “do nothing” to qubits 2
2 1 1 and 3. Similarly, the operators that apply a Hadamard gate to
Note that the symbol H^ is used in this paper for a Hadamard qubit 2 and qubit 3 are, respectively,
^
gate, not the Hamiltonian H. † ð2Þ
Exercise: Show that H^ is unitary. The adjoint H^ of a ma- H^ ¼ I^ H^ I;
^ (19)
trix is found by transposing it and then taking the complex ð3Þ
conjugate. H^ ¼ I^ I^ H:
^ (20)
The basis states are
It is not necessary at this point to fully understand the tensor-
1 0 product notation of Eqs. (18)–(20) (discussed further in Sec.
j0i ¼ ; j1i ¼ : (13)
0 1 VIII). It will suffice to have the following matrices for a
Hadamard gate operating on qubit 1, 2, or 3:
Applying a Hadamard gate to either basis state gives a super- 2 3
position of both basis states, for example, 1 0 0 0 1 0 0 0
6 7
60 1 0 0 0 1 0 0 7
^ ¼ p1ffiffiffi ðj0i þ j1iÞ: 6 7
Hj0i (14) 60 0 1 0 0 0 1 0 7
2 6 7
1 60 0 0 1 0 0 0 1 7
ð 1Þ 6 7
A Hadamard gate can also take a superposition of the basis H^ ¼ pffiffiffi 6 7; (21)
266
1 0 0 0 1 0 0 0 7
7
states and “put it back together” into a single basis state, for 6 0 1 0 0 0 1 0
6 0 77
example, 60 0 1 0 0
4 0 1 0 7 5
1 0 0 0 1 0 0 0 1
H^ pffiffiffi ðj0i j1iÞ ¼ j1i: (15)
2 2 3
1 0 1 0 0 0 0 0
Exercise: Verify Eqs. (14) and (15) by matrix multiplication. 6 7
60 1 0 1 0 0 0 0 7
6 7
6 1 0 1 0 0 0 0 0 7
B. The phase shift gate 6 7
1 6 0 1 0 1 0 0 0 0 7
ð 2Þ 6 7
Another important one-qubit gate is the phase shift gate. H^ ¼ pffiffiffi 6 7; (22)
266 0 0 0 0 1 0 1 0 7
The matrix for this gate is 7
60 0 0 0 0 1 0 1 7
6 7
60 0 0 0 1 0 1 0 7
^ 1 0 4 5
Rh ¼ : (16)
0 eih 0 0 0 0 0 1 0 1
691 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 691
2 3 ð3Þ ð2Þ ð1Þ
1 1 0 0 0 0 0 0 jWi ( H^ H^ H^ jWi: (26)
6 7
6 1 1 0 0 0 0 0 0 7
6 7 The gates are applied to jWi in the same order shown in the
60 0 1 1 0 0 0 0 7
6 7 quantum circuit diagram.26 They appear in reverse order in
ð3Þ 1 6 7
6 0 0 1 1 0 0 0 0 7 Eq. (26) because each successive matrix is applied on the
H^ ¼ pffiffiffi 6 7: (23)
26 6
0 0 0 0 1 1 0 0 7
7 left. As before, the computation is repeated to see what is
6 0 0 0 0 1 1 0 0 7 definite and what varies randomly.
6 7
60 0 0 0 0 0 1 1 7
4 5 • In Fig. 3(a), a Hadamard gate is applied to qubit 2. From
0 0 0 0 0 0 1 1 Eq. (14), this puts qubit 2 in an equal superposition j0i
and j1i. Therefore, the result of the calculation should
Comparing these with Eq. (12), the matrix elements of H^ are vary randomly between the two possibilities, j000i and
distributed around these larger matrices following a definite j010i.
pattern. Similarly, matrices can be written for a phase-shift • In Fig. 3(b), Hadamard gates are applied to qubits 1, 2,
gate on qubit 1, 2, or 3. Here is the matrix for a phase-shift and 3. This leaves each of the three qubits equally likely
gate on qubit 3: to be in the states j0i and j1i, with no correlations between
2 3 the qubits. Knowing the state of qubit 1, for example,
1 0 0 0 0 0 0 0 gives no information about the states of qubits 2 and 3.
6 7 The result of the calculation should vary randomly
6 0 eih 0 0 0 0 0 0 7
6 7 between all eight possibilities, j000i; j001i; …; j111i.
60 0 1 0 0 0 0 0 7
6 7 Putting the N-qubit register into an equal superposition of
6 0 0 0 eih 0 0 0 0 7
ð3Þ 6 7 all 2N basis states by applying a Hadamard gate to each
R^h ¼ 6 7: (24)
60 0 0 0 1 0 0 0 7 qubit is one of the important building blocks of many
6 7 quantum algorithms.
6 0 0 0 0 0 eih 0 0 7
6 7 • In Fig. 3(c), two successive Hadamard gates are applied to
60 0 0 0 0 0 1 0 7
4 5 the same qubit. As shown above, the Hadamard gate splits
0 0 0 0 0 0 0 eih a single quantum amplitude into two but it can also put
ð3Þ ð3Þ
two quantum amplitudes back together into one. In this
The matrix for R^h follows the same pattern as H^ , with case, the second Hadamard undoes the effect of the first
four copies of the original 2 2 matrix arrayed along the di- one, so the result of the calculation should always be
agonal of the 8 8 matrix. j000i.
• In Fig. 3(d), again two Hadamard gates are applied to the
D. Programming project 2: First full quantum same qubit. But now the phase-shift gate R^h with h ¼ p is
computations applied between the two Hadamard gates, shown by a box
with p in it. As in the previous case, the result of the calcu-
Set up and run the four quantum computations shown in lation is perfectly definite, but now the result is always
Fig. 3. In Fig. 3(a), jWi is initialized to the state j000i, then a j001i. The net effect of the three gates has been to flip
Hadamard gate is applied to qubit 2 by multiplying jWi on qubit 3 from j0i to j1i. Equations (14) and (15) show why
ð2Þ
the left by H^ : this works. This computation shows that using a phase-
shift gate to change the phase of quantum amplitudes can
ð2Þ
jWi ( H^ jWi: (25) indeed change the results of the calculation.
ð2Þ With these programs, a complete quantum computer has
Equation (22) gives the matrix for H^ . A more complex
been simulated. Section VI shows how, with a few additions,
computation is performed by successively multiplying the
Grover’s quantum search can be carried out.
quantum state on the left by operators for the specified gates.
For example, the gates in Fig. 3(b) are carried out by the
operation VI. GROVER’S QUANTUM SEARCH ALGORITHM
Consider a classical database like a phone book with D
names, each followed by a phone number. If a phone number
is given, how many entries of the phone book must be looked
at to find the corresponding name? Looking through the
phone book one might be lucky and find the phone number
on the first try, or unlucky and need to look at all D entries.
The average number of entries consulted will be halfway
between these extremes, D=2.
Here is another classical search problem: A logical func-
tion of N Boolean (T or F, meaning True or False) varia-
bles like Fig. 2 is specified, and it is known that the
function is T only for one specific combination of the
inputs (this is true for Fig. 2). To find this combination
among the D ¼ 2N possibilities for the inputs, one goes
Fig. 3. Four different three-qubit calculations to try. through the D possibilities until the one is found that yields
692 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 692
T. Again, on average it is necessary to go through D=2 of since j110i is the seventh basis vector.28 The oracle O^ is
the possibilities. clearly unitary: when multiplied by its adjoint it gives
Using Grover’s quantum search algorithm,8,16,27 the aver- the identity matrix. The operator J^ in Fig. 4 is like the ora-
age number of times the pdatabase
ffiffiffiffi must be consulted is cle, except that the 1 element is always in the first
reduced from D=2 to ðp=4Þ D. This is an enormous savings position:
if the size D of the database is large. However, there is a
catch: The database or logic function must be put in the form 2 3
1 0 0 0 0 0 0 0
of a quantum oracle. A classical oracle returns a one-bit 6 7
answer (yes or no) in response to a question (“Is the 437th 6 0 1 0 0 0 0 0 07
6 7
entry in the phone book 545–0111?” or “Is the logic function 6 0 0 1 0 0 0 0 07
6 7
T for inputs TFTFFTFT?”). A quantum oracle must accept a 6 0 0 0 1 0 0 0 07
^ 6 7
quantum superposition of questions, and return the corre- J¼6 7: (28)
sponding quantum superposition of answers. 6 0 0 0 0 1 0 0 07
6 7
By using a superposition, it is possible to ask all possible 6 0 0 0 0 0 1 0 07
6 7
questions of the oracle simultaneously. It might seem that a 6 0 0 0 0 0 0 1 07
4 5
quantum oracle only needs to be consulted once to find the 0 0 0 0 0 0 0 1
correct question, but measuring the qubits collapses the
quantum state so the complete superposition cannot be deter-
mined. Nevertheless, Grover showed how to get the needed Here is how Grover’s algorithm works. The first set of
information from a quantum oracle in far fewer tries than Hadamard gates in Fig. 4 creates an equal superposition of
would be needed classically. all 2N basis states. This superposition is passed to the oracle,
effectively asking all 2N questions at the same time. The ora-
A. Grover’s algorithm:Details cle flips the sign of the amplitude for the correct question.
The “Grover diffusion operator” in Fig. 4 is designed to con-
Figure 4 shows the quantum circuit for Grover’s algorithm. vert this phase difference, which is unmeasurable, into a
The size of the database that can be searched is D ¼ 2N , magnitude difference that will show up when the qubits are
where N is the number of qubits as usual. The diagram is measured.16 It might seem better to use an oracle that enco-
drawn for N ¼ 3 ) D ¼ 8. In addition to Hadamard gates, des its output in a directly usable form,
an operator O^ for the quantum oracle and a special operator J^
are needed. The oracle knows what the right question is to 2 3
give a “yes” or T response, and it functions as follows: 0 0 0 0 0 0 0 0
6 7
• If the oracle is given one of the D 1 wrong questions, it 60 0 0 0 0 0 0 07
6 7
returns its input state unchanged. For example, say the 60 0 0 0 0 0 0 07
6 7
right question (represented as a binary number) is 110. 60 0 0 0 0 0 0 07
0 6 7
^
Then, since 100 is not the right question, Oj100i ¼ j100i. O^ ¼ 6 7: (29)
60 0 0 0 0 0 0 07
• Conversely, if the oracle is given the right question, it 6 7
60 0 0 0 0 0 0 07
returns its input multiplied by 1. Continuing the example 6 7
^
above, this means Oj110i ¼ j110i. 60 0 0 0 0 0 1 07
4 5
Therefore the quantum oracle matrix is like the identity 0 0 0 0 0 0 0 0
matrix, except that it has 1 as the diagonal element for the
0
correct question. When that question is 110, the matrix is If O^ is applied to a superposition of all basis states, the
2 3 result is immediately the basis state for
0
the correct question
1 0 0 0 0 0 0 0 j110i. Unfortunately, a matrix like O^ is not unitary and so
6 7 does not represent a possible operation in a quantum
60 1 0 0 0 0 0 07
6 7 computer.
60 0 1 0 0 0 0 07
6 7
60 0 0 1 0 0 0 07
^ 6 7
O¼6 7; (27) B. Programming project 3: Implement Grover’s
60 0 0 0 1 0 0 07
6 7 quantum search
60 0 0 0 0 1 0 07
6 7
6 0 0 0 0 0 0 1 0 7 Implement the Grover search algorithm for N ¼ 3 qubits,
4 5
as diagrammed26 in Fig. 4. The needed matrices are given
0 0 0 0 0 0 0 1 above. The
pffiffiffiffiffioptimum
ffi number of repetitions should be close
to ðp=4Þ 2N but of course it must be an integer.
• Set the oracle so the correct question is 110 as above, and
with the optimum number of repetitions of the oracle plus
Grover diffusion block. Run the computation many times.
The measured result should be j110i more than 90% of
the time.16
• Change the oracle so a different question is correct.
• Change the number of repetitions. The percentage of time
that the result is correct should decrease with either more
Fig. 4. Quantum circuit diagram for Grover’s search algorithm. or fewer repetitions.
693 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 693
2 3
VII. GATES OPERATING ON MORE THAN ONE 1 0 0 0 0 0 0 0
QUBIT 6 7
60 1 0 0 0 0 0 07
6 7
In Fig. 1, there are six gates that connect two horizontal 60 0 0 0 0 0 1 07
6 7
lines. Each of these gates therefore operates simultaneously 60 0 0 0 0 0 0 17
ð2;1Þ 6 7
on two qubits. Consider the second two-qubit gate in Fig. 1, C^NOT ¼6 7: (33)
60 0 0 0 1 0 0 07
a “controlled NOT” or CNOT gate. This gate has a solid dot 6 7
on qubit 3 (numbering qubits from 1 at the top) connected by 60 0 0 0 0 1 0 07
6 7
a link to a 丣 symbol on qubit 2. The NOT function changes 60 0 1 0 0 0 0 07
4 5
j0i to j1i, and j1i to j0i. This CNOT gate applies a NOT 0 0 0 1 0 0 0 0
function to qubit 2 if and only if qubit 3 is in the state j1i.
That is, qubit 3 controls whether or not a NOT function is
applied to the qubit 2. Some other types of controlled gates The algorithm used to generate these matrices is given in
can be seen in Fig. 1. In each case, a solid dot on the control- Sec. VIII.
ling qubit is linked to a symbol indicating a conditional
action on another qubit. The matrix for a CNOT gate is A. Programming project 4: Computations using CNOT
2 3 gates
1 0 0 0
60 1 0 07 Try the quantum computations shown in Fig. 6, using the
6 7 CNOT operations of Eqs. (32) and (33).
C^NOT ¼ 6 7: (30)
40 0 0 15
(a) In this diagram, there is a Hadamard gate on qubit 2,
0 0 1 0 before qubit 2 is used to control qubit 3. The
Hadamard gate puts qubit 2 in a superposition of j0i
Exercise: Verify that this matrix carries out the CNOT and j1i [Eq. (14)]. When this superposition is used to
function of Fig. 5(a). It helps to write the four basis vectors control qubit 3, that qubit will also end up in a superpo-
j00i…j11i down the side and across the top of the matrix. sition. When the computation is carried out multiple
Given any one-qubit gate U, ^ it is possible to construct a
times, the output should vary randomly between j000i
two-qubit gate in which qubit 1 controls whether or not the and j011i. The measured values for qubits 2 and 3 are
operation U^ is applied to qubit 2. This “controlled-U” gate
both random, but they are correlated with each other.
has the symbol shown in Fig. 5(b) and has the matrix29
This is called an entangled state of qubits 2 and 3.
2 3 (b) This computation creates a cat state. A Hadamard gate
1 0 0 0
60 1 is used to put qubit 2 into a superposition of j0i and
6 0 0 77 j1i. Then two CNOTs are used to correlate qubits 3
C^U ¼ 6 7: (31)
4 0 0 U0;0 U0;1 5 and 1 with qubit 2. The measured results should vary
0 0 U1;0 U1;1 randomly between j000i and j111i, as they did with
the cat state of Eq. (7).
In the N ¼ 3 computer any of the three qubits can be the (c) This duplicates an earlier calculation. First a Hadamard
controlling bit and either of the remaining two qubits can be gate puts qubit 2 into a superposition of j0i and j1i.
the controlled bit, so there are six possible CNOT gates. It Then a second Hadamard gate converts the superposi-
will suffice to write out two of the six possibilities, first with tion back into the pure state j0i. With this computation,
qubit 2 controlling qubit 3, the measured result is always j000i.
2 3 (d) This is like the previous diagram, but now a CNOT has
1 0 0 0 0 0 0 0 been used to observe the state of qubit 2 when it is in a
6 7 superposition of j0i and j1i. Qubit 3 is flipped by the
60 1 0 0 0 0 0 07
6 7 CNOT to agree with the state of qubit 2, and when
60 0 0 1 0 0 0 07
6 7 qubit 3 is measured it reveals what the state of qubit 2
60 0 1 0 0 0 0 07
^ ð2;3Þ 6 7 was between the two Hadamard gates. Observing the
C NOT ¼ 6 7; (32)
60 0 0 0 1 0 0 07 state of qubit 2 destroys the quantum coherence of the
6 7
60 0 0 0 0 1 0 07
6 7
60 0 0 0 0 0 0 17
4 5
0 0 0 0 0 0 1 0
Fig. 5. (a) Symbol for a CNOT gate, where the first bit is the controlling bit.
(b) Symbol for the general controlled-U gate. The CNOT is a controlled-U
with U ¼ NOT. Fig. 6. Quantum computations using CNOT gates. See text for details.
694 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 694
ð2Þ ð2Þ
superposition, so the second Hadamard gate cannot put column of H^ is H6;4 . Using Eq. (35), this element is com-
qubit 2 back into the pure state j0i. The measured puted as
results for qubits 2 and 3 will be random and uncorre-
lated (equal probabilities for observing j000i; j001i; ð2Þ ð2Þ 1
H6;4 ¼ H½110;½100 ¼ d11 H1;0 d00 ¼ H1;0 ¼
pffiffiffi ; (36)
j010i; j011i). 2
The CNOT gate in Fig. 6(d) does not “do anything” to in agreement with Eq. (22). Notice how 6 and 4 have been
qubit 2, which always ends up in the same state it started in. written in binary as 110 and 100.
Thus, the destruction of coherence cannot be attributed to a Exercise: Use the bottom line of Eq. (35) to write out the
direct action of the CNOT on the thing being observed, qubit ð2Þ
2; rather, it is built into the way quantum mechanics 8 8 matrix for H^ and check that it agrees with Eq. (22).
describes the states of multiparticle systems. It helps to write ½pqr ranging from [000] to [111] down the
Optional exercise: In Fig. 6(d), the state of qubit 2 is not side of the 8 8 matrix, and ½p0 q0 r 0 ranging from [000] to
really observed until the qubits are measured. Figure out [111] across the top.
how to undo the observation of qubit 2 before the qubits are Shor’s algorithm will also require two-qubit gates.
measured by adding one more gate, and verify that with this Consider a CNOT gate in an N ¼ 3 system in which qubit
modification the results are the same as for Fig. 6(c). 2 controls qubit 1, as used in Fig. 6(b). The equivalent of
Figure 6(d) also illustrates a peculiarity of quantum com- Eq. (35) is
putation. Classically the only effect of the CNOT would be ð2;1Þ
on qubit 3. In a quantum calculation, the CNOT might be CNOT½pqr;½p0 q0 r0 ¼ CNOT½qp;½q0 p0 drr0 : (37)
used specifically for its effect on qubit 2.
This concludes the projects using N ¼ 3 qubits. In As with Eq. (35), we have Kronecker deltas for every qubit
Sections VIII and IX the number, the number of qubits is that the CNOT does not operate on, in this case only the third
increased, resulting in more challenging programming tasks. qubit with indices r; r0 . Since the second qubit with indices
This is a necessary prelude to trying Shor’s factoring algo- q; q0 is the controlling qubit for the CNOT, it is used as the
rithm, which requires at least N ¼ 7. first index for the CNOT matrix. For example, the element in
ð2;1Þ
the seventh row and third column of C^ , which is29 NOT
VIII. INCREASING THE NUMBER OF QUBITS ð2;1Þ
CNOT 6;2 , is
The challenge here is to make N as large as possible. It is
ð2;1Þ ð2;1Þ
addressed in two stages. In Sec. VIII A, the techniques used CNOT 6;2 ¼ CNOT½110;½010
above are extended to N > 3 simply by using bigger vectors
and matrices. On a 2012-era personal computer, it was possi- ¼ CNOT½11;½10 d00 ¼ CNOT 3;2 ¼ 1; (38)
ble to reach N ¼ 12 before the matrices became too large to
store in the computer. In Sec. VIII C, sparse-matrix techni- in agreement with Eq. (33). As above, 6 and 2 have been
ques are used to increase N beyond this limit by taking written in binary as 110 and 010, but now after the correct
advantage of the many zeros in the matrices. bits have been picked out according to Eq. (37) the binary
numbers 11 and 10 are converted back to 3 and 2 to index
A. Matrices for gates with an arbitrary number of qubits the C^NOT matrix, Eq. (30).
Now the computer program must generate the 2N 2N B. Programming project 5: Handle an arbitrary number
matrices for gates, and a more compact notation is needed to of qubits
represent these matrices. Let p and q be binary digits, each 0
or 1. A 2 2 matrix M^ can be represented in the form Mp;q , Program Grover’s search for an arbitrary number of qubits
N. Check
pffiffiffiffiffiffi that the optimum number of Grover iterations is
^ M0;0 M0;1 ðp=4Þ 2N , for N > 3. Then increase N as much as possible.
M¼ : (34)
M1;0 M1;1 Table I shows results obtained on a 2012-era personal
computer with 8 GB of RAM. Each additional qubit
Exercise: Show that the 2 2 identity matrix is Ip;q ¼ dpq , increased both the memory needed and the time to complete
the Kronecker delta. the calculation by a factor of about 4. With 13 qubits, the
Using N ¼ 3 to illustrate, let ½pqr be a 3-digit binary num- memory of the PC was exceeded.
ber, with each of p, q, and r equal to 0 or 1. Using this nota-
tion, the tensor product of Eq. (19) can be translated as C. Using sparse matrices
follows:
Many of the elements in the matrices of Eqs. (21)–(28) are
ð2Þ ð2Þ zero. Matrices like this that have only a small fraction of
H^ ¼ I^ H^ I^ () H½pqr;½p0 q0 r0 ¼ dpp0 Hq;q0 drr0 : nonzero elements are called sparse. It is wasteful of com-
(35) puter memory and time to store and compute with all these
zeros. Some computer languages have sparse-matrix func-
The Hadamard matrix operates on the indices q; q0 corre- tions built in. This section explains how to implement sparse
sponding to the second qubit, while Kronecker deltas give matrices without built-in functions, but either route can be
the identity operators for all the other qubits. pursued to increase N.
Here is an example: Let us assume that matrix indices start The 2N 2N matrix for a one-qubit gate [Eqs. (21)–(23)]
from zero,29 so the element in the seventh row and fifth has at most two nonzero elements per row. Similarly, the
695 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 695
Table I. Grover’s algorithm with full matrices. IX. SHOR’S QUANTUM FACTORING ALGORITHM
qubits Memory to Time to Time to do Pick two large prime numbers P1, P2, and compute their
N store matrices compute matrices Grover calculation product C ¼ P1 P2 ; for example, 241 683 ¼ 164603.
Commonly used methods of data encryption depend on the
10 104 MB 53 s 4.3 s
fact that it takes a lot of computer power to find the factors
11 450 MB 3.9 min 37 s
P1, P2 if only C is known (using bigger numbers than this
12 1.96 GB 17.6 min 3.8 min
example, of course). Shor’s algorithm17,30 can factor a large
13 Failed … …
composite (i.e., not prime) number C in far fewer steps than
any known classical algorithm, potentially rendering some
current encryption methods useless.
2N 2N matrix for a two-qubit gate has at most four nonzero
elements per row; CNOT gates [Eqs. (32)–(33)] have only
A. Shor’s algorithm step by step
one nonzero element per row. Therefore, the Hadamard ma-
trix of Eq. (21) can be stored as To state Shor’s algorithm, these two concepts are needed:
2 3 • The greatest common divisor of two integers gcdðp; qÞ is the
1 1
6 0; pffiffiffi ; 4; pffiffiffi 7 largest integer that divides both p and q with zero remainder.
6 2 2 7 For example, gcdð12; 18Þ ¼ 6 and gcdð12; 35Þ ¼ 1.
6 7
6 7 • The modular congruence p qðmod CÞ means p q is
6 1; p1ffiffiffi ; 5; p1ffiffiffi 7
6 7 an integer multiple of C. For example, 35 13ðmod 11Þ.
6 2 2 7
6 7 This can also be written pðmod CÞ ¼ qðmod CÞ where
6 7
6 7 “pðmod CÞ” means the remainder when p is divided by C.
ð1Þ 6 7
H^ ¼ 6 7: (39) So 35ðmod 11Þ ¼ 2, and 13ðmod 11Þ ¼ 2.
6 7
6 7
6 7 Given a composite integer C, the following steps will find a
6 7 nontrivial factor of C (nontrivial means other than 1 or C):31
6 7
6 7
6 7 (1) Check that C is odd and not a power of some smaller in-
6 7
6 7 teger. If C is even or a power, then a factor of C has been
6 1 1 7
4 5 found and we are done.
3; pffiffiffi ; 7; pffiffiffi
2 2 (2) Pick any integer a in the range 1 < a < C.
(3) Find gcdða; CÞ. If by luck the gcd is greater than 1, then
The first row ofpEq.
ffiffiffi (39) indicates that p
the
ffiffiffi nonzero elements a factor of C has been found (the gcd) and we are done.
in row 0 are 1= 2 in column 0 and 1= 2 in column 4.29 (It (4) Find the smallest integer p > 1 such that ap 1ðmod CÞ.
may be more convenient to store the integer column indices (5) If p is odd, or if p is even and ap=2 1ðmod CÞ, go
and complex matrix elements in two separate arrays.) A di- back to step 2 and pick a different a.
agonal matrix can be stored even more compactly, if desired, (6) The numbers P6 ¼ gcdðap=2 61; CÞ are nontrivial factors
since it is known that the column index equals the row index of C.
for the nonzero elements. Example: C 5 15
D. Programming project 6: Use sparse matrices to (1) 15 is odd and is not a power of a smaller integer, so
increase N proceed.
(2) Arbitrarily pick a ¼ 7.
Write functions to generate sparse matrix structures, or (3) gcdð7; 15Þ ¼ 1, so proceed.
figure out how to use built-in sparse matrix functions. (4) Try p’s starting with 2:
Then carry out Grover’s algorithm with N as large as • 72 ¼ 49 and 49ðmod 15Þ ¼ 4 6¼ 1.
possible. • 73 ¼ 343 and 343ðmod 15Þ ¼ 13 6¼ 1.
Table II shows results obtained with the same PC as used • 74 ¼ 2401 and 2401ðmod 15Þ ¼ 1, so the result of this
for Table I. Both the amount of memory used and the time step is p ¼ 4.
needed to compute the matrices were drastically less than (5) p ¼ 4 is even, and 74=2 ¼ 49. Since 49 6 1ðmod 15Þ, it
when full matrices were used. Extrapolating from this table, is not necessary to go back and pick a different a.
the storage limit of 8 GB would be reached at N ¼ 21 and the (6) Pþ ¼ gcdð50; 15Þ ¼ 5 and P ¼ gcdð48; 15Þ ¼ 3 are the
calculation would take a week to finish. sought-for factors of 15.
One should imagine implementing Shor’s algorithm for a
Table II. Grover’s algorithm with sparse matrices.
very large number C. A classical computer can quickly
qubits Memory to Time to Time to do
determine if C is a power, and quickly compute the gcd of
N store matrices compute matrices Grover calculation two large numbers using Euclid’s algorithm. So the only
thing for which a quantum computer is needed is step 4.
10 1.82 MB 0.172 s 3.6 s
11 4.4 MB 0.48 s 11.4 s B. The quantum part of Shor’s algorithm: Period-finding
12 9.9 MB 1.54 s 34 s
13 22 MB 5.4 s 1.78 min Step 4 is called “period finding,” because the function
14 46 MB 23 s 5.4 min f ðxÞ ¼ ax ðmod CÞ is periodic with period p. In the example
C ¼ 15, a ¼ 7, f(x) has period 4:
696 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 696
f ð0Þ ¼ 70 ðmod 15Þ ¼ 1 Figure 8 shows the quantum circuit for the N ¼ 7 Shor’s-
1 algorithm quantum computation, based on the design of
f ð1Þ ¼ 7 ðmod 15Þ ¼ 7
Vandersypen et al. In addition to elements like Hadamard
2
f ð2Þ ¼ 7 ðmod 15Þ ¼ 4 gates familiar from earlier projects there are two new
f ð3Þ ¼ 7 3
ðmod 15Þ ¼ 13 elements:
f ð4Þ ¼ 7 4
ðmod 15Þ ¼ 1
• There are three controlled phase shift gates, all in the
IQFT block. These are phase shift gates as used earlier,
f ð5Þ ¼ 75 ðmod 15Þ ¼ 7 but now controlled by another qubit.
f ð6Þ ¼ 7 6
ðmod 15Þ ¼ 4 • There are three quantum gates using the x-register bits
‘2 ; ‘1 ; ‘0 to control the f-register bits m3 ; m2 ; m1 ; m0 . The
.. gate controlled by bit ‘k conditionally multiplies the f-
.: (40) k
register by a2 ðmod 15Þ. Together the three gates multiply
Figure 7 shows the quantum circuit used to find p. The qubit the f-register by a‘0 þ2‘1 þ4‘2 ðmod 15Þ ¼ ax ðmod 15Þ ¼ f ðxÞ,
register is divided into two parts, the x-register with L qubits as required for step 2 of the period-finding calculation.32
initialized to the state j0…0i and the f-register with M qubits
initialized to the state j0…01i. The steps in the calculation
are: D. Programming project 7: Implement Shor’s algorithm
with seven qubits
(1) Apply a Hadamard gate to each of the L qubits in the
x-register, as indicated by the box with H L . This puts Set up the quantum computation diagrammed in Fig. 8
the x-register in a superposition of all 2L possible x and use it to factor C ¼ 15. The computation should work for
values. all values of a that pass steps 2 and 3 of Shor’s algorithm as
(2) Based on the value in the x-register, multiply the f-regis- listed in Table III. The measured values of the seven qubits
ter by ax ðmod CÞ. This leaves the f-register containing labeled on the right side of Fig. 8 should be used to print out
f(x). the two numbers x~=2L ¼ ð~ x 2 x~1 x~0 Þ=2L and f ¼ ðf3 f2 f1 f0 Þ.
(3) Measure the f-register. It doesn’t matter if the f-register For example, if the measured output is j0110100i then
is measured at this point, or later when the x-register is f ¼ 01002 ¼ 4 from the last four qubits and x~ ¼ 1102 ¼ 6
measured, or never, but the explanation of Fig. 7 is sim- from the first three qubits in reversed order, so
pler if the f-register is measured as shown. x~=2L ¼ 6=8 ¼ 0:75. Here’s what should happen:
(4) Perform an inverse quantum Fourier transform (IQFT) • The measured f should always be one of the p different
on the x-register. Since the Fourier transform of a func- values listed in Table III. For example, when a ¼ 7, f
tion has peaks at the frequencies present in the function, should vary randomly among 1, 7, 4, and 13. This is just a
the IQFT will make it possible to find the period ¼ 1/fre- check, as the f value is not used.
quency of f(x). The IQFT is discussed in more detail • The measured x~=2L should come out close to s / p for
below. some integer s. This is the actual output of the program,
(5) Measure the output x~ of the IQFT. Shor proved that the used to find the period p.
measured x~=2L equals approximately s / p, where s is an
unknown integer. This information is used to find p. For this project it is necessary to compute 27 27 matrices
For example, if x~=2L ¼ 0:32 13 ¼ 26 ¼ 39 it can be for the new gates in Fig. 8:
guessed that p is one of 3, 6, 9,…. These are checked to Controlled phase-shift gates: The 4 4 controlled
see which one satisfies ap 1ðmod CÞ and is therefore phase-shift gate is given by Eq. (31) with the phase shift
the true period p. matrix [Eq. (16)] used as the U submatrix. Then the
full 128 128 matrix is constructed using an equation like
Eq. (37).
C. Shor’s algorithm with seven qubits Gates to compute f(x): The 128 128 matrices for the
gates that compute f(x) are permutation matrices, which
The smallest numbers satisfying step 1 of the algorithm
means that each column is all 0’s except for one 1, and the 1
(odd composite integers that are not powers of another inte-
is in a different row in each column [like Eqs. (32)–(33)].
ger) are C ¼ 15; 21; 33; 35; 39; …. To date, physical quan-
Using C ¼ 15:
tum computers using Shor’s algorithm have factored the
numbers 15 and 21. Vandersypen et al.24 made physical (1) Compute A0 ¼ aðmod 15Þ; A1 ¼ a2 ðmod 15Þ, and A2
quantum computers with N ¼ 7 using nuclear spins as the ¼ a4 ðmod 15Þ.
qubits. They used Shor’s algorithm to factor C ¼ 15 using (2) Start with the first controlled gate in Fig. 8, controlled by
L ¼ 3 and M ¼ 4. ‘0 . In each column k ¼ 0…127 of the matrix,29 the
Fig. 7. The quantum circuit for the period-finding part of Shor’s algorithm. The top line marked with a slash and L denotes a group of L qubits. Similarly the
bottom line denotes a group of M qubits.
697 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 697
Fig. 8. Quantum circuit to implement Shor’s algorithm with N ¼ 7 qubits.
entries are all 0’s except for a 1 in some row j. To com- Similarly, using the x-register to control the f-register affects
pute j: the measured value of the x-register. Figure 9 shows how
(a) Write the column k as a 7-bit binary number k ¼ this works.
‘2 ‘1 ‘0 m3 m2 m1 m0 as on the left side of Fig. 8.
(b) If ‘0 ¼ 0, then j ¼ k. This puts the 1 in column k
• Figure 9(a) represents the quantum state jWi after the
on the diagonal, as in an identity matrix. Hadamard operation on the x-register and the multiplica-
tion of the f-register by f(x). Due to the Hadamard opera-
(Conceptually if ‘0 ¼ 0, the gate does nothing to
tion, every possible x value is equally likely. Due to
the f-register.)
(c) If ‘0 ¼ 1, then express the four-bit binary number the controlled-multiply operation, for every x value the
f-register contains one of the p possible f(x) values. (In
m3 m2 m1 m0 as an integer f. If f 15 (i.e., if f C),
Fig. 9(a), these are 1, 7, 4, 13.) Thus, jWi at this point in
then again set j ¼ k.
(d) If ‘0 ¼ 1 and f < 15, compute a new f value f 0 the calculation is an equal superposition of many terms,
each represented by one of the solid dots in Fig. 9(a). The
¼ A0 f ðmod 15Þ. Write this in binary as
dashed curves in Figs. 9(b) and 9(c) show that if the
f 0 ¼ m03 m02 m01 m00 . Then j is given by the 7-bit binary
f-register is measured, the result will be 1, 4, 7, or 13,
number j ¼ ‘2 ‘1 ‘0 m03 m02 m01 m00 . [Conceptually if
while if the x-register is measured, the result will be any
‘0 ¼ 1 then the f-register is multiplied by
integer.
A0 ðmod 15Þ.] • Now measure the f-register, and assume the result happens
(3) Use the same procedure to compute the other two con-
to be 7. According to basic quantum principles, a measure-
trolled gates, substituting ‘1 ; ‘2 for ‘0 and A1, A2 for A0.
ment causes the state jWi to collapse so that it agrees with
Optional exercise: (a) Show that a permutation matrix as the measured value. Therefore after measuring f ¼ 7 only
defined above is unitary. (b) (subtle) Show that the procedure the dots with f ¼ 7 [circled in Fig. 9(a)] remain in the
given above results in a permutation matrix, and therefore a superposition.
unitary matrix. • After measuring f, all possible values of the x-register are
This is all that is needed to try out Shor’s algorithm. Table no longer equally likely. Rather, P(x) is nonzero only for
IV shows the results of 100 quantum measurements when the x’s in the circled dots, so P(x) has become a comb
this was tried with a ¼ 7. The measured x~=2L values were of peaks separated by the period p ¼ 4 [solid curve in
always close to 04 ; 14 ; 24, or 34. Since these are multiples of 14, Fig. 9(c)].
they give a guess p ¼ 4 for the period (the correct answer). • The IQFT takes the Fourier transform of this comb-like
function. Because the function has period 4, its Fourier
E. How the quantum part of Shor’s algorithm works transform will contain the frequency x ¼ 1=4 and its har-
monics x ¼ 2=4 and 3/4. In a discrete FT, the frequencies
In Figs. 7 and 8, computations are carried out on the are read out as x ¼ x~=2L . Therefore when the x-register is
f-register, then no use is made of this register. A similar measured the probability will have peaks at x~=2L ¼ s=4 ¼
situation occurred earlier with Fig. 6(d): using qubit 2 to s=p for all integer harmonic numbers s, which is the
control qubit 3 affected the measured value of qubit 2. claimed result.
698 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 698
Fig. 9. How the quantum period-finding calculation works. See text for details.
More on the IQFT: In a classical discrete FT or IFT on gate carries out a reversible operation, the QFT can be
an input register with L values, the FT is applied to L input inverted by reversing the time order in which the gates are
numbers giving L output numbers. By contrast, here there applied: compare the IQFT in Fig. 8 with the bottom diagram
are 2L different x values represented by the 2L quantum in Fig. 10.
amplitudes in the superposition in the x-register, and the
IQFT operates on all 2L numbers at once. For the large prime G. Programming project 8: Implement Shor’s algorithm
numbers used in encryption, L could be several thousand and with N > 7
a classical Fourier transform on 2L numbers (or even storing
2L numbers classically) is completely impossible. Program Shor’s algorithm for arbitrary N, and see how big
a number can be factored. Table VI shows a few results
obtained in this way. In each case, the L used was smaller
F. Implementing Shor’s algorithm for arbitrary N than is listed in Table V. In the results for C ¼ 39, increasing
By using N > 7 it is possible to factor numbers C greater L from 6 to 8 gave measured x~=2L values much closer to the
than 15. The total number of qubits is N ¼ L þ M. The f- expected values 16 ; 26 ; 36… ¼ 0.1667, 0.3333, 0.5000,….
register holds binary values of ax ðmod CÞ, so M must satisfy
2M C. To be confident17 of finding the period p, the size L H. Using continued fractions to guess the period
of the x-register should satisfy 2L C2 . Table V shows the
required L and M. However, as seen above Shor’s algorithm The output of the period-finding calculation is x~=2L s=p
can work with smaller L than this table implies: for C ¼ 15, where s is an unknown integer, and is used to guess possible
L ¼ 3 ! N ¼ 7 was used. values for p. With a physical quantum computer, one would
Figure 10 shows how to build the QFT for arbitrary L be trying to find p after running the computation just once.
from Hadamard and controlled phase shift gates. For Shor’s Trial values for p are typically derived from x~=2L by using
algorithm, the inverse quantum Fourier transform (IQFT) is continued fractions. Having set up the computer to factor
needed. Quantum computing provides a way to invert a func- C ¼ 87 and choosing a ¼ 13, from Table VI one might get
tion that is not available classically. Since every quantum the single measured result x~=2L ¼ 0:6426. A continued-
fraction expansion34 of this number is
699 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 699
Fig. 10. (a) Recursive definition of the quantum Fourier transform (QFT) for L qubits in terms of the QFT for L 1 qubits. See Ref. 8 or 33 for a derivation.
(b) QFT’s for L ¼ 1, 2, and 3 from the recursive definition.
C, a classical computer can check which of the trial p’s satis- For such problems, even modest input size can result in an
fies ap 1ðmod CÞ. This gives the correct p ¼ 14, from impossibly long classical computation time.
which the factors of 87 are computed to be Factoring numbers is thought to lie in NP but not P and
gcdð1314=2 61; 87Þ ¼ 29; 3. thus to be exponentially difficult on a classical computer.
Conversely, the time needed to factor a number on a quan-
tum computer using Shor’s algorithm is only polynomial in
X. POSTSCRIPT: IS QUANTUM COMPUTATION the input size.36 In this sense, at least, it seems that gate-
TRULY POWERFUL? array quantum computation as described in this article is
inherently more powerful than classical computation.
As the projects in this paper illustrate, the classical resour- Finally, there are other models of quantum computation15
ces needed to simulate a quantum calculation grow exponen- (adiabatic quantum computing, quantum annealing, quantum
tially with the number of qubits (Tables II and VI). This emulation…) that may also offer possibilities for exponen-
suggests that a quantum computer should be exponentially tially exceeding the capabilities of any classical computer.
more powerful than a classical computer with the same num- Realizing these vast computational powers in the real world
ber of bits. However, there are classical algorithms to search will depend on continued progress in building physical quan-
databases and factor numbers that are far more efficient than tum computers.18
simulating Grover’s algorithm and Shor’s algorithm, making
the classical vs. quantum comparison subtle.35
Computer scientists classify problems by the way the clas- APPENDIX: PROGRAMMING HINTS
sical computation time t increases with the size S of the input
(e.g., digits in the number to be factored). The complexity 1. Binary numbers and array indices
class P consists of problems for which t grows no faster than
a power of S. The class NP includes P but also includes In some computer languages (Python, Cþþ,…), array
more difficult problems thought to require time that grows indices start from 0. If the variable psi is an 8-element array
faster than any power of the input size, for example, t / eS . representing the quantum state jWi in Eq. (5), psi[0] ¼ a
39 6 6 12 10 6 8 MB 5.8 s 0.8281 (16), 0.0 (16), 0.5 (15), 0.3281 (14), 0.6719 (11), 0.1719 (8)…
39 8 6 14 10 6 44 MB 72 s 0.5 (18), 0.0 (18), 0.1680 (16), 0.3320 (13), 0.8320 (11), 0.6680 (11)…
87 9 7 16 13 14 198 MB 32 min 0.6426 (9), 0.1426 (9), 0.0723 (8), 0.8574 (6), 0.5 (6), 0.2148 (6)…
700 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 700
and psi[7] ¼ h. In these languages, to find the basis state 23 () ð0; 0; 1; 0; 1; 1; 1Þ: (A2)
represented by psi[j], convert j into a binary number. For
example, j ¼ 6 corresponds to j110i. Here is a way to implement Eq. (35), to create the 2N 2N
In other computer languages (MATLAB, Mathematica,…), ðnÞ
matrix H^ that applies the gate H^ to qubit n [in Eq. (35),
array indices start from 1. In these languages, Eq. (5) implies N ¼ 3 and n ¼ 2]. The program should be checked by print-
psi(1) ¼ a and psi(8) ¼ h, so to find the basis state rep- ing out the N ¼ 3 Hadamard matrices [Eqs. (21)–(23)].
resented by psi(j), convert j 1 into a binary number.
Now j ¼ 7 corresponds to j110i. (i) Start with a D D array initialized to all zeros, where
D ¼ 2N .
ðnÞ
(ii) For each element Hj;k with j ¼ 0…D 1 and
2. Convention used for numbering qubits
k ¼ 0…D 1, convert j and k into lists of 1’s and 0’s
In a gate-array diagram like Fig. 1, qubit 1 is the top line giving binary representations of these numbers.29
and qubit N is the bottom line. For a basis state like j011i, (iii) Find the elements of each list at location n. These are
qubit 1 is the leftmost bit (0), and qubit N is the rightmost the numbers q; q0 in Eq. (35).
bit (1). It follows that basis states for an N-qubit register (iv) To enforce the Kronecker deltas in Eq. (35), only put
are read from top to bottom in a gate-array diagram. Thus in a nonzero element in the big array when the bit lists
Fig. 1 the register is initialized to the state j000bi. for j and k are equal to each other at all locations other
than n. When this condition is met, put into the loca-
tion j, k of the big array the q; q0 element of the
3. Hints for project 1: Measurement of the N-qubit
1-qubit Hadamard array [Eq. (12)].
register
To carry out a quantum measurement on the state jWi in 6. Hints for project 7: Shor’s algorithm with N 5 7 qubits
Eq. (5),
For the three gates implementing modular multiplication,
(1) Pick a random number r with 0
r < 1 (a built-in
print out the sum of each row and the sum of each column,
function in most languages).
and verify that every sum is 1.
(2) Carry out the following steps (r is fixed during these
Start without the IQFT, to check that the first part of the
steps; q is a new variable):
calculation is working. The measured x should be random,
(i) Set q ¼ jaj2 . If r < q the result is j000i; otherwise pro-
and the measured f should be f(x). For example, with a ¼ 7
ceed to the next step. when x~ ¼ 6 ¼ 1102 ; x ¼ 0112 ¼ 3 so from Eq. (40) f should
(ii) Set q ( q þ jbj2 . If r < q the result is j001i; otherwise be f ð3Þ ¼ 13.
proceed to the next step.25
(iii) Set q ( q þ jcj2 . If r < q the result is j010i; otherwise
proceed to the next step.:::ðthree steps omittedÞ::: 7. Hints for project 8: Shor’s algorithm with N > 7 qubits
(vii) Set q ( q þ jgj2 . If r < q the result is j110i; otherwise
The recursive definition of the QFT of Fig. 10 must be
the result is j111i. implemented for arbitrary L, with the gates in reverse order
These steps are designed to give Eq. (8): The probability to give the IQFT. Use Fig. 10 to write a routine (two nested
of measuring j000i is jaj2 , etc. They can be programmed as FOR loops) to print out and check the names of the gates for
above with seven IF statements, or a more elegant program any L. Then add the actual gates to the computation. The
can be devised that works for any N. output for L ¼ 3 should be:
Hadamard on 1
4. Hints for projects 2–4: Full quantum computations 1 controls pi/2 on 2
with N 5 3 qubits 1 controls pi/4 on 3
Hadamard on 2
To carry out Fig. 3(a) (a Hadamard gate applied to qubit
2 controls pi/2 on 3
2), multiply the columnð2Þvector Wk that represents jWi on the Hadamard on 3
left by the matrix for H^ given in Eq. (22):
a)
Electronic mail: [email protected]
X
7
ð2Þ
1
Although tensor products are mentioned, it is not assumed that the reader
Wj ( Hj;k Wk : (A1) is familiar with and able to compute tensor products. Therefore, an alterna-
k¼0 tive method is given based on bitwise manipulation of matrix indices
(Sec. VIII A). Similarly, a bitwise method is used to compute the modular-
This equation29 carries out the operation shown abstractly by arithmetic gates needed for Shor’s algorithm (Sec. IX D). A good under-
Eq. (25). All of the quantum computations in this paper are standing of binary numbers should suffice for both situations.
2
With four weekly meetings (one third of the semester) on quantum com-
done by carrying out multiple operations of the form of Eq. puting, students completed the three-qubit projects including Grover’s
(A1) in the order shown in the quantum gate array algorithm and a variable amount of the more advanced material.
diagram.22,26 Consistently reaching Shor’s algorithm would require additional meetings.
3
Working code for these projects in Mathematica is available by email
request to the author.
5. Hints for project 5: Using an arbitrary number of 4
“List of QC simulators,” Quantiki, <http://www.quantiki.org/wiki/
qubits N List_of_QC_simulators>.
5
Brian Hayes, “Programming your quantum computer,” Am. Sci. 102,
It is necessary to find or write functions to convert integers 22–25 (2014).
into lists of their binary digits, and vice versa. For example, 6
X.-D. Cai et al., “Experimental quantum computing to solve systems of
for N ¼ 7 the conversions for the integer 23 are linear equations,” Phys. Rev. Lett. 110, 230501-1–5 (2013).
701 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 701
7 24
Nanyang Xu et al., “Quantum factorization of 143 on a dipolar-coupling Lieven M. K. Vandersypen et al., “Experimental realization of Shor’s
nuclear magnetic resonance system,” Phys. Rev. Lett. 108, 130501-1–5 quantum factoring algorithm using nuclear magnetic resonance,” Nature
(2012). 414, 883–887 (2001).
8 25
Michael A. Nielsen and Isaac L. Chuang, Quantum Computation and The left-arrow notation, e.g., a ( a þ 1, is used to denote the assignment
Quantum Information (Cambridge U.P., Cambridge, 2000). of a new value to the program variable a. In most computer languages this
9 is written a¼aþ1.
Michel Le Bellac, A Short Introduction to Quantum Information and
26
Quantum Computation (Cambridge U.P., Cambridge, 2006). Gates can be moved along their lines as long as the order of operations on
10 each line is unchanged. So for Fig. 3(b) the three Hadamard gates could be
N. David Mermin, Quantum Computer Science: An Introduction
(Cambridge U.P., Cambridge, 2007). applied in any order.
11 27
Giuliano Benenti, Giulio Casati, and Giuliano Strini, Principles of Lov K. Grover, “A fast quantum mechanical algorithm for database
Quantum Computation and Information—Vol.1: Basic Concepts (World search,” arXiv:quant-ph/9605043 (1996), <http://arxiv.org/abs/quant-ph/
Scientific, Singapore, 2004). 9605043>.
28
12
Eleanor Rieffel and Wolfgang Polak, Quantum Computing: A Gentle One can object that to write Eq. (27), we need to know the answer to the
Introduction (MIT Press, Cambridge, MA, 2011). search problem. For a useful application of Grover’s algorithm, the oracle
13
Anirban Pathak, Elements of Quantum Computation and Quantum would be a quantum calculation that can be constructed without knowing
Communication (Taylor and Francis, Boca Raton, FL, 2013). the correct answer. For example, the oracle could implement the logic cir-
14
David McMahon, Quantum Computing Explained (Wiley, Hoboken, NJ, cuit of Fig. 2 using quantum gates. See Ref. 10, pp. 88–89, for further
2008). discussion.
29
15
“Quantum computer,” Wikipedia, <http://en.wikipedia.org/wiki/ This is written for array indices starting at zero as in Python or Cþþ (see
Quantum_computer>. Appendix).
30
16
“Grover’s algorithm,” Wikipedia, <http://en.wikipedia.org/wiki/ Peter W. Shor, “Polynomial-time algorithms for prime factorization and
Grover’s_algorithm>. discrete logarithms on a quantum computer,” arXiv:quant-ph/9508027
17
“Shor’s algorithm,” Wikipedia, <http://en.wikipedia.org/wiki/ (1995), <http://arxiv.org/abs/quant-ph/9508027>.
31
Shor’s_algorithm>. See Ref. 8 or 17 for proof. The branch of mathematics that studies primes,
18
Elizabeth Gibney, “Quantum computer quest,” Nature 516, 24–26 (2014). factoring, and other aspects of the integers is called number theory. It is
19
The particle could have a spatial wave function WðxÞ that stays fixed, for well known from number theory that steps 1–6 factor the number C.
Shor’s contribution was to prove that step 4 can be done much more
example.
20 quickly on a quantum computer than classically.
Although electrons are identical fermions, it is not necessary to antisym- 32
In physical quantum computers, gates typically operate on 1–3 qubits at a
metrize jWi if the electrons are not allowed to exchange places with one
time, making this part of the period-finding calculation more complicated
another.
21 than the IQFT.
In most computer languages jWi will simply be an eight-element array of 33
“Quantum Fourier transform,” Wikipedia, <http://en.wikipedia.org/wiki/
complex numbers. In matrix multiplications jWi must be treated as a col-
Quantum_Fourier_transform>.
umn vector: when multiplied on the left by a square matrix, the result is 34
To compute a continued-fraction expansion like Eq. (41), invert the num-
another column vector. ber, subtract off the integer part (which is the next number in the continued
22
If desired, the program can compute jWi once just before the measurement fraction), invert the fractional part, subtract off the integer part, and so on.
and then use this jWi as the input for multiple measurements. This would 35
Troels F. Rønnow et al., “Defining and detecting quantum speedup,”
not be possible in a physical quantum computer since measuring jWi Science 345, 420–424 (2014).
would collapse the quantum state. 36
This is a simplified description of computational complexity classes. For
23
The probability for any measurement result can be computed from Eq. (8) example, it has not been proven that NP is actually a different class than
without making simulated measurements. However, there is pedagogical P. Based on Shor’s algorithm, factoring is thought to lie in the class BQP
value in having the simulated quantum system act like a real quantum sys- (larger than P but not equal to NP) of problems that can be solved with
tem. Seeing the results from repeating the quantum measurement a small high probability in polynomial time on a quantum computer. See
number of times is closer to the operation of a physical quantum “Computational complexity theory,” Wikipedia, <http://en.wikipedia.org/
computer. wiki/Computational_complexity_theory>, and Ref. 8, Sec. 3.2.
702 Am. J. Phys., Vol. 83, No. 8, August 2015 D. Candela 702