Introduction To Classical and Quantum Computing
Introduction To Classical and Quantum Computing
Wong
www.thomaswong.net
For my students.
Preface
Dear Reader,
This textbook for newcomers who are interested in quantum computing as a po-
tential career, but who may not be ready for advanced books or courses. The only
prerequisite for this book is trigonometry, also called pre-calculus. You are not ex-
pected to have taken advanced math beyond that, and you are not expected to have
experience with programming. So, if you are an advanced high school student or a
beginning university student, this textbook is for you.
That said, this book is not merely a conceptual overview of quantum computing.
I will teach the math and programming skills that may be missing. Since you are
interested in quantum computing as a potential career, I want to equip you with the
skills you will need for more advanced topics.
If you are more advanced, and especially if you have already studied linear al-
gebra, then you may find this textbook too elementary. For a more mathematically
rigorous introduction to quantum information science, I refer you to Quantum Com-
putation and Quantum Information by Michael Nielsen and Isaac Chuang, affec-
tionately called “Mike and Ike,” like the chewy, fruit candy with the same name. It
is the standard advanced text, and for good reason.
I hope this textbook will help you realize that you can do it, that you can under-
stand quantum computing. I hope it will inspire you to study quantum computing
more deeply, and I hope that some of you might even choose quantum computing
as a career. If so, I look forward to calling you colleagues and learning from your
discoveries.
This textbook stemmed from an introductory special-topics course that I taught
at Creighton University, and I thank each class of students for sharing the journey of
developing and refining the course content. I must also thank those who have taught
me quantum computing in both formal and informal roles. I could not have done it
without you.
Tom Wong
vii
Contents
ix
x Contents
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Chapter 1
Classical Information and Computation
Computers have come a long way. The first computers were people, not machines,
who performed calculations. Indeed, the term “computer” dates as far back as the
early 1600’s, centuries before the digital age. Human computers persisted into mod-
ern history, with NASA, for example employing people to compute launch trajec-
tories for the space program and other scientific endeavors through the 1960’s. Of
course, mechanical and then electronic computers have since taken over, evolving
from massive machines that filled entire rooms, to personal computers on our desk-
tops, to smaller and smaller devices. Now, nearly everyone has a electronic com-
puter in their pocket—a smartphone—that is more powerful than the computers that
landed people on the moon. Computers have become so polished that we can use
and even program them without understanding how they work at a fundamental
level. That’s not a bad thing. It has allowed computers to become tools for more and
more people. My physics students can perform numerical computations and solve
scientific problems without needing to understand bits and bytes.
One day, quantum computing will get to this point of accessibility, where we can
use and program them without worrying about their details. But we are not quite
there, yet. In their development, quantum computers are where classical computers
were decades ago. Their inner working still matter, and to understand these inner
workings, it is helpful to understand the inner workings of regular, classical com-
puters. So, in this chapter, we will look at the basics of classical computing. If you
have studied the fundamentals classical computing or electrical engineering, this
may be review for you. Even so, the topics may be worth seeing again because they
have been carefully selected for their quantum analogues in later chapters.
Furthermore, quantum computing is not developing in isolation of classical com-
puting. Many of the design decisions for quantum computers stem out of what is
done with classical computers. Without knowing classical computing, some aspect
of quantum computing may seem arbitrary. A rudimentary understanding classical
computing makes it easier to understand quantum computing.
The following table lists many of the concepts we will be covering in this book.
In this chapter, we will cover the “Classical” column, beginning with bits. Then,
we will perform computation on these bits using logic gates and include discus-
1
2 1 Classical Information and Computation
sions about universal and reversible logic gates. The math of classical computing is
boolean algebra, and we can program classical circuits using hardware description
languages. We will then look at classes of problems that are easy or hard for com-
puters, and we will end this chapter with the prospect that quantum computers may
be significantly faster than classical computers at some tasks. In the next chapter
and through the rest of the book, we will fill in the quantum computing column of
the table.
Concept Classical Quantum
Fundamental Unit Bit Qubit
Gates Logic Gates Unitary Gates
Gates Reversible Sometimes Always
Universal Gate Set (Example) {NAND} {H, T, CNOT}
Programming Language (Example) Verilog OpenQASM
Algebra Boolean Linear
Error Correcting Code (Example) Repetition Code Shor Code
Complexity Class P BQP
Extended Church-Turing Thesis Supports Possibly Violates
1.1 Bits
The term bits has become fairly commonplace, where many people will state that
bits are zeros and ones. Whether you already knew that does not matter. The point
is, have we taken a moment to consider what this actually means? What are bits,
really? Are they just numbers? Why are they important for computers? Let us begin
our journey by exploring bits deeply.
1.1.1 Coins
Consider a coin, such as the United States one-cent penny from 2016, which features
former President Abraham Linclon as “heads” and the Union shield as “tails.”
Assuming that coins do not balance on their edges, a single coin lying on a flat
surface either has heads facing up or tails facing up. Let us call these two possible
conditions, or states, heads (H) and tails (T).
If we have two coins, there are four possible states: Both coins can be heads
(HH), the first can be heads and the second can be tails (HT), the first can be tails
1.1 Bits 3
and the second can be heads (TH), or both can be tails (TT). That is, the possible
states are:
HH, HT, TH, TT.
Since the first coin has two possible states (heads or tails), and the second coin has
two possible states (heads or tails), there are 2 × 2 = 22 = 4 possible states for the
two coins.
Adding a third coin, there are now eight possible states. They could all be heads,
some mixture of heads and tails, or all tails. Listing all the permutations, the possible
states are now
Since each of the three coins has two possible states, there are 2 × 2 × 2 = 23 = 8
possible states for three coins.
Generalizing this, if we have n coins, the possible states range from all heads,
through a mixture of heads and tails, to all tails:
1.1.2 Dice
It has six possible outcomes, the numbers 1 through 6. So, the possible states of a
die are
1, 2, 3, 4, 5, 6.
Next, say we have two dice. Each die can take values 1 through 6, so both could
be 1, the first could be 1 and the second could be 2, and so forth. If the first die is 3
and the second is 5, let us write the configuration as (3, 5). Then, listing all of these,
the possible states of two dice are
4 1 Classical Information and Computation
(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6),
(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6),
(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6),
(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6),
(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6),
(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6).
Since each die has six possible states, there are 6 × 6 = 62 = 36 possible states for
the two die.
Following the pattern, with three dice, there are 63 = 216 possible states. Listing
these would take too much space. Generalizing, with n dice, there are 6n possible
states.
Exercise 1.2. How many possible states do (a) four dice have? (b) five dice? You do not need to
list the states, just how many there are. A calculator may be useful.
Now, how much information can coins and dice carry? Say I am trying to com-
municate the colors of the rainbow, which in the United States are typically listed
as red, orange, yellow, green, blue, indigo, and violet.1 These seven colors can be
represented, or encoded by the possible configurations of three coins or two dice:
1 In American elementary schools, many students memorize this by taking the first letter of each
color and combining them into the acronym ROY G. BIV.
1.1 Bits 5
distinguish between six states (1 through 6). Since two states is the fewest number
of states that can be distinguished, a coin carries the smallest amount of information
possible. This leads us the following idea:
Something with two states carries the
smallest amount of information possible.
Exercise 1.3. Some board games use a twenty-sided die. How many twenty-sided die does it take
to encode the seven colors of the rainbow?
Exercise 1.4. How many (a) coins and (b) six-sided dice would it take to represent the 26 letters
of the English alphabet? Ignore upper and lowercase, spaces, punctuation, etc., so there’s only 26
letters total.
Many physical systems only have two states. We already discussed coins, which
can be heads or tails. Another example is a light switch, which can be “off” or
“on.” As another example, information is stored on optical discs (e.g., CDs, DVDs,
and Blu-ray discs) using a laser that burns holes into the disc, called “pits.” For
example, one of my students took a zoomed-in picture of a DVD using an atomic
force microscope, shown below:
Image credit: Jeffrey Y. Wong, Creighton University, taken August 24, 2018
The picture shows microscopic holes and trenches burned into the disk, i.e., the pits.
Where there is no hole is called a “land.” So, the two states are whether there is a pit
or a land, and to read the disc, a laser shines on the disc and detects whether there is
a pit or a land.
Physical systems with more than two possible states can be treated as only having
two if we simply ignore the rest of the possible states. For example, if I have an
electronic circuit, then many different voltage levels are possible.2 In conventional
2 Voltage is the amount of electrical pressure pushing charges in the circuit, but you do not need to
know this.
6 1 Classical Information and Computation
computers, however, we typically only use two values, 0 Volts and 5 Volts, so we
effectively only have to states.
All of these examples have, or effectively have, just two states, called by dif-
ferent names: heads/tails, off/on, pits/lands, and 0V/5V. Rather than using so many
different names, it is often easier to use generic names for the two states so that we
can describe the state without regard for the underlying physical system. Mathemat-
ically, it is convenient to use the binary digits 0 and 1. This is summarized in the
table below
Physical System States
Coin Heads Tails
Switch Off On
Disc Pit Land
Voltage 0V 5V
Binary Digit 0 1
With this convention, regardless of the convention, we can just refer to the two
possible states as 0 and 1, the binary digits.
Since binary digits are used so much in computing, an abbreviation for binary
digit was invented: the bit. So, a bit is just a binary digit, which can be 0 or 1, which
represent the states of any physical system with two states. Since systems with two
states carry the smallest amount of information possible, a bit carries the smallest
amount of information possible:
1.1.5 Binary
Previously, when we had three coins, we wrote the eight possible states as
Now, replacing heads and tails with the bits 0 and 1, the eight possible states are
now written as
000, 001, 010, 011, 100, 101, 110, 111.
In other words, we can write the state in terms of numbers, and numbers are useful
because we can use math to describe and manipulate them.
What kind of numbers are these states? They are binary numbers, or base 2
numbers. These are also called binary strings or bit strings. For example, if we
have five bits, one possible state is
11010 or 110102 ,
1.1 Bits 7
where the subscript of 2 can be included to clarify that it is a binary (base-2) number.
We pronounce this as “one one zero one zero,” and optionally say “base 2” after-
ward. It is not pronounced “eleven-thousand ten” because it is not a regular, decimal
(base-10) number. Actually, what decimal number does it correspond to?
To figure this out, let us first remind ourselves how normal decimal numbers
work. Consider the number “six-thousand one-hundred seventy-four” (6174). It has
a six in the thousands place, literally meaning there are six thousands, a one in the
hundreds place, meaning six hundreds, a seven in the tens place, meaning seven
tens, and a four in the ones place, meaning four ones. That is,
110102 = 1 · 24 + 1 · 23 + 0 · 22 + 1 · 21 + 0 · 20
= 1 · 16 + 1 · 8 + 0 · 4 + 1 · 2 + 0 · 1
= 26.
So, we have one in the sixteens place, one in the eights place, zero in the fours
place, one in the twos place, and zero in the ones place, and the binary number
11010 corresponds to the decimal number 26 twenty-six.
In this example 110102 , the leftmost bit contributes 16 to the number, the most
of any bit. For this reason, the leftmost bit is called the most significant bit. Simi-
larly, the rightmost bit can only contribute 1 to the number, and for this reason, the
rightmost bit is called the least significant bit.
We can also count in binary. To understand how, let us think about how we nor-
mally count from zero to one-hundred. For clarity, we write the leading zeros. To
begin, the rightmost digit increments from 0 to 9:
000, 001, 002, 003, 004, 005, 006, 007, 008, 009, . . .
The rightmost digit has reached its maximum value, so for the next number, it rolls
over from 9 to 0, and the middle digit increments by 1, yielding 010. Continuing,
010, 011, 012, 013, 014, 015, 016, 017, 018, 019, . . .
Again, the rightmost digit has reached its maximum value, so for the next num-
ber, it rolls over from 9 to 0, and the middle digit increments by 1, yielding 020.
Continuing,
020, 021, 022, 023, . . . , 098, 099, . . .
8 1 Classical Information and Computation
The rightmost digit has again reached its maximum value, so for the next number,
it rolls over from 9 to 0, and the middle digit needs to increment by 1. But it has
also reached its maximum value, so it also rolls over from 9 to 0, and the left digit
is incremented, yielding 100.
We can apply the same procedure to count in binary. Say we have a bit string
of length 3. It starts with 000, and we increment the rightmost digit, yielding 001.
Incrementing again, the rightmost bit has reached its maximum value (since a bit
can only be 0 or 1), so it rolls over from 1 to 0, and the middle bit is incremented,
yielding 010. Incrementing again, we have 011. Incrementing again, the rightmost
bit rolls over from 1 to 0, the middle bit also rolls over from 1 to 0, and the leftmost
bit increments from 0 to 1, yielding 100. Continuing this procedure, we can count
from 000 to 111, which is counting in decimal from 0 to 7:
Binary Decimal
(Base 2) (Base 10)
000 0
001 1
010 2
011 3
100 4
101 5
110 6
111 7
Exercise 1.5. Convert the following binary numbers (base 2) to decimal numbers (base 10):
(a) 101112 .
(b) 110010102 .
Exercise 1.6. Convert the following decimal numbers (base 10) to binary numbers (base 2):
(a) 42.
(b) 495.
Exercise 1.7. Base-16, commonly called hexadecimal, is another frequently used number system
in computing. The sixteen digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F. So the letter A is
ten in decimal, B is eleven in decimal, . . . , and F is fifteen in decimal. For example, converting the
hexadecimal number F2A to decimal,
(a) Convert the hexadecimal number 3B7C to a decimal (base 10) number.
(b) Convert the hexadecimal number FF to a binary (base 2) number. (So two hexadecimal num-
bers can represent eight bits.)
(c) HTML uses hexadecimal to encode colors using the RGB color model. RGB stands for the
(additive) primary colors red, green, and blue, and by adding together different amounts of
their light, the other colors can be produced. [From painting, you may be familiar with the
1.1 Bits 9
(subtractive) primary colors, red, yellow, and blue.] The amount of red, green, and blue ranges
from 0 to 255, with 0 being none of the color, and 255 being the full amount of the color. This
range of 0 to 255 corresponds to the hexadecimal numbers 00 through FF. An HTML color
code uses six hexadecimal numbers, like FA10E4, with the left two digits (FA) corresponding
to the amount of red, the middle two digits (10) corresponding to the amount of green, and
the right two digits (E4) corresponding to the amount of blue. This particular mix of colors
results in a bright pink. Convert the hexadecimal numbers FA, 10, and E4 to decimal.
Exercise 1.8. Negative numbers can be encoded in binary using two’s complement, where the most
significant bit is negative, while the remaining bits are positive. For example, in two’s complement,
110102 = 1 · (−24 ) + 1 · 23 + 0 · 22 + 1 · 21 + 0 · 20
= 1 · (−16) + 1 · 8 + 0 · 4 + 1 · 2 + 0 · 1
= −6.
Binary Decimal
(Two’s Complement) (Base 10)
000 ?
001 ?
010 ?
011 ?
100 ?
101 ?
110 ?
111 ?
1.1.6 ASCII
Computers store information using bits (0’s and 1’s), but in our world, we use often
store information and communicate using text (letters, punctuation, etc.). How do
we bridge this divide? How do we encode text using bits?
Historically, computers encoded letters, numbers, symbols, and special com-
mands (like carriage return or newline) using the American Standard Code for Infor-
mation Interchange, commonly abbreviated as ASCII (pronounced ass-key). ASCII
uses 7 bits, so they have 27 = 128 possible states from 0000000 through 1111111. Of
these, ninety-five of the bitstrings encode printable characters, and they are shown
in Table 1.1. The remaining thirty-three bitstrings encode non-printable characters,
like the “escape” key.
For example, the following binary string encodes the text “Tom.”
| {z } 1101111
1010100 | {z } = Tom
| {z } 1101101
T o m
So, if I want to send you my name, I can just send you these twenty-one bits.
10 1 Classical Information and Computation
Table 1.1: Printable ASCII characters (glyphs) and their binary and decimal encod-
ings.
In modern times, there are many more characters to encode. There are other lan-
guages with different alphabets or characters, plus emoji and other symbols. So,
more than 7 bits are needed to encode all of them. The most common modern stan-
dard is UTF-8, or the Unicode Transformation Format, and it uses up to 32 bits (for
232 = 4 294 967 296 states). The first 128 bitstrings in UTF-8 are the ASCII charac-
ters.
Exercise 1.9. Write your first name as an ASCII bit string.
1.2 Logic Gates 11
In the previous section, we introduced bits, the fundamental unit of classical in-
formation. We can encode whatever information we would like using bits, such as
words, characters, and even pictures. In this section, we explore how to manipulate
bits so that we can compute using them.
We manipulate bits using what are called logic gates, which take one or more
bits as inputs and depending on the input, it outputs one or more bits. Let us explore
the simplest examples of logic gates next, which manipulate a single bit. Then, we
will learn about logic gates that act on two bits. Following this, we will discuss what
logic gates physically, and explain how they can perform all computations.
The simplest logic gates take one bit as input and then outputs one bit, which we
can draw as a circuit diagram:
This circuit is read left to right. The input bit on the left travels along the line or wire
into the gate, which I have drawn as a generic box. A bit comes out of the gate on
the right, traveling along the line, and it is the output.
Since the input is a single bit, it can only be a zero or a one. We can list these two
possibilities in a table called a truth table:
Input Output
0 ?
1 ?
Depending on which gate we have, the outputs will be different, so I have used
question marks as placeholders for now. How many possible outputs are there? Well,
there are two outputs, and each output can be 0 or 1, so there are 2 × 2 = 4 possible
outputs. Hence, there are four possible single-bit gates, which we describe now.
• The identity gate does nothing to the bit: 0 remains 0, and 1 remains 1. The
identity gate is sometimes depicted by a triangle:
AA
A A 0 0
1 1
In our circuit diagram, we have labeled the input bit as A. Since it goes through
the identity gate (the triangle) unchanged, the output on the other side is also A.
Or as a wire, A comes in, nothing happens, and A comes out. Above, we also
12 1 Classical Information and Computation
filled in the truth table so that the input and output are both simply A. The first
row of the truth table indicates that if the input is 0, the output is 0. The second
row indicates that if the input is 1, the output is 1.
Since the identity gate does nothing, we often omit the triangle and just draw a
longer wire:
A A
Again, this is read left-to-right, so the input bit A moves through the wire and
comes out the other side as the output, unchanged.
The identity gate is sometimes called the buffer gate.
• The NOT gate flips a bit from 0 to 1, or 1 to 0. Its circuit diagram is a triangle
with a small circle:
AA
A A 0 1
1 0
The input bit A goes through the NOT gate from the left, and the resulting output
is A, where the overline denotes negation (i.e., the flipped or opposite bit). The
behavior of this circuit is completely described by the above truth table, and it
shows that when 0 is the input, 1 is the output, and vice versa.
In many texts, A is also denoted ¬A, where ¬ means negation. The NOT gate is
sometimes called the inverter gate.
• The always 0 gate always outputs 0, regardless of input. It does not have a
standard circuit diagram since it is not commonly used, but its truth table is
A0
00
10
The first line of the truth table indicates that when the input is 0, the output is 0.
The second line indicates that when the input is 1, the output is 0.
• The always 1 gate always outputs 1, regardless of input. It does not have a
standard circuit diagram since it is not commonly used, but its truth table is
A1
01
11
The first line of the truth table indicates that when the input is 0, the output is 1.
The second line indicates that when the input is 1, the output is 1.
These are all four possible logic gates with a single input and a single output.
1.2 Logic Gates 13
A two-bit logic gate takes two bits as input, say A and B. Although a two-bit logic
gate can have multiple outputs, the simplest case just has one output. So, its circuit
diagram and truth table would look like
A B Output
A 0 0 ?
Gate Output 0 1 ?
B 1 0 ?
1 1 ?
This truth table has four rows because there are four possible inputs: A and B can
both be 0, A can be 0 and B can be 1, A can be 1 and B can be 0, or both can be
1. Note we listed these in numerical order, since 00, 01, 10, and 11 are the decimal
numbers 0, 1, 2, and 3. Writing them in numerical order is the convention.
Depending on the gate, the outputs will be different, so they have question marks
in the above truth table for now. Since each of the four outputs can be 0 or 1, there
are 24 = 16 possible two-bit gates. Next, we discuss five of the most important ones.
• The AND gate outputs 1 only when both input bits are 1. Its circuit diagram and
truth table are
A B AB
A 0 0 0
AB 0 1 0
B 1 0 0
1 1 1
In this circuit diagram, two bits A and B go through the AND gate, resulting in
AB. Note that standard multiplication works here: 0 · 0 = 0, 0 · 1 = 0, 1 · 0 = 0,
and 1 · 1 = 1. In many texts, AB is also denoted A ∧ B. This is called the AND
gate because it outputs 1 when A and B are 1. Or, in the language of logic, if we
take 0 to be false and 1 to be true, then AB is true when A and B are both true.
We can take the AND of more than two bits by nesting AND gates. For example,
with three bits we have the following circuit diagram and truth table:
A BC AB ABC
0 0 0 0 0
0 0 1 0 0
A 0 1 0 0 0
AB
B 0 1 1 0 0
ABC 1 0 0 0 0
C
1 0 1 0 0
1 1 0 1 0
1 1 1 1 1
To find the above truth table, we can first calculate AB, which is the output of
the first AND gate. Then, to get the final output, we can take the AND of AB
14 1 Classical Information and Computation
with C, and we see that this is precisely ABC, the AND of all three bits, because
the output is 1 only when all three inputs are 1. Then, for simplicity, we often
draw this with a single AND gate, but with three inputs:
A
B ABC
C
With more inputs, additional AND gates can be nested, or more input lines can
be draw into a single AND gate.
• The OR gate, which outputs 1 if either input (or both) is 1. Its circuit diagram
and truth table are
A B A+B
A 0 0 0
A+B 0 1 1
B 1 0 1
1 1 1
Following many texts, we denote the OR of A and Bs as A + B, although this is
not actually addition since 1 + 1 = 1. In some texts, it is also denoted A ∨ B.
• The Exclusive OR (XOR) gate, which outputs 1 when only one input is one, but
not both. Its circuit diagram and truth table are
A B A⊕B
A 0 0 0
A⊕B 0 1 1
B 1 0 1
1 1 0
We write the XOR of A and B as A ⊕ B, i.e., a plus sign with a circle around it.
Mathematically, ⊕ is addition modulo 2, meaning we take the remainder after
dividing by 2. You are probably familiar with modulo in other contexts, like a
circle has 360◦ , so 370◦ is equivalent to 10◦ . Mathematically, we would write
this as 370◦ (mod 360◦ ) = 10◦ (mod 360◦ ), meaning when you divide 370◦ by
360◦ , you get a remainder of 10◦ . With ⊕ we take the remainder after dividing
by 2, so we have
0 (mod 2) = 0
1 (mod 2) = 1
2 (mod 2) = 0
3 (mod 2) = 1
4 (mod 2) = 0
5 (mod 2) = 1
..
.
1.2 Logic Gates 15
Exercise 1.10. Consider the following gate that inverts the inputs before passing them into an OR
gate, sometimes called a negative-OR gate:
A
A+B
B
Exercise 1.11. Consider the following gate that inverts the inputs before passing them into an AND
gate, sometimes called a negative-AND gate:
A
AB
B
Exercise 1.12. Consider the XOR of three bits A, B, and C, which can either be two two-bit XOR
gates strung together, or a single three-bit XOR gate:
16 1 Classical Information and Computation
A
A⊕B
B
A⊕B⊕C
C
A
B A⊕B⊕C
C
(a) What is the truth table for this circuit?
(b) When there is an even number of 1’s in the input (we call this even parity), what is the output?
(c) When there is an odd number of 1’s in the input (we call this odd parity), what is the output?
Exercise 1.13. What is the truth table for the following circuit diagram?
A
B
Output
So far, we have discussed what logic gates do, but we have not discussed what logic
gates are. Let us address that here. While there are many different ways to make
logic gates, but the most common way is using electric circuits. I do not assume that
you know circuits, so we will start slowly.
To begin, here is a drawing of a circuit that consists of a battery, two switches, a
light bulb, and some wires to connect them:
1.2 Logic Gates 17
In the above drawing, the two switches correspond to inputs A and B. They are both
in the “on” position, so both inputs are 1. In this case, electricity is able to flow
through the circuit, coming out from the left side of the battery (the side of the
battery with the bump, or the positive side), down through the switches, right across
the bottom wire, up through the light bulb, and back into the right side of the battery
(the flat side of the battery, or the negative side). Electricity flows from the positive
side of the battery to the negative side, and this counter-clockwise flow is indicated
by arrows on the wires. Since electricity is flowing through the light bulb, it turns
on. The light bulb is the output, and so the output is 1 when both inputs A and B are
1.
Next, let us turn off only the first switch while keeping the second switch on, so
A = 0 and B = 1:
In the above drawing, the electricity is unable to flow in the circuit because the first
switch is turned off. This disconnects the connections, so the electricity does not
have a path to flow. It does not matter that switch B is still on. So, the light bulb is
off, and the output is 0.
18 1 Classical Information and Computation
Let us try the opposite. We turn on switch A and turn off switch B, so A = 1 and
B = 0:
In the above drawing, electricity again is unable to flow. It does not matter that
electricity can get through switch A, it cannot get through switch B. So, the light
bulb is off, and the output is 0.
Finally, let us turn off both switches, so A = 0 and B = 0:
Reordering these results so that the inputs are in numerical order, the truth table
for this circuit is
A B Output
0 0 0
0 1 0
1 0 0
1 1 1
This is the truth table for an AND gate, so the circuit we have been examining is an
AND gate. The light bulb only lights up with switch A and switch B are both on.
This is an example of how to create a logic gate using an electrical circuit.
1.2 Logic Gates 19
Let us look at another circuit, this time consisting of one switch (input) and one
light bulb (output):
In the above picture, current flows out from the top of the battery (the positive side,
which has a bump). It cannot pass through the switch because the switch is off, so
it travels through the light bulb and returns into the bottom of the battery. So, when
A = 0, the output is 1.
Now, let us flip on the switch:
Current again flows out from the top of the battery. Since the switch is on, the
current can flow through either the switch or the light bulb. In turns out that given
this option, electricity will simply flow through the switch only and not the light
bulb. This is because it takes practically no effort to flow through the switch, but
it takes considerable effort to flow through the light bulb. Since no electricity is
flowing through the light bulb, it remains off. Thus, when A = 1, the output is 0.
Summarizing these results in a truth table,
A Output
0 1
1 0
This is the truth table for a NOT gate, so the circuit in this example is a NOT gate.
Similarly, we can create simple electrical circuits that implement each of the
other logic gates.
Exercise 1.15. Consider the following electrical circuit:
20 1 Classical Information and Computation
We have seen that electrical circuits can be used to create logic gates by con-
necting switches in various ways. The switches themselves have changed over time,
however. Early electrical computers used relays or vacuum tubes as the switches.
A relay is a switch that uses an electrically controlled magnet (i.e., an “electro-
magnetic”) to turn on and off the switch. In a vacuum tube (also called a vacuum
valve), one can turn on and off the flow of electricity between two pieces of metal,
one called an anode, and other called a cathode, that is heated. For example, in
the 1940’s, during World War II, the British built a now-famous computer called
Colossus to help break German codes. The first generation Colossus computer had
roughly 1,600 vacuum tubes, and the second generation had about 2,400. Vacuum
tubes often failed, however, and needed replacement.
Nowadays, computers use transistors are used as the switches. They are typically
made of silicon, a common semiconductor, with some other elements to control how
easily electricity flows through certain areas. Transistors have several benefits. They
have no moving parts (i.e., they are “solid-state” devices), so they are more reliable.
They are smaller, which allows computers to be smaller, and they are also faster.
Currently, a single computer processor can have tens of billions of transistors, which
is a huge improvement over Colossus’s 2,400.
Exercise 1.17. Visit the website https://en.wikipedia.org/wiki/Transistor_cou
nt.
(a) Pick an older computer processor. Which one did you pick, what year was it introduced, and
how many transistors did it have?
(b) Pick a newer computer processor. Which one did you pick, what year was it introduced, and
how many transistors did it have?
Previously, we described five of the sixteen possible two-bit gates (AND, OR, XOR,
NAND, and NOR). But what about the other nine? To make matters worse, for
a three-bit gate, the truth table has eight entries, and each output can be 0 or 1,
meaning there are 28 = 256 possible three-bit gates. Do we need to list all of them,
too?
Thankfully, the answer is no. We do not need separate gates for all of these pos-
sibilities because we can reproduce all of them using just a few type of gates. We
call a set of gates that can perform all possible logic operations a universal gate set,
and there are some examples:
• {NOT, AND, OR} is a universal gate set. So given any truth table, we can im-
plement it using only NOT, AND, and OR gates. For example, consider the
following truth table for a circuit with three inputs, A, B, and C:
22 1 Classical Information and Computation
A B C Output
0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0
We can create a circuit composed of NOTs, ANDs, and ORs with this truth
table by looking at every case where the output is 1, then combining them all
together using ORs. To begin with the first line of the truth table, the output is 1
when A = 0, B = 0, and C = 0. This is ABC. Next, the output is 1 when A = 0,
B = 0, and C = 1. This is ABC. Jumping to the fifth line of the truth table, the
output is 1 when A = 1, B = 0, and C = 0, which is ABC. Finally, from the
seventh line of the truth table, the output is 1 when A = 1, B = 1, and C = 0,
which is ABC. The output is 1 whenever any of these are true, so the circuit is
ABC + ABC + ABC + ABC. As a circuit diagram, it is:
This is a rather complicated circuit, and later in this chapter, we will learn an
algebraic way to simplify such circuits (it simplifies to A + B + AC). But for
now, the important point is that we can always use NOT, AND, and OR gates in
this manner to implement any truth table.
Exercise 1.18. Draw a circuit diagram using only NOT, AND, and OR gates that implements the
XOR gate.
1.2 Logic Gates 23
Exercise 1.19. Draw a circuit diagram using only NOT, AND, and OR gates that corresponds to
the following truth table with input bits A and B:
A B C Output
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1
• {NOT, AND} is a universal gate set. To prove this, note the following circuit of
NOTs and ANDs calculates the OR of bits A and B:
A A B A B AB AB
0 0 1 1 1 0
AB 0 1 1 0 0 1
1 0 0 1 0 1
B 1 1 0 0 0 1
First, the inputs A and B go through NOT gates, resulting in A and B. Then,
those become the inputs into an AND gate, resulting in AB. Finally, this is in-
verted, yielding AB. This is the exact same truth table as A + B, so this circuit,
which only contains NOTs and an AND, implements the OR gate. Then, since
{NOT, AND, OR} is a universal gate set, just {NOT, AND} is also a universal
gate set.
Exercise 1.20. Draw a circuit diagram using only NOT and AND gates that corresponds to the
following truth table with input bits A, B, and C:
A B C Output
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 0
• {NAND} is a universal gate set, so we say that NAND is a universal gate. So,
with only NAND gates, one can construct all logic gates. To prove this, let us
show that NAND gates can produce the NOT and AND gates, since we already
know that {NOT, AND} is universal.
First, we can get a NOT gate from NAND by connecting a bit to both inputs:
A AA AA
A AA 0 0 1
1 1 0
24 1 Classical Information and Computation
the solid dot simply means that the wires are connected there. Later, wires will
cross without connecting, and there won’t be a solid dot.
Now, to get AND, note that NAND followed by NOT is simply AND. That is,
if we apply NOT twice, they cancel out, leaving AND:
A B AB AB
A AB 0 0 1 0
AB 0 1 1 0
B 1 0 1 0
1 1 0 1
Since {NOT, AND} is a universal set of gates, and NAND can reproduce both
of them, NAND is a universal gate.
Exercise 1.21. Draw a circuit diagram using only NAND gates that implements the OR gate.
Exercise 1.22. Draw a circuit diagram using only NAND gates that implements the following truth
table:
A B Output
0 0 1
0 1 0
1 0 0
1 1 1
Exercise 1.23. Draw a circuit diagram using only NAND gates that corresponds to the following
truth table with input bits A, B, and C:
A B C Output
0 0 0 1
0 0 1 0
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 1
Hint: You could create a circuit using {NOT, AND, OR}, then replace the ORs with NOTs and
ANDs so that only {NOT, AND} are used, and then replace the NOTs and ANDs with NANDs.
This would be a lot of NAND gates, however! To save you from this tedium, I will tell you that
this truth table can be created with just two NAND gates.
Exercise 1.24. In this problem, we will prove that {NOT, OR} is universal. To do this, we already
know that {NOT, AND, OR} is universal, so we simply need to show that AND gates can be
constructed using {NOT, OR}.
Write the truth table for the following circuit, and verify that it corresponds to the AND gate.
1.3 Adders and Verilog 25
Output
• {NOR} is a universal gate set, so NOR is a universal gate. See Exercise 1.25.
Exercise 1.25. In this problem, we will prove that NOR is a universal gate. To do this, we know
from Exercise 1.24 that {NOT, OR} is universal, so we simply need to show that NOT and OR can
be constructed using NOR gates.
(a) Write the truth table for the following circuit, and verify that it corresponds to the NOT gate.
(b) Write the truth table for the following circuit, and verify that it corresponds to the OR gate.
As an application of the logic gates we have introduced, let us create a logic circuit
that adds two binary numbers. First, we give an overview of how to manually add
binary numbers. Then, we will construct different circuits for adding the various
parts of a binary number, and we will code these circuits using Verilog, a hardware
abstraction language. Finally, we will assemble these circuits into a full circuit that
adds binary numbers. In doing so, we see that logic circuits can be used to compute
things, like adding numbers, and indeed everything that a computer does is based
on logic circuits.
Say we want to add two binary numbers, like 1011 “+” 1110, where “+” denotes
normal addition, not OR. So we want to find
1011
“+” 1110
(carry) 0
Starting with the rightmost bits, 1 plus 0 is 1. Nothing car-
ries to the next column, so we write a small zero above the 1011
next column. “+” 1110
(sum) 1
26 1 Classical Information and Computation
Now for the second bits (from the right), we have the carry (carry) 10
of zero plus one plus one. In decimal, 0 + 1 + 1 = 2, but in 1011
binary, it is 10. So, the sum at the bottom is 0, and 1 carries
“+” 1110
to the next column.
(sum) 01
(carry) 110
For the third bits (from the right), we have 1 + 0 + 1, which
again is 2 in decimal or 10 in binary, so the sum at the 1011
bottom is 0, and 1 carries out to the next column. “+” 1110
(sum) 001
(carry) C4 C3 C2 C1
A3 A2 A1 A0
“+” B3 B2 B1 B0
(sum) S4 S3 S2 S1 S0
Let us begin by creating a circuit that adds two bits. In general, there are four possi-
bilities for these two bits:
1.3 Adders and Verilog 27
A
S
B
If you click the Execute button in the top-left corner of the webpage, the following
appears in the Result section of the webpage:
In the first and second lines, $ indicates that a command is being given. In the first
line, the command is iverilog, which is the compiler. The option -o main speci-
fies that the output (-o) should be a file called main, so after this command is run, we
should get an executable file called main. Continuing, the first line, *.v denotes all
files with the extension .v, so we are compiling all files with that extension. Notice
from the left pane that our code is in a file called main.v, so this will be compiled.
Now, in the second line, vpp is the simulator, and the executable we are simulating
is called main. Our program outputs Hello, World to the third line.
Now, in the left panel, we can replace the code with the Verilog code for the half
adder:
module main;
reg A,B;
wire C,S;
and and1(C,A,B);
xor xor1(S,A,B);
initial begin
A=0
B=1;
#5; // Wait 5 time units.
$display("Carry = ",C);
1.3 Adders and Verilog 29
$display("Sum = ",S);
end
endmodule
In the main module, we define two registers A and B. Each register allows the as-
signment and storage of a bit, which we will later do in the initialization block. We
also define two wires C and D. A wire takes some value depending on the circuit,
and we cannot simply define it to take some value. Next, we create the circuit by
creating an instance of the AND gate which we call and1, and an instance of the
OR gate which we call or1. The arguments of the gates are the outputs of the gates,
followed by the inputs. Now for the initial block, we assign the registers A and B the
values 0 and 1, respectively. After this assignment has been made, we wait 5 of the
simulation’s time units so that the circuit has had a chance to respond to our inputs.
Then we print the values of the carry and sum.
To make the half adder more easily reusable, we can replace it by a single circuit
symbol, which we label HA for half adder:
A S
HA
B C
In Verilog, we do this by creating a module (or function) for the half adder:
module halfadd(C,S,A,B);
input A, B;
output C, S;
xor xor1(S,A,B);
and and1(C,A,B);
endmodule
module main;
reg A,B;
wire C,S;
halfadd half1(C,S,A,B);
initial begin
A=0;
B=1;
#5; // Wait 5 time units.
$display("Carry = ",C);
$display("Sum = ",S);
end
endmodule
Here, the name of the module is halfadd, and it takes four parameters (C,S,A,B),
which we specify as inputs and outputs. The half adder contains an instance of the
AND gate and an instance of the XOR gate. In the main module, we instantiate the
half adder, and the instance is named half1.
30 1 Classical Information and Computation
For the next bits, we need to add them along with whatever was carried in. Since
this allows for a carry it, it is called a full adder. Let us call the bits A and B and the
bit we carry in Cin . The outputs are the carry out Cout and the sum S. Here is a truth
table for what the addition should do:
A B Cin C S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1
From the truth table, we see that the carry out is 1 when A and B are both 1, or when
Cin = 1 and A ⊕ B = 1:
Cout = AB +Cin (A ⊕ B).
From this truth table, we also see that the sum S is 1 whenever one of the inputs A,
B, or Cin are 1, or when all three of them are 1. This is equivalent to the XOR of all
three bits:
S = A ⊕ B ⊕Cin .
Putting these together, the logic circuit is
Cin
S
A
B Cout
Note that there are two groups containing an AND and an XOR:
1.3 Adders and Verilog 31
Cin
S
A
B Cout
Cin S
HA
A
HA Cout
B
To make the full adder more easily reusable, we can replace it by a single circuit
symbol, which we label FA for full adder:
Cin S
A FA
B Cout
xor xor1(S,A,B);
and and1(C,A,B);
endmodule
module fulladd(Cout,S,A,B,Cin);
input A, B, Cin;
output Cout, S;
wire w1, w2, w3;
halfadd half1(w1,w2,A,B);
halfadd half2(w3,S,w2,Cin);
or or1(Cout,w1,w3);
endmodule
module main;
32 1 Classical Information and Computation
reg A,B,Cin;
wire Cout,S;
fulladd full1(Cout,S,A,B,Cin);
initial begin
A=0;
B=1;
Cin=1;
#5; // Wait 5 time units.
$display("Carry = ",Cout);
$display("Sum = ",S);
end
endmodule
Exercise 1.26. The full adder from class contains two XOR gates, two AND gates, and one OR
gate. Replace the OR gate with an XOR gate. What is the truth table of this new circuit? How does
it compare to the truth table of the full adder from class?
Using these adders, we can assemble them to add binary numbers. Let us denote
the bits of the binary numbers A3 A2 A1 A0 and B3 B2 B1 B0 . We can use a half adder to
add bits A0 and B0 , then carry the bit into a full adder to add bits A1 and B1 , then
continue adding the successive bits using full adders:
A0 S0
HA
B0 S1
A1 FA
B1 S2
A2 FA
B2 S3
A3 FA
B3 Cout = S4
This is called a ripple-carry adder, since the carry from one addition ripples to the
next addition.
Let us code it in Verilog:
module halfadd(C,S,A,B);
input A, B;
output C, S;
xor xor1(S,A,B);
1.3 Adders and Verilog 33
and and1(C,A,B);
endmodule
module fulladd(Cout,S,A,B,Cin);
input A, B, Cin;
output Cout, S;
wire w1, w2, w3;
halfadd half1(w1,w2,A,B);
halfadd half2(w3,S,w2,Cin);
or or1(Cout,w1,w3);
endmodule
module rippleadd(Cout,S,A,B);
input [3:0] A, B;
output [3:0] S;
output Cout;
wire C1, C2, C3;
halfadd half1(C1,S[0],A[0],B[0]);
fulladd full1(C2,S[1],A[1],B[1],C1);
fulladd full2(C3,S[2],A[2],B[2],C2);
fulladd full3(Cout,S[3],A[3],B[3],C3);
endmodule
module main;
reg [3:0] A,B;
wire [3:0] S;
wire Cout;
rippleadd ripple1(Cout,S,A,B);
initial begin
A=4’b1011;
B=4’b0011;
#5; // Wait 5 time units.
$display(A,"+",B,"=",S);
$display("%b",A,"+%b",B,"=%B",S);
$display("Carry = ",Cout);
end
endmodule
Note reg [3:0] A,B; creates two 4-bit registers, where A[3] refers to the leftmost
bit of A, and A[0] refers to the rightmost bit of A. In the initial block, A and B are now
binary numbers of length 4. Normally, displaying A and B prints them as decimal
numbers, so we use the %b format code to print in binary instead.
34 1 Classical Information and Computation
For simplicity, one may prefer to only have full adders in the circuit. To do this, we
can replace the first half adder with a full adder with a carry-in of 0.
C0 = 0 S0
A0 FA
B0 S1
A1 FA
B1 S2
A2 FA
B2 S3
A3 FA
B3 Cout = S4
(carry) C4 C3 C2 C1 C0
A3 A2 A1 A0
“+” B3 B2 B1 B0
(sum) S4 S3 S2 S1 S0
but fixing C0 = 0.
If the ripple-carry adder consists entirely of full adders, then adding two n-bit num-
bers requires n full adders. Each full adder uses five logic gates, for a total of 5n
logic gates.
Of course, since the first full adder has a carry-in of zero, it can be replaced with
a half adder. This reduces the number of gates by three, so the number of logic gates
is 4n + 2.
Exercise 1.27. Using the 4-bit adder that we coded in Verilog, what is 1001 plus 0111? Give both
S and Cout . Does this result make sense?
Exercise 1.28. Code an 8-bit adder in Verilog. Use it to add 10101101 and 00111001.
A
C
B
Using your Verilog code, try all possible inputs for A and B and fill in the outputs in the following
truth table:
A B C
0 0 ?
0 1 ?
1 0 ?
1 1 ?
Do your results make sense?
Previously, we learned that {NOT, AND, OR} is a universal gate set. We demon-
strated this by creating a circuit that implemented a given truth table. The circuit was
ABC + ABC + ABC + ABC, and we claimed that it could be simplified to A + B + AC.
Now, we are going to prove this. One way is to show that their truth tables are the
same. But, it would be nice to have an algebraic way to show that their outputs are
equal, i.e.,
ABC + ABC + ABC + ABC = A + B + AC.
In this section, we will learn how to do this using the rules of algebra for bits, called
boolean algebra. These rules will allow us to simplify complicated circuits in a
systematic, algebraic manner.
First, we need to learn how to read boolean expressions. Consider the following
expression on three inputs A, B, and C, which contains one OR and one AND:
A + BC.
When implementing this, do we do the OR first, i.e., (A + B)C, or the AND first,
i.e., A + (BC)? Does it even matter? To see, let us work out the truth table for each
option:
36 1 Classical Information and Computation
A B C (A + B)C A + (BC)
0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 1 1 1 1
1 0 0 0 1
1 0 1 1 1
1 1 0 0 1
1 1 1 1 1
So the order in which we do the operations matters. Just like in regular math, we
multiply (and divide) before adding (and subtracting), the convention in boolean
algebra is that AND is done first, then OR. Thus,
A + BC = A + (BC).
AND and OR also follow several familiar properties from elementary algebra. First,
they are associative:
• ABC = (AB)C = A(BC)
• A + B +C = (A + B) +C = A + (B +C)
AND and OR are also commutative:
• AB = BA
• A+B = B+A
Finally, both are distributive, meaning AND distributes into OR, and OR distributes
into AND:
• A(B +C) = AB + AC
• A + (BC) = (A + B)(A +C)
The last point is likely the most foreign. It is not true when adding and multiplying
numbers (e.g., 2 + 3 · 4 6= (2 + 3)(2 + 4), since the left-hand side evaluates to 14, and
the right-hand side evaluates to 30), but it is true of AND/OR with bits!
Our first identities involve 0 and 1. They can be proved by writing out their truth
tables, or by the arguments given below.
• A0 = 0, since A and 0 are never both 1.
• A1 = A, since A and 1 are both 1 when A is 1.
1.4 Circuit Simplification and Boolean Algebra 37
There are several identities involving logic gates on only a single bit A and its inverse
A. They can be proved by writing out their truth tables, or by the arguments given
below.
• A = A, since inverting twice results in the original bit.
• AA = A, since A AND A are both 1 when A is 1.
• AA = 0, since A and A are inverses, they are never both 1.
• A + A = A, since A OR A are 1 when A = 1.
• A + A = 1, since A and A are inverses, one of them is always 1.
These identities now differ substantially from elementary algebra, e.g., 3 · 3 = 32 .
There are some important identities involving two bits A and B. They can be proved
by writing out their truth tables, or since they are less obvious, we also provide
algebraic proofs for each.
• A + AB = A. For AB to be 1, A must be 1. As an algebraic proof,
A + AB = A1 + AB = A(1 + B) = A1 = A.
The next two points are called De Morgan’s Laws, which states that the NOT of
an AND is the OR of the NOTs, and the NOT of an OR is the AND of the NOTs.
• AB = A + B. For AB to be 1, AB must be 0. Thus, A and B are not both 1. At
least one of them must be 0, which is A + B. In Exercise 1.10, this statement
that NAND and negative-OR are equivalent was proved using truth tables. The
following algebraic proof is more complicated, so consider it optional.
38 1 Classical Information and Computation
X = X1 = X(X +Y ) = XX + XY = 0 + XY
= XY + XY = (X + X)Y = 1Y = Y.
AB + (A + B) = AB + (A + B)(1) A1 = A
= AB + (A + B)(A + A) A+A = 1
= AB + AA + AA + BA + BA Distributive Property
= AB + 0 + A + AB + BA AA = 0, AA = 1, Commutation
= A(B + B) + A + BA AA = 0, AA = 1, Distributive Property
= A(1) + A + BA B+B = 1
= 1. A + A = 1, 1 + A = 1
Next,
(A + B) + AB = (A + B)(A + A) + AB = AA + AA + BA + BA + AB
= (A + BA) + A(B + B) = A + A = 1.
1.4 Circuit Simplification and Boolean Algebra 39
AA = A + A = A.
We also showed that the OR gate, A + B, could be implemented using three NOTs
and one AND, AB. We proved this by writing the truth table, but now we can also
prove it algebraically using De Morgan’s theorem:
AB = A + B = A + B.
Similarly, we showed using a truth table that the AND gate, AB, could be implement
using three NOTs and one OR, A + B. Using De Morgan’s theorem,
A + B = AB = AB.
Using these boolean identities, let go through three examples of simplifying cir-
cuits using boolean algebra, beginning with the example from the beginning of the
section.
1. Back to the example that started this section, let us show that ABC + ABC +
ABC + ABC = A + B + AC using boolean algebra:
ABC + ABC + ABC + ABC = AB(C +C) + A(B + B)C Distributive Property
= AB1 + A1C A+A = 1
= AB + AC A1 = A
= A + B + AC. De Morgan’s Law
Note the second-to-last line uses three NOT gates, two AND gates, and one OR
gate, whereas the last line uses two NOT gates, one AND gate, and two OR
gates, so it uses one fewer gate.
2. As another example, consider the following circuit with seven gates:
40 1 Classical Information and Computation
B ABC + ABC
This circuit implements ABC + ABC. Let us use boolean algebra to simplify it:
This complicated circuit is equivalent to (A + B)C, which only has three gates:
A
(A + B)C
B
C
3. For our third example, we have (A + B)(A + B +C)C:
A
B
(A + B)(A + B + C)C
C
As drawn, it has five gates. Two of the gates, however, have three inputs, and if
we were to replace each of them with two two-bit gates, we would have seven
1.4 Circuit Simplification and Boolean Algebra 41
(A + B)(A + B +C)C
= AAC + ABC + ACC + BAC + BBC + BCC Distributive Property
= 0C + ABC + A0 + BAC + BC + B0 AA = 0
= ABC + BAC + BC A0 = 0, A + 0 = A
= (A + A + 1)BC Distributive Property
= (1)BC A+1=1
= BC. 1A = A
So, the output of the circuit is only dependent on B and C, not A. It only needs
one NOT gate and one AND gate.
Exercise 1.30. Simplify A(A + B). Hint: Your final result should be a single logic gate.
Exercise 1.31. Simplify (A + B)(A + B +C). Hint: Your final result should have one NOT gate and
two OR gates.
Exercise 1.32. Simplify the following circuit:
A
B
C
Exercise 1.33. Consider the following truth table with input bits A, B, and C:
A B C Output
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 0
(a) Create a circuit consisting of NOT, AND, and OR gates that implements the truth table.
(b) Simplify your previous circuit using boolean algebra.
42 1 Classical Information and Computation
A reversible gate, is a logic gate where, given the output of the gate, we can always
determine what the input was. An example is the NOT gate:
AA
A A 0 1
1 0
From its truth table, the outputs of NOT are unique, so it is possible to reverse the
operation. That is, if we know that the output of the NOT gate is 1, we know that
the input must have been 0, and if we know that the output was 0, we know that
the input must have been 1. The gate is reversible because, given the output, we can
always determine the input.
An irreversible gate is the opposite of a reversible gate. Given the output of the gate,
we generally cannot determine what the input was. An example is the AND gate:
A B AB
A 0 0 0
AB 0 1 0
B 1 0 0
1 1 1
From the truth table, if the output of the AND gate is 1, the we know with certainty
that the inputs were both 1. If the output of the gate is 0, however, then it is impossi-
ble to know from this information alone which of the other three inputs (00, 01, and
10) were used. So in general, we are unable to determine the input to the AND gate
from its output. Thus, it is irreversible.
Notice the AND gate has two input bits, which have four possible states (00, 01,
10, and 11), and one output bit, which has two possible states (0 and 1). Since there
are fewer possibilities for the outputs than the inputs, the gate must be irreversible.
Conversely, if there are fewer input bits than output bits, the circuit is still irre-
versible because some of the outputs will be undetermined. For example, consider
the following truth table with one input bit and two output bits:
ABC
0 0 1
1 1 0
1.5 Reversible Logic Gates 43
The question, “If both outputs are 0, what was the input?” is undefined by the truth
table, so its inverse is not completely specified.
Thus, for a logic gate to be reversible, it must have the same number of input bits
and output bits. The converse is not true, however. Just because a logic gate has the
same number of input bits and output bits does not necessarily mean it is reversible.
For example, for the gates corresponding to the following truth tables, the gate on
the left is reversible, but the gate on the right is irreversible:
ABCD ABCD
0 0 0 1 0 0 0 0
0 1 1 0 0 1 0 1
1 0 1 1 1 0 0 1
1 1 0 0 1 1 1 0
Reversible Irreversible
The gate on the left is reversible because, given the output, it is always possible to
determine the input. The gate on the right is irreversible because given the output
01, one does not know if the input was 01 or 10.
Another way to contrast reversible and irreversible gates is whether information
is lost. With a reversible gate, no information is lost since we can always recover
the inputs from the outputs. With an irreversible gate, however, information is lost
since, given an output, we generally do not know what the inputs were.
Exercise 1.34. Are the following gates reversible or irreversible? (a) OR. (b) XOR. (c) NAND. (d)
NOR.
Exercise 1.35. A gate has two inputs (A and B) and two outputs (C and D). Its truth table is shown
below.
A B C D
0 0 0 0
0 1 1 1
1 0 1 0
1 1 0 1
Is the gate reversible or irreversible?
A • A0
B × B0
C × C0
It takes three inputs A, B, and C, and has three outputs A0 , B0 , and C0 . A is the control bit, as indicated
by the dot in the circuit diagram. If A = 0, then nothing happens to B and C. If A = 1, however,
then B and C are swapped, as indicated by the ×’s. The control bit is unchanged, so A0 = A. Thus,
it is a Controlled-SWAP gate.
(a) Write the truth table for the Fredkin gate.
(b) Based on the truth table, is the Fredkin gate reversible or irreversible? Why?
44 1 Classical Information and Computation
We have learned that the AND gate is irreversible, meaning we lose information
when we use an AND gate because we generally cannot reconstruct the inputs from
the outputs. It would be nice to have a reversible version of the AND gate, and in
this section we introduce one called the Toffoli gate.
The Toffoli gate has three inputs A, B, and C. To be reversible, it needs to have
three outputs, and they are A, B, and AB ⊕C:
A A
B Toffoli B
C AB ⊕ C
The Toffoli gate can be constructed using an AND gate and an XOR gate. We take
the AND of A and B, and we take the XOR of AB with C to get AB ⊕C.
A A
B B
AB
AB ⊕ C
C
From before, when C = 0, the third output is AB. When C = 1, the third output is
AB ⊕ C = AB ⊕ 1 = AB, so it is opposite AB. Furthermore, since AB is the NAND
of A and B, and since NAND is universal, the Toffoli gate is also universal.
Also from the truth table, notice the third bit is flipped when A and B are both 1.
For this reason, the Toffoli gate is also called the Controlled-Controlled-NOT gate
or CCNOT gate. Whether the third bit is flipped is controlled by whether the first
two bits are 1. That is, if A = B = 1, then the Toffoli gate flips C, and otherwise it
does nothing. For this reason, the Toffoli gate can be drawn as
A • A
B • B
C AB ⊕ C
In this circuit diagram, the dots indicate the control bits, and the ⊕ indicates a bit
flip.
Exercise 1.37. Consider the anti-Toffoli gate, which flips the third bit if the first two bits are both
0. Whereas a control is drawn as a solid dot •, an anti-control is drawn as an open dot ◦. So, the
anti-Toffoli gate is drawn as
A A
B B
C AB ⊕ C
(a) Write the truth table for the anti-Toffoli gate.
(b) In the above picture of the anti-Toffoli gate, AB appears in the third output. Which law from
boolean algebra says that this is equal to A + B?
(c) When C = 0, is the third output the AND, OR, XOR, NAND, or NOR of A and B?
(d) When C = 1, is the third output the AND, OR, XOR, NAND, or NOR of A and B?
(e) Construct an anti-Toffoli gate using one Toffoli gate and four NOT gates.
In the last section, we showed that the AND gate can be made reversible by XORing
its output whith a third input C. This procedure works in general, not just for for
AND gates. Say the gate has inputs A and B and outputs f (A, B), which is a function
that outputs 0 or 1 depending on the inputs A and B:
A
Gate f (A, B)
B
For example, for the AND gate, the function would map f (0, 0) = 0, f (0, 1) = 0,
f (1, 0) = 0, and f (1, 1) = 1. We can make this gate reversible by XORing its output
with a third input C:
46 1 Classical Information and Computation
A B C A B f (A, B) ⊕C
A A 0 0 0 0 0 f (0, 0)
B B 0 0 1 0 0 f (0, 0)
0 1 0 0 1 f (0, 1)
Gate f (A, B) 0 1 1 0 1 f (0, 1)
1 0 0 1 0 f (1, 0)
f (A, B) ⊕ C 1 0 1 1 0 f (1, 0)
C
1 1 0 1 1 f (1, 1)
1 1 1 1 1 f (1, 1)
In the rightmost column of the truth table, we used f (A, B) ⊕ 0 = f (A, B) and
f (A, B) ⊕ 1 = f (A, B). So, this implements the original gate when C = 0. But, the
overall circuit is reversible. In the output, every permutation of A and B shows up
twice, once with f (A, B) and once with f (A, B), ensuring that the outputs are unique.
We can generalize this technique several ways. First, the gate could be a function
of any number of variables. For example, say we have a gate with three inputs A, B,
C, and one output f (A, B,C):
A
B Gate f (A, B, C)
C
This must be irreversible because there are fewer outputs than inputs. To make it
reversible, we add a fourth input D that we XOR with f (A, B,C):
A A
B B
C C
Gate f (A, B, C)
f (A, B, C) ⊕ D
D
Now, when D = 0, the bottom wire outputs f (A, B,C), and when D = 1, the bottom
wire outputs f (A, B,C). The truth table for this is
1.5 Reversible Logic Gates 47
A B C D A B C D ⊕ f (A, B,C)
0 0 0 0 0 0 0 f (0, 0, 0)
0 0 0 1 0 0 0 f (0, 0, 0)
0 0 1 0 0 0 1 f (0, 0, 1)
0 0 1 1 0 0 1 f (0, 0, 1)
0 1 0 0 0 1 0 f (0, 1, 0)
0 1 0 1 0 1 0 f (0, 1, 0)
0 1 1 0 0 1 1 f (0, 1, 1)
0 1 1 1 0 1 1 f (0, 1, 1)
1 0 0 0 1 0 0 f (1, 0, 0)
1 0 0 1 1 0 0 f (1, 0, 0)
1 0 1 0 1 0 1 f (1, 0, 1)
1 0 1 1 1 0 1 f (1, 0, 1)
1 1 0 0 1 1 0 f (1, 1, 0)
1 1 0 1 1 1 0 f (1, 1, 0)
1 1 1 0 1 1 1 f (1, 1, 1)
1 1 1 1 1 1 1 f (1, 1, 1)
In the output, every permutation of A, B, and C shows up twice, once with f (A, B,C)
and f (A, B,C), ensuring that the outputs are unique. So, this circuit is reversible.
We can also generalize the technique to gates with multiple outputs. For example,
say we have an irreversible gate with two inputs A and B and two outputs f (A, B)
and g(A, B), which are functions of the inputs:
A f (A, B)
Gate
B g(A, B)
To make this reversible, follow the same procedure, but now we add two extra inputs
and two XOR gates, one each for f (A, B) and g(A, B):
A A
B B
f (A, B)
Gate
f (A, B) ⊕ C
C
g(A, B)
g(A, B) ⊕ D
D
48 1 Classical Information and Computation
Exercise 1.39. Write the truth table for XOR. Is it reversible or irreversible? If it is irreversible,
turn it into a reversible circuit, and write the truth table of the reversible circuit.
1.6 Error Correction 49
Exercise 1.40. Consider the following two-bit gates, each with two outputs.
(a) The gate
A A⊕B
Gate
B B
A A⊕B
Gate
B AB
Cin S
A FA
B Cout
High energy particles like cosmic rays, neutrons produced in the earth’s atmosphere,
radiation from nuclear testing, and radiation from particle accelerators can strike
computers, causing bits to flip from 0 to 1 and from 1 to 0. This is known as a single
event upset.
Similarly, if we are transmitting data through the internet, then some bits may be
corrupted. For example, photons traveling through a fiber optic cable can leak out
due to imperfections in the cable. The sender may have sent a 1, but the receiver
gets a 0.
Fortunately, as long as the number of errors is limited, there are schemes to detect
and/or correct errors.
Exercise 1.42. Watch “The Universe is Hostile to Computers” by Vertasium on YouTube:
https://www.youtube.com/watch?v=AaZ_RSt0KP8
(a) For the election in Belgium, Maria Vindevogel received 4096 extra votes because the thirteen
bit had flipped from 0 to 1, which added 212 = 4096 extra votes to her count. How many extra
votes would she have received if the fourteenth bit had flipped instead?
(b) “If an alpha particle struck in just the right place, it could create a large number of free charge
carriers, causing electrons to accumulate in the well, flipping a one to a zero. This is known
as a .”
(c) “The reason this problem was identified in the 1970s was because chip components had been
to the point where a single alpha particle could produce enough charge
to flip a bit.”
(d) “Victor Hess had discovered , high energy radia-
tion from space. But what were these rays exactly, and where were they coming from? Well,
today we know they aren’t electromagnetic rays, as many expected, but .
Around 90% are protons, 9% are helium nuclei, and 1% are heavier nuclei. Some of them are
from the sun, but they have comparatively low energy. High energy cosmic rays moving very
close to the speed of light come from exploding stars, supernovae, in our own galaxy and
in others. And the highest energy particles are thought to come from
, including the super massive black holes at the center of galaxies.”
(e) “But primary cosmic rays like these don’t make it down to earth’s surface. Instead, they
collide with air molecules around 25 kilometers above the ground and create new parti-
cles like pions. These collide and decay into other particles like neutrons, protons, muons,
electrons, positrons, and photons, which in turn collide with other molecules in one long
. So from a single primary cosmic ray comes a shower of particles
streaming toward the earth. It is one of these particles that investigators suspect struck a
in a computer in Belgium, flipping the thirteenth bit from a zero to a
one and giving Maria Vindevogel 4096 extra votes.”
(f) “These days, there are a number of ways computer chips are made resilient in the face of bit
flips, like (or ECC) mem-
ory.”
(g) “In 1996, IBM estimated that for each 256 megabytes of RAM, one bit flip occurs per
. And the main culprit seems to be neutrons created in the shower of
particles from .”
(h) For a commercial airplane “At cruising altitude, this increases the chance of a single event
upset by times.”
(i) “On one five day [space shuttle] mission, STS 48, there were separate
bit flips.”
(j) “Above the atmosphere, cosmic rays are so energetic sometimes you can even see them.”
“Once in awhile you have your eye closed and you’re not asleep yet, and if you wait a little
while, you occasionally will see a of light. And we think it is heavy
particles or individual bursts of energy coming from radiation that are either going through
the eyeball itself or the optic nerve. And the way that your body registers radiation going
through it is amazingly enough by showing you a little flash in one of your eyes just to re-
mind you that you are subject to the radiation of not only our sun, but
of the universe that is radiating at you.”
The simplest way to detect errors is to use repeat each bit multiple times so that
multiple physical bits encode a single logical bit. This is called the repetition code.
For example, say we use two physical bits to encode one logical bit, i.e.,
1.6 Error Correction 51
00 encodes 0,
11 encodes 1.
So if we want to send the letter “Q” in ASCII, which is represented by the seven-bit
string 1010001, we would actually send
11 00 11 00 00 00 11.
If one of the physical bits is flipped, say due to a single event upset or transmis-
sion error, then instead of receiving 00 or 11, the recipient would receive
01 or 10.
If the recipient gets either of these pairs, they know that an error has occurred. This
is an example of an error-detecting code. The recipient can then request that the
message be resent.
The binary strings 00, 01, 10, and 11 are called codewords.
The parity of a bit string is whether the bit string has an even or odd number of
1’s. The codewords 00 and 11 have even parity, while the codewords 01 an 10 have
odd parity, so parity can be used to distinguish whether an error has occurred. Such
indications of errors are called error syndromes.
Note parity can be computed using XOR, since 0 ⊕ 0 = 1 ⊕ 1 = 0 indicates even
parity, while 0 ⊕ 1 = 1 ⊕ 0 = 1 indicates odd parity.
If more than one error occurs, then our encoding is unable to reliably detect
the error. For example, if we transmit 00 and both bits get flipped to 11, then the
recipient has no way of knowing that an error occurred. So if we use the repetition
code with two physical bits per logical bit, only one-bit errors can be detected.
Exercise 1.43. Another error-detecting scheme is to send a parity bit. For example, say I want to
transmit to you the character “Q” in ASCII, which is encoded by seven bits 1010001. The parity
of this bit string is odd, since there is an odd number of 1’s in the string. (Alternatively, taking the
XOR of all the bits yields 1.) I append this parity of 1 to the transmission, so I send you the eight
bits 10100011.
Say the bits you actually receive are 11100011.
(a) Calculate the parity of the first seven bits you received.
(b) Does the parity you calculated in part (a) match the parity bit (the last bit)?
(c) Did an error occur in the transmission? Why or why not?
(d) Can this scheme reliably detect if more than one error occurred? Why or why not?
Besides being able to detect errors, it would be nice to also fix them. We call this
error correction.
For example, if we use three bits for the repetition code, then
52 1 Classical Information and Computation
000 encodes 0,
111 encodes 1.
Now if a single bit gets flipped, the possible codewords are 001, 010, 100, 110, 101,
and 011. We can correct the error by taking the majority vote:
Codeword b2 ⊕ b1 b1 ⊕ b0
000 0 0
001 0 1
010 1 1
011 1 0
100 1 0
101 1 1
110 0 1
111 0 0
So if both parity bits are 0, the codeword is either 000 or 111, and there is no error.
If the left parity bit is 0 and the right is 1, then the codeword is either 001 or 110,
and the rightmost bit was flipped. If the left parity bit is 1 and the second is 0, then
the codeword is either 011 or 100, and the left bit was flipped. Finally, if both parity
bits are 1, then the codeword is 010 or 101, and the middle bit was flipped.
Note using parity checks allows us to detect and correct errors without needing
to know the codeword. If we know that a bit flip occurred in the middle bit, we do
not need to know if the codeword was 010 or 101. We can simply flip the middle
bit to correct it, and proceed. This will be an important notion in quantum error
correction, where knowing the codeword can ruin the computation, so we must be
able to correct errors without knowing the precise codeword.
Since this error-correcting code uses three physical bits to encode a logical bit, it
is more likely that a bit will flip because there are more of them. If a single bit flips,
it can be corrected. If two or all three bits flip, however, we cannot correct the error.
We say an uncorrectable error has occurred. Using a litle math, we can determine
when error correction decreases the chance of an uncorrectable error because we can
fix single bit flips, or when it increases the chance of an uncorrectable erro because
there are more bits to flip.
First, let p denote the probability of a single bit flipping. Without error correction,
if the bit flips an uncorrectable error has occurred, so the probability of an uncor-
rectable occurring is p. Now with three physical bits representing one logical bit,
1.6 Error Correction 53
single-bit errors can be corrected, so an uncorrectable error only occurs when two
or three of the bits get flipped. The probability of two specific bits getting flipped is
p2 , and since there are three combinations for two of the three bits to be flipped (the
two bits could be the first two bits, the last two bits, or the first and last bits), the
probability of any two bits getting flipped is 3p2 . Another way to get the coefficient
of 3 is using the combination “3 choose 2,” where “n choose k” is
n!
nCk = ,
k!(n − k)!
3p2 + p3 .
As long as this is less than p, which is the probability that an error occurs without
error correction, then it is favorable to do error correction. That is, it is favorable to
do error correction when
3p2 + p3 < p.
54 1 Classical Information and Computation
We can solve this inequality using a computer algebra system. As promised, we will
provide both Mathematica and SageMath code:
• Mathematica. Using Mathematica’s Reduce function, the inequality can be
solved using
Reduce[3 pˆ2 + pˆ3 < p, p]
Executing this, the output is
1 √ 1 √
p< −3 − 13 || 0 < p < −3 + 13 .
2 2
In this result, the double bar || means “or.” So there are two situations when
√ 3-bit repetition code is better√than no error correction, when p < (−3 −
the
13)/2 or when 0 < p < (−3 + 13)/2. We can find numerical values for the
expressions using Mathematica’s N function:
N[Reduce[3 pˆ2 + pˆ3 < p, p]]
The output of this is
• SageMath. Using SageMath’s solve function, the inequality can be solved us-
ing
sage: p = var(’p’)
sage: solve(3*p**2 + p**3 < p, p)
[[p < -1/2*sqrt(13) - 3/2], [p > 0, p < 1/2*sqrt(13) - 3/2]]
So there are two situations
√ when the 3-bit repetition code√
is better than no error
correction, when p < − 13)/2 − 3/2 or when 0 < p < 13/2 − 3/2. We can
find numerical values for the expressions using SageMath’s n function:
sage: n(-1/2*sqrt(13) - 3/2)
-3.30277563773199
sage: n(1/2*sqrt(13) - 3/2)
0.302775637731995
Thus, using either Mathematica or SageMath, the repetition code with three bits is
better when the probability of a single bit flip is
The first case is negative, which cannot be possible since p is a probability. So, the
second case is the correct solution, and the 3-bit repetition code is better than no
error correction when
0 < p < 0.303.
Thus, as long as the error probability is less than 0.303, error correction is beneficial.
For example, if p = 0.1, then 3p2 + p3 = 0.031, so there is a smaller chance that an
uncorrectable error occurs. If p = 0.4, then 3p2 + p3 = 0.544, so an uncorrectable
error is more likely to occur.
1.7 Computational Complexity 55
Exercise 1.44. Using the repetition code, you have a three-bit string that encodes a logical bit.
(a) If the parity of the left two bits is odd, and the parity of the right two bits is also odd, has a
single-bit error occurred? If yes, which bit was flipped (the left, middle, or right bit)?
(b) If the parity of the left two bits is odd, but the parity of the right two bits is even, has a
single-bit error occurred? If yes, which bit was flipped (the left, middle, or right bit)?
(c) If the probability of a single bit flipping is p = 0.2, what is the probability that an uncorrectable
error occurs with the 3-bit repetition code? In this case, does the 3-bit repetition code increase
or decrease the probability of an uncorrectable error occurring?
(d) If the probability of a single bit flipping is p = 0.4, what is the probability that an uncorrectable
error occurs with the 3-bit repetition code? In this case, does the 3-bit repetition code increase
or decrease the probability of an uncorrectable error occurring?
Exercise 1.45. Using the repetition code, you have a five-bit string that encodes a logical bit. This
allows for the correction of two-bit errors, since three correct bits is the majority vote over the two
incorrect bits. Say you receive a codeword b4 b3 b2 b1 b0 .
(a) If b4 ⊕ b3 = 0, b3 ⊕ b2 = 0, b2 ⊕ b1 = 1, and b1 ⊕ b0 = 1, did an error occur? If an error has
occurred, which bit(s) were flipped?
(b) If b4 ⊕ b3 = 0, b3 ⊕ b2 = 1, b2 ⊕ b1 = 0, and b1 ⊕ b0 = 1, did an error occur? If an error has
occurred, which bit(s) were flipped?
(c) If the probability of a single bit flipping is p, what is the probability of an uncorrectable error
occurring with this repetition code of 5 bits? Hint: One and two-bit flips can be corrected,
so uncorrectable errors occur if three, four, or five bits are flipped. The probability of each
occurring also depends on the number of combinations of bits flipping.
(d) For what values of p is your result in part (c) less than p (the error rate without error correc-
tion)? Just give the range to three decimal places.
(e) If p = 0.1, what is the probability that an uncorrectable error occurs with the 5-bit repetition
code? In this case, does the 5-bit repetition code increase or decrease the probability of an
uncorrectable error occurring?
(f) For comparison, if p = 0.1, what is the probability that an uncorrectable error occurs with the
3-bit repetition code? How does it compare to the 5-bit code?
Say we want to add two 4-bit numbers using the ripple-carry adder from Sec-
tion 1.3.4. This takes one half-adder, which has two logic gates, and three full-
adders, which each have five logic gates, for a total of 2 + 3 · 5 = 17 logic gates. If
we generalize this and add two n-bit numbers with a ripple-carry adder, it takes one
half-adder and (n − 1) full-adders, for a total of 2 + (n − 1)5 = 5n − 3 logic gates.
So as the length n of the binary numbers increases, the number of logic gates 5n − 3
scales linearly with n. This scaling can be expressed through asymptotic notation.
Big-O notation is used to give an upper bound on the asymptotic behavior of a
function. We write 5n − 3 = O(n2 ) to mean that 5n − 3 scales less than or equal to
n2 for large n. Mathematically, f (n) = O(g(n)) means there exists constants c and
n0 such that f (n) ≤ cg(n) for all values of n greater than n0 . Some other examples
include 5n − 3 = O(n log(n)) and 5n − 3 = O(2n ). Since the scaling is “less than
56 1 Classical Information and Computation
or equal to,” we can also write 5n − 3 = O(n). Big-O is the most commonly used
asymptotic notation, and it is useful for specifying the worst-case behavior of an
algorithm. In our case, the number of logic gates needed to add two binary numbers
of length n is no worse than linear, i.e., O(n).
If we want the inequality to be strictly “less than,” we use little-o notation. So we
can write 5n − 3 = o(n2 ), but 5n − 3 6= o(n). Mathematically, f (n) = o(g(n)) means
there exists constants c and n0 such that f (n) < cg(n) for all values of n greater than
n0 .
Similarly, a lower bound on the asymptotic behavior √ of a function is denoted
using big-Omega notation.
√ We can write 5n − 3 = Ω ( n) to mean that 5n − 3 is
lower bounded by n for large n. Mathematically, f (n) = Ω (g(n)) means there
exists constants c and n0 such that f (n) ≤ cg(n) for all values of n greater than
n0 . We could also write 5n − 3 = Ω (1) to mean that 5n − 3 is lower bounded by
a constant. Since the inequality is “greater than or equal to,” we can also write
5n − 3 = Ω (n).
As before, if we want the inequality to be strictly “greater than,” we use √ a low-
ercase symbol, or little-omega notation. So we can write 5n − 3 = ω( n), but
5n − 3 6= ω(n). Mathematically, f (n) = o(g(n)) means there exists constants c and
n0 such that f (n) < cg(n) for all values of n greater than n0 .
Finally, to specify that 5n − 3 scales linearly with n, we use big-Theta notation.
We write this as 5n − 3 = Θ (n), and it means that 5n − 3 is both upper bounded
and lower bounded by n, asymptotically. That is, 5n − 3 = O(n) and 5n − 3 = Ω (n).
Combining the mathematical definitions of each, f (n) = Θ (g(n)) means there exists
constants c1 , c2 , and n0 such that c1 g(n) ≤ f (n) ≤ c2 g(n) for all values of n greater
than n0 .
These asymptotic notations are summarized in Table 1.2.
Exercise 1.46. Consider the following functions, one quadratic and another cubic:
(a) Evaluate both functions when n = 100. Which is bigger, f (100) or g(100)?
(b) Evaluate both functions when n = 500. Which is bigger, f (500) or g(500)?
(c) At what value of n does g(n) surpass f (n)?
1.7 Computational Complexity 57
Exercise 1.47. Match each function to a possible asymptotic notation. Although each asymptotic
notation can match multiple functions, each should only be used once.
√
(a) 3n2 + 7n + 4 (i) o( n)
(b) log n (ii) Ω (2n )
(c) 5en (iii) Θ (n2 )
√
(d) π n (iv) O(1)
(e) π (v) ω(1)
Since each logic gate takes some time to apply, we say an algorithm that utilizes,
say, O(n) logic gates, takes O(n) time, ignoring the fact that some of the logic gates
can be done in parallel.
In computer science, an algorithm is called efficient if it takes polynomial time,
√ as 0.5
such the ripple-carry adder’s 5n − 3 gates, or any other polynomial, like n2.5 ,
n = n , or n1000 . On the other hand, an algorithm is inefficient if it takes expo-
nential time, such as 2n or en/1000 . In practice, an algorithm that takes n1000 time
may be less useful than one that takes en/1000 because for small problem sizes, this
polynomial is bigger than the exponential. But eventually, as n gets larger and larger,
the exponential will surpass the polynomial. but the distinction between algorithms
that take polynomial or exponential time is still useful.
Different problems are easier or harder than others, and we classify them using
complexity classes, or classes of problems of some complexity or difficulty. Prob-
lems that can be solved efficiently (in polynomial time) by classical computer are
in the complexity class P, which stands for Polynomial-Time. Some problems in P
include:
• Matchmaking in the “stable marriage problem.” Given n men and n women
who have ranked the other group in order of preference, marry the men with the
women so that no two people would rather be with each other than with their
spouses. The Gale-Shapley algorithm solves this in O(n2 ) time.
• Determining if a number is prime. Recall a prime number is a whole number
greater than 1 whose only factors are 1 and itself, so 2, 3, 5, 7, 11, and 13 are
all prime numbers, but 4 is not prime because it is equal to 2 × 2, and 6 is not
58 1 Classical Information and Computation
prime because it is equal to 2×3. If the number to be tested has n digits, then the
Agrawal–Kayal–Saxena (AKS) class of algorithms can determine if the number
is prime in O(n6 ) time, ignoring factors log(n).
• Maximizing a linear function constrained by linear inequalities, also known as
“linear programming.” If there are n variables, algorithms exist that run in O(n3 )
and faster.
Another complexity class is the problems for which a solution can be quickly
verified by a computer in polynomial time. This class is called NP (the N stands
for a non-deterministic Turing machine—more on this later, and the P stands for
polynomial), and it includes problems such as:
• Factoring, since it is easy to multiply the factors together to verify it equals the
original number.
• Testing if two networks are equivalent, since it is easy to verify that two net-
works are equivalent if a map between the two is provided. This is knows at the
“graph isomorphism” problem.
Many problems of practical importance are in the class NP.
Certain problems within NP have a special property called completeness, and we
call these problems NP- COMPLETE. If we can find an efficient solution to any NP-
COMPLETE, then we can use it to find an efficient solution to any NP problem. That
is, the overhead in applying the algorithm to other NP problems is at most poly-
nomial, so an efficient solution to one NP- COMPLETE problem yields an efficient
solution to all NP problems. Some NP- COMPLETE problems include:
• Solving n × n Sudoku puzzles.
• Finding the shortest possible tour that visits a list of cities exactly once and
returns to its starting point, known as the “traveling salesman problem.”
• Determining whether a tour that visits each location once and returns to its
starting point exists, which is called the “Hamiltonian path problem.”
• Determining whether a set of items can fit into certain boxes, known as the “bin
packing problem.”
A literal million dollar question is whether P and NP are equal. The Clay Math-
ematics Institute will pay one million U.S. dollars to whomever can prove whether
the problems whose solutions are efficiently found are the same as the problems
whose solutions are efficiently verified. It is known that all problems in P are con-
tained within NP, since one can efficiently solve the problem and compare it to the
proposed solution. But it is unknown if NP contains any problems that are not in P.
The general conjecture is that P 6= NP.
Another complexity class is PSPACE, which contains all the problems that can
be solved by a computer using a polynomial amount of memory, without any limits
on time. Generalizations of many games are in PSPACE, such as:
• Determining whether a winning strategy exists for the game of checkers gener-
alised to an n × n board.
• Winning a generalized level of Super Mario Bros.
1.7 Computational Complexity 59
PSPACE
NP-
complete
NP
Fig. 1.1: The generally accepted relationships between the complexity classes NP,
NP- COMPLETE, P, and PSPACE.
Exercise 1.50. Visit the Clay Mathematics Institute website at claymath.org and find the Mil-
lenium Problems.
(a) List the seven Millenium Prize Problems. Is each problem solved or unsolved?
(b) Who first proposed the P vs NP problem? In what year?
Exercise 1.51. Visit the Wikipedia list of NP- COMPLETE problems at https://en.wikiped
ia.org/wiki/List_of_NP-complete_problems. Describe three NP- COMPLETE prob-
lems.
60 1 Classical Information and Computation
So far, we have examined circuit-based computers consisting of bits and logic gates.
While circuit-based computers are very useful in practice, they are not always the
best kind of computer to study mathematically. Other types of computers, or models
of computation, exist and may be more convenient. In this section, we examine the
Turing machine, which was introduced in 1936 and is still used today by computer
scientists who study the abilities and limitations of computers.
1.8.1 Components
(tape)
1. An tape divided into cells, each with a symbol from some finite alphabet. The
left end of the tape is denoted by ., and the right end of the tape can extend as
far as needed. For the examples and problems in this textbook, the remaining
cells can contain nothing (blank), 0, or 1.
2. A head that can read or write to the tape, and then move left one cell (←), right
one cell (→), or stay put (•).
3. A register that stores the state of the Turing machine. Only a finite number
of states are allowed. Two special states are required, a starting state qs and a
halting state qh indicating that the program has finished.
4. A list of instructions or program. For each step, the Turing machine goes down
the list of instructions until it finds a line matching the current state of the ma-
chine and the current symbol on the tape. Then it write to the tape, moves ac-
cording to the instruction, and updates the state of the machine.
While modern computers are not built like this, Turing machines can compute ev-
erything that a circuit-based computer can compute. But since they are easier to
describe mathematically, they allow computer scientists to study the power and lim-
itations of computers. Before we discuss that, let us look at an example of how a
Turing machine can compute something.
Exercise 1.52. Visit the Wikipedia page on Model of Computation at https://en.wikiped
ia.org/wiki/Model_of_computation. Describe three models of computation besides
the Turing model.
1.8 Turing Machines 61
Let us show how a Turing machine can increment (add 1 to) a binary number. First,
consider incrementing 1011 by hand:
(carry) 1 1 1
1011
“+”0001
(sum) 1100
From this process, we can deduce an algorithm for how to increment binary num-
bers. Starting at the right, when we add 1 to 1, we get 0, and we carry a 1 to the next
column. Again, we add 1 to 1 and get 0, carrying 1 to the next column. Now we add
1 to 0, which yields 1, and nothing carries. Any remaining digits will be unchanged.
Thus, to increment binary numbers, we can flip all 1’s on the right to 0’s, and when
we reach the rightmost 0, we flip it to a 1.
This computation can be done using a Turing machine using the following pro-
gram:
. 1 0 1 1 ...
Now it goes through its list of instructions from top to bottom, searching for the first
instruction that matches its current state qs with current tape symbol .. It finds the
entry (qs , ., ., →, q1 ). So it writes . to the tape (so it is unchanged), moves one cell
right, and updates the state register to q1 :
62 1 Classical Information and Computation
q1
. 1 0 1 1 ...
The Turing machine again goes through its list of instructions from top to bottom,
searching for the first instruction that matches its current state q1 with current tape
symbol 1. It finds the entry (q1 , 1, 1, →, q1 ). So it writes 1 to the tape (so it is un-
changed), moves one cell right, and sets the state register to q1 (so it is unchanged):
q1
. 1 0 1 1 ...
Now the head reads 0 on the tape, so it searches its program from top to bottom,
finding the instruction (q1 , 0, 0, →, q1 ). Following this, it writes 0 to the tape (so it
is unchanged), moves the head right, and sets the state to q1 (so it is unchanged):
q1
. 1 0 1 1 ...
Now the tape reads 1, so the machine follows the instruction (q1 , 1, 1, →, q1 ). Fol-
lowing this, the head moves right again (while the tape and state are unchanged):
q1
. 1 0 1 1 ...
The machine again follows the instruction (q1 , 1, 1, →, q1 ), moving the head right:
q1
. 1 0 1 1 ...
Thus far, while the Turing machine was in the state q1 , the head moved to the right
past all the bits. Now the head reads blank, so the Turing machine follows (q1 , , , ←
, q2 ), writing a blank symbol (so it is unchanged), moving the head left, and updating
the state to q2 :
q2
. 1 0 1 1 ...
The head is now at the rightmost bit, and the Turing machine follows (q2 , 1, 0, ←
, q2 ), writing a 0 and moving left:
q2
. 1 0 1 0 ...
1.8 Turing Machines 63
The Turing machine follows (q2 , 1, 0, ←, q2 ) again, writing a 0 and moving left:
q2
. 1 0 0 0 ...
. 1 1 0 0 ...
So in the state q2 , the Turing machine moves to the left, flipping all 1’s to 0’s, until
it sees the first 0, at which it flips it to a 1 and halts. As seen on the final tape, we
have incremented 1011 to 1100, as expected.
Exercise 1.53. A Turing machine follows the program shown below:
(e) From your answers in parts (a) through (d), what does this Turing machine compute?
(f) What the program do if the tape initially has more than two bits?
Exercise 1.54. Write a program for a Turing machine that calculates the parity of a bit string of
arbitrary length. Assume the tape starts with the symbol . and is followed by the bit string, which
can be any length, followed by blanks. Write the parity as 0 or 1 (even or odd) to the blank after
the bit string, and then halt.
64 1 Classical Information and Computation
Turing machines are important for many reasons, but especially because of two
long-held beliefs regarding computation:
• The Church-Turing Thesis says that everything that is computable can be com-
puted with a Turing machine, although it could take a long time (e.g., exponen-
tial time).
This suggests that there are problems that cannot be computed. They are called
undecidable problems, the most famous of which is the halting problem (see
Exercise 1.55). Aside from such uncomputable problems, everything else can
be computed, and it can be computed using a Turing machine.
• The Strong Church-Turing Thesis says that any model of computation, be it
the circuit model or something else, can be simulated by a probabilistic Turing
machine with at most polynomial overhead.
A probabilistic Turing machine is one where the state of the system can be set
by the flip of a coin. Simulation with at most polynomial overhead implies that
an efficient algorithm on any kind of computer means an efficient algorithm also
exists on a Turing machine.
Quantum computers would not violate the regular Church-Turing Thesis. That
is, what is impossible to compute remains impossible. The hope, however, is that
quantum computers can violate the Strong Church-Turing Thesis, that they will ef-
ficiently solve problems that are inefficient on classical computers.
While there is no proof of this hope, there is strong evidence. Here are three
examples:
• Quantum computers can efficiently factor numbers (using Shor’s algorithm,
which appears in a later chapter in this book). Factoring is believed to be slow
for classical computers (factoring is in NP, and no algorithm has been found to
put it in P). This difficulty is the basis for a RSA, a widely used type of cryp-
tography. RSA will be explained later in this book. This would be a very useful
application of quantum computers, but currently, quantum computers are too
small to factor anything larger than 21 = 3 × 7.
• Determining the results of random quantum programs is called random circuit
sampling. This is easy for quantum computers, but it is believed to be hard for
classical computers. In 2019, scientists at Google and the University of Califor-
nia, Santa Barbara (UCSB) used a quantum processor to perform random cir-
cuit sampling. It took their quantum processor about three minutes and twenty
seconds. In contrast, they argued that a typical classical computer would take
approximately 10 000 years. Shortly after this result was published, IBM argued
that Summit, the largest supercomputer in the world at the time, could theoret-
ically perform the computation in 2.5 days rather than 10 000 years if it also
used its massive amounts of hard drive disk storage. This approach could use
1.8 Turing Machines 65
3 For the advanced reader, the experiment used 53 quantum bits. It takes 253 numbers to write
down the state of these quantum bits, and if each number uses 8 bytes, storing the entire quantum
state would take 253 · 8 = 256 = 72.058 × 1015 bytes = 72.058 petabytes.
66 1 Classical Information and Computation
PSPACE
NP-
complete
NP
BQP
Fig. 1.2: The generally accepted relationships between the complexity classes BQP,
NP, NP- COMPLETE, P, and PSPACE.
in BQP. The question is how much bigger BQP is than P, if at all. A proof of this may
be distant, however, as whether NP and PSPACE are bigger than P is still unsolved.
Exercise 1.55. The halting problem is to determine whether a computer program halts (finishes
running) or runs forever (like an infinite loop). Turing proved in 1936 that an algorithm to solve
this in general does not exist. For this problem, let us go through a sketch of a modern proof.
Say there exists a program H(P) that returns true if the program P halts and false otherwise. We
will be proving that such a program cannot exist. In pseudocode, we can write H(P) as
program H(P):
if P halts
return true
else:
return false
end
Next, if H(P) exists, we can write a program Z(P) that does the opposite. Z(P) takes a program P
and runs/loops forever if H(P) returns true, and it halts if H(P) returns false. In pseudocode,
program Z(P):
if H(P) is true:
run forever
else:
halt
end
(a) Say H says that some program P1 halts, i.e., H(P1 ) returns true. Does Z(P1 ) halt or run forever?
(b) Say H says that some program P2 runs forever, i.e., H(P2 ) returns false. Does Z(P2 ) halt or
run forever?
Now consider what happens if we run Z(Z), i.e., program Z with its own code as input.
(c) If H(Z) returns true, does Z(Z) halt or run forever? Where is the contradiction?
1.8 Turing Machines 67
(d) If H(Z) returns false, does Z(Z) halt or run forever? Where is the contradiction?
These contradictions mean that H(P) cannot exist.
Exercise 1.56. Watch “The Halting Problem - An Impossible Problem to Solve” by Up and Atom
on YouTube:
https://www.youtube.com/watch?v=t37GQgUPa6k
https://en.wikipedia.org/wiki/List_of_undecidable_problems
Describe three undecidable problems.
https://www.nature.com/articles/s41586-019-1666-5
From the abstract, fill in the blanks.
(a) “Our Sycamore processor takes about 200 seconds to sample one instance of a quantum circuit
.”
(b) “This dramatic increase in speed compared to
classical algorithms is an experimental realization of [computational]
.”
Exercise 1.59. Read the following excerpts from Scott Aaronson’s article “The Limits of Quantum
Computers” in Scientific American (March 2008):
(a) Page 65, box titled “The Good News.” What would be the “killer app” for quantum comput-
ers?
(b) Page 66, box titled “What Classical Computers Can and Cannot Do.” Fill in the blank: “NP-
complete problems are the of the NP problems.”
(c) Page 67, box titled “Where Quantum Computers Fit In.” Are quantum computers expected to
solve some, most, or all problems in NP?
68 1 Classical Information and Computation
1.9 Summary
The smallest unit of classical information is the bit, which has two possible states, 0
or 1. Bits can be used to encode information, such as using ASCII. Bits are operated
on by logic gates, including NOT, AND, OR, XOR, NAND, and NOR. Together,
these gates can be used to perform any computation, and sets of these gates are
also universal, such as {NOT, AND, OR} and {NAND}. The mathematics that de-
scribes logic gates is called boolean algebra. Logic gates can be made reversible,
and the Toffoli gate is a reversible version of the AND gate. In physical systems,
errors sometimes occur, but as long as the error rates are sufficiently low, they can
be corrected. Classical computers can efficiently solve some problems, while other
problems take an exponential amount of time. It is believed that quantum computers
can efficiently solve some of the problems that are hard for classical computers.
Chapter 2
One Quantum Bit
Quantum physics has a reputation for being difficult and confusing, exacerbated by
(incorrect) references to quantum mechanics in movies to justify their sci-fi plot ele-
ments. To help reduce this intimidation factor and ease the introduction of quantum
computing, I made a board game called Qubit Touchdown. It is shown in Fig. 2.1,
and it is available print-on-demand from The Game Crafter.1
Qubit Touchdown is a two player game. It consists of a game board, shown in
Fig. 2.2a, an orange football token, a die with only zeros and ones on it, and fifty-
two action cards, shown in Fig. 2.2b. To start the game, one player “kicks off” by
rolling the binary die, and the football token starts at zero or one, depending on
the outcome of the roll. Beginning with the other player, players take turns playing
action cards, which move the football according to the lines and arrows√on the game
board. For example, at position 0, H moves the ball to position +; X move the
ball to −i; X and Y move the ball to to 1; and Z, I, and S keep the ball at position 0.
For the measurement card, if the football is at 0 or 1, nothing happens. Otherwise,
one kicks off again by rolling the binary die. When a player scores a touchdown by
moving the ball into their opponent’s endzone, they roll the die to kick off again,
and play continues with the other player. Whoever scores the most touchdowns, by
the time all the action cards have been used, wins.
All of these game mechanics come from quantum computing.
1 https://www.thegamecrafter.com/games/qubit-touchdown
69
70 2 One Quantum Bit
It is not necessary to play Qubit Touchdown in order to continue with this text-
book.
Exercise 2.1. In Qubit Touchdown,√say the ball is at position 0. Where would it move if you played
(a) X, (b) Y , (c) Z, (d) H, (e) S, (f) X, (g) I, (h) measurement?
Exercise 2.2. In Qubit Touchdown,√say the ball is at position i. Where would it move if you played
(a) X, (b) Y , (c) Z, (d) H, (e) S, (f) X, (g) I, (h) measurement?
2.2 Superposition
A quantum bit, or qubit, is both similar to and different from classical bits in some
important ways. First, like a classical bit, a qubit can take two values, 0 or 1. Using
bra-ket notation or Dirac notation from quantum physics, we write 0 and 1 enclosed
between a vertical bar and an angle bracket called a ket:
|0i, |1i.
Although it may seem strange to write a quantum 0 and a quantum 1 like this, it will
be very useful later.
We can visualize these distinct states, |0i and |1i, as the north and south poles of
a unit sphere called the Bloch sphere:
2.2 Superposition 71
(a) (b)
Fig. 2.2: The Qubit Touchdown (a) game board and (b) action cards.
72 2 One Quantum Bit
z
|0i
y
x
|1i
Following the standard physics convention, the x-axis comes out of the page, the
y-axis points to the side, and z-axis is oriented up. Then, since the Bloch sphere
has radius 1, |0i corresponds to the (x, y, z) point (0, 0, 1), and |1i corresponds to
(0, 0, −1).
2.2.2 Superposition
If we had a classical bit, |0i and |1i would be the only two states. But the laws
of quantum mechanics allow a qubit to be a combination of |0i and |1i, called a
superposition of |0i and |1i. For example, here is a state that is equal parts |0i and
|1i:
1
√ (|0i + |1i) .
2
Since this state is half |0i and half |1i, it should be on the equator of the Bloch
sphere, which is halfway between the north and south poles:
z
y
x
This state is at the (x, y, z) point (1, 0, 0), where the Bloch sphere intersects the x-
axis. Later, we will learn how to calculate these coordinates, but for now, we will
focus on building geometric intuition.
2.2 Superposition 73
There are many other states on the equator of the Bloch sphere, all of which are
equal parts |0i and |1i. We can reach them by changing the relative phase of |0i and
|1i. For example, if we instead use a negative sign, we get
z
1
√ (|0i − |1i)
2 y
x
1
√ (|0i + i|1i)
2 y
x
We see that imaginary and complex numbers are used in quantum computing. In
case it has been awhile since you have used them, in the next section, we will review
complex numbers in detail. Continuing, to reach the −y-axis at (0, −1, 0), we use a
phase of −i:
z
1
√ (|0i − i|1i)
2 y
x
74 2 One Quantum Bit
These states appear frequently enough that they were given the names “plus,” “mi-
nus,” “i,” and “minus i”:
1
|+i = √ (|0i + |1i) ,
2
1
|−i = √ (|0i − |1i) ,
2
(2.1)
1
|ii = √ (|0i + i|1i) ,
2
1
|−ii = √ (|0i − i|1i) .
2
Drawing them together with |0i and |1i on the Bloch sphere, we get the following,
which also appears on the back of every action card in Qubit Touchdown:
z
|0i
|−i
|−ii
|ii
|+i y
x
|1i
In Qubit Touchdown, these six states correspond to the six positions on the game
board in Fig. 2.2a. The football token corresponds to a qubit, so the ball moving
around the game board corresponds to a qubit changing between these states (more
on this later).
Of course, there are many other points on the equator of the Bloch sphere. We
can reach them using other complex phases, such as
2.2 Superposition 75
1
√ |0i + eiπ/6 |1i
2 y
x
Superpositions are not restricted to the equator, either. They can favor |0i or |1i by
being in the northern or southern hemisphere, such as
√
3 1 2 1 − 2i
|0i + |1i |0i + |1i
2 2 3 3
z z
y y
x x
In fact,
Again, we will later see how to calculate where a quantum state is on the Bloch
sphere. Before we do that, however, we need to review complex numbers and discuss
measuring qubits.
Exercise 2.3. Draw a Bloch sphere and label the following locations:
(a) Where a qubit is exactly |0i.
(b) Where a qubit is exactly |1i.
(c) Where a qubit is half |0i and half |1i.
(d) Where a qubit is more |0i than |1i.
(e) Where a qubit is more |1i than |0i.
76 2 One Quantum Bit
z = x + iy.
√
For example, 1 + i 3 is a complex number. The parts are called components. We
denote the real component of z as ℜ(z), and it just equals x:
ℜ(z) = x.
ℑ(z) = y.
√ √ √
For example, ℜ(1 + i 3) = 1 and ℑ(1 + i 3) = 3. Real numbers are complex
numbers; their imaginary parts are just zero. Similarly, imaginary numbers are com-
plex numbers; their real parts are just zero.
The above form x + iy is called the Cartesian form or rectangular form of a com-
plex number. In quantum computing, it is often useful to write a complex number
as its length times r its complex phase eiθ :
z = reiθ .
This is called the polar form of a complex number, and any complex number can be
written this way. To convert from the Cartesian form x + iy to the polar form reiθ ,
we use the following equations, which we will prove in a moment:
p
r = x2 + y2 , (2.2)
y
θ = tan−1 . (2.3)
x
To convert from the polar form reiθ to the Cartesian form x+iy, we use the following
equations, which we will also prove in a moment:
x = r cos θ (2.4)
y = r sin θ . (2.5)
Im
x + iy
r y
θ
x Re
Its length is r, and the angle it makes counter-clockwise from the real axis is
θ . Since x and y are the legs of a right triangle, and r is the hypotenuse, the
2 2 2
p says that x + y = r . Taking the square root of each
Pythagorean theorem
2 2
side, we get r = x + y , which proves Eq. (2.2).
Next, from the drawing, θ is an angle in a right triangle, and y is opposite of
θ , x is adjacent, and r is the hypotenuse. We can relate these quantities using
the trigonometric functions sine, cosine, and tangent.2 First, using sine, we get
sin θ = y/r. Multiplying both sides by r, we get y = r sin θ , which is Eq. (2.5).
Next using cosine, we get cos θ = x/r. Multiplying both sides by r, we get
x = r cos θ , which is Eq. (2.4). Finally, using tangent, we get tan θ = y/x. Taking
the inverse tangent of both sides, we get θ = tan−1 (y/x), which is Eq. (2.3).
• Algebraically, we can write a complex phase in Cartesian form using Euler’s
formula, which says says that
If you have taken more advanced math than is required for this textbook, you
may be familiar with Euler’s formula and its proof. (It is typically seen during
the second semester of calculus.) If not, no worries. You do not need to know
where it comes from. It is simple enough to memorize now, which you should
do, since it will show up again.
Now using Euler’s formula, we can write the polar form of a complex number
as
z = reiθ = r (cos θ + i sin θ ) = r| cos
{z θ} +i r| sin
{z θ} .
x y
Thus, the real part of z is x = r cos θ , which is Eq. (2.4), and the imaginary part
is y = r sin θ , which is Eq. (2.5). If we take the sum of the squares of these
components, we get
2 Many people remember the trigonometric functions using the mnemonic SOH-CAH-TOA, where
sine is opposite over hypotenuse (SOH), cosine is adjacent over hypotenuse (CAH), and tangent is
opposite over adjacent (TOA).
78 2 One Quantum Bit
In the last equality, we used the Pythagorean identity sin2 θ +p cos2 θ = 1 from
trigonometry. Taking the square root of both sides, we get r = x2 + y2 , which
proves Eq. (2.2). Next, if we divide y = r sin θ by x = r cos θ , we get
y sin θ
= = tan θ .
x cos θ
Taking the inverse tangent of both sides, we get Eq. (2.3).
√
For example, say we have a complex number in Cartesian form, z = 1+i 3. To con-
√ 2 √ √
q
vert this to polar form, we calculate its length r = 12 + 3 = 1 + 3 = 4 = 2
√
and angle θ = tan−1 ( 3/1) = π/3 radians or 60◦ . The convention is to use radians,
so putting these together, the polar form is z = 2eiπ/3 .
There are a few more aspects of complex numbers that come up frequently in
quantum computing, so let us review them now:
• The complex conjugate (or just conjugate) of a complex number is the complex
number obtained by negating its imaginary part. That is, we replace i with −i.
We denote the complex conjugate of z as z∗ , so if z = x + iy = reiθ , then
z∗ = x − iy = re−iθ .
√ ∗ √ ∗
From the previous example, 1 + i 3 = 1 − i 3 and 2eiπ/3 = 2e−iπ/3 .
• The norm of a complex number z, which we denote |z|, is simply its length r:
|z| = r.
√
Then from the previous example, |1 + i 3| = 2, since its length r is 2.
• The norm-square of a complex number z, which we denote |z|2 , is simply the
square of its norm, so it is r2 :
|z|2 = r2 .
√
From the previous example, |1 + i 3|2 = 4. One way to calculate the norm-
square is multiplying a complex number by its conjugate:
We can prove that this works using either the Cartesian and polar form. In Carte-
sian form,
Finally, Eq. (2.7) gives us another way to find the norm of z, by taking the square
root of z times its conjugate z∗ :
2.3 Measurement 79
q √ √
|z| = |z|2 = z∗ z = zz∗ .
2.3 Measurement
In a previous section, we had the following qubit, which was on the equator of the
Bloch sphere:
z
1
√ |0i + eiπ/6 |1i
2 y
x
Although the laws of quantum mechanics permit this superposition of |0i and |1i,
it also demands that if we measure the qubit, such as at the end of a computation
in order to read the result, we get a single, definite value. That is, we get |0i or
|1i, each with some probability, not a superposition of |0i and |1i. Geometrically,
this particular qubit lies on the equator, halfway between the north and south poles,
so if we measure it, we get |0i with probability 1/2 or |1i with probability 1/2. To
80 2 One Quantum Bit
calculate these probabilities, we take the norm-square of the coefficient of |0i or |1i.
That is, the probability of getting |0i is
1 2 1
√ = ,
2 2
2 1 − 2i
|0i + |1i y
3 3
x
Geometrically, since the qubit is closer to the south pole, we expect that the prob-
ability of getting |1i is greater than the probability of getting |0i. To get the exact
probabilities, we calculate the norm-square of each amplitude:
2
2
= 4,
3 9
and
1 − 2i 2 1 − 2i 1 + 2i 1 + 2i − 2i − 4i2
5
3 = 3 = = .
3 3 9
So, if we measure the qubit, the probability of getting |0i is 4/9, and the probability
of getting |1i is 5/9. As expected, the probability of getting |1i is greater than the
probability of getting |0i, and also note that the total probability is 4/9 + 5/9 = 1,
as it must.
Exercise 2.6. A qubit is in the state
2.3 Measurement 81
√
1+i 3 2−i
|0i + |1i.
3 3
If you measure the qubit, what is the probability of getting
(a) |0i?
(b) |1i?
Say a qubit is superposition of |0i and |1i. Say we measure it, and the outcome
is |0i. Now for something new: The qubit is no longer in a superposition of |0i and
|1i. It is now simply |0i, and we know because we measured it. Measuring the qubit
changed it. It forced it to take a stand. We say the state has collapsed to |0i. If we
measure the qubit again, we get |0i with probability 1. This aspect of measurement
is important enough to box:
In Qubit Touchdown, playing the measurement action card, or kicking off after a
touchdown, corresponds to measuring the qubit. For the measurement action card,
if the ball is at position 0 or 1, then nothing happens, just like measuring a qubit in
state |0i or |1i returns the same state with probability 1. If the ball is at position i or
−i, then the binary die is rolled, and the ball is moved to 0 or 1 with 50% probability
each, just like measuring a qubit in the |ii or |−ii states collapses the state to |0i or
|1i with 50% probability. Finally, after scoring a touchdown, the ball is either at the
+ or − positions in the endzones. Then, the binary die is rolled, moving the ball
to 0 or 1 with 50% probability each. In the same way, when a qubit is in the |+i
or |−i state, measuring it yields |0i or |1i with 50% probability each, and the state
collapses to whichever state was measured.
Exercise 2.7. A qubit is in the state
2 1 + 2i
|0i + |1i.
3 3
Say you measure the qubit and get |0i. If you measure the qubit a second time, what is the proba-
bility of getting
(a) |0i?
(b) |1i?
2.3.2 Normalization
We normalize this state by finding the normalization constant A that ensures that the
total probability is 1. So,
√ √
1 = (A 2)(A 2)∗ + (Ai)(Ai)∗
= 2|A|2 + |A|2
= 3|A|2
1
|A|2 = .
3
As we will prove later, the overall phase does not matter, so we might as well pick
A to be real. Thus,
1
A= √ ,
3
and the normalized state is
1 √
√ 2|0i + i|1i .
3
Even though we introduced |0i and |1i as the north and south poles, respectively, of
the Bloch sphere, the Bloch sphere is not a planet, and it is not spinning. Then, any
two opposite points could be taken as the north and south poles. For example, |+i
and |−i could be the north and south poles, or |ii and |−ii, or any opposite points.
A set of distinct measurement outcomes is called a basis, and {|0i, |1i} is called the
Z-basis because they lie on the z-axis of the Bloch sphere. Similarly, {|+i, |−i} is
called the X-basis because they lie on the x-axis of the Bloch sphere, and {|ii, |−ii}
is called the Y -basis because they lie on the y-axis of the Bloch sphere. We can
measure with respect to any of these bases, or with respect to any two states on
opposite sides of the Bloch sphere.
For example, consider a qubit in the state
2.3 Measurement 83
√
3 1
|0i + |1i,
2 2
which appears on the Bloch sphere as shown below:
z
√
3 1 |0i
|0i + |1i
2 2
|−i
|−ii •
|ii
|+i y
x
|1i
Let us measure this with respect to four different bases: the Z-basis {|0i, |1i}, the
X-basis {|+i, |−i}, the Y -basis {|ii, |−ii}, and a fourth basis (below).
1. If we measure the qubit in the Z-basis {|0i, |1i}, then we get |0i with probability
3/4 or |1i with probability 1/4.
2. What if we measure in the X-basis {|+i, |−i} instead? Geometrically, we the
probability of getting |+i should be much higher than the probability of getting
|−i because the state is so much closer to |+i on the Bloch sphere. To calculate
the probabilities precisely, we need to express the state in terms of |+i and |−i
so that we can identify the amplitudes and then find their norm-squares. From
the definitions of |+i and |−i in Eq. (2.1), we have
1 1
|0i = √ (|+i + |−i) , |1i = √ (|+i − |−i) .
2 2
Substituting into the state of our qubit,
√ √
3 1 3 1 1 1
|0i + |1i = √ (|+i + |−i) + √ (|+i − |−i)
2 2 2 2 2 2
√ √
3+1 3−1
= √ |+i + √ |−i.
2 2 2 2
Now the amplitudes are easy to identify, and we can find the probabilities by
taking their norm-squares. The probability of measuring |+i is
√
3 + 1 2 √3 + 2
√ = ≈ 0.93,
2 2 4
This is consistent with the Bloch sphere, since the state is much closer to |+i
than it is to |−i.
3. Now what if we measure in the {|ii, |−ii} basis? Geometrically, the Bloch
sphere reveals that the state is halfway between |ii and |−ii, so we get one
or the other with a probability 1/2 each. We can also calculate this by rewrit-
ing the state in terms of |ii and |−ii and then finding the norm-square of the
amplitudes. From the definitions of |ii and |−ii in Eq. (2.1),
1 −i
|0i = √ (|ii + |−ii) , |1i = √ (|ii − |−ii) .
2 2
Substituting,
√ √
3 1 3 1 −i
|0i + |1i = √ (|ii + |−ii) + √ (|ii − |−ii)
2 2 2 2 2
√ √
3−i 3+i
= √ |ii + √ |−ii.
2 2 2 2
So, the probability of getting |ii is
√
3 − i 2 3 + 1 1
√ = = ,
2 2 8 2
as expected.
4. Consider the following two states, which we will call |ai and |bi:
√
3 i
|ai = |0i + |1i,
2 √ 2
i 3
|bi = |0i + |1i.
2 2
Here they are on the Bloch sphere:
2.3 Measurement 85
z
√ √
3 1 3 i
|0i + |1i |0i + |1i
2 2 2 2
•
y
x
√
i 3
|0i + |1i
2 2
|ai is located 30◦ above the y-axis, and |bi is located 30◦ below the −y-axis.
(You will prove this in Exercise 2.16.) Since they are located on opposite points
of the Bloch sphere, they are a basis. Let us measure in this {|ai, |bi} basis.
Geometrically, the qubit is closer to |ai, so we expect a higher probability of
getting |ai than |bi. To calculate the precise numbers, we first write |0i and |1i
in terms of |ai and |bi:
√ √
3 i −i 3
|0i = |ai − |bi, |1i = |ai + |bi.
2 2 2 2
Substituting, the state of our qubit is
√ √ √ ! √ !
3 1 3 3 i 1 −i 3
|0i + |1i = |ai − |bi + |ai + |bi
2 2 2 2 2 2 2 2
√
3−i 3(1 − i)
= |ai + |bi.
4 4
Taking the norm-square of each amplitude, the probability of getting |ai is
3 − i 2 9 + 1 5
4 = 16 = 8 ,
Later, when we describe qubits in the mathematics of linear algebra, we will see
another way to convert between bases.
Exercise 2.10. A qubit is in the state
√
1 3
|0i − |1i.
2 2
86 2 One Quantum Bit
(a) If you measure it in the Z-basis {|0i, |1i}, what states can you get and with what probabilities?
(b) Write the qubit’s state in terms of |+i and |−i.
(c) If you measure it in the basis {|+i, |−i}, what states can you get and with what probabilities?
Exercise 2.11. The following two states are opposite points on the Bloch sphere:
√
3 i
|ai = |0i + |1i,
2 2
√
i 3
|bi = |0i + |1i.
2 2
So, we can measure relative to them. Now consider a qubit in the state
√
1 3
|0i − |1i.
2 2
(a) Write the qubit’s state in terms of |ai and |bi.
(b) If you measure the qubit in the basis {|ai, |bi}, what states can you get and with what proba-
bilities?
We have already seen that measuring a qubit collapses the state to whatever was
measured. This can lead to interesting statistics, even moreso if we change the mea-
surement basis. For example, consider the following three measurements:
1. Say we first measure the qubit in the Z-basis {|0i, |1i}. Then the qubit collapses
to |0i or |1i.
2. Next, if we measure in the basis {|+i, |−i}, then since both |0i and |1i are
halfway between |+i and |−i, the qubit collapses to |+i or |−i, each with
probability 1/2.
3. If we then measure in the Z-basis {|0i, |1i}, then since |+i and |−i are halfway
between |0i and |1i, the probability of each is 1/2. We can continue alternating
between these two measurement bases, each time having a 50:50 chance of
getting each outcome.
Exercise 2.12. A qubit is in the state |0i. If you measure it in the X-basis {|+i, |−i} and then
measure it again in the Z-basis {|0i, |1i}, what is the probability of getting
(a) |0i?
(b) |1i?
We know that a qubit can be visualized as a point on the Bloch sphere. Now, let us
explain how to determine where the point should be.
2.4 Bloch Sphere Mapping 87
Say the qubit from the last section is multiplied by an overall, global phase:
√ !
iθ 3 1
e |0i + |1i ,
2 2
for some angle θ . If we measure this in the Z-basis, {|0i, |1i} the probability of
getting |0i is
√ 2
iθ 3 3
e = ,
2 4
iθ 1 2 1
e = ,
2 4
as they were without the global phase. So the phase does not change anything.
If we instead measure in the X-basis {|+i, |−i}, then we can rewrite the state as
√ √ !
iθ 3+1 3−1
e √ |+i + √ |−i .
2 2 2 2
as they were before without the global phase. So again, the phase does not change
anything.
This is true no matter what measurement basis we use, and it leads to the follow-
ing result:
As such, global phases can be dropped/ignored. States that differ by a global phase
are actually the same state; they correspond to the same point on the Bloch sphere.
Note that a relative phase is physically significant, such as
88 2 One Quantum Bit
1
|+i = √ (|0i + |1i)
2
vs
1 1
|ii = √ (|0i + i|1i) = √ |0i + eiπ/2 |1i .
2 2
These correspond to different points on the Bloch sphere, and they can be distin-
guished by measurements in appropriate bases. Although measuring |+i and |ii in
the Z-basis yields the same statistics, i.e., |0i with probability 1/2 or |1i with prob-
ability 1/2, measuring in the X-basis {|+i, |−i}yields different results. Measuring
|+i in the X-basis always yields |+i, but measuring |ii in the X-basis yields |+i or
|−i with 50:50 probability.
Exercise 2.13. Is there a measurement that can distinguish the following pairs of states? If yes,
give a measurement. If no, explain your reasoning.
1 eiπ/8
(a) |+i = √ (|0i + |1i) and eiπ/8 |+i = √ (|0i + |1i).
2 2
1 1
(b) |+i = √ (|0i + |1i) and |−i = √ (|0i − |1i).
2 2
(c) |0i and eiπ/4 |0i.
A generic quantum state is typically called ψ (the Greek letter “psi,” which is pro-
nounced “sigh”), and since it is quantum, we write it as a ket |ψi. Now say we have
a generic qubit |ψi with some amplitudes α and β :
where |α|2 + |β |2 = 1 for normalization. Since the global phase does not matter, we
can assume that α is real and positive, and β may be complex. To determine the
location of this qubit on the Bloch sphere, we first parameterize, or write in terms
of other parameters, α and β in terms of two angles θ and φ :
θ θ
α = cos , β = eiφ sin .
2 2
With 0 ≤ θ ≤ π and 0 ≤ φ < 2π, this captures all the properties we need: α is
real and positive, β is complex, and the state is normalized. Substituting, we have
rewritten the qubit’s state as
θ θ
|ψi = cos |0i + eiφ sin |1i. (2.8)
2 2
where ≡ denotes “equivalent to” since global phase does not matter and can be
dropped. Comparing this to the Bloch sphere parameterization in Eq. (2.8), we still
need to change the minus sign to a plus sign. We can do this using eiπ = −1. Then,
the state is √ √
3 iπ −iπ/6 1 3 1
|0i + e e |1i = |0i + ei5π/6 |1i.
2 2 2 2
Now it takes the form of Eq. (2.8), and we identify
√
θ 3 θ 1
cos = , eiφ = ei5π/6 , sin = .
2 2 2 2
Solving the first or last equation for θ using the inverse cosine or inverse sine, and
solving the second equation for φ , we get
π 5π
θ= , φ= .
3 6
Thus, plugging into Eq. (2.8), the state of the qubit is equivalent to
π/3 π/3
cos |0i + ei5π/6 sin |1i.
2 2
Next, to map this qubit to a location on the Bloch sphere, we identify θ and φ as
the following angles:
90 2 One Quantum Bit
z
θ
y
φ
So, θ measures the angle down from the north pole, called the polar angle, and φ
measures the angle across from the x-axis in the xy plane, called the azimuthal angle.
If you have taken more advanced mathematics than is required for this book, these
are precisely spherical coordinates where the radius of 1. Spherical coordinates are
typically covered in the third semester of calculus, but everything you need for this
textbook will be explained here.
Returning to our example, we had θ = π/3 and φ = 5π/6, so the state is located
on the Bloch sphere at an angle of π/3 or 60◦ from the north pole, and 5π/6 or 150◦
rotated to the side:
z
√
3+i 3 1
|0i − |1i y
4 2
x
z z
sin
θ
cos θ
θ zoom
−−−→ 1
y
θ
φ
y
φ
x
x
In this picture, we drew a dashed line from the point to the z-axis, perpendicular to
it. This creates a right triangle, which we shaded gray. Since the radius of the Bloch
sphere is 1, the hypotenuse of the gray triangle is 1, which we labeled in the picture.
Then, since sin θ is opposite over hypotenuse, and the hypotenuse is 1, we get that
sin θ is the opposite side, which we labeled in the picture. Similarly, since cos θ is
adjacent over hypotenuse, and the hypotenuse is 1, we get that cos θ is the adjacent
side, which we also labeled in the picture. This adjacent side gives the z-coordinate
(height) of the point, i.e., z = cos θ .
To get the x and y coordinates, we look at the projection onto the xy-plane:
z
sin
θ
cos θ
θ
φ
os
y
θc
sin
θ
sin
φ
sin θ sin φ
x
First, we copied sin θ from the top dashed line to below, since they are opposite sides
of a rectangle. This is the hypotenuse of the bottom gray triangle. Then, since sin φ
is opposite over hypotenuse, we get that the opposite side is equal to the hypotenuse
times sin φ , or sin θ cos φ , which we labeled in the drawing. This corresponds to
the y-coordinate (distance to the right) of the point, i.e., y = sin θ sin φ . Similarly,
since cos φ is adjacent over hypotenuse, we get that the adjacent side is equal to
the hypotenuse times cos φ , or sin θ cos φ . This is labeled as the left side of the
gray triangle in the drawing, and it corresponds to the x-coordinate (distance out of
the page), i.e., x = sin θ cos φ . Combining these results with the z-coordinate, we
can convert from spherical coordinates to Cartesian coordinates using the following
formulas:
92 2 One Quantum Bit
x = sin θ cos φ ,
y = sin θ sin φ , (2.9)
z = cos θ .
Continuing the previous example, we had θ = π/3 and φ = 5π/6, so the (x, y, z)
coordinates are of the qubit are
√ √
π 5π 3 − 3 3
x = sin cos = · =− ,
3 6 2 2 4
π 5π √3 1 √3
y = sin sin = · = ,
3 6 2 2 4
π 1
z = cos = .
3 2
√
So, the qubit corresponds to point (−3/4, 3/4, 1/2) on the Bloch sphere.
Exercise 2.14. A qubit is in the state
1
|ii = √ (|0i + i|1i) .
2
(a) Where on the Bloch sphere is this state? Give your answer in (θ , φ ) coordinates.
(b) Where on the Bloch sphere is this state? Give your answer in (x, y, z) coordinates.
(c) Sketch the point on the Bloch sphere.
Exercise 2.15. A qubit is in the state
√
1−i 3
√ |0i + |1i.
2 2 2
(a) Where on the Bloch sphere is this state? Give your answer in (θ , φ ) coordinates.
(b) Where on the Bloch sphere is this state? Give your answer in (x, y, z) coordinates.
(c) Sketch the point on the Bloch sphere.
Exercise 2.16. Consider the following two states from Exercise 2.11:
√
3 i
|ai = |0i + |1i,
2 2
√
i 3
|bi = |0i + |1i.
2 2
Prove these are opposite points of the Bloch sphere by finding their points in spherical coordinates
(θa , φa ) and (θb , φb ). Verify that θb = π − θa and φb = φa + π, which means they lie on opposite
points of the Bloch sphere.
Physically, any quantum system with two distinct states can be used as a qubit.
While one is not expected to be familiar with quantum physics, some examples
include:
2.5 Physical Qubits 93
Quantum gates act on qubits, like logic gates act on bits. In this section, we will
explore what quantum gates are.
A quantum gate transforms the state of a qubit into other states. As we will
see later, we often use the capital letter U to denote a quantum gate. For example,
consider a quantum gate that performs the following map:
√
2−i 1
U|0i = |0i − |1i,
2 √ 2
1 2+i
U|1i = |0i + |1i.
2 2
2.6 Quantum Gates 95
For this to be a valid quantum gate, the total probability must remain 1. Assuming
the original state was normalized, i.e., |α|2 + |β |2 = 1, we can calculate the total
probability by summing the norm-square of each amplitude to see if it is still 1:
√ 2 √ 2
2−i 1 1 2 + i
+ β + −α + β
α
2 2 2 2
√ ! √ !
2−i 1 ∗ 2+i ∗1
= α +β α +β
2 2 2 2
√ ! √ !
1 2+i ∗1 ∗ 2−i
+ −α + β −α + β
2 2 2 2
√ √ √ √
( 2 − i)( 2 + i) 2−i 2+i 1
= |α|2 + αβ ∗ + β α∗ + |β |2
4 √ √ 2 √4 √ 4
1 2 − i 2 + i ( 2 + i)( 2 − i)
+ |α|2 − αβ ∗ − β α∗ + |β |2
4 4 4 4
23 21 21 23
= |α| + |β | + |α| + |β |
4 4 4 4
= |α|2 + |β |2
= 1.
Quantum gates are linear maps that keep the total probability equal to 1.
Later, when we introduce linear algebra, we will learn that operators correspond
to tables of numbers called matrices, and valid quantum gates are types of matrices
called unitary matrices. That is why quantum gates are often labeled U, for unitary.
Exercise 2.21. Consider a map U that transforms the Z-basis states as follows:
Exercise 2.22. Consider a map U that transforms the Z-basis states as follows:
√ √
3 3+i
U|0i = |0i + |1i,
2 4
√ √
3+i 3 + 3i
U|0i = |0i − |1i.
4 4
Say |ψi = α|0i + β |1i is a normalized quantum state, i.e., |α|2 + |β |2 = 1.
(a) Calculate U|ψi.
(b) From your answer to (a), is U a valid quantum gate? Explain your reasoning.
Recall from Section 1.5 that a classical logic gate is reversible its outputs are unique.
For example, a gate with input A and output B with the following truth table is
reversible, since it is always possible to determine the input from the output:
AB
0 1
1 0
Thus, the gate does the following to a bit:
0 → 1,
1 → 0.
How would this gate act on a qubit? It would map the following:
Gate|0i = |1i,
Gate|1i = |0i.
|β |2 + |α|2 = |α|2 + |β |2 = 1.
Thus, this classical reversible logic gate is also a valid quantum gate.
This is true in general. Any classical reversible logic gate simply permutes (shuf-
fles) the amplitudes around. This chapter is just on a single qubit, but jumping ahead
to the next chapter on multiple qubits, if there are more than two amplitudes, a
2.6 Quantum Gates 97
classical reversible logic gate would similarly just permute them, so the state stays
normalized. Thus,
In contrast, irreversible gates are not valid quantum gates. For example, consider
the irreversible gate with the following truth table:
AB
0 0
1 0
This would act on the basis states of a qubit as:
Gate|0i = |0i,
Gate|1i = |0i.
Now, the amplitudes are not permuted. Instead, they were combined. The total prob-
ability of this is
|α + β |2 = |α|2 + |β |2 + α ∗ β + αβ ∗ = 1 + α ∗ β + αβ ∗ 6= 1,
Although any probability-preserving linear map is a valid quantum gate, let us list
some important one-qubit gates that frequently appear in quantum computing:
98 2 One Quantum Bit
• The identity gate turns |0i into |0i and |1i into |1i, hence doing nothing:
I|0i = |0i,
I|1i = |1i.
This is a classical reversible gate (the identity gate), so it keeps states normal-
ized and is a valid quantum gate.
In Qubit Touchdown, this corresponds to the Identity Gate action card, which
does nothing to the football’s position.
• The Pauli X-Gate, or NOT gate, turns |0i into |1i, and |1i into |0i:
X|0i = |1i,
X|1i = |0i.
This is a classical reversible gate (the NOT gate), so it keeps states normalized
and is a valid quantum gate.
On the Bloch sphere, it can be shown that X is a rotation of 180◦ about the
x-axis:
z
y
x
With this rotation in mind, we geometrically see that X causes |0i (the north
pole) to rotate to |1i (the south pole), and vice versa. We also see that |ii and
|−ii rotate to each other, whereas |+i and |−i are unchanged. Note, however,
that mathematically X|−i = −|−i ≡ |−i since the global phase does not matter.
If we apply the X-gate twice, we rotate around the x-axis of the Bloch sphere
by 360◦ , which does nothing. Then, X 2 = I. We can use this fact to simplify
consecutive applications of X. For example,
500
X 1001 = X 1000 X = X 2 X = I 500 X = X.
Y |0i = i|1i,
Y |1i = −i|0i.
2.6 Quantum Gates 99
This is not a classical gate at all because of the i and −i. Let us prove that it is
a valid quantum gate by acting on a general superposition:
y
x
So, if we apply the Y -gate twice, we rotate around the y-axis of the Bloch sphere
by 360◦ , which does nothing. Then, Y 2 = I. In Qubit Touchdown, the Y Gate
action card corresponds to this quantum gate.
• The Pauli Z-Gate keeps |0i as |0i and turns |1i into −|1i:
Z|0i = |0i,
Z|1i = −|1i.
This is not a classical gate at all. In Exercise 2.27, you will show that this is a
valid quantum gate.
On the Bloch sphere, it can be shown that Z is a rotation of 180◦ about the
z-axis:
100 2 One Quantum Bit
y
x
• Phase gate, which is the square root of the Z-gate (i.e., S2 = Z).
S|0i = |0i,
S|1i = i|1i.
In Exercise 2.27, you will show that this is a valid quantum gate.
On the Bloch sphere, it can be shown that S is a rotation of 90◦ about the z-axis:
z
90◦
y
x
T |0i = |0i,
T |1i = eiπ/4 |1i.
In Exercise 2.27, you will show that this is a valid quantum gate.
On the Bloch sphere, T is a rotation of 45◦ about the z-axis:
z
45◦
y
x
Exercise 2.27. Consider the gate Rz (θ ), which rotates about the z-axis by angle θ :
Rz (θ )|0i = |0i,
Rz (θ )|1i = eiθ |1i.
The Z-gate, S-gate, and T -gate are all specific instances of the Rz gate, with Z = Rz (π), S =
Rz (π/2), and T = Rz (π/4). Say |ψi = α|0i+β |1i is a normalized quantum state, i.e., |α|2 +|β |2 =
1.
(a) Calculate Rz (θ )|ψi.
(b) Show that the total probability of Rz (θ )|ψi is 1, so Rz (θ ) is a valid quantum gate, and hence,
Z, S, and T are all valid quantum gates.
• The Hadamard gate turns |0i into |+i, and |1i into |−i:
1
H|0i = √ (|0i + |1i) = |+i,
2
1
H|1i = √ (|0i − |1i) = |−i.
2
In Exercise 2.28, you will show that this is a valid quantum gate.
On the Bloch sphere, it can be shown that H is a rotation of 180◦ about the
x + z-axis:
102 2 One Quantum Bit
x+z
y
x
Then, H maps between |0i and |+i, between |1i and |−i, and between |ii and
|−ii. Also, applying H twice rotates by 360◦ , which does nothing. So, H 2 = I.
In Qubit Touchdown, the H action card moves the football between 0 and +,
between 1 and −, and between i and −i. This corresponds to the Hadamard
gate, and we have already proved these moves geometrically. Let us also prove
them algebraically. From the definition of the Hadamard gate, we already have
H|0i = |+i and H|1i = |−i. Going in the other direction,
1
H|+i = H √ (|0i + |1i)
2
1
= √ (H|0i + H|1i)
2
1 1 1
= √ √ (|0i + |1i) + √ (|0i − |1i)
2 2 2
= |0i,
H|−i = |1i.
We also have
1
H|ii = H √ (|0i + i|1i)
2
1
= √ (H|0i + iH|1i)
2
1 1 i
= √ √ (|0i + |1i) + √ (|0i − |1i)
2 2 2
1 1+i 1−i
=√ √ |0i + √ |1i
2 2 2
1 iπ/4
= √ e |0i + e−iπ/4 |1i
2
2.6 Quantum Gates 103
iπ/4 1 −iπ/2
= e √ |0i + e |1i
2
1
= eiπ/4 √ (|0i − i|1i)
2
iπ/4
= e |−ii
≡ |−ii.
We can combine these quantum gates to create all sorts of states. For example,
1
HST H|0i = HST √ (|0i + |1i)
2
1
= HS √ |0i + eiπ/4 |1i
2
1
= H √ |0i + ei3π/4 |1i
2
1 1 i3π/4 1
= √ √ (|0i + |1i) + e √ (|0i − |1i)
2 2 2
1 h i3π/4
i3π/4
i
= 1+e |0i + 1 − e |1i ,
2
where to go from the second to the third line, we used Euler’s formula Eq. (2.6) so
that eiθ + e−iθ = 2 cos θ . Similarly, the probability of getting |1i is
2 1 √ !
1 i3π/4 3π 1 2
1−e = 2 − 2 cos = 1+ ≈ 0.854.
2 4 2 2 2
Exercise 2.31. Prove that HXH = Z by showing that HXH|0i and Z|0i result in the same state,
and HXH|1i and Z|1i result in the same state. Such an equation is called a circuit identity.
You may have noticed that all the quantum gates from the last sections were rotations
by some angle around some axis. This is true in general:
This is because rotations on the Bloch sphere satisfy the two properties that we
require of quantum gates: they are linear maps, and they keep the total probability
equal to 1. Proving that rotations are linear requires some math, which we sketch
below. Rotations keep the total probability equal to 1 because a qubit is a point on
the Bloch sphere, so if we rotate it, it remains a point on the Bloch sphere.
Mathematically, say we rotate by angle θ about some axis of rotation, which we
can specify in terms of the x-, y-, and z-axes. We denote the direction of the x-axis
by x̂, the direction of the y-axis by ŷ, and the direction of the z-axis by ẑ. Then, we
can denote the axis of rotation by n̂, and
n̂ = nx x̂ + ny ŷ + nz ẑ.
Note n̂ is a unit vector, meaning it must has length 1, i.e., n2x + n2y + n2z = 1.
For example, the Hadamard gate is a rotation by θ = 180◦ = π radians about the
axis halfway between the x- and z-axes, and we can express this axis by
1 1
n̂ = √ x̂ + √ ẑ,
2 2
Now, we state as a fact (without proof) that a rotation by angle θ about axis
n̂ = (nx , ny , nz ) can be written in terms of I, X, Y , and Z:
iγ θ θ
U = e cos I − i sin (nx X + nyY + nz Z) , (2.10)
2 2
where γ is a global phase that we can set to anything (or drop), since it does not have
any physical relevance. √ √
Returning to our example of the Hadamard gate, with θ = π and n̂ = (1/ 2, 0, 1/ 2),
we have
π 1
iγ π 1
U = e cos I − i sin √ X + 0Y + √ Z
2 2 2 2
1
= −ieiγ √ (X + Z) .
2
To show that U is the Hadamard gate, let us see how it acts on |0i and |1i:
1 1
U|0i = −ieiγ √ (X + Z) |0i = −ieiγ √ (X|0i + Z|0i)
2 2
1
= −ieiγ √ (|1i + |0i) = −ieiγ |+i,
2
iγ 1 1
U|1i = −ie √ (X + Z) |1i = −ieiγ √ (X|1i + Z|1i)
2 2
1
= −ieiγ √ (|0i − |1i) = −ieiγ |−i.
2
We can drop the global phase of −ieiγ , so U|0i = |+i and U|1i = |−i, which is the
Hadamard gate. Alternatively, we can choose γ = π/2, and then since −i = ei3π/2 ,
we the global phase is −ieiγ = ei3π/2 eiπ/2 = ei2π = 1.
We can also use Eq. (2.10) to prove that rotations are linear. Note U is a sum of I,
X, Y , and Z with numbers as coefficients; we call this a linear combination of I, X,
Y , and Z. Since each of these gates distribute over superpositions, U also distributes
over superpositions, and so it is linear:
θ θ
U(α|0i + β |1i) = eiγ cos I − i sin (nx X + nyY + nz Z) (α|0i + β |1i)
2 2
θ θ
= αeiγ cos I − i sin (nx X + nyY + nz Z) |0i
2 2
θ θ
+ β eiγ cos I − i sin (nx X + nyY + nz Z) |1i)
2 2
= αU|0i + βU|1i.
Exercise 2.34. Consider a rotation by 180◦ around the axis equidistant from the x-, y-, and z-axes.
Below, the first picture shows the axis on the Bloch sphere. It may be a little difficult to visualize,
however, so another description of it using a cube is shown below in the second picture. The cube
has a corner at the origin and edges of length s along the x-, y-, and z-axes. Then, the axis of rotation
goes through the origin and the point (s, s, s), and the axis of rotation is drawn as a thicker line.
z
z
x+y+z
s
(s, s, s)
y
s
x y
s
x
(a) Draw the Bloch sphere and show where |0i goes after applying the rotation. Do this without
any calculations by visualizing the rotation on the Bloch sphere.
(b) Draw the Bloch sphere and show where |1i goes after applying the rotation. Do this without
any calculations by visualizing the rotation on the Bloch sphere.
(c) What is n̂? Hint: It should have equal components in the x-, y-, and z-axes, and it should be a
unit vector.
(d) Use Eq. (2.10) to express the rotation U is terms of I, X, Y , and Z.
(e) Find U|0i.
(f) Find U|1i.
Just like we can draw a classical circuit diagram consisting of bits and logic gates,
we can draw quantum circuit diagrams consisting of qubits and quantum gates. For
example, ZSH|0i is
|0i H S Z
The circuit is read left-to-right, just like a classical circuit diagram. So we start with
a single qubit in the |0i state and apply a Hadamard gate H to it, followed by a phase
gate S, and finally a Z gate. From the previous section, this results in the |−ii state,
and if we measure the qubit in the Z-basis, we get |0i or |1i with equal probability.
We can also explicitly draw the measurement as a meter:
|0i H S Z
2.7 Quantum Circuits 107
There are a variety of quantum gates that users can drag and drop onto the circuit,
and users can also make custom gates. Each qubit is initially a |0i, as shown below:
The state of each qubit is visualized a couple ways. First, the probability that a
measurement of the qubit yields |1i is zero, and this is labeled as “Off.” Second,
the Bloch sphere representation of the qubit is shown, and the state is a point at the
north pole, as expected.
Now if we apply the X gate, Quirk shows the following:
Note Quirk uses ⊕ to denote a single X gate, whereas we use X (later, we will
also use ⊕ when the X gate is controlled by another qubit). Since X|0i = |1i, the
probability of measuring |1i is 1. This is labeled as “On,” and the Bloch sphere now
shows the state at the south pole, as expected.
Now consider H|0i = |+i:
In Quirk, it shows a 50% chance of being |1i when measured in the Z-basis, and it
lies on the x-axis of the Bloch sphere, as expected.
108 2 One Quantum Bit
Finally, let us verify HST H|0i, which we earlier computed by hand to have a
0.146 probability of being |0i and a 0.854 probability of being |1i. As a quantum
circuit,
|0i H T S H
Quirk displays that the probability of measuring |1i is 85.4% = 0.854, in agreement
with our previous calculation.
Exercise 2.35. Answer the following about HY T HX|0i.
(a) Draw HY T HX|0i as a quantum circuit.
(b) Using Quirk, sketch where HY T HX|0i appears on the Bloch sphere.
(c) Using Quirk, if you measure HY T HX|0i in the Z-basis, what is the probability that you get
|0i and the probability that you get |1i?
2.8 Summary
The smallest unit of quantum information is the qubit. Besides having two orthog-
onal states |0i and |1i, a qubit can be a superposition of them with complex am-
plitudes. The norm-square of the amplitudes gives the probability of measuring the
qubit as a 0 or 1, and depending on the outcome, the qubit collapses to |0i or |1i.
Qubits can also be measured in other orthonormal bases. A qubit can be visualized
on the Bloch sphere. Qubits are operated on by quantum gates, which are linear
maps that keep the total probability equal to 1. A single-qubit gate is a rotation on
the Bloch sphere. A quantum circuit is a drawing to depict what quantum gates act
on a qubit, and an online simulator for quantum circuits is Quirk.
Chapter 3
Linear Algebra
So far, we have done quantum computing using elementary algebra and using the
fact that quantum gates are linear, so they distribute across superpositions. This can
be tedious, however, such as when calculating HY T HX(α|0i + β |1i). Fortunately,
there is an easier way to do the math of quantum computing using linear algebra,
where numbers are arranged in columns, rows, and tables (called matrices). Ulti-
mately, linear algebra is a tool. Learning how to use a new tool may be difficult at
first, but ultimately, it makes the job easier. We will show how linear algebra can be
used for many of the calculations from the previous chapter.
We had a similar progression with classical computing. In the previous chapter,
we could do all of classical computing using truth tables, including to show that
ABC + ABC + ABC + ABC = A + B + AC. Boolean algebra makes such calculations
easier, but it requires becoming proficient enough with the tool. Linear algebra is to
quantum computing as boolean algebra is to classical computing.
We write |0i and |1i as column vectors, which are vertical lists of numbers:
1 0
|0i = , |1i = .
0 1
Our notation is to write vectors in large parenthesis, but some people use square
brackets instead. These are called column vectors because they have a single col-
umn, and they have length 2 because they have two entries. Writing |0i and |1i, this
way, it is easy to write superpositions of them. A generic qubit with amplitudes α
and β would be
109
110 3 Linear Algebra
1 0 α 0
|ψi = α|0i + β |1i = α +β = +
0 1 0 β
α
= .
β
If you measure this qubit in the Z-basis {|0i, |1i}, what states can you get and with what probabil-
ities?
hψ| = α ∗ β ∗ .
3.2 Inner Products 111
hψ| = α ∗ β ∗ = α ∗ 0 + 0 β ∗ = α ∗ 1 0 + β ∗ 0 1
= α ∗ h0| + β ∗ h1|.
Notice this has amplitudes α ∗ and β ∗ , so when we go from |ψi to hψ|, we need to
take the complex conjugate of the amplitudes, and flipping between kets and bras is
called “taking the dual”. For example, taking the dual of |ii = √12 |0i + √i2 |1i, we
get
1 i
hi| = √ h0| − √ h1|.
2 2
To summarize, to go between kets and bras,
α
hψ| = α ∗ β ∗ ,
|ψi = ⇐⇒
β
|ψi = α|0i + β |1i ⇐⇒ hψ| = α ∗ h0| + β ∗ h1|.
Exercise 3.3. Consider the following two states |ai and |bi:
√
3 1 2 1 − 2i
|ai = |0i + |1i, |bi = |0i + |1i.
2 2 3 3
Answer the following questions:
(a) What is ha| in terms of h0| and h1|?
(b) What is ha| as a row vector?
(c) What is hb| in terms of h0| and h1|?
(d) What is hb| as a row vector?
One way to multiply |ψi and |φ i is taking their inner product, and it is defined as
hψ|φ i, which is called a bra-ket or bracket:
∗ ∗
γ
hψ|φ i = α β .
δ
From linear algebra, we multiply these vectors by multiplying the first entry of each
vector together, multiplying the second entry of each vector together, and adding
them (i.e., taking their dot product):
hφ |ψi = α ∗ γ + β ∗ δ .
Note the result is just a number, or scalar. So, an inner product are also called a
scalar product.
The inner product of |ψi and |φ i is just the complex conjugate of the inner prod-
uct of |φ i and |ψi:
hψ|φ i = hφ |ψi∗ .
To prove this, we can just do a simple calculation:
α
hψ|φ i = γ δ∗ ∗
= γ ∗ α + δ ∗ β = (γα ∗ + δ β ∗ )∗ = (α ∗ γ + β ∗ δ )∗
β
= hφ |ψi∗ .
Inner products have several uses, which we will see next and throughout this
chapter.
Exercise 3.4. Consider
√
3+i 3 1
|ai = |0i + |1i,
4 2
√
1 15
|bi = |0i + |1i.
4 4
(a) Find ha|bi.
(b) Find hb|ai.
(c) What is the relationship between your answers to parts (a) and (b)?
3.2.2 Orthonormality
So, hψ|ψi is just the total probability, and if hψ|ψi = 1, the state |ψi is normal-
ized.
• Consider the inner product of the Z-basis states |0i and |1i:
0
h0|1i = 1 0 = 1 · 0 + 0 · 1 = 0 + 0 = 0.
1
Next, consider the inner product of the X-basis states |+i and |−i:
1 1 1 1 1 1
|+ih−| = √ 11 √ = 11 = (1 − 1) = 0.
2 2 −1 2 −1 2
Finally, consider the inner product of the Y -basis states |ii and |−ii:
1 1 1 1 1 1
1 + i2 = 0,
|iih−i| = √ 1 −i √ = 1 −i =
2 2 −i 2 −i 2
√
where we used i2 = ( −1)2 = −1.
In all of these, the outcome was zero. In fact, any two states on opposite sides of
the Bloch sphere have zero inner product (Exercise 3.8). We say that states with
zero inner product are orthogonal. Thus, orthogonal states are distinct measure-
ment outcomes.
• These two properties, normalized and orthogonal, can be combined as a single
word, orthonormal. So {|0i, |1i} are orthonormal because each state is individ-
ually normalized, and they are orthogonal to each other. Similarly, {|+i, |−i}
are orthonormal, and {|ii, |−ii} are orthonormal.
Exercise 3.8. Say we have two qubits |ai and |bi. We can parameterize them in spherical coordi-
nates (θ , φ ) on the Bloch sphere:
θa θa
|ai = cos |0i + eiφa sin |1i,
2 2
θb θb
|bi = cos |0i + eiφb sin |1i.
2 2
Now say |ai and |bi lie on opposite points of the Bloch sphere. This means θb = π − θa and
φb = φa + π. Show that ha|bi = 0, i.e., they are orthogonal. Possibly useful trigonometric identities:
Next, inner products can be used to find the amplitudes of quantum states, which
can be norm-squared to yield measurement probabilities. The amplitudes can also
be used to change the basis. As an example, consider a qubit in the following state:
√
3 1
|ψi = |0i + |1i.
2 2
It appears on the Bloch sphere as shown below:
z
√
3 1 |0i
|0i + |1i
2 2
|−i
|−ii •
|ii
|+i y
x
|1i
In Section 2.3.3, we measured this qubit in the Z-basis, X-basis, Y -basis, and a
fourth basis. Let us see how to reproduce the results using inner products.
First, we want to find the possible measurement outcomes and their probabilities
if we measure |ψi in the {|0i, |1i} basis. Although we can just “read off” the ampli-
tudes of |ψi with respect to |0i and |1i and take the norm-square of each to find the
3.2 Inner Products 115
probabilities, we can also find the amplitudes inner products and the orthonormality
of {|0i, |1i}. For example, the amplitude of |0i is
√ ! √ √
3 1 3 1 3
h0|ψi = h0| |0i + |1i = h0|0i + h0|1i = .
2 2 2 | {z } 2 | {z } 2
1 0
When calculating this, the amplitude from |1i vanishes because |0i and |1i are or-
thogonal (i.e., h0|1i = 0). We get just the amplitude from |0i because |0i is normal-
ized (i.e., h0|0i = 1). Similarly, the amplitude of |1i is
√ ! √
3 1 3 1 1
h1|ψi = h1| |0i + |1i = h1|0i + h1|1i = .
2 2 2 | {z } 2 | {z } 2
0 1
Taking the norm-square of each of these, the possible measurement outcomes are
|0i with probability 3/4 and |1i with probability 1/2. Since h0|ψi and h1|ψi are the
amplitudes of |0i and |1i, respectively, we can write the state of the qubit as
This technique is far more useful when we cannot just read off the amplitudes,
such as when measuring in other bases. Let us measure in the X-basis {|+i, |−i}
now. The amplitude of |+i is
√ !
1 3 1
h+|ψi = √ (h0| + h1|) |0i + |1i
2 2 2
√ √ !
1 3 1 3 1
=√ h0|0i + h0|1i + h1|0i + h1|1i
2 2 | {z } 2 | {z } 2 | {z } 2 | {z }
1 0 0 1
√ !
1 3 1
=√ +
2 2 2
√
3+1
= √ .
2 2
This agrees with our calculation in Section 2.3.3. Similarly, the amplitude of |−i is
h−|ψi, but now let us calculate it using linear algebra:
√ √ ! √
1 3/2 1 3 1 3−1
h−|ψi = √ 1 −1 =√ − = √ .
2 1/2 2 2 2 2 2
Again, this agrees with Section 2.3.3. This approach is especially convenient be-
cause we can do the calculations using a computer algebra system that supports
linear algebra, like Mathematica or SageMath:
116 3 Linear Algebra
• In Mathematica,
psi={{Sqrt[3]/2},{1/2}};
minus={{1/Sqrt[2]},{-1/Sqrt[2]}};
ConjugateTranspose[minus].psi
The first line defines a column vector named psi (for |ψi), and the second line
defines a column vector named minus (for |−i). To take their inner product,
we take the conjugate transpose of minus (which is h−|) and perform a vector
multiplication (denoted by the period) with psi. The result of this is
√
3−1
√ ,
2 2
as expected.
• In SageMath,
sage: psi = vector([sqrt(3)/2,1/2])
sage: minus = vector([1/sqrt(2),-1/sqrt(2)])
sage: minus.conjugate()*psi
1/4*sqrt(3)*sqrt(2) - 1/4*sqrt(2)
The first line defines a vector psi (for |ψi), and the second line defines a vector
minus (for |−i). Note we do not need to specify whether it is a column vector
or row vector in SageMath. It will automatically transpose the vector to what-
ever shape is needed. In the third line, we calculate minus.conjugate() (for
h−|) and multiply it onto psi using an asterisks, yielding the fourth line as the
answer. Let us simplify it:
√ √ √ √
√ √ √ 3 2 2 3−1
(1/4) 3 2 − (1/4) 2 = − = √ .
4 4 2 2
This matches what we expected.
Since h+|ψi and h−|ψi are the amplitudes of |+i and |−i, respectively, we can
write the state of the qubit in the {|+i, |−i} basis as
In Section 2.3.3, we also measured |ψi in the Y -basis and in a fourth basis. These
will be left as Exercise 3.9.
In general,
For an orthonormal basis {|ai, |bi}, the state of a qubit can be written as
Finally, we end with some terminology. We have been saying that ha|ψi is the
amplitude of |ψi in |ai. We can also say that ha|ψi is the amount of |ψi that is in
|ai. Or, ha|ψi is the amount that |ψi and |ai overlap. In mathematical language,
ha|ψi is the projection of |ψi onto |ai or |bi.
Exercise 3.9. Consider a qubit in the following state
√
3 1
|ψi = |0i + |1i.
2 2
Consider measuring this qubit in the Y -basis {|ii, |−ii} and the orthonormal basis {|ai, |bi}, where
√
3 i
|ai = |0i + |1i,
2 2
√
i 3
|bi = |0i + |1i.
2 2
(a) Calculate hi|ψi.
(b) Calculate h−i|ψi.
(c) If you measure the qubit in the Y -basis, what states can you get and with what probabilities?
(d) Calculate ha|ψi.
(e) Calculate hb|ψi.
(f)If you measure the qubit in the {|ai, |bi} basis, what states can you get and with what proba-
bilities?
Hint: Your answers should agree with Section 2.3.3.
Exercise 3.10. Consider a qubit in the following state
√
3+i 3 1
|ψi = |0i − |1i,
4 2
which lies on the Bloch sphere at (θ , φ ) = (π/2, 5π/6).
(a) If you measure it in the Z-basis {|0i, |1i}, what states can you get and with what probabilities?
(b) If you measure it in the X-basis {|+i, |−i}, what states can you get and with what probabili-
ties?
(c) If you measure it in the Y -basis {|ii, |−ii}, what states can you get and with what probabili-
ties?
Exercise 3.11. A qubit is in the state
1 1 − 2i
|ψi = √ .
6 1
Recall a quantum gate U generally turns |0i and |1i into superpositions of |0i and
|1i:
118 3 Linear Algebra
a
U|0i = a|0i + b|1i = ,
b
c
U|1i = c|0i + d|1i = .
d
This is a 2×2 matrix because it has two rows and two columns. Plugging this matrix
into U|0i and U|1i, we get
ac 1 a
U|0i = = ,
bd 0 b
ac 0 c
U|1i = = .
bd 1 d
These correctly suggest that we can multiply a matrix and a vector in the following
manner: To get the first (second) entry, we take the first (second) row of the matrix
and multiply it component-by-component with the vector, then add the results:
ac 1 a·1+c·0 a
= = ,
bd 0 b·1+b·0 b
ac 0 a·0+c·1 c
= = .
bd 1 b·0+b·1 d
This is exactly the way matrices and vectors multiply in linear algebra.
Of course, U can also act on superpositions. If a qubit is in the state
α
|ψi = α|0i + β |1i = ,
β
Let us show that the matrix representation of this yields the expected result:
ac α aα + cβ
U|ψi = = .
bd β bα + dβ
3.3 Quantum Gates 119
Quantum gates are matrices that keep the total probability equal to 1.
For example, we previously showed in Section 2.6.1 that the following is a valid
quantum gate because it keeps the total probability equal to 1:
√ √ !
2−i 1 2−i
U|0i = |0i − |1i = 2 ,
2 2 − 12
√ 1
!
1 2+i √ 2
U|1i = |0i + |1i = 2+i .
2 2 2
Then, as a matrix, √ !
2−i 1
U= 2 √2 .
− 12 2+i
2
In Quirk, we can create this quantum gate by clicking the “Make Gate” button at
the top of the page. A dialog box will pop up with different options, and we want to
create a gate from a matrix:
We enter the matrix, give it a name, and then click “Create Matrix Gate.” Then, it
will appear in the bottom-right toolbar under “Custom Gates:”
120 3 Linear Algebra
We can drag this onto the main circuit like any other gate. Here it is, along with a T
and H gate:
From this, we see that HTU|0i has a 57.3% chance of collapsing to |1i, and hence
a 42.7% chance of collapsing to a |0i.
Exercise 3.12. Consider an operator U performs the following mapping on the Z-basis states:
1 1 1 −i
U|0i = √ , U|1i = √ .
2 −i 2 1
(a) What is U as amatrix?
α
(b) What is U ?
β
(c) From your answer to (b), is U a valid quantum gate? Explain your reasoning.
Exercise 3.13. A quantum gate U performs the following mapping on the Z-basis states:
1
U|0i = √ [(3 + i)|0i − (1 + i)|1i] ,
2 3
1
U|1i = √ [(1 − i)|0i + (3 − i)|1i] .
2 3
(a) What is U as a matrix?
(b) Create U as a custom gate in Quirk. Using Quirk, if you measure HUH|0i, what are the
possible outcomes, and with what probabilities?
Of particular note is
10
I= .
01
This is called the 2 × 2 identity matrix. When it acts on a vector, it does nothing. For
example,
10 α 1α + 0β α
= = .
01 β 0α + 1β β
The same is true of large matrices. A N × N matrix with 1’s on the diagonal and 0’s
everywhere else is called the N × N identity matrix.
Exercise 3.14. Recall from Eq. (2.10) than a single-qubit gate is a rotation by some angle θ about
some axis n̂ = (nx , ny , nz ). Consider a quantum gate that rotates by 90◦ about the y-axis. Using
Eq. (2.10) with γ = 1, what is this gate as a matrix?
Using linear algebra, we can compute the effect of a sequence of quantum gates. For
example, we had previously shown that
1 h i
HST H|0i = 1 + ei3π/4 |0i + 1 − ei3π/4 |1i .
2
Now, we can perform this same calculation using linear algebra by multiply each
matrix onto the vector:
122 3 Linear Algebra
1 1 1 10 1 0 1 1 1 1
HST H|0i = √ iπ/4 √
2 1 −1 0 i 0 e 2 1 −1 0
1 1 1 10 1 0 1
= iπ/4
2 1 −1 0 i 0e 1
1 1 1 10 1
=
2 1 −1 0 i eiπ/4
1 1 1 1
=
2 1 −1 ei3π/4
1 1 + ei3π/4
= .
2 1 − ei3π/4
We can also compute this using any computing system that supports linear algebra,
such as Mathematica or SageMath:
• In Mathematica,
zero = {{1}, {0}};
H = 1/Sqrt[2] {{1, 1}, {1, -1}};
S = {{1, 0}, {0, I}};
T = {{1, 0}, {0, Eˆ(I Pi/4)}};
H.S.T.H.zero
The first line defines a column vector named zero, and the second, third, and
fourth lines define the quantum gates as matrices. The fifth line multiplies them
together. Note a period (.) must be used for matrix multiplication; an asterisk
(*) denotes element-by-element multiplication. The output of this code is
1 1 iπ 1 1 iπ
+ ie 4 , − ie 4 ,
2 2 2 2
The first line defines a column vector named zero, and the next three lines define
the quantum gates as matrices. The fifth line multiplies them together, and the
final two lines are the output, which is a column vector:
i 1√
2 + 12
4 − 4 √ .
− 4i − 14 2 + 12
3.3 Quantum Gates 123
√ √ √
Since (i/4 − 1/4) 2 = (i − 1)/2 2 and (i − 1)/ 2 = ei3π/4 , this becomes
1 1 + ei3π/4
,
2 1 − ei3π/4
where √ !
2−i 1
U= 2 √ 2 .
2+i
− 12 2
Calculate HTU|0i.
In Exercise 2.31, we proved the circuit identity HXH = Z by showing that HXH|0i
and Z|0i result in the same state, and HXH|1i and Z|1i result in the same state. We
can prove HXH = Z another way using linear algebra:
1 1 1 01 1 1 1
HXH = √ √
2 1 −1 10 2 1 −1
1 1 1 01 1 1
= .
2 1 −1 10 1 −1
To begin, let us multiply the two matrices on the right (X and H). The procedure is
very similar to multiplying a matrix onto a column vector, except we now have two
column vectors. So, we distribute the middle matrix across the two column vectors
of the rightmost matrix.
!
1 1 1 01 1 01 1
HXH =
2 1 −1 10 1 10 −1
!
1 1 1 1 −1
=
2 1 −1 1 1
1 1 1 1 −1
= .
2 1 −1 1 1
Now, we can multiply these two matrices by again distributing the left matrix so that
it multiples both columns of the right vector:
124 3 Linear Algebra
!
1 1 1 1 1 1 −1
HXH =
2 1 −1 1 1 −1 1
!
1 2 0 1 2 0 1 0
= = =
2 0 −2 2 0 −2 0 −1
= Z.
So, we have proved HXH = Z. We can also perform these calculations using a com-
puter algebra system that supports linear algebra, such as Mathematica and Sage-
Math:
• In Mathematica,
H = 1/Sqrt[2] {{1, 1}, {1, -1}};
X = {{0, 1}, {1, 0}};
H.X.H
This defines the H and X gates as matrices and then multiplies them together,
and the output is
{{1,0},{0,-1}}
This defines the H and X gates as matrices and then multiplies them together,
and the output is a 2 × 2 matrix, which is precisely Z.
3.3.5 Unitarity
Recall from Section 3.3.1 that if a quantum gate U transforms |0i and |1i as follows
a
U|0i = a|0i + b|1i = ,
b
c
U|1i = c|0i + d|1i = ,
d
We see that U|ψi is a column vector, so we can also write it as a ket |Uψi:
U|ψi = |Uψi.
where the second equality comes from the convention for multiplying a row vector
and a matrix, where the first column of the matrix is multiplied by the row vector
according to the usual rule to yield the first entry, and the second column of the
matrix is multiplied by the row vector according to the usual to yield the second
entry. As another proof, a property of the (conjugate) transpose that (AB)† = B† A† ,
and |ψi† = hψ|, so
To summarize,
|Uψi = U|ψi,
(3.1)
hUψ| = hψ|U † .
Using this, we can come up with an easy way to determine whether a matrix
keeps the total probability equal to 1. Consider a quantum gate (matrix) U. If it acts
on |ψi, we have
U|ψi = |Uψi.
For U to be a quantum gate, this must be normalized. That is, the inner product of
|Uψi with itself must equal 1:
hUψ|Uψi = 1
hψ|U †U|ψi = hψ|ψi
U †U = I.
Quantum gates are unitary matrices, and unitary matrices are quantum gates.
This is why we typically use U to denote a quantum gate. It stands for unitary.
As an example application of this, is the following matrix a quantum gate?
1 1 i
U=√
2 −i 1
Exercise 3.18. Is
1 1 1
U=√
2 i −i
a quantum gate? If so, what is U|0i, and what is U|1i?
3.3.6 Reversibility
MM −1 = M −1 M = I.
So, if we multiply a vector by both a matrix and its inverse, nothing happens to the
vector because this is equivalent to multiplying it by the identity matrix.
Now, since a quantum gate U must be unitary, it satisfies
UU † = U †U = I.
If we have a qubit and we applied a quantum gate U, we can undo the gate by
applying U † :
U †U|ψi = I|ψi = |ψi.
Instead of multiplying |ψi and |φ i as an inner product hψ|φ i, where the bra is on
the left and the ket is on the right, another way to multiply them is by having the ket
on the left and the bra on the right, which is called an outer product:
α
γ∗ δ ∗ .
|ψihφ | =
β
To multiply these vectors according to the rules of linear algebra, we multiply each
row of |ψi by each column of |φ i, resulting in
∗ ∗ ∗
αγ αδ ∗
α ∗ α αγ αδ
|ψihφ | = γ δ∗ = = .
β β β γ∗ βδ∗ β γ∗ β δ ∗
The result is a 2 × 2 matrix. So, whereas inner products result in scalars, outer prod-
ucts result in matrices, and we can add outer products together to construct various
quantum gates.
For example, consider
U = |1ih0| + |0ih1|.
Let us find how this acts on |ψi = α|0i + β |1i and show that it is a valid quantum
gate.
128 3 Linear Algebra
This is precisely the matrix for the X gate. To confirm that it is a valid quantum gate,
we simply show that it is unitary, i.e., if U †U = I:
01 01 10
U †U = = = I.
10 10 01
The outer product of |φ i and |ψi is just the conjugate transpose of the outer
product of |ψi and |φ i:
|φ ihψ| = |ψihφ |† .
We can prove this through a simple calculation:
∗ †
γα γβ ∗
∗
αγ αδ ∗
γ
α∗ β ∗ = = |ψihφ |† .
|φ ihψ| = =
δ δ α∗ δ β ∗ β γ∗ β δ ∗
|iih−|.
1 1 1 1
√ |0ih0| + √ |0ih1| + √ |1ih0| − √ |1ih1|.
2 2 2 2
(a) What is it as a matrix?
(b) Is this a valid quantum gate?
Recall from Section 3.2.3 that for any orthonormal basis {|ai, |bi}, the state of a
qubit can be written as
3.4 Outer Products 129
As indicated above, the inner products are just scalars/numbers, so instead of multi-
ply them onto the vectors |ai and |bi on the left, we can equivalently multiply them
on the right:
|ψi = |ai ha|ψi +|bi hb|ψi .
| {z } | {z }
scalar scalar
Both of these terms are a ket times a bra times a ket. To make this more clear, we
can write them as
|ψi = |aiha||ψi + |bihb||ψi.
Now, notice we have two outer products, |aiha| and |bihb|. Since they are both
multiplying |ψi, we can factor to get, which are matrices:
|ψi = |aiha| + |bihb| |ψi.
|aiha| + |bihb| = I.
This is called the completeness relation, and it indicates the state of any qubit can be
expressed in terms of |ai and |bi, a property we call completeness. We say {|ai, |bi}
forms a complete orthonormal basis. All the bases we have discussed (any two states
on opposite sides on the Bloch sphere) are complete.
Let us box this:
|aiha| + |bihb| = I.
Exercise 3.22. Verify that {|+i, |−i} is a complete orthonormal basis by showing that
|+ih+| + |−ih−| = I.
Exercise 3.23. Verify that {|0i, |+i} is a not a complete orthonormal basis by showing that
|0ih0| + |+ih+| 6= I.
130 3 Linear Algebra
3.5 Summary
4.1.1 Mechanics
IBM Research released an open-source board game called Entanglion to teach the
fundamental ideas and mechanics of quantum computing. It is available at https:
//entanglion.github.io, and anyone can download and print the game board
and pieces. The complete rules are available at the website, but let us summarize the
most important parts here, since they reflect the rules of quantum computing.
Entanglion is a two-player collaborative game, and the goal to collect, as a team,
eight components to build a quantum computer that are scattered across different
planets, while avoiding detection by the planetary defenses. There are three galax-
ies in the Entanglion universe: Centarious, Superious, and Entanglion, as shown in
Fig. 4.1. Centarious has two planets, Zero and One, and Superious also has two plan-
ets, Plus and Minus. On the other hand, Entanglion has eight planets, each holding
one component to build a quantum computer.
Each player has one spaceship, and one is red while the other is blue. Players
determine the starting locations of their spaceships by rolling a die that only has 0
or 1 as the outcome. This is called the Centarious die because the outcomes of 0 and
1 correspond to the planets Zero and One, both in the galaxy Centarious.
The players take turns moving their spaceships to different planets by playing en-
gine cards H, X, CNOT, and SWAP. As shown in Fig. 4.1, different engine cards are
used for transitions between different planets. In Centarious and Superious, play-
131
132 4 Multiple Quantum Bits
Fig. 4.1: The universe for the Entanglion board game. It consists of three galax-
ies: Centarious, Superious, and Entanglion. The two players’ spaceships (red and
blue) move across the board from planet from planet according to the labeled paths.
Dashed paths correspond to the red player, and solid paths correspond to the blue
player. Inside the Entanglion galaxy, both spaceships move together.
ers’ spaceships can be on different planets. To move into the Entanglion galaxy, one
player must be in Centarious, and the other player must be in Superious. Then the
player in Centarious uses a CNOT engine card, and both spaceships move to the
same planet in the Entanglion galaxy. This planet is where a horizontal line from
the Centarious planet intersects with the vertical line from the Superious planet, as
shown by the lines . pppppppppp / in Fig. 4.1. For example, if the red player is at One,
and the blue player is at Plus, and the red player uses a CNOT engine card, then
the red player moves horizontally from One to Psi Plus, and the blue player moves
4.1 Entanglion: A Quantum Computing Board Game 133
vertically from Plus to Psi Plus. Inside the Entanglion galaxy, the spaceships always
move together as a pair, so they are always at the same planet.
Anytime the spaceships move to a planet in the Entanglion galaxy, or when a
player attempts to retrieve a component to build a quantum computer, there is a
chance they will be detected by the planetary defenses. The roll of an eight-sized die
determines this, and if the spaceships are detected, both of them move to a random
planet in Centarious determining by rolling the Centarious die.
The game also contains a shuffled deck of event cards, which are played when-
ever the spaceships are detected by planetary defenses, or after six engine cards
have been played. The cards are named after important scientists who contributed to
quantum physics and quantum computing, or after quantum effects. The mechanics
of these cards do not precisely correlate with actual quantum computing, so we limit
our discussion of them here.
Exercise 4.1. Refer to the Entanglion game board in Fig. 4.1.
(a) When can a player use CNOT to move between planets Zero and One?
(b) If the red player is at planet Zero and the blue player is at planet Minus, and the red player
uses a CNOT engine card, where do the players move?
(c) How can the players move between planets Psi Plus and Omega Three?
The rules of Entanglion reflect how quantum computers work. We will explore these
connections in detail throughout this chapter, but here is a quick summary:
• The red and blue spaceships are qubits.
• The planets are various states that qubits can be in. Centarious contains the
classical states 0 and 1, Superious contains two linear combinations or super-
positions of the states 0 and 1, and Entanglion contains eight entangled states,
where the states of the two qubits are intertwined, so the spaceships move to-
gether.
• The engine cards H, X, CNOT, and SWAP are quantum gates that are applied to
the qubits. This transforms the qubits to different states, or moves the spaceships
to different planets.
• Detection by planetary defenses corresponds to a measurement. Measuring a
qubit yields a classical 0 or 1 with some probability, so the spaceships move to
planet Zero or One according to a roll of the Centarious die.
When we have multiple qubits, we write their states as a tensor product ⊗. For
example, two qubits, both in the |0i state, are written
|0i ⊗ |0i,
and this is pronounced “zero tensor zero.” Often, we compress the notation and leave
out the tensor product in both writing and speech:
|0i|0i.
|00i.
With two qubits, the Z-basis is {|00i, |01i, |10i, |11i}. A general state is a super-
position of these basis states:
If we measure these two qubits in the Z-basis, we get |00i with probability |c0 |2 ,
|01i with probability |c1 |2 , |10i with probability |c2 |2 , or |11i with probability |c3 |2 .
Thus, the total probability is |c0 |2 + |c1 |2 + |c2 |2 + |c3 |2 , and it should equal 1.
With three qubits, there are eight Z-basis states |000i, |001i, |010i, |011i, |100i,
|101i, |110i, and |111i. Sometimes, these binary strings are written as decimal num-
bers |0i, |1i, . . . , |7i. Inspired by this, let us call the right qubit the zeroth qubit, the
middle qubit the first qubit, and the left qubit the second qubit, so a Z-basis state
takes the form
|b2 b1 b0 i.
Then, the decimal representation of this is
22 b2 + 21 b1 + 20 b0 .
In other words, we label qubits right-to-left, starting with zero. This convention,
where the rightmost qubit is the zeroth qubit, is called little endian. Quirk and many
quantum programming languages, including those in Chapter 5, also use little en-
dian. In contrast, the opposite convention, where the leftmost qubit is the zeroth
qubit, is called big endian. Of note, Nielsen and Chuang’s standard advanced text-
book uses the big endian convention. Disputes over which convention is “better”
has raged classical computing for decades, and the same debates carry into quantum
computing. The reality is that you should be able to use both, but for consistency,
we use little endian throughout this textbook. Next, the general state of three qubits
4.2 States and Measurement 135
This has N amplitudes c0 through cN−1 . Thus, if we have just n = 300 qubits, then
we must keep track of N = 2300 ≈ 2.04 × 1090 amplitudes, which is more than the
number of atoms in the visible universe (1078 to 1082 ). This is evidence, but not a
proof, that it is difficult for classical computers to simulate quantum computers. In
terms of complexity classes, this is evidence that P 6= BQP. It is evidence because
classical computers cannot keep track of this many amplitudes, but it is not a proof
because it is unknown whether quantum computers need all these amplitudes. If
quantum computers can function with much fewer amplitudes (a polynomial num-
ber instead of an exponential number in n), a classical computer would be able to
keep track of all of them.
We can also use powers to simplify the notation. If we have n qubits, each in the
state |0i, we can write the state as
θ θ
cos |0i + eiφ sin |1i,
2 2
with the coordinates (θ , φ ) interpreted as a point on the Bloch sphere. With two
qubits, however, we have four complex amplitudes c0 , c1 , c2 , c3 (although one can
be made real by factoring out an global phase), and unfortunately, this is too many
parameters to represent in three-dimensions. There is no Bloch sphere representa-
tion for a general multi-qubit state.
The tensor product also works for bras, so
Then, the inner product of, say h01| and |00i, is obtained by matching up qubits. For
example,
h01|00i = h0|0i · h1|0i = 0.
| {z } | {z }
1 0
In linear algebra, the tensor product is simply the Kronecker product, which is ob-
tained by multiplying each term of the first matrix/vector by the entire second ma-
trix/vector. For example, with two qubits,
1 1
1 0 0
1 1
|00i = |0i|0i = |0i ⊗ |0i = ⊗ = 1 = 0 .
0 0
0
0 0
0 0
1 1 1
1 0
|01i = |0i|1i = |0i ⊗ |1i = ⊗ = 0 = 0 .
0 1
0
1 0
1 0
0 0 0
0 1
|10i = |1i|0i = |1i ⊗ |0i = ⊗ = 1 = 1 .
1 0
1
0 0
1 0
0 0 0
0 1
|11i = |1i|1i = |1i ⊗ |1i = ⊗ = 0 = 0 .
1 0
1
1 1
Then,
c0
c1
c0 |00i + c1 |01i + c2 |10i + c3 |11i =
c2 .
c3
Similarly, with three qubits, its state can be written as a column vector with eight
elements:
4.2 States and Measurement 137
c0
7 c1
∑ c j | ji = c0 |0i + c1 |1i + · · · + c7 |7i = . .
j=0 ..
c7
With n qubits, the vector has N = 2n elements:
c0
N−1 c1
|ψi = ∑ c j | ji = c0 |0i + c1 |1i + · · · + cN−1 |N − 1i = .
..
j=0 .
cN−1
With bras, the Kronecker product is still the tensor product. For example,
h00| = h0| ⊗ h0| = 1 0 1 0 = 1 0 0 0 .
Exercise 4.6. Show that {|00i, |01i, |10i, |11i} is a complete orthonormal basis for the state of two
qubits by showing that it satisfies the completeness relation
If we measured both qubits, we would get |00i with probability 1/2, |01i with prob-
ability 1/4, |10i with probability 3/16, or |11i with probability 1/16.
Now, instead of measuring both qubits, let us only measure the left qubit. This
yields |0i or |1i with some probabilities, and the state collapses to some state, so the
outcomes are
The probability of getting |0i when measuring the left qubit is given by the sum of
the norm-squares of the amplitudes of |00i and |01i, since those both have the left
qubit as |0i. That is, the probability of getting |0i is
1 2 1 2 3
√ + = .
2 2 4
Similarly, if the outcome is |1i, then from the |10i and |11i states, the probability is
√ 2
3 1 2 1
+ = .
4 4 4
where A is a normalization constant. Similarly, if the outcome is |1i, then the state
collapses to the terms where the left qubit is |1i, so it becomes
√ !
3 1
B |10i + |11i .
4 4
4.2 States and Measurement 139
√
where B is a normalization constants. Normalizing these, we get A = 2/ 3 and
B = 2, so measuring the left qubit yields
r
3 2 1
|0i with probability , and the state collapses to |00i + √ |01i,
4 3 3
√
1 3 1
|1i with probability , and the state collapses to |10i + |11i.
4 2 2
We can apply these ideas to any number of qubits. For example, if we have three
qubits in the state
and we measure the left and middle qubits, the possible outcomes are
c0 |000i + c1 |001i
p with probability |c0 |2 + |c1 |2 ,
|c0 |2 + |c1 |2
c2 |010i + c3 |011i
p with probability |c2 |2 + |c3 |2 ,
|c2 |2 + |c3 |2
c4 |100i + c5 |101i
p with probability |c4 |2 + |c5 |2 ,
|c4 |2 + |c5 |2
c6 |110i + c7 |111i
p with probability |c6 |2 + |c7 |2 .
|c6 |2 + |c7 |2
We have answered the question of what happens when we measure just a single qubit
or a subset of qubits. Now, let us take this a step further and consider what happens
if we measure the qubits, one after another. For example, in the last section, we
started with two qubits in the state
140 4 Multiple Quantum Bits
√
1 1 3 1
√ |00i + |01i + |10i + |11i.
2 2 4 4
32 1
Prob(|00i) = Prob(first left |0i) Prob(then right |0i) = = ,
43 2
31 1
Prob(|01i) = Prob(first left |0i) Prob(then right |1i) = = ,
43 4
13 3
Prob(|10i) = Prob(first left |1i) Prob(then right |0i) = = ,
4 4 16
11 1
Prob(|11i) = Prob(first left |1i) Prob(then right |1i) = = .
4 4 16
Notice these outcomes and probabilities are exactly the same as if we had measured
both qubits at the same time, as they should be. Measuring both qubits is the same
as measuring one after another, assuming the state was not modified between the
two measurements.
Exercise 4.9. Consider the two-qubit state
√
1 1 1 3
|00i + |01i + √ |10i + |11i.
4 2 2 4
If you measure only the left qubit, what are the resulting states, and with what probabilities?
1 1 1 1 1 1 1 1
|000i + √ |001i + √ |010i + |011i + |100i + |101i + |110i + √ |111i.
6 3 2 6 2 6 3 6 3
If you measure only the left and right qubits, but not the middle qubit, what are the resulting states,
and with what probabilities?
4.3 Entanglement 141
4.3 Entanglement
Some quantum states can be factored into (the tensor product of) individual qubit
states. For example,
1 1 1
(|00i − |01i + |10i − |11i) = √ (|0i + |1i) ⊗ √ (|0i − |1i)
2 2 2
| {z } | {z }
|+i |−i
= |+i ⊗ |−i
= |+i|−i.
To confirm this to yourself, work it out in reverse order by multiplying out the states
and showing that you get the original state. Such factorizable states are called prod-
uct states or simply separable states. Each single-qubit state can be visualized on
the Bloch sphere, so |+i|−i would be two Bloch spheres, with the first at the x-axis,
and the other at the −x-axis:
z z
y y
x x
Let us work through an example of how to factor a state. Say two qubits are in
the state
1 √ √
√ 3|00i − 3|01i + |10i − |11i .
2 2
We want to write this as the product of two single-qubit states,
|ψ1 i|ψ0 i,
where
|ψ1 i = α1 |0i + β1 |1i, |ψ0 i = α0 |0i + β0 |1i.
Then,
Plugging this into the second equation, we can solve for β0 in terms of α0 :
β0 = −α0 .
−1 −1
√ = √ ,
2 2 2 2
which is a true statement, so it is satisfied, although it does not tell us anything new.
So, we have solve for α1 , β1 , and β0 in terms of α0 , and this is actually sufficient.
Plugging into the product state,
(αn−1 |0i + βn−1 |0i) ⊗ · · · ⊗ (α1 |0i + β1 |1i) ⊗ (α0 |0i + β0 |1i) .
4.3 Entanglement 143
This only has 2n amplitudes, so a classical computer can efficiently store the ampli-
tudes of product states. If quantum computers only used product states, they would
be efficiently simulated by classical computers.
There exist quantum states that cannot be factored into product states. These are
called entangled states. For example, with two qubits,
Φ = √1 (|00i + |11i)
+
2
cannot be written as |ψ1 i|ψ0 i. As a proof, let us try writing it in a product using the
same procedure as in the last section:
In the Entanglion board game, the planets within the Entanglion galaxy corre-
spond to two-qubit states that are entangled. Planet Phi Plus is precisely |Φ + i.
We will discuss entanglement in more detail in Chapter 6.
Exercise 4.11. Are each of the following states a product state or entangled state? If it is a product
state, give the factorization.
1
(a) √ (|01i + |10i).
2
1
(b) √ (|10i + i|11i).
2
144 4 Multiple Quantum Bits
Exercise 4.12. Are each of the following states a product state or entangled state? If it is a product
state, give the factorization.
1 √ √
(a) 3|00i − 3|01i + 3|10i − |11i .
4 r
1 2
(b) √ |0i|+i + |1i|−i.
3 3
Say we have multiple qubits, and we want to apply a single-qubit gate (like I, X,
Y , Z, S, T , or H) to just a single qubit. For example, say we have two qubits in the
|00i = |0i ⊗ |0i state, and we want to apply the Hadamard gate to the left qubit,
but leave the right qubit alone (i.e., apply the identity gate to it). We write the gates
using a tensor product, so we write
To draw as a quantum circuit, we use the convention that the rightmost qubit corre-
sponds to the top row of the quantum circuit, and the leftmost qubit corresponds to
the bottom row of the quantum circuit:
|0i I |0i
|0i H or |0i H
We follow this convention so that it matches Quirk, and in Chapter 5 the IBM Quan-
tum Compuser. Just so you are aware, other textbooks may follow the opposite con-
vention, where the leftmost qubit corresponsd to the top row of the quantum circuit.
We can find H ⊗ I as a matrix a couple different ways. First, we can find how
H ⊗ I acts on each of the basis states |00i, |01i, |10i, |11i. We already found how it
4.4 Quantum Gates 145
The second way to find this matrix is by taking the Kronecker product of H and X:
10 10
1· 1·
1 1 1 10 1 0 1 0 1
H ⊗I = √ ⊗ =√
2 1 −1 0 1 2 1·
1 0 1 0
−1 ·
01 01
10 1 0
1 0 1 0 1
=√ .
2 1 0 −1 0
0 1 0 −1
This matches what we previously obtained. We can also find the Kronecker product
using Mathematica or SageMath:
• In Mathematica,
H=1/Sqrt[2]*{{1,1},{1,-1}};
eye={{1,0},{0,1}};
KroneckerProduct[H,eye]
• In SageMath,
sage: H = 1/sqrt(2) * Matrix([[1,1],[1,-1]])
sage: eye = Matrix([[1,0],[0,1]])
sage: H.tensor_product(eye)
146 4 Multiple Quantum Bits
As another example, to act on the left qubit with H and the right qubit with X,
we would write H ⊗ X, so
1
(H ⊗ X)|0i|0i = |+i|1i = √ (|01i + |11i) .
2
As a quantum circuit, we would draw this as
|0i X
|0i H
Since the top qubit is |1i, Quirk correctly shows that the probability of getting 1
when measuring it is 100% (On), and it corresponds to the south pole of the Bloch
sphere. Similarly, the right qubit is |+i, and Quirk correctly shows it as having
probability 50% that measuring it yields 1, and it corresponds to the x-axis of the
Bloch sphere. In additional, Quirk also depicts the amplitudes on the real-imaginary
plane, labeled “Final amplitudes.” There are four boxes, and the top-left box depicts
the amplitude of √|00i, which is zero, and the top-right box depicts the amplitude of
|01i, which is 1/ 2. Since this is real, it corresponds to a vector pointing along the
real axis of the real-imaginary
√ plane. The background is also half filled, indicating
a probability of |1/ 2|2 = 1/2. Mousing over, we get
√
and amplitude is also explicitly given as 0.70711 = 1/ 2, which has a phase or
angle of 0◦ on the real-imaginary plane since it is purely real, and a norm-square
magnitude square of 50%. The bottom-left box depicts the amplitude of |10i, which
zero, and finally the bottom-right box depicts the amplitude of |11i, which is
is √
1/ 2.
As a third example, if we have n qubits, and we want to apply H to all n qubits,
we can write H ⊗ H ⊗ · · · ⊗ H as H ⊗n . For example,
H ⊗n |0i⊗n = |+i⊗n .
4.4 Quantum Gates 147
Note one-qubit gates are unable to create entangled states because each qubit
evolves independently of the others. To create entanglement, we need quantum gates
that operate on multiple qubits at a time.
Exercise 4.13. In this problem, you will prove some of the game mechanics of Entanglion. Please
refer to Fig. 4.1 for the game board. If the players are on planet Psi Plus, and either player uses an
X engine card, they both move to planet Phi Plus, and vice versa. Similarly, if the players are on
planet Psi Minus, and either player uses an X engine card, they both move to planet Phi Minus,
and vice versa. These planets correspond to the following states:
Φ = √1 (|00i + |11i) ,
+
2
− 1
Φ = √ (|00i − |11i) ,
2
+ 1
Ψ = √ (|01i + |10i) ,
2
− 1
Ψ = √ (|01i − |10i) .
2
(a) Show that when the X gate is applied to either qubit of |Ψ + i, the result is |Φ + i, up to a global
phase.
(b) Show that when the X gate is applied to either qubit of |Φ + i, the result is |Ψ + i, up to a global
phase.
(c) Show that when the X gate is applied to either qubit of |Ψ − i, the result is |Φ − i, up to a global
phase.
(d) Show that when the X gate is applied to either qubit of |Φ − i, the result is |Ψ − i, up to a global
phase.
Quantum gates can also operate on two qubits at the same time. Some important
examples include:
• The CNOT gate or controlled-NOT gate, or since the X gate is the NOT gate,
the cX gate or controlled-X gate, inverts the right qubit if the left qubit is 1:
CNOT|00i = |00i,
CNOT|01i = |01i,
CNOT|10i = |11i,
CNOT|11i = |10i.
148 4 Multiple Quantum Bits
The left qubit is called the control qubit, and the right qubit is called the target
qubit. Note the control qubit is unchanged by CNOT, whereas the target qubit
becomes the XOR (exclusive OR) of the inputs:
0010
For example, acting on a general superposition,
1 0 0 0 c0 c0
0 1 0 0 c1 c1
CNOT (c0 |00i + c1 |01i + c2 |10i + c3 |11i) =
0
= .
0 0 1 c2 c3
0 0 1 0 c3 c2
•
The solid dot indicates control, and the ⊕ denotes the target, which is the
XOR of the control and the target. Simulating this in Quirk, we drag an X-
gate onto the top line and a “Control” solid dot, which is in the top Toolbox
under “Probes,” onto the bottom line:
4.4 Quantum Gates 149
We also clicked on the initial state of the control qubit to change it to |1i (alter-
natively, we could leave the initial state as |0i and apply X to it, resulting in |1i).
This triggers the CNOT, changing the target from |0i to |1i. The result is that
both qubits are “On” with 100% probability, they are both at the south poles of
their Bloch spheres, and the amplitude of |11i is 1.
To further clarify the control and target qubits, we may write CNOT with sub-
scripts:
CNOTi j = CNOT with qubit i as the control and qubit j as the target.
Since we label the qubits from right-to-left starting with 0, we have been using
CNOT = CNOT10 .
If we instead want the control and target to be flipped, it would be CNOT01 , and
we would draw the circuit as
•
To simulate this in Quirk, we just put the control on the zeroth qubit and the X
gate on the first qubit:
We set the control qubit to |1i, and so the CNOT gate flipped the target to |1i.
Another way to flip the control and target qubits is to apply Hadamard gates to
both sides of the CNOT:
H H •
=
H • H
In other words,
(H ⊗ H)CNOT(H ⊗ H) = CNOT01
We can prove this circuit identity using either elementary algebra or linear alge-
bra. First, using elementary algebra, the right-hand-side of equation yields the
following when applied to a superposition of the Z-basis states:
150 4 Multiple Quantum Bits
This is the same state, and so we have proved the circuit identity. It was rather
tedious, however. Proving the circuit identity using linear algebra is easier. First,
note that
4.4 Quantum Gates 151
1 0 0 0
0 0 0 1
CNOT01 =
0
,
0 1 0
0 1 0 0
since its columns show that |00i stays |00i, |01i becomes |10i, |01i stays |01i,
and |11i becomes |01i. Now let us show that (H ⊗ H)CNOT(H ⊗ H) corre-
sponds to the same matrix. First,
1 1 1 1
1 1 1 1 1 1 1 1 −1 1 −1
H ⊗H = √ ⊗√ = .
2 1 −1 2 1 −1 2 1 1 −1 −1
1 −1 −1 1
Then,
(H ⊗ H)CNOT(H ⊗ H)
1 1 1 1 1000 1 1 1 1
1 1 −1 1 −1 0 1 0 0 1 1 −1 1 −1
=
2 1 1 −1 −1
0 0 0 1 2 1 1 −1 −1
1 −1 −1 1 0010 1 −1 −1 1
1 1 1 1 1 1 1 1
1 1 −1 1 −1 1 −1 1 −1
=
4 1 1 −1 −1 1 −1 −1 1
1 −1 −1 1 1 1 −1 −1
4000
1 0 0 0 4
=
4 0 0 4 0
0400
1000
0 0 0 1
=0 0 1 0 .
0100
This is precisely CNOT01 , and so we have proved the circuit identity using
linear algebra. We also could have computed it using Mathematica or SageMath.
Simulating the identity in Quirk,
Since the top qubit is initially |1i, and it is now the control qubit, the top qubit
gets flipped to |1i. So, both qubits are “On.”
152 4 Multiple Quantum Bits
The CNOT gate is important because it can produce entanglement. For example,
1
CNOT|+i|0i = √ (|00i + |11i) = Φ + ,
2
1
CNOT|−i|0i = √ (|00i − |11i) = Φ − ,
2
1
CNOT|+i|1i = √ (|01i + |10i) = Ψ + ,
2
1
CNOT|−i|1i = √ (|01i − |10i) = Ψ − .
2
In each of these, we started with product states and ended up with maximually
entangled pairs of qubits. These are the only four maximally entangled pairs,
and they are known as the Bell states or EPR states or EPR pairs (for Einstein,
Podolsky, and Rosen who objected to the measurement of one entangled particle
to instantly determine the state of the other, which they derided as “spooky
action at a distance”—more on this in Section 6.2.1). These four states form a
complete orthonormal basis called the Bell basis (see Exercise 4.19).
In Entanglion (see Fig. 4.1), the player who uses the CNOT engine card is the
target qubit, and the other player is the control qubit. So, playing a CNOT engine
card while at planet Zero, while your teammate is at planet Plus, causes both of
you to move to planet Phi Plus. Similarly, the spaceships go from planets Zero
and Minus to Phi Minus, One and Plus to Psi Plus, and One and Minus to Psi
Minus.
Exercise 4.15. Prove the following circuit identities, such as by finding the matrix representation
of each circuit.
(a) CNOT(X ⊗ I) = (X ⊗ X)CNOT.
X
=
X • • X
(b) CNOT(I ⊗ X) = (I ⊗ X)CNOT.
X X
=
• •
(c) CNOT(Z ⊗ I) = (Z ⊗ I)CNOT.
=
Z • • Z
(d) CNOT(I ⊗ Z) = (Z ⊗ Z)CNOT.
Z Z
=
• • Z
4.4 Quantum Gates 153
Exercise 4.16. Consider the following circuit, which consists of two CNOTs.
A • A0
B • B0
C C0
(a) What is the truth table for this circuit?
(b) How does it compare to the reversible circuit for XOR in Exercise 1.39?
Exercise 4.17. Recall CNOT flips the right qubit if the left qubit is 1. The anti-controlled NOT
gate flips the right qubit if the left qubit is 0. As a quantum circuit, the anti-control is drawn as an
open dot instead of a solid dot. Prove that it can be obtained from an ordinary CNOT by applying
an X gate to each side of the control:
=
X • X
Exercise 4.18. If we apply CNOT in the Z-basis {|00i, |01i, |10i, |11i}, the left qubit acts as the
control and the right qubit acts as the target. In this problem, we will prove that in the X-basis
{|++i, |+−i, |−+i, |−−i, if the right qubit is |−i, the left qubit gets flipped between |+i and |−i,
so the control and target are reversed. That is,
CNOT|+i|+i = |+i|+i,
CNOT|+i|−i = |−i|−i,
CNOT|−i|+i = |−i|+i,
CNOT|−i|−i = |+i|−i.
To prove these four equations, we start with the circuit identity from the main text:
(H ⊗ H)CNOT(H ⊗ H) = CNOT01 .
Now it is straightforward to prove how CNOT acts in the X-basis. Beginning with |++i,
Work out how CNOT acts on the remaining three basis states |+−i, |−+i, and |−−i.
154 4 Multiple Quantum Bits
Exercise 4.19. Prove that the Bell basis satisfies the completeness relation:
+
+ −
− +
+ −
−
Φ Φ + Φ Φ + Ψ Ψ + Ψ Ψ = I,
• Just like CNOT, the controlled-U gate applies some quantum gate U to the right
qubit if the left qubit is 1:
cU|00i = |00i,
cU|01i = |01i,
cU|10i = |1i ⊗U|0i,
cU|11i = |1i ⊗U|1i.
To get the matrix representation of cU, first say U acts on a single qubit as
So, U as a 2 × 2 matrix is
ac
U= .
bd
Then,
cU|00i = |00i,
cU|01i = |01i,
cU|10i = |1i ⊗ (a|0i + b|1i) = a|10i + b|11i,
cU|11i = |1i ⊗ (c|0i + d|1i) = c|10i + d|11i.
00cd
This agrees with
1 0 0 0
0 1 0 0
CNOT = cX =
0
.
0 0 1
0 0 1 0
Some examples are controlled-Z and controlled-phase:
Z S
• •
4.4 Quantum Gates 155
SWAP|00i = |00i,
SWAP|01i = |10i,
SWAP|10i = |01i,
SWAP|11i = |11i.
In other words,
SWAP|ai|bi = |bi|ai.
This gate cannot produce entanglement because it simply swaps the factors, so
product states remain product states. Acting on a superposition,
0001
For example, acting on a general superposition,
1 0 0 0 c0 c0
0 0 1 0 c1 c2
SWAP (c0 |00i + c1 |01i + c2 |10i + c3 |11i) =
0
= .
1 0 0 c2 c1
0 0 0 1 c3 c3
We also included an X-gate so that the top qubit is a |1i. This swaps with the
bottom qubit, which then swaps with the middle qubit, so the result is that the
middle qubit is |1i.
A SWAP gate can also be created using three CNOT gates:
× •
=
× • •
Or as an equation,
As a proof, we can work through what each CNOT does and show that the result
is a SWAP:
CNOT 01 CNOT
|ai|bi −−−→ |ai|a ⊕ bi −−−−→ |a ⊕ a ⊕ bi|a ⊕ bi = |(a ⊕ a) ⊕ bi|a ⊕ bi
CNOT
= |0 ⊕ bi|a ⊕ bi = |bi|a ⊕ bi −−−→ |b, a ⊕ b ⊕ bi = |bi|ai.
As another proof, we can multiply the three CNOTs as matrices and show that
we get the matrix of a SWAP:
1000 1000 1000 1000
0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0
0 0 0 1 0 0 1 0 0 0 0 1 = 0 1 0 0 = SWAP.
Exercise 4.21. Entanglion contains four yellow planets besides the Bell States. Please see the game
board at Fig. 4.1. They are labeled Omega Zero through Omega Four. These are not standard names,
but they correspond to the quantum states
1
|ω0 i = (|00i − |01i + |10i + |11i) ,
2
1
|ω1 i = (−|00i + |01i + |10i + |11i) ,
2
1
|ω2 i = (|00i + |01i + |10i − |11i) ,
2
1
|ω3 i = (|00i + |01i − |10i + |11i) .
2
The blue player corresponds to the left qubit, and the red player corresponds to the right qubit.
(a) Show that when the SWAP gate is applied to |ω0 i, we get |ω3 i.
4.4 Quantum Gates 157
(b) Show that when X is applied to the left qubit of |ω1 i, we get |ω3 i.
(c) Show that when CNOT01 is applied to |ω2 i, we get |ω0 i.
(d) Show that when CNOT = CNOT10 is applied to |ω3 i, we get |ω2 i.
Exercise 4.22. The Mølmer-Sørensen (MS) gate is a two-qubit gate that can be naturally imple-
mented on trapped ion quantum computers. It transforms Z-basis states by
1
|00i → √ (|00i + i|11i) ,
2
1
|01i → √ (|01i − i|10i) ,
2
1
|10i → √ (|10i − i|01i) ,
2
1
|11i → √ (|11i + i|00i) .
2
(a) What is the MS-gate as a matrix?
(b) Show that MS8 = I. (You may use a computer.)
A three-qubit gate that often appears in quantum computing is the Toffoli gate
or Controlled-Controlled NOT gate that we discussed in chapter 1. Since it is re-
versible, it is a quantum gate, and it flips the right qubit if the left and middle qubits
are 1:
Toffoli|000i = |000i,
Toffoli|001i = |001i,
Toffoli|010i = |010i,
Toffoli|011i = |011i,
Toffoli|100i = |100i,
Toffoli|101i = |101i,
Toffoli|110i = |111i,
Toffoli|111i = |110i.
Or
Toffoli|ai|bi|ci = |ai|bi|ab ⊕ ci.
Recall from Section 1.5.3 that the Toffoli gate is universal for classical comput-
ing, and any efficient classical algorithm can be converted into an efficient algorithm
only utilizing Toffoli gates. Since the Toffoli gate is a quantum gate, quantum com-
puters can efficiently do everything a classical computer can efficiently do. In terms
of complexity classes, P is contained within BQP.
As a matrix, the columns correspond to Toffoli acting on |000i, |001i, . . . , |111i:
158 4 Multiple Quantum Bits
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 1 0 0 0 0
Toffoli =
0
.
0 0 0 1 0 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
As a quantum circuit, solid dots indicate the control qubits, and ⊕ indicates the
target qubit:
•
•
In Quirk, we simply drag two control dots onto the circuit, along with the X-gate:
We made the bottom two qubits both in the |1i state, so the Toffoli gate flips the top
qubit to |1i.
Exercise 4.23. Show that the Toffoli gate can be constructed from the one-qubit gates Hadamard
H, phase S, T , and T † , plus the two-qubit CNOT gate:
H T† T T† T H
• = • • T †
T† S
• • • • • T
U41 α +U43 β β2
but this requires knowing α and β , which we do not know. Any general solution
requires knowing α and β , so there is no operator U that allows us to copy a general,
unknown quantum state.
As another “proof,” U|ψi|0i = |ψi|ψi is akin to going from ψ to ψ 2 , and this is
quadratic, not linear. The mathematics we are using is called linear algebra because
matrices are linear. Vectors are transformed by linear transformations.
This result is called the no-cloning theorem. While classical information can
be cloned (just read and copy the bits), quantum information can not generally be
cloned.
Using this theorem, some scientists have proposed quantum software that cannot
be copied or pirated, and quantum money that cannot be copied or counterfeited,
but that is beyond the scope of this textbook.
Exercise 4.24. Say there is a unitary U that is able to clone qubits in two known states |ψi and
|φ i. That is,
U|ψi|0i = |ψi|ψi,
U|φ i|0i = |φ i|φ i.
160 4 Multiple Quantum Bits
For example, an operator that can clone both |0i and |1i is CNOT, since CNOT|00i = |00i and
CNOT|10i = |11i. Taking the inner product of the previous two equations,
For hψ|φ i to be equal to its square, it must equal 0 or 1. Thus, |ψi = |φ i, or |ψi and |φ i are
orthogonal. Thus, an operator can only clone states that are orthogonal.
Does there exist a quantum operator U that can clone both
(a) |+i and |−i?
(b) |ii and |−ii?
(c) |0i and |+i?
In Section 1.3, after defining classical bits and logic gates, we demonstrated how
to compute something: the sum of two binary numbers, each of length n. Now that
we have defined qubits and quantum gates, let us also construct quantum circuits
that add binary numbers. Before we do that, however, let us review the classical
ripple-carry adder.
(carry) 1 1 1 0 0
1011
“+” 1110
(sum) 11001
Or in terms of variables,
(carry) c4 c3 c2 c1 c0
a3 a2 a1 a0
“+” b3 b2 b1 b0
(sum) s4 s3 s2 s1 s0
where the initial carry in is c0 = 0. In general, if the binary numbers have length n,
then the output has length n + 1.
4.5 Quantum Adders 161
Classically, we can add binary numbers using the ripple-carry adder from Sec-
tion 1.3.4:
C0 = 0 S0
A0 FA
B0 S1
A1 FA
B1 S2
A2 FA
B2 S3
A3 FA
B3 Cout = S4
Each full adder has three inputs: a carry in Cin and two bits A and B. From these, it
computes the sum S = A ⊕ B ⊕ Cin and the carry out is Cout = AB + Cin (A ⊕ B) or
AB ⊕Cin (A ⊕ B) (see Exercise 1.26).
This full adder is not reversible, since it does not have enough outputs to uniquely
determine the inputs. So, it is not a quantum gate. There are several ways, however,
to convert it to a quantum gate.
1. From Exercise 1.41 of Section 1.5.4, we can turn the full adder into a reversible
circuit by taking the XOR of each of its outputs with and extra bit:
162 4 Multiple Quantum Bits
Cin Cin
A A
B B
S
FA
Cout
S⊕D
D
Cout ⊕ E
E
This entire circuit can be drawn as a single gate with five inputs and five outputs:
Cin Cin
A A
B FA B
Rev
D S⊕D
E Cout ⊕ E
Cin
S
A
B Cout
One approach is to replace all five of these logic gates with (more than five)
NAND gates. Then, we can implement each NAND gate using a Toffoli gate,
4.5 Quantum Adders 163
which is reversible and a quantum gate. While this works in principle, the pro-
cedure can be wasteful, leading to extra gates and qubits.
3. Adapting the previous method, instead of converting all the logic gates into
NAND gates and then Toffoli gates, we convert each logic gate into a re-
versible/quantum gate more directly. The basic logic gates are NOT, AND, OR,
XOR, NAND, and NOR. From Section 2.6.3, the X-gate is simply the NOT
gate. From Section 1.5.3, the Toffoli gate can implement AND and NAND by
setting its third bit to 0 or 1, respectively. From Exercise 1.37, the anti-Toffoli
gate can implement NOR and OR by setting its third bit to 0 or 1, respectively.
From Exercise 4.16, two CNOT gates can be used to implement XOR. These
results are summarized in the following table:
Classical Reversible/Quantum
NOT A A X-Gate A X A
A A • A
AND AB Toffoli B • B
B 0 AB
A A
A
OR A+B anti-Toffoli B B
B
1 A+B
A • A
A
XOR A⊕B CNOTs B • B
B
0 A⊕B
A A • A
NAND AB Toffoli B • B
B 1 AB
A A
A
NOR A+B anti-Toffoli B B
B
0 A+B
Replacing each gate, we get the following circuit:
164 4 Multiple Quantum Bits
Cin • Cin
A • • A
B • • • B
A⊕B
0 • • A⊕B
A ⊕ B ⊕ Cin
0 S
AB
0 AB
Cin (A ⊕ B)
0 Cin (A ⊕ B)
0 X Cout
The first two CNOTs implement an XOR gate (A ⊕ B), and the next two CNOTs
also implement an XOR gate, computing A ⊕ B ⊕Cin = S. Next, the Toffoli gate
implements an AND gate (AB), and another Toffoli gate implements another
AND gate (Cin (A ⊕ B)). Finally, an X-gate turns an extra bit from a 0 to a 1,
and together with an anti-Toffoli gate, they implement an OR gate, yielding
AB + Cin (A ⊕ B) = Cout . Since these are all reversible gates, it is a quantum
circuit.
Notice this method uses several extra bits. Besides the inputs (Cin , A, and B) and
the outputs (S and Cout ), three extra bits were used for intermediate calculations:
A ⊕ B, AB, and Cin (A ⊕ B). These extra bits are called ancilla bits or ancillary
bits, and in quantum circuits, they should be cleaned up by turning them back
into zeros. This is so they can be reused in later parts of a circuit and so that they
do not cause unintended entanglement. One method for cleaning up ancillary
bits is called uncomputation, where we apply in reverse order the inverses of
the gates that were used to calculate the ancillas. Since the Toffoli and CNOT
gates are their own inverses, the full adder becomes
Cin • • Cin
A • • • • A
B • • • • • B
0 • • • 0
0 S
0 0
0 0
0 X Cout
We see that with all three inputs Ci n, A, and B set to 1, both the sum S and
carry-out Cout are 1, as expected, and each ofthe three ancilla qubits correctly
start and end in |0i.
4. We can come up with a more clever implementation that uses fewer gates and
bits. Let us do this over the next several sections.
Quantumly, we can encode the binary numbers in two quantum registers |ai and |bi.
One way to add them reversibly is to replace |bi with the sum:
|ai|bi → |ai|si,
where s = a + b. For example, using the quantum adder, 1011 + 1110 = 11010,
would be
|1110i|01011i → |1110i|11010i.
By adding this way, it is always possible to determine the inputs: a is the left register,
and b can be obtained by subtracting a from s. Since the sum can have length n + 1,
this means our second register needs an extra qubit |bn i that is initially |0i:
where s = a + b.
In the intermediate steps of the computation, the quantum adder also needs to
keep track of carry bits, so we also have a quantum register of length n for the carry
bits:
|ci = |cn−1 i . . . |c1 i|c0 i.
Initially, this carry register contains all zeros, and at the end of our computation, we
should restore them to all zeros. This way, we can use them to add other numbers
166 4 Multiple Quantum Bits
together. Also, if we do not reset them, they could be entangled with our results and
cause unintended interference. These qubits, which are used in intermediate steps of
the computation, are called garbage qubits or junk qubits, and they need to be reset
to |0i. We will discuss this more later.
Putting these together, we want our quantum adder to map
|ai|bi|ci → |ai|si|ci.
Since CNOT|ai|bi = |ai|a ⊕ bi, we can implement the sum using two CNOTs:
|ci i • |ci i
|ai i • |ai i
|bi i |ai ⊕ bi ⊕ ci i
The first CNOT turns |bi i into |ai ⊕ bi i, and the second CNOT turns it into |ai ⊕ bi ⊕ ci i:
|ci i
|ci i • |ci i
|ai i
|ai i • |ai i
|ai ⊕ bi i
|bi i |ai ⊕ bi ⊕ ci i
There are also several ways to create custom gates in Quirk. One way is to first
create the sum by dragging and dropping controls and X-gates:
Next, we can click on the “Make Gate” button at the top of the page. A dialog box
will pop up with different options, and we want to create a gate from the circuit we
just drew:
4.5 Quantum Adders 167
We can either turn the whole circuit into a gate, or we can just select the first two
columns (1:2). Let us also name the gate “S.” If we click “Create Custom Gate,” we
return to the main screen, and now our gate appears at the bootm right toolbar under
“Custom Gates:”
We can drag this onto the main circuit like any other gate:
By changing the initial state of the qubits to |1i, we can also test the circuit to verify
that it adds correctly.
168 4 Multiple Quantum Bits
and this allows us to create the AND of A and B, XORed with C. Then, a quantum
carry circuit is
|ci i • |ci i
|ai i • • |ai i
|bi i • • |ai ⊕ bi i
|ci+1 i |ai bi ⊕ ci (ai ⊕ bi ) ⊕ ci+1 i
Now let us construct the circuit, which is from a paper by Vedral, Barenco, and Ekert
in 1996. We order the wires to alternate between ci , ai , and bi :
4.5 Quantum Adders 169
c0 = 0
a0
b0
c1 = 0
a1
b1
c2 = 0
a2
b2
c3 = 0
a3
b3
b4 = 0
For the first operation, we can either calculate the sum s0 using our sum circuit S,
or we can calculate the carry c1 using our carry circuit C. If we begin by calculating
s0 , then we no longer have b0 , but we need b0 to calculate the carry c1 . So, let us
calculate the carry first:
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
b1 b1
c2 = 0 c2 = 0
a2 a2
b2 b2
c3 = 0 c3 = 0
a3 a3
b3 b3
b4 = 0 b4 = 0
Now that we have c01 , we can either add it to a1 and b1 , or we can calculate the carry
c02 . Again, if we add first, then we no longer have b1 to calculate the carry. So, let us
calculate the next carry, and repeating this argument, we calculate all the carries:
170 4 Multiple Quantum Bits
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
C
b1 a1 ⊕ b1
c2 = 0 c02
a2 a2
C
b2 a2 ⊕ b2
c3 = 0 c03
a3 a3
C
b3 a3 ⊕ b3
b4 = 0 s4
Note the last carry is corresponds to the leftmost bit of the sum s4 . Now to calculate
s3 using our sum circuit S, we need the inputs to be c03 , a3 , and b3 , but currently
the third input is a3 ⊕ b3 . To make this third input simply b3 , we CNOT a3 with it,
resulting in a3 ⊕ (a3 ⊕ b3 ) = (a3 ⊕ a3 ) ⊕ b3 = 0 ⊕ b3 = b3 :
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
C
b1 a1 ⊕ b1
c2 = 0 c02
a2 a2
C
b2 a2 ⊕ b2
c3 = 0 c03
a3 • a3
C
b3 b3
b4 = 0 s4
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
C
b1 a1 ⊕ b1
c2 = 0 c02
a2 a2
C
b2 a2 ⊕ b2
c3 = 0 c03
a3 • S a3
C
b3 s3
b4 = 0 s4
Next, we need to undo c03 so that we just have c3 = 0. We can do this by inverting
the carry gate:
•
• •
C† =
• •
Note since C is a quantum gate, it is unitary, so its inverse is equal to its conjugate
transpose (i.e., C−1 = C† ). Applying this,
172 4 Multiple Quantum Bits
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
C
b1 a1 ⊕ b1
c2 = 0 c02
a2 a2
C C†
b2 b2
c3 = 0 c3 = 0
a3 • S a3
C
b3 s3
b4 = 0 s4
This also converted a2 ⊕ b2 back to b2 , so we can again use the sum circuit to find
s2 = a2 ⊕ b2 ⊕ c02 :
c0 = 0 c0 = 0
a0 a0
C
b0 a0 ⊕ b0
c1 = 0 c01
a1 a1
C
b1 a1 ⊕ b1
c2 = 0 c02
a2 S a2
C C†
b2 s2
c3 = 0 c3 = 0
a3 • S a3
C
b3 s3
b4 = 0 s4
c0 = 0 c0 = 0
a0 S a0
C C†
b0 s0
c1 = 0 c1 = 0
a1 S a1
C C†
b1 s1
c2 = 0 c2 = 0
a2 S a2
C C†
b2 s2
c3 = 0 c3 = 0
a3 • S a3
C
b3 s3
b4 = 0 s4
This is our quantum ripple-carry adder, and |bi has been replaced by |si (while
keeping |ai and |ci unchanged), as we wanted.
Note the qubits in this circuit have a different order. Rather than taking
|ai|bi|ci → |ai|si|ci,
|b4 i|b3 i|a3 i|c3 i|b2 i|a2 i|c2 i|b1 i|a1 i|c1 i|b0 i|a0 i|c0 i
to
|s4 i|s3 i|a3 i|c3 i|s2 i|a2 i|c2 i|s1 i|a1 i|c1 i|s0 i|a0 i|c0 i.
Let us verify our quantum circuit in Quirk by adding |ai = |1011i and |bi =
|01110i, which should result in |si = |11001i. With the qubit ordering from above,
the quantum ripple-carry adder should take where all the carry qubits are |0i at the
start and end of the computation, so the quantum ripple-carry adder should take
|0110100110010i → |1110000010110i.
Exercise 4.26. We can use an adder to subtract binary numbers by using the fact that
a − b = a + b.
In Qurik, modify your circuit from Exercise 4.25 to subtract 1111 − 1011. Do this by adding X-
gates to each bit of the input a (this gives a). Then, the adder computes a + b. Then, add X-gates to
each bit of s, except for the extra bit s4 , since it is not needed and should stay 0. This gives a + b.
Exercise 4.27. While teaching a course on quantum computing in Fall 2018, one of my Creighton
University students, Lorenzo Riva, proposed the following change to the quantum ripple-carry
adder:
4.5 Quantum Adders 175
c0 = 0 c0 = 0
a0 S a0
C C†
b0 s0
c1 = 0 c1 = 0
a1 S a1
C C†
b1 s1
c2 = 0 c2 = 0
a2 S a2
C C†
b2 s2
c3 = 0 • c3 = 0
a3 a3
C
b3 s3
b4 = 0 s4
√
That is, the CNOT between a3 and b3 , and the bottommost S (sum, not the S = Z gate), can be
replaced by a single CNOT between c3 and b3 .
(a) Explain why this simplification is correct.
(b) If each binary number has length n, how many Toffoli gates and how many CNOT gates does
this circuit use?
Exercise 4.28. In this exercise, we will learn about another quantum adder that does not need
carry bits. It is called Draper’s adder, and it uses the “quantum Fourier transform,” which will be
discussed later in the textbook in Section 7.7.3.
First, let us define a single-qubit gate Rr that rotates about the z-axis of the Bloch sphere by
angle 360◦ /2r . For example, R1 = Z is a rotation by 180◦ , R2 = S is a rotation by 90◦ , R3 = T is a
rotation by 45◦ , and R4 is a rotation by 22.5◦ . Rotations about axes can be created in Quirk using
the “Make Gate” feature, e.g., R4 is
We also have the conjugate transpose (or inverse) of the rotation, which we denote R†r , and it rotates
about the z-axis by −360◦ /2r . For example, R†4 rotates by −22.5◦ , and since it is a negative angle,
it rotates the “other way.”
176 4 Multiple Quantum Bits
|ai|bi → |a + bi|bi,
and it does not use any carry qubits. Instead of using the ripple-carry adder, The circuit for the
adder is a little long, so we break it up over three parts:
Part 1
b0 ...
b1 ...
b2 ...
b3 ...
a0 • • • H ...
a1 • • H R2 ...
a2 • H R2 R3 ...
a3 H R2 R3 R4 ...
... R2† H • • s1
Implement Draper’s adder in Quirk and use it to add |ai = |0111i and |bi = |0011i.
4.5 Quantum Adders 177
Generalizing this, adding two n-bit strings uses n carry gates C, n − 1 inverses of the
carry gate C† , n sum gates S, and an extra CNOT gate. Each C and C† gate uses two
Toffoli gates and one CNOT gate, and each S gate uses two CNOT gates. The total
number of quantum gates to add two n-bit strings is summarized in the following
table:
Gate No. of Gates Total No. of Toffolis Total No. of CNOTs
C n 2n n
C† n−1 2(n − 1) n−1
S n 0 2n
Extra CNOT 1 0 1
4n − 2 4n
Altogether, the quantum ripple-carry adder uses 4n − 2 Toffoli gates and 4n CNOT
gates, which is linear in n, i.e., Θ (n), so the algorithm is efficient.
Exercise 4.29. How many Toffoli gates and how many CNOT gates does the quantum ripple-carry
adder need to add two (a) 4-bit strings, (b) 8-bit strings.
Exercise 4.30. To add two binary numbers of length 4, our quantum ripple-carry adder used 13
qubits. How many qubits does the quantum ripple-carry adder need to add two binary numbers of
length n?
Note our quantum ripple-carry adder is a quantum circuit, so it can also act on
superpositions. For example, if |ai is an equal superposition of 6 and 3, i.e.,
1
|ai = √ (|0110i + |0011i) ,
2
and if |bi is 11, then
|bi = |01011i,
then the quantum ripple-carry adder turns |ai|bi into
1
√ (|0110i|10011i + |0011i|01110i) .
2
It may appear as though we solved two addition problems at once, i.e., in “paral-
lel,” since both 6 + 11 = 15 and 3 + 11 = 14 appear in the answers as |10011i and
|01110i, respectively, but this is not the case. We measure the result, we get the re-
sult to one sum with probability 1/2, and the result of the other sum with probability
178 4 Multiple Quantum Bits
1/2. In contrast, in parallel computing, two computers calculate both answers at the
same time, so we get both sums at the end.
It is incorrect to think of a quantum computer as a massively parallel classical
computer because we must measure and only get one result. In fact, this misunder-
standing is so common that it might be best to avoid the term “parallel” altogether
when describing quantum computing.
Exercise 4.31. Read “Quantum Computing: A Soccer Analogy” at
https://medium.com/@thomaswong_8663/quantum-computing-a-soccer-a
nalogy-1335644a1472
Answer the following questions and fill in the blanks.
(a) Who is the author of the article, and what is their relationship with the author of this textbook?
(b) “Analogously, the essence of quantum computing is to change the rules so that a computer
can now use its “ .” That is, the rules of the game are changed from
the laws of classical physics to the laws of physics. As a result, a
quantum computer can solve problems faster by using its “hands.” For
other problems, using one’s “feet” is better, so a quantum computer is
for these problems.”
4.6.1 Definition
A universal gate set allows us to approximate any unitary to any desired precision.
Proving that a set of gates is universal is a more advanced topic, which we do
not discuss in this textbook. Nielsen and Chuang is a good resource for additional
details.
There are several components that we need for a set of quantum gates to be universal.
• Interference/superposition. We must be able to produce superpositions. For ex-
ample the Hadamard gate can create superpositions, such as H|0i = |+i. Other
gates are not. Z, S, and T only apply phases; they do not create superpositions of
|0i and 1. Similarly, the X and CNOT gates only flip |0i and |1i, so they cannot
create superpositions. Y only applies phases and flips, so again superpositions
cannot be created by it.
• Entanglement. We must be able to entangle qubits. One-qubit gates, such as
H, cannot do this since it only acts on a single qubit. It must act on at least
two qubits to produce entanglement. CNOT can produce entanglement since
CNOT|+i|0i = |Φ + i. Not all two qubit gates produce entanglement, however.
The SWAP gate cannot generate entanglement since it only swaps two qubits.
4.6 Universal Quantum Gates 179
• Complex amplitudes. CNOT and H only contain real numbers, so they do not
produce states with complex amplitudes. (The set {CNOT, H} produces all or-
thogonal matrices, not all unitary matrices.)
• Contain more than the Clifford group. The Clifford group is the set of gates
{CNOT, H, S}1 , and although this set satisfies all of the previous requirements
(entanglement, interference, and complex amplitudes), the Gottesman-Knill
theorem says that a quantum circuit containing only these gates is efficiently
simulated by a classical computer. That is, the Clifford group is only as power-
ful as a classical computer, so a universal quantum gate set should contain more
than this.
It is unknown if these are sufficient requirements for a set of quantum gates to be
universal. It may be that a set satisfies all of these properties, but is still not universal.
Exercise 4.32. What property are each of the following gate sets lacking to be universal for quan-
tum computing?
(a) {Toffoli, H, Z}.
(b) {H, X,Y, Z, S, T }.
(c) {SWAP, H, S, T }.
Although the Clifford group {CNOT, H, S} is not universal for quantum com-
puting, replacing H with Rπ/8 is universal for quantum computing.
1 Mathematically, the Clifford group is the normalizer of the Pauli group, which is generated by
the Pauli matrices.
2 A. Barenco, C. H. Bennett, R. Cleve, D. P. DiVincenzo, N. H. Margolus, P. W. Shor, T. Sleator, J.
A. Smolin, and H. Weinfurter. Elementary gates for quantum computation. Phys. Rev. A 52, 3457
(1995).
3 P. O. Boykin, T. Mor, M. Pulver, V. Roychowdhury, and F. Vatan. A new universal and fault-
Exercise 4.33. The Clifford group {CNOT, H, S} is not universal for quantum computing. Give
three ways to modify the Clifford group so that it is universal for quantum computing.
The Solovay-Kitaev theorem says that with any universal gate set, we can approx-
imate a quantum gate on n qubits to precision ε using Θ (2n logc (1/ε)) gates for
some constant c. The dependence on the number of qubits 2n is what we might ex-
pect since an operator on n qubits is a matrix of 2n × 2n entries. The dependence on
the precision logc (1/ε) is great! The precision ε is the “distance” (in some measure-
ment or metric) that the approximate quantum gate is to the actual quantum gate,
and we want it to be small. So 1/ε is big, but taking the logarithm of it makes it
small. A logarithm to a constant power is a polynomial of a logarithm, so logc is
also called polylog. This is also considered small. Thus, this dependence means our
approximation quickly converges on the actual quantum gate.
Recall any complex number z has a real part x and an imaginary part y, i.e., z = x+iy.
Since x and y are real numbers, this means we can express any complex number as
two real numbers (x, y) and keep track of the fact that they play different roles. So
in theory, we can formulate all of quantum computing just in terms of real numbers.
Then, quantum states are real-valued vectors, and instead of evolving by unitary
matrices U, which satisfy U †U = UU † = I, they evolve by real orthogonal matrices
O, which satisfy
O| O = OO| = 1.
This is because O has real entries, so taking its complex conjugate does nothing.
Then, the above equation is simply the definition of unitarity, but with O† = O| .
Since it is possible to formulate quantum computing without complex numbers,
a universal set of quantum gates technically does not need to produce states with
complex amplitudes. Then, the following sets are also universal for quantum com-
puting:
4A. Y. Kitaev. Quantum computations: Algorithms and error correction. RMS: Russian Mathe-
matical Surveys 52, 1191 (1997).
4.6 Universal Quantum Gates 181
cH|00i = |00i,
cH|01i = |01i,
1
cH|10i = |1+i = √ (|10i + |11i) ,
2
1
cH|11i = |1−i = √ (|10i − |11i) .
2
This can simulate both the Toffoli gate and the Hadamard gate, so from the
previous bullet, it is universal.
• {CNOT, any single-qubit gate whose square is basis-changing} is universal for
quantum computing.8 An example of a single-qubit gate whose square is basis-
changing is the following gate U:
√
3 1
U|0i = |0i + |1i,
2 2
√
−1 3
U|1i = |0i + |1i.
2 2
5 Y. Shi, Both Toffoli and Controlled-NOT need little help to do universal quantum computation,
arXiv:quant-ph/0205115 (2002).
6 D. Aharonov, A Simple Proof that Toffoli and Hadamard are Quantum Universal, arXiv:quant-
ph/0301040 (2003).
7 D. J. Shepherd, T. Franz, and R. F. Werner, Universally Programmable Quantum Cellular Au-
arXiv:quant-ph/0205115 (2002).
182 4 Multiple Quantum Bits
√ ! √
−1 3 − 3 1
U 2 |1i = U |0i + |1i = |0i + |1i.
2 2 2 2
H 2 |0i = |0i,
H 2 |1i = |1i.
So, {CNOT, H} is not universal, whereas from the first bullet point, {Toffoli, H}
is universal.
4.7.1 Decoherence
y
x
|1i
The north pole corresponds to |0i and the south pole corresponds to |1i. For a clas-
sical bit, these would be the only possible states, and the only error is for the bit to
completely flip between the north and south poles. For a qubit, however, every loca-
tion on the Bloch sphere is a different state. For example, beginning at |0i, instead
of completely flipping to |1i, a qubit could experience a partial bit flip error, where
it only rotates a little toward |1i:
4.7 Quantum Error Correction 183
z
|0i
y
x
|1i
Since a full bit flip corresponds to the X gate, and the X gate is a rotation about
the x-axis by π = 180◦ , a partial bit flip corresponds to rotating about the x-axis by
some angle. So, in the above figure, the state is moving leftward, down the Bloch
sphere, in the yz-plane. This small change is an error.
To further complicate matters, a qubit’s state is not just its latitude up and down
the Bloch sphere, but also its longitude around the Bloch sphere. For example, if a
qubit initially in the |+i state gets bumped to the side, we get a different state:
z
|+i y
x
This is called a phase flip error, because rotations around the √ z-axis correspond
changes
√ in the relative phase. For example, |+i = (|0i + |1i)/ 2 and |−i = (|0i −
|1i)/ 2 lie on opposite sides of the equator.
Since qubits are more sensitive to errors than classical bits, small interactions
with the environment can move the qubit to a different location on the Bloch sphere.
This process is called decoherence. In practice, decoherence is the biggest obstacle
to building large-scale quantum computers, since it is very difficult to isolate a qubit
from its environment while making it accessible for quantum gates and measure-
ments.
Next, we will see how to correct for bit-flip errors and then phase-flip errors.
Then, we will combine both types of error correction into what is known as the
Shor code.
184 4 Multiple Quantum Bits
To make it possible to correct bit-flip errors, we use three physical qubits to encode
each logical qubit:
|0L i = |000i, |1L i = |111i,
where subscript L denotes a logical qubit. A logical qubit is, in general, a superpo-
sition of |0L i and |1L i:
We would like to detect this error and correct it. Classically, we could just measure
the bits, see which one disagrees with the others, and then flip it back to correct
it. Quantumly, however, if we measure the bits (or even just a single bit), the state
collapses to |100i or |011i, and we lose the superposition. So, instead of measuring
the bits, we follow Section 1.6 and measure the parity of adjacent qubits. Recall
that the parity of two bits, a and b, can be calculated using Exclusive OR. That is,
parity(a, b) = a ⊕ b. Also recall that CNOT|ai|bi = |ai|a ⊕ bi. Then, we can use
two CNOTs to calculate the the parity of two qubits, putting the answer in an ancilla
qubit:
|ai • |ai
|bi • |bi
|0 ⊕ ai = |ai
|0i |a ⊕ bi
With three qubits, we can calculate the parities of adjacent qubits by doing this
twice:
|ai • |ai
|bi • • |bi
|ci • |ci
|0i |a ⊕ bi
|0i |b ⊕ ci
In this example, the parity of the left two qubits is 1, and the parity of the right two
qubits is 0. This tells us that the left two qubits differ, and the right two qubits are the
same. Then, we know the left qubit has flipped, and we inferred this without directly
measuring and collapsing the state. This is called an error syndrome. To correct
4.7 Quantum Error Correction 185
the error, we can simply apply (X ⊗ I ⊗ I), which results in β |111i + α|000i =
α|000i + β |111i, thus correcting the error.
Now, say there is a partial flip. In Section 2.6.4, it was stated that on the Bloch
sphere, a rotation by angle θ about the axis n̂ = (nx , ny , nz ) is given by Eq. (2.10):
iα θ θ
e cos I − i sin (nx X + nyY + nz Z) , (2.10 revisited)
2 2
where eiα is a global phase, so α can be chosen as we please. Now, a partial bit flip
corresponds to a rotation about the x-axis by some angle θ , so we have n̂ = (1, 0, 0).
We also choose α = π/2. Then, the rotation corresponds to
θ θ
i cos I + sin X.
2 2
q √
Letting ε = sin(θ /2), we get cos(θ /2) = 1 − sin2 (θ /2) = 1 − ε 2 , so the rota-
tion is
p
2
p
2 10 01
i 1 − ε I + εX = i 1 − ε +ε
01 10
√
i 1 − ε2 √ ε
= .
ε i 1 − ε2
When θ = π, ε = 1, and we get |0i → |1i and |1i → |0i, which is a complete bit
flip, or the X gate.
For example, if the left qubit partially flips,
p p
α|000i + β |111i → α i 1 − ε 2 |000i + ε|100i + β i 1 − ε 2 |111i + ε|011i
p p
= αi 1 − ε 2 |000i + αε|100i + β i 1 − ε 2 |111i + β ε|011i
p p
= αi 1 − ε 2 |000i + β ε|011i + αε|100i + β i 1 − ε 2 |111i.
Now, we measure the parity of adjacent qubits. Labeling the qubits |q2 q1 q0 i, we get
the following possible outcomes with corresponding probabilities:
• parity(q2 , q1 ) = 0 and parity(q1 , q0 ) = 0 with probability
p 2 p 2
αi 1 − ε 2 + β i 1 − ε 2 = |α|2 1 − ε 2 + |β |2 1 − ε 2
= |α|2 + |β |2 1 − ε 2
186 4 Multiple Quantum Bits
= 1 − ε 2,
= |ε|2 ,
|q1 i • • X
|q2 i • X
|0i • • |0i
|0i • • |0i
4.7 Quantum Error Correction 187
The first four columns are the CNOTs that calculate the parities of adjacent qubits.
Then, measure these parities, as shown by the meter symbols, which results in clas-
sical bits. We denote these classical bits/wires using double lines. We end with
three X gates conditioned on these classical bits/parities. If both parities are 1,
then q1 flipped, so we apply an X gate to it to correct it. If parity(q2 , q1 ) = 0 and
parity(q1 , q0 ) = 1, then q0 flipped, so we apply an X gate to it to correct it. Finally,
if parity(q2 , q1 ) = 1 and parity(q1 , q0 ) = 0, then q2 flipped, so we apply an X gate
to it to correct it. We end by resetting the ancillas to |0i, indicated by the boxes with
|0i in them. Simulate this in Quirk (see https://bit.ly/3jZ4zKQ),
In this simulation, the logical qubit starts as |0L i = |000i. In the first column of
the circuit, we can visualize the states on Bloch spheres and confirm that we have
|000i. In the second column, we introduce a bit-flip error by applying a bit-flip X t ,
with t varying from 0 to 360◦ , to q2 . In the third row, we again visualize the states
on the Bloch sphere, confirming that |q2 i 6= |0i. Then, we have our error correction
circuit, and at the very end of the circuit, the top three qubits are restored to |000i,
as expected. Note Quirk does not have a “reset” tool, so the ancillas have not yet
been restored to |0i. This would need to be done to repeat the circuit.
We can modify the above circuit using the principle of deferred measurement,
which says,
|q1 i • • X
|q2 i • X
|0i • • • |0i
|0i • • |0i
188 4 Multiple Quantum Bits
Phrased another way, we can collapse and then do the controlled operations, or we
can do the controlled operations in superposition, and then collapse. Let us prove
this for our previous example, where the qubits started in the state α|000i + β |111i,
but then the left qubit partially flips with amplitude ε. From earlier, the state after
the first four CNOTs is, if we include the ancilla qubits, is
p p
αi 1 − ε 2 |00000i + β ε|10011i + αε|10100i + β i 1 − ε 2 |00111i.
Recall the qubits are ordered as |parity(q2 , q1 )i|parity(q1 , q0 )i|q2 i|q1 i|q0 i. Now, if
we apply the controlled and anti-controlled X gates to correct the answers, the state
becomes
p p
αi 1 − ε 2 |00000i + β ε|10111i + αε|10000i + β i 1 − ε 2 |00111i
p
= i 1 − ε 2 |00i (α|000i + β |111i) + ε|10i (α|000i + β |111i)
p
= i 1 − ε 2 |00i + ε|10i (α|000i + β |111i) .
The top three qubits have been successfully corrected to |000i. Again, we have not
reset the ancilla qubits at the end of this circuit, which would be necessary to repeat
the error correction scheme.
4.7 Quantum Error Correction 189
Exercise 4.34. In this exercise, we will work through an exercise for encoding a qubit in the bit-flip
code. Say we have a single qubit in the state
We want to encode this using the bit-flip code. we add two more qubits to our system, all initially
in |0i, so our three qubits are in the state
|0i
|0i =
|ψi • • •
We can similarly correct phase-flip errors by using three physical qubits to encode
each logical qubit, but instead of using three |0i’s and |1i’s, we use three |+i’s and
|−i’s, i.e.,
|0L i = |+ + +i, |1L i = |− − −i,
so a general superposition is
A way to create this encoding is given in Exercise 4.36. The reason why we use |+i
and |−i is because a complete phase flip (the Z gate) switches between these states:
1 Z 1
|+i = (|0i + |1i) −
→ (|0i − |1i) = |−i,
2 2
1 Z 1
|−i = (|0i − |1i) −→ (|0i + |1i) = |+i.
2 2
Say the left qubit experiences a complete phase flip:
then we detect and correct this just like we did for the bit-flip error, except working
in the X-basis. So, we measure the parity of consecutive qubits in the X-basis, which
is 0 if the number of minuses is even and 1 if the number of minuses is odd. In
Exercise 4.37 and Exercise 4.38, you will show that the parities can be calculated
using
|ai H • H |ai
|bi H • • H |bi
|ci H • H |ci
|0i |a ⊕ bi
|0i |b ⊕ ci
In our example where the left qubit experienced a phase flip, we get parity 1 for the
left two qubits and parity 0 for the right two qubits, implying that the first qubit is
flipped. So we apply (Z ⊗ I ⊗ I), restoring α|+ + +i + β |− − −i.
Now for partial phase flips, a partial phase flip corresponds to a rotation about the
z-axis by some angle θ , so again using Eq. (2.10) with α = π/2 and ε = sin(θ /2),
but now with n̂ = (0, 0, 1), we get that the rotation is
p p 10 1 0
i 1 − ε 2 I + εZ = i 1 − ε 2 +ε
01 0 −1
√
2
i 1−ε +ε √ 0
= .
0 i 1 − ε2 − ε
Note when θ = π, ε = 1, and we get |0i → |0i and |1i → −|1i, which is a complete
phase flip, or the Z gate. Let us see how a partial phase flip transforms |+i and |−i:
4.7 Quantum Error Correction 191
1
|+i = √ (|0i + |1i)
2
1 h p p i
→√ i 1 − ε 2 + ε |0i + i 1 − ε 2 − ε |1i
2
p 1 1
→ i 1 − ε 2 √ (|0i + |1i) + ε √ (|0i − |1i)
2 2
p
2
→ i 1 − ε |+i + ε|−i,
1
|−i = √ (|0i − |1i)
2
1 h p p i
→√ i 1 − ε 2 + ε |0i − i 1 − ε 2 − ε |1i
2
1 p 1
→ ε √ (|0i + |1i) + i 1 − ε 2 √ (|0i + |1i)
2 2
p
→ ε|+i + i 1 − ε 2 |−i.
Now, we measure the parity of adjacent qubits in the X-basis (i.e., whether the num-
ber of |−i’s are even or odd). We get:
• parity(q2 , q1 ) = 0 and parity(q1 , q0 ) = 0 with probability
p 2 p 2
αi 1 − ε 2 + β i 1 − ε 2 = |α|2 1 − ε 2 + |β |2 1 − ε 2
= |α|2 + |β |2 1 − ε 2
= 1 − ε 2,
= |ε|2 ,
|q1 i H • • H Z
|q2 i H • H Z
|0i • • |0i
|0i • • |0i
The top three qubits are |0L i = |+ + +i, and the bottom two qubits will be used to
calculate the parities. In the first column of the circuit, we can visualize the states
on Bloch spheres and confirm that we have |+ + +i. Next, we simulate an error by
applying a phase-flip Z t , with t varying from 0 to 360◦ ), to the middle qubit. Now,
another set of Bloch spheres confirms that the middle qubit has changed. We want
to correct this so that we end up with |+ + +i again. The rest of our circuit is the
same as the bit-flip circuit, except we apply Hadamard gates before and after it so
that we work in the X-basis. In the output, we see that we have restored |+ + +i.
We can move the phase flip to any of the top three qubits, and our error-correcting
circuit will restore the state to |+ + +i. Note we also need to reset the ancilla qubits.
We can simulate this in Quirk (https://bit.ly/3e7dNQR):
4.7 Quantum Error Correction 193
Exercise 4.36. In this exercise, we will work through an exercise for encoding a qubit in the phase-
flip code. Say we have a single qubit in the state
We want to encode this using the bit-flip code. we add two more qubits to our system, all initially
in |0i, so our three qubits are in the state
α|0L i + β |1L i,
where |0L i and |1L i are encoded using the phase-flip code:
α|+ + +i + β |− − −i.
Now, the left physical qubit suffers a slight phase flip, causing the state to become
p p
α i 1 − ε 2 |+ + +i + ε|− + +i + β i 1 − ε 2 |− − −i + ε|+ − −i .
To detect/correct this, you measure the parity of the left two qubits and the parity of the right two
qubits, both in the X-basis.
(a) What is the probability that both parities are even? If this probability is nonzero, say you
get this outcome. What is the state after the measurement? What gate(s) should you apply to
correct the error, if any?
(b) What is the probability that the parity of the left two qubits is odd and the parity of the right
two qubits is even? If this probability is nonzero, say you get this outcome. What is the state
after the measurement? What gate(s) should you apply to correct the error, if any?
(c) What is the probability that the parity of the left two qubits is even and the parity of the right
two qubits is odd? If this probability is nonzero, say you get this outcome. What is the state
after the measurement? What gate(s) should you apply to correct the error, if any?
(d) What is the probability that both parities are odd? If this probability is nonzero, say you get
this outcome. What is the state after the measurement? What gate(s) should you apply to
correct the error, if any?
We can combine the phase-flip code and bit-flip code to correct both kinds of errors.
We begin with the phase-flip code, so we can correct phase-flip errors. That is,
|0L i = |+ + +i
1 1 1
= √ (|0i + |1i) √ (|0i + |1i) √ (|0i + |1i)
2 2 2
1
= 3/2 (|0i + |1i) (|0i + |1i) (|0i + |1i) .
2
Then, so that we can correct bit-flip errors, we replace each of the three qubits with
three qubits using the bit-flip encoding, i.e., |0i → |000i and |1i → |111i, so that
each logical qubit is encoded using nine physical qubits:
1
|0L i = (|000i + |111i) (|000i + |111i) (|000i + |111i) .
23/2
4.7 Quantum Error Correction 195
Similarly, we begin with |1L i = |− − −i and replace |0i → |000i and |1i → |111i:
1
|1L i = (|000i − |111i) (|000i − |111i) (|000i − |111i) .
23/2
Then, the state of a general logical qubit is
α
α|0L i + β |1L i = (|000i + |111i) (|000i + |111i) (|000i + |111i)
23/2
β
+ 3/2 (|000i − |111i) (|000i − |111i) (|000i − |111i) .
2
This encoding is called the Shor code, and it is named after its inventor, Peter Shor,
who proposed it in 1995 and, by doing so, invented quantum error correction. It uses
nine physical qubits to encode one logical qubit. A way to create this encoding is
given in Exercise 4.40.
Exercise 4.40. A qubit can be encoded using the Shor code by first encoding it in the three-qubit
phase-flip code (Exercise 4.36) followed by encoding each of the three qubits using the three-qubit
bit-flip code (Exercise 4.34), which results in nine qubits total. Applying these encodings one after
another, a method called concatenation, yields the following circuit:
|0i
|0i
|0i H • H •
|0i
|0i =
|0i H • H •
|0i
|0i
|ψi • H • • H •
In the above circuit, the large dashed box to the left is the phase-flip encoding, which turns
|0i → |+ + +i and |1i → |− − −i. Then, the three dashed boxes in the middle are each the bit-flip
encoding, which turns |0i → |000i and |1i → |111i.
If the initial state of the circuit is |ψ00000000i, where |ψi = α|0i + β |1i, show that:
(a) The state of the circuit after the first column (after the CNOT with two targets) is
α|000i|000i|000i + β |100i|100i|100i.
(b) The state of the circuit after the the second column (after the Hadamard gates) is
α
√ (|000i + |100i) (|000i + |100i) (|000i + |100i)
2
β
+ √ (|000i − |100i) (|000i − |100i) (|000i − |100i) .
2
(c) The final state of the circuit is
196 4 Multiple Quantum Bits
α
(|000i + |111i) (|000i + |111i) (|000i + |111i)
23/2
β
+ 3/2 (|000i + |111i) (|000i + |111i) (|000i + |111i) .
2
This is precisely α|0L i + β |1L i.
Let us see how to correct bit flips and phase flips using the Shor code, beginning
with bit flips. First, remember that the qubits are ordered q8 q7 . . . q0 . Say q8 and q3
both experience complete bit flips. Then, the state of the system is
α
(|100i + |011i) (|001i + |110i) (|000i + |111i)
23/2
β
+ 3/2 (|100i − |011i) (|001i − |110i) (|000i − |111i) .
2
To detect this, we measure the parities of adjacent qubits within each triplet. In this
example, we would get:
This tells us that the eighth qubit and third qubit have flipped, so we can apply X
gates to to those two qubits to correct them. Similarly, if there is a partial bit flip,
measuring all the parities to be zero collapses the state and automatically corrects it,
or if there is a discrepancy, we apply X to the appropriate bit to correct it. This also
works with partial bit flips. Measuring the parities of adjacent qubits might collapse
the state and correct the errors, or it might collapse the state into a full bit flip, which
we correct as previously described.
Exercise 4.41. A logical qubit is encoded using nine physical qubits in the Shor code. In each
triplet, you measure the parity of adjacent qubits and get the following results:
Are there any bit flip errors? If so, which bits flipped, and what can you do to correct them?
Exercise 4.42. Bit flips can be corrected in the Shor code using the following quantum circuit:
4.7 Quantum Error Correction 197
|q0 i • X
|q1 i • • X
|q2 i • X
|q3 i • X
|q4 i • • X
|q5 i • X
|q6 i • X
|q7 i • • X
|q8 i • X
|0i • • |0i • • |0i • • |0i
|0i • • |0i • • |0i • • |0i
The first third of the circuit measures the parities of adjacent qubits in the top three qubits, correct
any errors, and reset the ancillas. The middle third of the circuit calculates the parities of adjacent
qubits in the next triplet, correcting any errors. Finally, it does the same for the last triplet of qubits.
Using Quirk, simulate this circuit by inserting it into the following circuit (see https://bi
t.ly/3D1kRKI):
The first part of this circuit applies the Hadamard gate to |q8 i, turning it into |+i. Then, it uses
the circuit in Exercise 4.40 to encode this in the Shor code. Then, the X t gates applies a partial bit
flip to one qubit in each triplet. In the middle section, you should add the previously given circuit.
Although there is no reset feature in Quirk, you can use the postselection tool as a workaround. In
Quirk, it is drawn as the outer product |0ih0|). It measures a qubit in the {|0i, |1i} basis, and if the
result is |0i, the calculation continues. Otherwise, the simulation starts over. For our purposes, it
has the effect of guaranteeing that the ancilla qubit is |0i before proceeding. A true “reset” feature
would allow us to continue with the ancilla as |0i without the risk of restarting the simulation. In
the last section of the circuit, we undo the Shor encoding and Hadamard gate so that all the qubits
are |0i again. Verify that your circuit does this.
Next, let us see how the Shor code also allows us to correct phase flips. Say q3
experiences a complete phase flip. Then, the state of the system is
198 4 Multiple Quantum Bits
α
(|000i + |111i) (|000i − |111i) (|000i + |111i)
23/2
β
+ 3/2 (|000i − |111i) (|000i + |111i) (|000i − |111i) .
2
Then, we can measure the
√ “phase parity” of adjacent triplets, i.e., whether the num-
ber of (|000i − |111i)/ 2 triplets is even or odd. This is similar to the phase flip
code, where we measured the parity in the X basis, which whether the number of
|−i was even or odd. How to measure this parity is shown in Exercise 4.43). In our
example, we would get
This indicates that the middle triplet needs to be flipped, so we apply the Z gate
to any one of the three qubits in that triplet. That is, we can apply the Z gate to
either q5 , q4 , or q3 , correcting the error. Similarly, when there is a partial phase flip,
if we measure all the phase parities and get zero, the state collapsed and corrected
the error, and if there was a discrepancy in phase parities, we apply a Z gate to the
appropriate triplet to correct it.
By alternating between correcting bit-flip errors and phase-flip errors, the Shor
code corrects all quantum errors, assuming each triplet experiences at most one bit-
flip error per correction cycle, and at most one triplet experiences a phase-flip error
per correction cycle.
A quantum computer that accumulates errors slowly enough that errors can be
corrected is called fault tolerant. Depending on the error correcting code that is used,
the maximum correctable error rate can vary, and this is an area of active research.
At the time of this writing, a fault tolerant quantum computer does not yet exist, and
one could argue that building one is the “holy grail” of the field.
Exercise 4.43. The following circuit, which computes the phase parity of adjacent triplets in the
Shor code:
|q0 i
|q1 i
|q2 i • H • H •
|q3 i
|q4 i
|q5 i • H • • H •
|q6 i
|q7 i
|q8 i • H • H •
|0i
|0i
Comparing this circuit with Exercise 4.40, the first two layers of this circuit partially unencodes
the qubit. Then the middle four layers (the CNOTs) calculates the parity of adjacent triplets in two
4.7 Quantum Error Correction 199
ancilla qubits. Then, the final two layers reencodes the qubit. In this exercise, we will work through
this for an example.
(a) A qubit α|0L i + β |1L i is encoded using the nine-qubit Shor code, but a physical qubit in the
middle triplet experienced a phase flip, so the state of the nine qubits is
α
(|000i + |111i) (|000i − |111i) (|000i + |111i)
23/2
β
+ 3/2 (|000i − |111i) (|000i + |111i) (|000i − |111i) .
2
Show that after the first two columns of the circuit (the CNOTs with two targets and the
Hadamards), the state of the nine qubits is
α|000100000i + β |100000100i.
(b) Show that after the middle layers of the circuit (CNOTs), the ancilla qubits now store the
phase parities of adjacent triplets.
(c) Show that at the end of the circuit, the state of the nine qubits is again
α
(|000i + |111i) (|000i − |111i) (|000i + |111i)
23/2
β
+ 3/2 (|000i − |111i) (|000i + |111i) (|000i − |111i) .
2
(d) From the parities in (b), what gate should be applied to fix the phase flip, and to which qubit?
Exercise 4.44. Construct a quantum circuit that corrects when a triplet experiences a phase flip
error in the Shor code. To check your answer, simulate your circuit in Quirk by inserting it into the
following circuit (see https://bit.ly/3kmbTAm):
The first part of this circuit applies the Hadamard gate to |q8 i, turning it into |+i. Then, it uses the
circuit in Exercise 4.40 to encode this in the Shor code. Then, the Z t gates applies a partial bit flip
to one triplet. In the middle section, you should add the previously given circuit. As described in
Exercise 4.44, postselect on |0i as a workaround for resetting the ancilla qubits. In the last section
of the circuit, we undo the Shor encoding and Hadamard gate so that all the qubits are |0i again.
Verify that your circuit does this. Try moving the Z t gate around to different triplets to ensure that
it the phase flip is corrected in all instances, as long as at most one triplet experiences a phase flip.
200 4 Multiple Quantum Bits
Exercise 4.45. You have a logical qubit encoded in nine physical qubits using the Shor code. Let
us label the qubits q8 q7 . . . q0 . They are grouped into three triplets (triplet2 , triplet1 , triplet0 ).
(a) You begin by detecting bit flip errors. Within each triplet, you measure the parity of adjacent
qubits in the Z-basis. Here are the results:
Are there any bit flip errors? If so, which bits flipped, and what can you do to correct them?
(b) Next, you measure the parities of adjacent triplets in the H3 -basis. Here are the results:
Was there a phase flip error? If so, which triplet flipped, and what can you do to correct it?
4.8 Summary
The state of multiple qubits is written as as a tensor product. With n qubits, there
are 2n orthonormal basis states, and a general state is a superposition of these basis
states. In a product state, measuring one qubit cannot affect the others, while in an
entangled state, measuring one qubit can affect the other qubits. A quantum gate on
n qubits is a 2n × 2n unitary matrix. There are various ways to add binary numbers
on a quantum computer. A universal set of quantum gates can approximate any
quantum gate to any desired precision. Quantum bits can suffer from both bit-flip
and phase-flip errors, but they can be corrected, so building a quantum computer
“only” requires really good qubits, not perfect qubits.
Chapter 5
Quantum Programming
Quantum computing is currently emerging from the research lab onto the market-
place. Many companies are building prototype quantum processors, and although
these devices are not yet good enough for fault-tolerant quantum computation,
they may still have uses. These rudimentary quantum processors are called noisy
intermediate-scale quantum (NISQ) devices, where noisy means they suffer from
too much decoherence to be fault-tolerant, and intermediate-scale means they have
a moderate number of qubits, say roughly fifty to a few hundred. NISQ devices were
used to demonstrate quantum computational supremacy, which we briefly discussed
in Section 1.8.3.
Many companies have made their rudimentary quantum processors available for
people to experiment with. In this chapter, we will learn how to program IBM’s
quantum computers over the internet. This is not an endorsement of their product
or services, and other companies have similar tools for programming their quantum
devices, which you are encouraged to explore on your own. Rather, IBM has made
several of their quantum processors freely available to the public, making them a
prudent choice for a textbook. Furthermore, after learning one quantum program-
ming toolkit, it will be easier to learn others, as there are many similarities across
them.
5.1.1 Services
IBM was the first to make their quantum processors available over the internet (over
the “cloud”), and their online platform is called IBM Quantum Experience. It can
be accessed at https://quantum-computing.ibm.com. Their smaller quantum
processors are available to the public, and access to their larger, newer processors is
available commercially. When we log in, we first see the Dashboard:
201
202 5 Quantum Programming
We can go to the Services page to view a list of quantum processors available to us.
To get to the Services page, we can click on the menu icon in the top-left corner of
the Dashboard, then click “Services:”
On the Services page, we can click the “Systems” tab and then filter by “Your sys-
tems:”
This is the list of quantum processors that are available to us. If we click on a pro-
cessor, such as ibmq manila, we can see more information about it:
204 5 Quantum Programming
We see that this quantum processor has five qubits arranged in a line. This arrange-
ment, or topology, can affect which quantum gates can be naturally applied. For
example, we can naturally apply CNOT between qubits 0 and 1. If we want to apply
CNOT between qubits 0 and 2, however, we would need to, for example, SWAP
qubits 2 and 1, apply CNOT between 0 and 1, then SWAP 1 back with 2.
|0i
|0i
|0i H • •
H⊗I⊗I 1
|000i −−−−→ √ (|000i + |100i)
2
CNOT21 1
−−−−→ √ (|000i + |110i)
2
CNOT20 1
−−−−→ √ (|000i + |111i) .
2
This state is known as the Greenberger–Horne–Zeilinger state (GHZ state). It is an
entangled state, and we will revisit it in the next chapter. If we measure it, we find
that all the qubits are 0 with probability 1/2 or all 1 with probability 1/2.
Using the Circuit Composer, we can create this circuit by dragging a Hadamard
gate and two CNOT gates onto the circuit:
206 5 Quantum Programming
To change the control and target of CNOTs, we double-clicked on them and modify
which qubit is the control and which is the target. For example, for CNOT21 , the
control and target are set as shown below:
In the Circuit Composer, we also deleted some qubits so that there are only three.
At the bottom of the webpage, the Circuit Composer automatically simulated the
circuit, showing histograms indicating that the circuit yields |000i with probability
50% or |111i with probability 50%, as expected.
To run this on an actual quantum processor, we need to add at least one measure-
ment. Let us measure all three qubits by adding measurement gates to the Circuit
Composer:
5.1 IBM Quantum Experience 207
Now, the histograms at the bottom of the screen have changed. Instead of giving
both |000i and |111i, we only get one of them. This is because when measuring
the qubits, we only get |000i or |111i, not both. The Circuit Composer is choosing
one of them using a pseudo-random number generator. At the top-right corner of the
screen, we can change the “Visualization seed”, which is a number that the pseudo-
random number generator starts with to generate pseudo-random numbers.
We can run the quantum circuit on one of IBM’s actual quantum processors. At the
top of the Circuit Composer, there is a button that says “Setup and run.” Clicking
that shows the following menu:
From this menu, we can select the quantum system on which to run the circuit. The
number of shots defaults to 1024, meaning it will run our circuit 1024 times and
return a histogram of the measurement outcomes. Ideally, we expect to get |000i
512 times and |111i 512 times. We also see our job limit. Each user is limited to
having five jobs in the queue at a time. Clicking “Run on ibmq xxx” adds our job
208 5 Quantum Programming
to the queue. We can see its status by clicking the “Jobs” tab on the left side of the
screen:
When it is done, we can click it to see the results. Here is what we got for the
histogram:
Theoretically, we expect 000 or 111, each half the time. Due to a limited number of
shots and decoherence in the quantum processor, however, the results deviated from
our expectations. The results page also shows the actual quantum circuit that was
run, which is called the transpiled circuit:
5.1 IBM Quantum Experience 209
That is, it might not be possible to run our original quantum circuit on the device due
to the topology or gate set available to the processor, so the software will transpile
or convert our circuit to an equivalent one that can be physically run.
Exercise 5.1. Using the drag-and-drop Circuit Composer in IBM Quantum Experience, run the
following circuit on one of IBM’s quantum processors:
|0i X • |1i
|0i X • |1i
|0i |1i
5.1.4 Simulator
Sometimes, it can take a long time for a job to make it through the queue for an
actual quantum processor. Or, the available quantum processors have too few qubits.
In these cases, using a simulator rather than an actual quantum processor may be
favorable. Let us try this for the previous circuit that creates the GHZ state. Clicking
“Setup and run,” let us run the circuit on ibm qasm simulator:
Out of the 1024 shots, the simulator measured |000i 493 times and |111i 531 times.
Due to the limited number of shots and the use of a pseudo-random number gen-
erator to simulate the results, the results were not perfectly 50% each, but they are
pretty close.
Exercise 5.2. Simulate the circuit from Exercise 5.1 using ibm qasm simulator. Draw the resulting
histogram showing the probability of each measurement outcome.
5.2.1 OpenQASM
Rather than dragging and dropping quantum gates to create a circuit, they can also
be written using programming languages. We can describe quantum circuits using
OpenQASM, where Open refers to the specification being open or freely available,
and QASM (pronounced kazm) stands for quantum assembly language. Despite the
name “assembly language,” it is really more of a hardware description language
like Verilog (see Section 1.3), where we defined registers and wires, listed logic
gates with their inputs and outputs, and defined modules/functions. A document
describing OpenQASM is available at https://arxiv.org/abs/1707.03429.
Here is an example of a simple OpenQASM program:
OPENQASM 2.0;
qreg q[3];
creg c[3];
U(pi,0,pi) q[0];
CX q[0], q[1];
measure q -> c;
212 5 Quantum Programming
The first line specifies that it is an OpenQASM program, version 2.0. Then we define
a quantum register or array named q consisting of 3 qubits, |q2 i|q1 i|q0 i. All of these
qubits are initially |0i, so q is initially |000i. This is followed by a classical register
named c consisting of 3 bits, also indexed c2 c1 c0 , and all these bits are initially
0. Next, we apply a one-qubit quantum gate U(0,pi,0) to qubit q[0], where the
one-qubit gate is parameterized as
−i(φ +λ )/2
cos(θ /2) −e−i(φ −λ )/2 sin(θ /2)
e
U(θ , φ , λ ) = .
ei(φ −λ )/2 sin(θ /2) ei(φ +λ )/2 cos(θ /2)
With appropriate choices for the angles, any one-qubit gate can be written this way,
up to a global phase. Technically, this is a rotation about the z-axis of the Bloch
sphere by λ , followed by a rotation about the y-axis by θ , followed by another
rotation about the z-axis, but by φ . In this example, when (θ , φ , λ ) = (π, 0, π), we
get
−iπ/2
e cos(π/2) −eπ/2 sin(π/2)
U(π, 0, π) = −iπ/2
e sin(π/2) eiπ/2 cos(π/2)
0 −i 01
= = −i = −iX ≡ X,
−i 0 10
where in the last step, ≡ means “equivalent to” because the global phase of −i can
be dropped. So, this gate transforms q from |000i to i|001i, but the global phase
can be ignored, so it is just |001i. Next, CNOT (cX) is applied with q[0] as the
control and q[1] as the target, transforming the state from |100i to |110i. Finally, q
is measured, and the resulting bits are placed in the classical register c. So, c[2] =
0, c[1] = 1, and c[0] = 1.
U(θ , φ , λ ) and CX are the only two gates that OpenQASM has built-in because it
is a universal gate set. That is, recall from Section 4.6 that {CNOT, all single-qubit gates}
is universal for quantum computing. We can, however, define our own gates so that
they are easier to use. Rewriting our previous code,
OPENQASM 2.0;
qreg q[3];
creg c[3];
x q[0]
CX q[0], q[1];
measure q -> c;
5.2 Quantum Assembly Language 213
Comments are preceded by two slashes //, and names must start with a lowercase
letter, so our gate is called x, not X.
Rather than writing all one-qubit gates in the form U(θ , φ , λ ) or defining them our-
selves, it would be convenient if commonly used quantum gates like X, Y , Z, H,
and others were predefined. Thankfully, these and many of the gates used by IBM
Quantum Experience are defined in the library qelib1.inc, which we can include
in OpenQASM. So our previous code can be written as
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
x q[0]
cx q[0], q[1];
measure q -> c;
Note CX has been replaced by cx, since we are now using CNOT defined in the
Quantum Experience library instead of the CNOT that is native to OpenQASM.
Exercise 5.3. Recall OpenQASM parameterizes an arbitrary single-qubit gate as
−i(φ +λ )/2
cos(θ /2) −e−i(φ −λ )/2 sin(θ /2)
e
U(θ , φ , λ ) = .
ei(φ −λ )/2 sin(θ /2) ei(φ +λ )/2 cos(θ /2)
u1 (λ ) = U(0, 0, λ ),
u2 (φ , λ ) = U(π/2, φ , λ ),
u3 (θ , φ , λ ) = U(θ , φ , λ ).
Besides dragging and dropping quantum gates, IBM Quantum Experience also sup-
ports programming using OpenQASM. From the previous circuit for the GHZ state,
we can go to the menu and select “View” and then “Code Editor:”
Then, the Code Editor will appear on the right side of the screen:
Now, let us write some OpenQASM code to add 1110 + 1011 = 11001 using the
quantum ripple-carry adder in Section 4.5.6 and simulate it in IBM Quantum Expe-
5.2 Quantum Assembly Language 215
rience. We can define our own quantum gates to implement the sum S, carry C, and
inverse carry C† . Note s is the S-gate, so we cannot use it as an identifier/name.
OPENQASM 2.0;
In the Circuit Composer, if we click the button on the left to view the “Composer
files,” there is a button to upload an OpenQASM circuit:
The circuit is too tall to include all of it in the above picture. Also, the histograms at
the bottom of the circuit is not available because our circuit uses too many qubits.
If we look at the Bloch spheres, however, at the end of the circuit, we see that
b4 b3 b2 b1 b0 = 11001, so the addition circuit works, as expected. We can also run the
circuit on the ibm qasm simulator backend, which yields the following results:
Again, the sum is 11001, as expected. Finally, we can run it on an actual quantum
processor, yielding the following:
The histogram cannot fit all the results. If we click the three dots in the upper-right
corner of the histogram, we can download the results as a CSV file. Here’s what we
get:
Computational basis states,Probabilities
00000,27.832
00001,16.113
00010,8.398
00011,4.199
00100,7.031
00101,3.125
00110,1.27
00111,1.367
01000,5.176
01001,2.246
01010,1.367
01011,0.391
01100,1.074
01101,0.586
01110,0.098
218 5 Quantum Programming
01111,0.293
10000,7.324
10001,3.711
10010,2.246
10011,0.879
10100,1.758
10101,0.781
10110,0.488
10111,0.293
11000,1.074
11001,0.488
11011,0.195
11100,0.098
11101,0.098
So, out of 1024 shots, the probability of getting the correct answer of 11001 is
0.488%, or less than half a percent. That is quite bad. To understand why, let us look
at the transpiled circuit. It is so long that we split it across six images:
5.2 Quantum Assembly Language 219
Since the transpiled circuit is so long, our qubits are noisy, and we do not yet have
error correction, the errors just kept accumulating. It is unsurprising, then, that our
histogram was so wrong.
Exercise 5.4. Consider the following quantum circuit:
|0i • H •
|0i •
|0i H • Z
5.3 Qiskit
Besides the Circuit Composer and OpenQASM editor, IBM has provided another
way to program their quantum processors. It is called Qiskit, where QIS stands for
quantum information science, and kit refers to a software development kit (SDK).
Qiskit is pronounced “kiz kit,” although some variants exist, like, “quiz kit” and
“kiss kit.” Qiskit is not a programming language, but is rather a toolkit or package
for the Python programming language. Qiskit is the most powerful way to program
IBM’s quantum computers because it provides more functionality than the other
approaches, and it also allows users to use Python’s vast network of packages and
libraries. More information about Qiskit is available at qiskit.org.
You can use Qiskit inside IBM Quantum Experience. To view a circuit as Qiskit
code, in the Circuit Composer, just select “Qiskit” in the Code Editor. For the GHZ
state, we get the following:
5.3 Qiskit 221
circuit.h(qreg_q[2])
circuit.cx(qreg_q[2], qreg_q[1])
circuit.cx(qreg_q[2], qreg_q[0])
circuit.measure(qreg_q[0], creg_c[0])
circuit.measure(qreg_q[1], creg_c[1])
circuit.measure(qreg_q[2], creg_c[2])
The first line imports from the Qiskit package functions to define quantum registers,
classical registers, and quantum circuits. In the second line, we import from the
numpy package the number pi. Although it is not used in this circuit, it is used in
many circuits, so it is included here for convenience.
In the next block of lines, the code defines a quantum register of length 3, la-
beled q, with the variable name qreg q. Then, the three qubits would be qreg q[0],
qreg q[1], and qreg q[2]. Similarly, the next line defines a classical register of
length 3, labeled c, with the variable name creg c, so the bits are creg c[0],
creg c[1], and creg c[2]. After that, a quantum circuit is created containing the
quantum and classical registers, and we name it circuit.
Finally, in the last block of 6 lines, we add a Hadamard gate to our quantum
circuit, and it is applied to qubit qreg q[2]. Then, we add a CNOT (cX) gate, with
qreg q[2] as the control and qreg q[1] as the target. Then, we add another CNOT
gate, again with qreg q[2] as the control, but now with qreg q[0] as the target. In
the final three lines, we add measurements to the circuit, and the result of measuring
qubit qreg q[0] is placed in the classical bit creg c[0], and so forth.
In the Circuit Composer, the Qiskit code is “read only,” so it cannot be modified. To
modify it, we click “Open in Quantum Lab.” This opens a Jupyter notebook, where
Python code can be executed and the results displayed in an interactive manner:
222 5 Quantum Programming
The first cell contains mostly the code from before, but now it starts and ends with
some additional code:
from ibm_quantum_widgets import CircuitComposer
editor = CircuitComposer(circuit=circuit)
editor
The first line loads a package that will allow us to view the Circuit Composer from
within the Quantum Lab. The second-to-last line creates the Circuit Composer as
an object named editor, and the last line displays the editor. We can run this cell by
selecting the cell and clicking the . Run button, or by pressing Shift+Enter on your
keyboard. When we do, we get the following:
5.3 Qiskit 223
After running the cell, a second, empty cell appears below. We can put any Python
code we would like. For example, we can draw the circuit without using the en-
tire Circuit Composer using the draw function within QuantumCircuit to draw a
picture of our quantum circuit:
QuantumCircuit.draw(circuit)
As another example, we can print the OpenQASM code for the quantum circuit:
print(circuit.qasm())
5.3.3 Simulator
Now, let us simulate this quantum circuit. The simulators are contained in a Qiskit
library called Aer, which we can import and then view the backends:
# Import the Qiskit Aer library
from qiskit import Aer
Let us use qasm simulator as the backend and execute the circuit on it:
# Choose the qasm_simulator backend.
backend = Aer.get_backend(’qasm_simulator’)
We can use the job monitor function to see if the job is completed. It needs to be
imported from Qiskit.
# Check the status of the job.
from qiskit.tools.monitor import job_monitor
job_monitor(job)
If the job has not yet run, the job monitor will periodically update itself. Once the
job is done, we can get a count of the results and print them:
# Get a count of the results.
count = job.result().get_counts()
We expect that each result should appear 512 times (half of 1024 shots), but just like
flipping a coin 1024 times may not yield heads 512 times and tails 512 times, there
is some deviation because of statistics, not because of errors. This is a simulation,
not actual noisy quantum hardware.
We can also plot a histogram of the counts by importing the qiskit.visualizations
package and calling the plot histogram function:
# Import visualizations from Qiskit.
from qiskit.visualization import *
Now let us run the quantum circuit on one of IBM’s actual quantum processors.
First, we need to load our IBM Quantum account to see what quantum processors
are available to us:
# Import the Qiskit IBMQ library.
from qiskit import IBMQ
This prints the quantum processors that are available to us, which includes the public
ones and any that we have paid access to. The output is
[<IBMQSimulator(’ibmq_qasm_simulator’) from IBMQ(hub=’ibm-q’,
,→ group=’open’, project=’main’)>,
<IBMQBackend(’ibmqx2’) from IBMQ(hub=’ibm-q’, group=’open’,
,→ project=’main’)>,
<IBMQBackend(’ibmq_16_melbourne’) from IBMQ(hub=’ibm-q’,
,→ group=’open’, project=’main’)>,
<IBMQBackend(’ibmq_vigo’) from IBMQ(hub=’ibm-q’, group=’open
,→ ’, project=’main’)>,
<IBMQBackend(’ibmq_ourense’) from IBMQ(hub=’ibm-q’, group=’
,→ open’, project=’main’)>,
<IBMQBackend(’ibmq_valencia’) from IBMQ(hub=’ibm-q’, group=’
,→ open’, project=’main’)>,
<IBMQBackend(’ibmq_armonk’) from IBMQ(hub=’ibm-q’, group=’
,→ open’, project=’main’)>,
<IBMQBackend(’ibmq_athens’) from IBMQ(hub=’ibm-q’, group=’
,→ open’, project=’main’)>,
<IBMQBackend(’ibmq_santiago’) from IBMQ(hub=’ibm-q’, group=’
,→ open’, project=’main’)>]
As before, let us pick a particular hardware backend and execute our circuit. We have
previously imported the necessary functions and libraries, so we can just execute the
commands:
# Choose a quantum processor as the backend.
backend = provider.get_backend(’ibmq_athens’)
Again, we can use the job monitor function to see if the job is completed. It is
already imported, so we do not need to import it again:
job_monitor(job)
Once it has finished, we can get a count of the results, print them, and print a his-
togram:
5.3 Qiskit 227
count = job.result().get_counts()
print(count)
plot_histogram(counts)
The output is
{’000’: 499, ’001’: 7, ’010’: 8, ’011’: 13, ’100’: 1, ’101’:
,→ 22, ’110’: 17, ’111’: 457}
Most of the time, the result of the quantum circuit is 000 or 111, but due to decoher-
ence, some other results are also obtained.
Exercise 5.5. In IBM Quantum Experience, go to the Quantum Lab through the menu:
In the Quantum Lab, create a new Jupyter notebook for Qiskit (the arrow below indicates which
button to click):
The notebook should be blank. Starting with this, import the necessary Qiskit libraries and func-
tions and create the following quantum circuit:
228 5 Quantum Programming
|0i S H T H
|0i H •
OpenQASM and Qiskit were both developed by IBM to work with their quantum
devices. They are not the only options for quantum programming, however. Here
are a few others, which you are free to explore on your own, and there are more
beyond this list:
• Cirq is Google’s Python software library for programming their quantum com-
puters.
• Microsoft developed a programming language called Q# (pronounced “Q-
sharp”) specifically for programming quantum computers.
• Quil is a quantum “instruction set architecture” made by Rigetti for program-
ming its quantum computers. It is similar to OpenQASM. PyQuil is a library
for programming Quil in Python.
5.5 Summary
In this chapter, we will explore entanglement in greater detail. We will see that if two
people, Alice and Bob, share entangled qubits, they can exploit this entanglement
to send information to each other using various protocols. With superdense coding,
Alice can send one qubit to Bob, but Bob will be able to discern two classical bits
worth of information. With quantum teleportation, Alice will be able to teleport a
qubit to Bob by only sending him classical information. We will also see a quantum
protocol that does not use entanglement: quantum key distribution, Alice and Bob
will be able to agree on a secret key or code, and the laws of quantum mechanics
prevent a third party from learning the key.
Quantum entanglement has its limitations, however. Although measuring a qubit
causes its entangled pair to also collapse, this cannot be used to communicate faster-
than-light. This is called the no-signaling principal.
6.1 Measurements
In Section 4.3, we learned about product states, which can be factored into the tensor
product of single-qubit states, and entangled states, which could not. For example,
1 1 1
(|00i − |01i + |10i − |11i) = √ (|0i + |1i) ⊗ √ (|0i − |1i)
2 2 2
| {z } | {z }
|+i |−i
= |+i ⊗ |−i
= |+i|−i
229
230 6 Entanglement and Quantum Protocols
Now, if we measure a single qubit in a product state, it does not affect the other
qubit. For example, if we measure the left qubit of |+i|−i, we get |0i or |1i, each
with probability 1/2. Thus, the resulting states are |0i|−i or |1i|−i. The right qubit
remains |−i, unaltered by the measurement of the left qubit. A product state has no
entanglement.
If we measure a single qubit in an entangled state, it can affect √ the other qubits. For
example, consider the entangled state |Φ + i = (|00i + |11i) / 2. If we measure the
left qubit, we get |0i or |1i, each with probability 1/2, and the state collapses to |00i
or |11i, respectively. So, if we measure the left qubit and get |0i, we know that the
right qubit is also in the state |0i, and similarly, if we measures the left qubit and get
|1i, we know that the right qubit is also in the state |1i.
This is another way to identiy entanglement. A measurement of one qubit affects
the other qubit.
In this example, |Φ + i has the maximum amount of entanglement, since mea-
suring one qubit completely determines what the other qubit will be. That is, if we
measure the left qubit and get |0i, we know with certainty that a measurement of
the second qubit will also yield |0i. The same holds for |1i. We say such entangled
states are maximally entangled.
With two qubits, there are four maximally entangled states. They are the Bell
states:
Φ = √1 (|00i + |11i) ,
+
2
− 1
Φ = √ (|00i − |11i) ,
2
+ 1
Ψ = √ (|01i + |10i) ,
2
− 1
Ψ = √ (|01i − |10i) .
2
This state is entangled because it cannot be factored into the tensor product of single-
qubit states (see Exercise 6.1). If we measure only the left qubit, we get
• 0 with probability 3/4, and the state collapses to
1 1
√ (|00i + |01i) = |0i √ (|0i + |1i) .
2 2
• 1 with probability 1/4, and the state collapses to
√ √ !
3 1 3 1
|10i + |11i = |1i |0i + |1i .
2 2 2 2
We see that measuring the left qubit does√ affect the right qubit because in one case,
the right qubit√collapses to (|0i + |1i)/ 2, while in the other case, the right qubit
collapses to ( 3|0i + |1i)/2. So, there is entanglement. Next, if we measure the
right qubit, we may get 0 or 1, with probabilities 50:50 or 0.75:0.25, depending on
which state the qubit was in. So, even though measuring the left qubit affected the
right qubit, it did not completely determine what a measurement of the right qubit
would yield. Since we do not know exactly what the right qubit will be, the original
state is not maximally entangled. We say it is partially entangled.
Various ways the quantify the amount of entanglement have been proposed. They
all agree that product states have no entanglement, and they largely agree on which
states are maximally entangled. They disagree on the degree to which partially en-
tangled states are entangled. This is an area of active research and is beyond the
scope of this textbook.
Exercise 6.1. Consider the following state of two qubits:
√ √ √
3 3 3 1
√ |00i + √ |01i + |10i + |11i.
2 2 2 2 4 4
If you measure the left qubit, what outcomes can you get, what are the corresponding probabilities
of those outcomes, and what does the state collapse to for each outcome? Is this state a product
state, partially entangled state, or maximally entangled state?
1
√ (|01i + |10i) .
2
If you measure the left qubit, what outcomes can you get, what are the corresponding probabilities
of those outcomes, and what does the state collapse to for each outcome? Is this state a product
state, partially entangled state, or maximally entangled state?
232 6 Entanglement and Quantum Protocols
In 1935, Einstein, Podolsky, and Rosen (EPR) published a paper where they took
entangled measurements a step further. Einstein is famous for his Theory of Relativ-
ity, among other breakthroughs, and one idea that he pioneered was that the speed
of light acted as a universal speed limit, that nothing can travel through space faster
than light. This notion that no influence can propagate faster than light is called
locality. The EPR paradox uses locality to question the meaning of quantum me-
chanics.
As a modern take on the EPR experiment, we take two qubits and prepare them
in the |Φ + i state (such as by starting with |00i, applying H ⊗ I to get |+i|0i, and
then applying CNOT to get |Φ + i). Now, we separate the two qubits so that two
scientists, conventionally named Alice and Bob (A and B), each have one qubit:
1
√ (|00i + |11i)
2
Alice Bob
Now, say Alice measures her qubit, and then Bob measures his qubit right after,
so quickly after Alice’s measurement that any influence from Alice would have to
travel faster than light in order to affect Bob’s qubit. Alice and Bob can be suffi-
ciently far apart to make this feasible.
According to the laws of quantum mechanics, whenever Alice measures |0i, Bob
should also measure |0i, since the moment Alice measures |0i, the state of the qubits
is now |00i. The same goes for |1i and |1i. EPR proposed that for this to be true,
either Alice’s measurement was able to influence Bob’s measurement faster than
light, or the measurement outcomes were predetermined by some hidden variable
that quantum mechanics did not account for, meaning quantum mechanics was in-
complete. EPR’s proposal became known as the EPR paradox.
EPR considered the former option absurd, and Einstein even called the faster-
than-light collapse of the quantum state “spooky action-at-a-distance” to deride it.
EPR advocated the latter option, that the measurement outcomes are determined be-
fore the measurement in such a way that quantum mechanics does not account for.
This belief is called realism, that the qubits have actual, real values before measure-
ment.
Note EPR did not argue against quantum mechanics’ correctness, just its com-
pleteness. That is, they did agree that whenever Alice measured |0i, Bob would
also measure |0i, and the same goes for |1i. But they argued that a local hidden
variable theory must exist that explains these outcomes assuming locality, and this
more complete theory would replace quantum mechanics.
The hardware necessary to do this experiment was not available at the time, and
would not be for nearly fifty years in the early 1980’s, long after Einstein’s death in
1955. Even if scientists were able to do the experiment at the time, it would not have
resolved the paradox. If scientists did the experiment and Alice and Bob’s qubits
6.2 Bell Inequalities 233
In 1964, John Stewart Bell proved an important result that would further distinguish
quantum mechanics from the local hidden variable theories that EPR, and others,
proposed. Bell calculated the measurement statistics of quantum mechanics for a
general problem. Then, he calculated the measurement statistics of any local hidden
variable theory and showed that they must obey an inequality. The amazing result is
that the quantum mechanical statistics disobeyed or violated the inequality, giving a
way to experimentally determine whether nature followed quantum mechanics or a
local hidden variable theory. If the experiment agreed with quantum mechanics, then
nature is not described by any local hidden variable theory, and if the experiment
agreed with the inequality, the quantum mechanics is not simply incomplete, it is
wrong, because it made an incorrect prediction. An experiment that tests this is
called a Bell test.
The general result that Bell proved is beyond the scope of this textbook, but we
will focus on a particular Bell inequality proposed by Clauser, Horne, Shimony, and
Holt (CHSH). In this experiment, Alice and Bob each have one qubit in the |Φ + i
state:
1
√ (|00i + |11i)
2
Alice Bob
Alice will either measure her qubit in the Z-basis A = {|0i, |1i} or the X-basis A0 =
{|+i, |−i}. Of course, measuring A is just a typical measurement. Measuring in the
X-basis can be done by first applying H, which turns |+i to |0i and |−i to |1i, and
then measuring in the Z-basis:
234 6 Entanglement and Quantum Protocols
A0 H
Bob will measure his qubit in one of two bases. The first is
( )
1 h √ i 1 h √ i
B= p √ (1 + 2)|0i + |1i , p √ (1 − 2)|0i + |1i ,
4+2 2 4−2 2
1 h √ i x+z
p √ (1 + 2)|0i + |1i
4+2 2 •
y
x •
1 h √ i
p √ (1 − 2)|0i + |1i
4−2 2
The other basis for Bob is
( )
0 1 h √ i 1 h √ i
B = p √ (−1 − 2)|0i + |1i , p √ (−1 + 2)|0i + |1i ,
4+2 2 4−2 2
y
x
•
1 h √ i
p √ (−1 + 2)|0i + |1i
4−2 2
To measure in these bases, we apply the following gates, and then measure in the
Z-basis:
6.2 Bell Inequalities 235
B S H T H
B0 S H T† H
These circuits convert the basis states to |0i and |1i, and then we measure in the
Z-basis (see Exercise 6.4).
Exercise 6.4. Consider Bob’s two measurement bases, B and B0 :
( )
1 h √ i 1 h √ i
B= p √ (1 + 2)|0i + |1i , p √ (1 − 2)|0i + |1i .
4+2 2 4−2 2
( )
0 1 h √ i 1 h √ i
B = p √ (−1 − 2)|0i + |1i , p √ (−1 + 2)|0i + |1i ,
4+2 2 4−2 2
(a) Show that the following quantum circuit converts the basis states of B to |0i and |1i, up to a
global phase.
B S H T H
(b) Show that the following quantum circuit converts the basis states of B0 to |0i and |1i, up to a
global phase.
B0 S H T† H
You can do these calculations by hand if you would like, but I recommend using a computer algebra
system.
So, there are four combinations of bases, AB, AB0 , A0 B, and A0 B0 . For each of
these four bases, Alice and Bob run the experiment many times, and they get some
probability distribution for them both getting 0, getting 0 and 1, getting 1 and 0, and
both getting 1:
P00 , P01 , P10 , P11 .
Now Alice and Bob interpret their measurement results as +1 and −1. So, if Alice
measures |0i, she records it as +1, and when she gets |1i, she records it as −1. Bob
does the same thing. If we multiply their measurement results, then |00i would be
(1)(1) = 1, |01i would be (0)(−1) = −1, |10i would be (−1)(0) = −1, and |11i
would be (−1)(−1) = 1. Then, the average outcome are these values multiplied by
their probabilites, added together:
This is called the quantum correlation, and it is simply the average or expected
value of the product of their measurement results. Finally, consider the following
quantity that comes from adding/subtracting the quantum correlations for the four
measurement bases:
The CHSH inequality is a Bell inequality that says for any local hidden variable
theory,
|S| ≤ 2.
The derivation of this is beyond the scope of this textbook. We can calculate, how-
ever, what quantum mechanics predicts S should be. First, if Alice measures in the
Z-basis A and Bob measures in the basis B, then the circuit we apply is
S H T H
|Φ+ i
Then, the state at the end of the circuit, before the measurements, is
The output is
1 + eiπ/4 2 2 + √2
P00 = √ = ,
2 2 8
1 − eiπ/4 2 2 − √2
P01 = √ = ,
2 2 8
1 − eiπ/4 2 2 − √2
P10 = i √ = ,
2 2 8
1 + eiπ/4 2 2 + √2
P11 = i √ = .
2 2 8
find
(a) P00 .
(b) P01 .
(c) P10 .
(d) P11 .
(e) E(A0 , B0 ) = P00 − P01 − P10 + P11 .
You can do these calculations by hand if you would like, but I recommend using a linear algebra
on a computer, like Mathematica.
Then,
4 √
S = E(A, B) + E(A, B0 ) + E(A0 , B) − E(A0 , B0 ) = √ = 2 2 ≈ 2.83.
2
238 6 Entanglement and Quantum Protocols
Thus, we see that quantum mechanics violates the CHSH inequality, since quantum
mechanics predicts S = 2.83, while any local hidden variable theory predicts |S| < 2.
Now, the true value of S is a matter of experiment. If we get a value of S greater than
2, then local hidden variable theories are wrong. If we get a value less that 2, then
quantum mechanics is not only incomplete, but wrong because it made an incorrect
prediction.
Tsirelson proved that the value for S that we just calculated is the maximum
amount that S can be using quantum mechanics, i.e.,
√
|S| ≤ 2 2.
Since we have access to IBM’s quantum processors, let us try running the CHSH
experiment to see if we get a value of |S| less than or greater than 2.
We need to run four experiments, one for each of the measurement bases AB,
AB0 , A0 B, and A0 B0 . Beginning with AB:
In this circuit, we have two qubits that are initially |00i. We apply H ⊗ I to get |+0i
and then CNOT to get |Φ + i. Now Alice measures in the Z-basis A, and Bob mea-
sures in the B basis. We are not interested in the simulation because the simulation
is simply following the laws of quantum mechanics, and we want to test if quantum
mechanics is actually correct experimentally or not. Running this on ibmq athens,
we get the following results:
6.2 Bell Inequalities 239
Note you also ran this circuit on a quantum processor in Exercise 5.5.
Next, for AB0 , Alice again measures in the Z-basis, but now Bob measures in the
0
B basis:
Third, for A0 B, Alice now measures in the X-basis, and Bob measures in the B
basis:
Finally, for A0 B0 , Alice measures in the X-basis, and Bob measures in the B0 basis:
Since this is greater than 2, quantum mechanics is right. Nature is not described by
a local hidden variable theory.
6.2 Bell Inequalities 241
Our previous experiment using a quantum processor is not very precise, however.
We do not know how much time occurred between the measurements of the two
qubits, so we have not guaranteed that we measured them so closely that an influ-
ence would need to travel faster than light. Far more precise experiments, not using
quantum processors, were performed in the early 1980’s by Alain Aspect and others
(see Exercise 6.6), and Alice and Bob’s qubits were sufficiently far apart that the
collapse of one would need to travel faster than light to affect Bob’s qubit. They
showed that the Bell inequalities were indeed violated, meaning the universe is not
locally realistic (i.e., not described by local hidden variable theories). Since then,
even more experiments have been done that have closed various “loopholes” that
skeptics have raised (see Exercise 6.7).
Exercise 6.6. Go to https://journals.aps.org/prl/abstract/10.1103/PhysRe
vLett.49.91 and download the PDF.
(a) From the abstract, the scientists measured pairs of what?
(b) From Eq. (2), Bell’s inequalities say that for “realistic local theories,” the quantity S must be
between what two values?
(c) From Eq. (6), quantum mechanics predicts what value of S for the experiment?
(d) From Eq. (5), what value of S did they actually measure?
(e) Did their measured value for S in (d) agree or disagree with Bell’s inequalitiy for realistic
local theories in (b)?
From the above experiments, the collapse of an entangled state occurs faster than
light. While this seems to violate a fundamental tenant of physics, that nothing can
travel through space faster than light, it does not permit information to be transmitted
faster than light. So, while the universe permits “spooky action at a distance,” it does
not permit “spooky communication at a distance.”
Say Alice and Bob share seven maximally entangled pairs of qubits, with each
pair in the state |Φ + i. Alice is on Earth, and Bob travels to another galaxy with his
qubits.
242 6 Entanglement and Quantum Protocols
1
√ (|00i + |11i)
2
1
√ (|00i + |11i)
2
1
√ (|00i + |11i)
2
1
√ (|00i + |11i)
2
Alice Bob
1
√ (|00i + |11i)
2
1
√ (|00i + |11i)
2
1
√ (|00i + |11i)
2
Alice wants to send the ASCII letter “Q” (i.e., the bit string 1010001) to Bob. Clas-
sically, Alice would need to physically send the bits, such as via radio waves trans-
mitted through space, or by writing a letter and sending it on a spaceship. Either
way, her message (the letter “Q”) cannot travel faster than light. But quantumly, can
they use their entangled qubits to communicate faster than light?
What might Alice try doing? If she measures each of her seven qubits, she gets
|0i or |1i for each qubit, and Bob’s qubits would match hers. But since she gets
each value with probability 1/2, Alice and Bob get matching random strings, not
1010001 that encodes the letter Q.
In hopes of getting something less random, suppose Alice measures a qubit in
the Z-basis {|0i, |1i} to try to communicate 0, and in the X-basis {|+i, |−i} to try
to communicate 1. So to send 1010001, Alice would measure her first qubit in the
X-basis, her second qubit in the Z-basis, her third qubit in the X-basis, her fourth
through sixth qubits in the Z-basis, and her seventh qubit in the X-basis. When
she measures in the Z-basis, she gets |0i or |1i, each with probability 1/2. If she
measures in the X-basis, then we can show that (see Exercise 6.8):
Φ = √1 (|++i + |−−i) ,
+
2
and so she gets |+i or |−i, each with probability 1/2. Bob’s qubits match Alice’s
exactly. To determine the message, Bob needs to determine which basis the qubits
were measured in. But there is no way for him to do that. If he measures in the same
basis as Alice, he gets the same result as Alice. But if he measures in the wrong basis
(X when Alice used Z, or Z when Alice used X), he gets an answer with probability
6.2 Bell Inequalities 243
1/2. Either way, he does not know if the answer he got was correct or random unless
he calls Alice on the phone and asks her, which is a classical communication bound
by the speed of light.
In fact, no matter what Alice and Bob try, they cannot use entanglement to com-
municate faster than light. This is called the no-signaling principle, and it is a state-
ment of no faster-than-light communication or no superluminal communication.
Exercise 6.8. Show that |Φ + i = √1 (|00i + |11i), in the X-basis, is
2
1
√ (|++i + |−−i) .
2
The Bell tests show that nature is not locally realistic. Whether locality is wrong,
realism is wrong, or both, however, is still a matter of debate.
Our explanation of quantum computing and quantum mechanics, that quantum
states are superpositions and measurement collapses the state, is known as the
Copenhagen interpretation. It was formulated by Niels Bohr and Werner Heisen-
berg, two of the “fathers” of quantum mechanics, in the 1920’s, and is named after
Copenhagen, Denmark because that is where they worked. It is the most popular
interpretation of quantum mechanics. Following this interpretation, the EPR para-
dox would be resolved by the understanding that quantum states are superpositions
and do not take “real” values until measurement, and the collapse occurs instanta-
neously, although information cannot be communicated faster than light.
It is not the only interpretation, however, and others explain the EPR paradox
differently:
• Some physicsts theorize that entangled particles are connected through worm-
holes or Einstein-Rosen bridges. Then, the collapse of entangled states does not
occur faster than light because these bridges provide shortcuts across spacetime.
• In pilot wave theory or de Broglie-Bohm theory or Bohmian mechanics, an ac-
tual, real particle interacts with a wave that guides it, and the wave evolves by
the laws of quantum mechanics. This maintains realism but abandons locality.
From the Bell tests, our universe is not described by a local hidden variable
theory, but that does not exclude the possibility that nature obeys a non-local
hidden variable theory. Pilot wave theory is a non-local hidden variable theory.
• In the many-worlds interpretation, quantum states do not collapse. Rather, when
a measurement is made, parallel universes are created, one where each possible
outcome occurred. Both outcomes are equally real in each universe. This is local
and realistic
Exercise 6.9. Watch “Quantum Entanglement and the Great Bohr-Einstein Debate” by PBS Space-
time at https://www.youtube.com/watch?v=tafGL02EUOA. Fill in the blanks.
244 6 Entanglement and Quantum Protocols
(a) “This notion that the universe exists independent of the mind of the observer is called
in physics.”
(b) “Niels Bohr insisted that it was meaningless to assign reality to the universe in the absence of
observation; in the intervals between measurements, quantum systems truly exist as a fuzzy
mixture of all possible properties, what we call a of states.”
(c) “Albert Einstein insisted on an objective reality, a reality independent of our observation of it.
He insisted that the wave function, and by extension quantum mechanics, is .
There must exist what we call that reflect a more
physical underlying reality.”
(d) “[EPR] proposed a quantum scenario that showed in order to abandon the assumption of
realism, you also had to to abandon a concept almost as sacred— .
is the idea that each bit of the universe only acts on its immediate sur-
roundings. This is fundamental to Einstein’s relativity, which tells us that the chain of cause
and effect can’t propagate any faster than the
.”
(e) “Quantum mechanics requires that we describe the particle pair with a single combined
wave function that encompasses all possible states of both particles. We call such particles
an . Now, according to the Copenhagen interpre-
tation, any measurement of one particle automatically collapses the entire entangled wave
function and so affects the results of measurements of other particle. That’s an influence that
could theoretically be transmitted across any distance, and even back in
time, violating locality and possibly violating causality. Einstein et al. thought this very silly.
They thought that every special point in the universe must be real and physical and defined
by knowable quantities, that
could affect each other no faster than the speed of light.”
(f) “Measurement the alignment of the measured particle.”
(g) “Scenario one, if Einstein was right, imagine the response of each particle to all pos-
sible spin measurements is encoded in each particle at the moment of their creation as
local to each particle. we
do later to one particle will then effect the other. When we later measure the spins of both
particles, there will be a correlation in the results because the particles were once connected.
But there’ll be due to our choice of measurement
axis.”
(h) “Scenario two—Bohr was right. What if between creating and measurement, the electron and
positron only exist as a wave function of all possible states. In that case, measurement of one
particle spin should cause the entire wave function to collapse, to take on
. Both particles should then manifest opposite spins along whichever
axis we choose for one of the particles. That should lead to a correlation between our choice
of measurement axis for the first particle and the spin of direction then measured for the
second. This is exactly the at a distance that made
Einstein so uncomfortable.”
(i) “John Steward Bell figured out a set of observable results, the so-called
, that we expect to see in the case that Einstein was right and quantum
mechanics needs local hidden variables. But if an entanglement experiment violates the Bell
inequalities, then local realism is also violated.”
(j) “But in the early ’80s, French physicist Alain Aspect succeeded. Instead of looking at the en-
tangled spins of an electron-positron pair, he used photon pairs with entangled polarizations.
[...] And Aspect showed that there was a correlation between the choice of polarization mea-
surement axis for one photon and the final polarization direction and its entangled partner.
The Bell inequalities were . The experiment was even set up so that the
influence had to travel between the photons at the
speed of light.”
(k) “It’s now been thoroughly confirmed that the Bell inequalities are violated, suggesting that the
wave function cannot have .”
6.3 Monogamy of Entanglement 245
(l) “The results of these entanglement experiments do seem to violate local realism. But that may
mean a violation of , or just of .”
(m) “Non-locality requires that entangled particles affect each other instantaneously. That sounds
blasphemous to anyone who accepts Einstein’s theory of relativity. However, non-locality and
relativity can actually be perfectly . Relativity requires that causality is
preserved, so no faster than light flow. But none of these entanglement
experiments allow any real information to be transmitted between particles. It’s only possible
to see the influence between the entangled partners after measurements have been made and
those measurements are compared.”
(n) “The interpretation remains consistent with all quantum observations.”
Classical correlations can be shared among multiple parties. For example, say there
are three people, Alice, Bob, and Charlie, and they each have a bit. Say Alice and
Bob’s bits are perfectly correlated: if Alice’s bit is 0, then Bob’s bit is also 0, and if
Alice’s bit is 1, then Bob’s bit is also 1:
Alice Bob
0 0
1 1
Classically, it is possible for Charlie’s bit to be perfectly correlated with Alice’s bit
as well:
Alice Bob Charlie
0 0 0
1 1 1
This satisfies two properties. First, if we remove Charlie, we retain perfect corre-
lation between Alice and Bob. Second, if we remove Bob, Alice and Charlie have
the same correlation as Alice did with Bob. Put another way, if we swap Bob and
Charlie, we get the same distribution. Together, Alice is perfectly correlated with
both Bob and Charlie.
Is this possible for quantum entanglement? That is, if Alice, Bob, and Charlie have
qubits, is it possible for them to satisfy the two properties above? Note qubits that are
perfectly entangled are called maximally entangled, and it is possible for qubits to be
partially entangled. Determining the amount of entanglement between qubits is an
active area of research involving entanglement measures. One maximally entangled
state is |Φ + i, so say Alice and Bob’s qubit are in this state:
246 6 Entanglement and Quantum Protocols
1
√ (|0i ⊗ |0i + |1i ⊗ |1i) ⊗ |ψc i.
2
But now the second property is not satisfied—swapping Bob and Charlie yields a
different state. Alice is not entangled with them to the same degree. In fact, Alice is
maximally entangled with Bob, but she is not entangled at all with Charlie!
From this result, We say that entanglement is monogamous, like a monogamous
relationship between people is exclusive. Two people perfectly “entangled” with
each other (Alice and Bob) are not entangled at all with a third party (Charlie).
Finally, note if Alice and Bob are partially entangled, then it is possible for there
to be some entanglement with Charlie. A proper treatment of this involves express-
ing quantum states, not as kets or vectors, but as density matrices, which also allow
probabilistic mixtures of kets, but that is beyond the scope of this textbook.
Exercise 6.10. The Greenberger–Horne–Zeilinger state (GHZ state) is an entangled state of three-
qubits. It is:
1
√ (|000i + |111i) .
2
(a) Verify that the GHZ state is produced by the following quantum circuit:
|0i
|0i •
|0i H •
(b) If we measure the left qubit, what are the possible resulting states and with what probabilities?
(c) Are the resulting states after the measurement entangled?
1
|W i = √ (|001i + |010i + |100i) .
3
(a) Verify that the W state is produced by the following quantum circuit:
|0i X
|0i H
|0i U
where q
2
3 − √13
U = q .
√1 2
3 3
6.4 Superdense Coding 247
Also, as a reminder, the hollow circle is anti-control. So the second gate applies the Hadamard
gate to the middle qubit if the bottom qubit is 0.
(b) If we measure the left qubit, what are the possible resulting states and with what probabilities?
(c) Are the resulting states after the measurement entangled?
Alice wants to send classical information to Bob, say one of four possible restaurant
options: American, Chinese, Italian, or Mexican. She can either send her preference
using classical bits or qubits, and we will see that if Alice and Bob share entangle-
ment, Alice only needs to send half the number of qubits as she would bits.
Classically, Alice would have to send two bits to Bob, since two bits have four
possible states 00, 01, 10, or 11. One bit would not suffice, since it only has two
states 0 or 1.
Quantumly, Alice can send just one qubit, but it needs to be entangled with a second
qubit that Bob already has. Say Alice and Bob share a pair of entangled qubits in
the |Φ + i state:
1
√ (|00i + |11i)
2
Alice Bob
Depending on which of the four options Alice wants to communicate to Bob, she
can apply quantum gates to her qubit, then send her one qubit to Bob so that Bob
ends up with both qubits:
• If Alice wants to send 00, she does nothing to her qubit, and sends it to Bob so
that he has both qubits.
• If Alice wants to send 01, she applies the X gate to her qubit, which transforms
|Φ + i to
Ψ = √1 (|10i + |01i) .
+
2
Then she sends her qubit to Bob, so that he has both qubits.
248 6 Entanglement and Quantum Protocols
• If Alice wants to send 10, she applies the Z gate to her qubit, which transforms
|Φ + i to
Φ = √1 (|00i − |11i) .
−
2
Then she sends her qubit to Bob, so that he has both qubits.
• Finally, if Alice wants to send 11, she applies both X and Z to her qubit. Apply-
ing X transforms |Φ + i to |Ψ + i, and appling Z transforms |Ψ + i to
Ψ = √1 (|01i − |10i) .
−
2
Then Alice sends her qubit to Bob, so that he has both qubits.
Now Bob has both qubits, and they are in one of four states:
Φ = √1 (|00i + |11i) ,
+
2
+ 1
Ψ = √ (|01i + |10i) ,
2
− 1
Φ = √ (|00i − |11i) ,
2
− 1
Ψ = √ (|01i − |10i) .
2
Since these four states are orthonormal, they form a measurement basis. Bob can
measure the two qubits in this Bell basis to distinguish them, thus determining what
Alice wanted to send. This is called a Bell measurement.
Another way to understand the Bell measurement is to apply CNOT and then
H ⊗ I, then measuring in the Z-basis. That is,
+ CNOT 1 H⊗I
Φ −−−→ √ (|00i + |10i) = |+i|0i −−→= |00i,
2
+ CNOT 1 H⊗I
Ψ −−−→ √ (|01i + |11i) = |+i|1i −−→= |01i,
2
− CNOT 1 H⊗I
Φ −−−→ √ (|00i − |10i) = |−i|0i −−→= |10i,
2
− CNOT 1 H⊗I
Ψ −−−→ √ (|01i − |11i) = |−i|1i −−→= |11i.
2
Computationally, this protocol still requires two qubits, as it must because
Holevo’s theorem says that n qubits can only store n bits of classical information.
Yet as a communication protocol, it only requires one qubit to be sent.
Generalizing this, if Alice and Bob share n pairs of entangled qubits (so there are
2n qubits total), then Alice can measure each of her n qubits or not depending on
what she wants to send, then send them to Bob.
6.5 Quantum Teleportation 249
Exercise 6.12. Verify that |Φ + i, |Ψ + i, |Φ − i, and |Ψ − i are orthonormal to each other. That is,
calculate hΦ + |Φ − i, etc.
Exercise 6.13. Say Alice wants to send one of sixteen possible states to Bob.
(a) How many classical bits would Alice need to send to Bob?
(b) How many qubits would Alice need to send to Bob if they share entanglement?
(c) How many qubits total would it take, counting both Alice’s and Bob’s qubits?
Exercise 6.14. Consider the following quantum circuit that implements superdense coding.
|0i
|0i H • X Z • H
The bottom qubit is Alice’s, and the top qubit is Bob’s. In the first dotted square, Alice and Bob
create the maximally entangled state |Φ + i by applying H ⊗ I and CNOT . Bob then goes away on a
trip. Alice wants to send Bob a message, so in the second dotted square, she applies X followed by
Z to her qubit. Then, she sends her qubit to Bob. Bob now has both qubits. To read Alice’s message,
in the third square, he applies CNOT followed by H ⊗ I, and then measures in the Z-basis.
(a) What result should Bob get for his measurement?
(b) Run the circuit on an actual quantum processor using IBM Quantum Experience. Which pro-
cessor did you use? What histogram of results do you get?
(c) How would you modify the circuit so that Alice sends |01i to Bob?
In the previous section on superdense coding, we used qubits can be used to send
classical information. In this section, we consider the opposite. We can only send
bits, but we want to send quantum information. In particular, Alice would like to
send a qubit in some unknown state |ψi = α|0i + β |1i to Bob.
Alice cannot measure the qubit, or she will collapse it and lose the superposition it
may be in.
Even if she did know the state, the amplitudes are in general complex numbers
that can take rational or irrational values, and the irrational values would take an
infinite number of bits to express.
250 6 Entanglement and Quantum Protocols
If Alice and Bob already share entanglement, they can use it to teleport the quantum
state. Say they share a pair of entangled qubits in the state |Φ + i. Alice has one of
these qubits, plus the one she wants to send |ψi. Bob has the other entangled qubit.
|ψi = α|0i + β|1i 1
√ (|00i + |11i)
2
Alice Bob
Altogether, the three qubits are in the state
1 1
= α|0i √ (|00i + |11i) + β |1i √ (|00i + |11i)
2 2
1
= √ [α (|000i + |011i) + β (|100i + |111i)] .
2
The left two qubits belong to Alice, and the right qubit belongs to Bob. First, Alice
applies a CNOT gate to her two qubits, resulting in the state
1
√ [α (|000i + |011i) + β (|110i + |101i)] .
2
Next, she applies a Hadamard gate to her left qubit, yielding
1
√ [α (|+00i + |+11i) + β (|−10i + |−01i)]
2
1
= [α (|0i + |1i) (|00i + |11i) + β (|0i − |1i) (|10i + |01i)]
2
1h
= |00i (α|0i + β |1i) + |01i (β |0i + α|1i)
2 i
+ |10i (α|0i − β |1i) + |11i (−β |0i + α|1i) .
Then, Alice measures her two qubits. She gets 00, 01, 10, 11, each with probability
1/4. So after the measurement, the possible states are
Now Alice tells Bob the results of her measurement, which are two classical bits.
This is a classical communication. Bob uses this information to possibly apply quan-
tum gates to his qubit.
6.5 Quantum Teleportation 251
• If Alice’s measurement was 00, Bob does nothing because his qubit is now in
the state |ψi that Alice wanted to send him.
• If Alice’s measurement was 01, then Bob applies an X gate to his qubit, trans-
forming it into |ψi.
• If Alice’s measurement was 10, then Bob applies a Z gate to his qubit, trans-
forming it into |ψi.
• Finally, if Alice’s qubit was 11, then Bob applies an X gate followed by a Z
gate, transforming it into |ψi.
So Bob’s qubit is now in the state |ψi, achieving the goal of transferring the state of
Alice’s qubit to Bob’s qubit. This is called quantum teleportation. Note that Alice’s
qubit was not physically transferred to Bob, only information about what state it
was in. In the process, Alice had to measure her qubit, destroying the quantum in-
formation. This is necessary because of the no-cloning theorem. Furthermore, even
though the state of Bob’s qubit changed instantly when Alice measured her qubits
and collapsed the state, this information was not useful until Alice told Bob the re-
sult of her measurement, which is a classical communication bounded by the speed
of light. So, quantum teleportation cannot be performed faster than light, which is
consistent with the no-signaling principle.
Quantum teleportation is implemented by the following circuit:
X Z |ψi
|Φ+ i
•
|ψi • H •
The top qubit is Bob’s, and the bottom two qubits are Alice’s. The bottom qubit
starts in the unknown state |ψi, which Alice wants to teleport to Bob, and the top
two qubits start in the entangled state |Φ + i. Alice applies a CNOT to her qubits
followed by H on her bottom qubit. Then she measures her qubits, getting 00, 01, 10,
or 11. If her right qubit was 1, Bob applies X, and if her left qubit was 1, Bob applied
Z. These are Controlled-X and Controlled-Z gates. This completes the teleportation,
and Bob’s qubit ends up in the state |ψi.
Using the principle of deferred measurement (introduced on page 187), we can
move the measurements after the controls, and replace the controls with quantum
ones:
X Z |ψi
+
|Φ i
•
|ψi • H •
Exercise 6.15. Alice wants to teleport a qubit in an unknown state |ψi = α|0i + β |1i to Bob.
Instead of sharing two entangled qubits in the |Φ + i state, they share two entangled qubits in the
|Ψ + i state:
|ψi = α|0i + β|1i 1
√ (|01i + |10i)
2
Alice Bob
Altogether, the initial state of the system is
1
|ψiΨ + = (α|0i + β |1i) √ (|01i + |10i)
2
1
= √ (α|001i + α|010i + β |101i + β |110i) .
2
So, the left two qubits are Alice’s, and the right qubit is Bob’s.
(a) Show that if Alice applies CNOT to her two qubits, followed by H to her left qubit, the state
of the system becomes
1
|00i (β |0i + α|1i) + |01i (α|0i + β |1i)
2
+ |10i (−β |0i + α|1i) + |11i (α|0i − β |1i) .
(b) Next, Alice measures both of her qubits. What values can she get, with what probabilities,
and what does the state collapse to in each case?
(c) Finally, Alice tells Bob the results of her measurement. For each possible result, what should
Bob do to his qubit so that is α|0i + β |1i, the state that Alice wanted to teleport to him?
Exercise 6.16. Alice wants to teleport a qubit in an unknown state |ψi = α|0i + β |1i to Bob.
Instead of sharing two entangled qubits in a Bell state, they share three entangled qubits in the
GHZ state:
1
|GHZi = √ (|000i + |111i) .
2
The left two qubits are with Alice, and the right qubit is with Bob.
|ψi = α|0i + β|1i 1
√ (|000i + |111i)
Alice 2
Bob
1
|ψi|GHZi = (α|0i + β |1i) √ (|000i + |111i)
2
1
= √ (α|0000i + α|0111i + β |1000i + β |1111i) .
2
6.5 Quantum Teleportation 253
So, the left three qubits are Alice’s, and the right qubit is Bob’s.
(a) Show that if Alice applies CNOT21 (recall the qubits are numbered right-to-left starting with
zero), followed by CNOT32 , followed by H ⊗ I ⊗ I ⊗ I, the state of the system becomes
1
|000i (α|0i + β |1i) + |010i (β |0i + α|1i)
2
+ |100i (α|0i − β |1i) + |110i (−β |0i + α|1i) .
(b) Next, Alice measures all three of her qubits. What values can she get, with what probabilities,
and what does the state collapse to in each case?
(c) Finally, Alice tells Bob the results of her measurement. For each possible result, what should
Bob do to his qubit so that is α|0i + β |1i, the state that Alice wanted to teleport to him?
Exercise 6.17. Alice wants to teleport a qubit in an unknown state |ψi = α|0i + β |1i to Charlie,
and Bob is helping her. They share three entangled qubits in the GHZ state:
1
|GHZi = √ (|000i + |111i) .
2
The left qubit is Alice’s, the middle qubit is Bob’s, and the right qubit is Charlie’s.
Charlie
Altogether, the initial state of the system is
1
|ψi|GHZi = (α|0i + β |1i) √ (|000i + |111i)
2
1
= √ (α|0000i + α|0111i + β |1000i + β |1111i) .
2
So, the left two qubits are Alice’s, the second-to-right qubit is Bob’s, and the right qubit is Charlie’s.
(a) Show that if Alice applies CNOT to her qubits (so the far left qubit is the control and the
second-to-left qubit is the target) and then the Hadamard gate to her left qubit, the state of the
system becomes
1
|00i (α|00i + β |11i) + |01i (β |00i + α|11i)
2
+ |10i (α|00i − β |11i) + |11i (−β |00i + α|11i) .
(b) Next, Alice measures her two qubits and makes the results known. What values can she get,
with what probabilities, and what does the state collapse to in each case?
(c) After Alice has completed the above, Bob applies the Hadamard gate to his qubit. Show that
the state of the system after Bob does this is the following four states, depending on the result
of Alice’s measurement:
254 6 Entanglement and Quantum Protocols
1
√ |00i |0i (α|0i + β |1i) + |1i (α|0i − β |1i) ,
2
1
√ |01i |0i (β |0i + α|1i) + |1i (β |0i − α|1i) ,
2
1
√ |10i |0i (α|0i + β |1i) + |1i (α|0i − β |1i) ,
2
1
√ |11i |0i (−β |0i + α|1i) + |1i (−β |0i − α|1i) .
2
(d) Then, Bob measures his qubit and makes his result known. For each of the above states, what
does the state collapse to? For each possible outcome, what quantum gate(s) should Charlie
apply to his qubit so that it is α|0i + β |1i, the state that Alice wanted to teleport to him?
6.6.1 Encryption
Alice and Bob would like to send private messages to each other over the internet.
This means others can see the bits they send, but the meaning should be hidden from
everyone except Alice and Bob.
To do this, Alice and Bob need to have a secret key or code that only they know.
Using this secret key, they can encrypt their messages to each other.
For example, say Alice and Bob share a secret key of fourteen random bits that
only they know:
key = 1101011 0011011.
Alice wants to send “Hi” to Bob, which in ASCII is 1001000 1101001. This is called
the plaintext. If she sends these bits, everyone will know that the message is “Hi.”
So instead, she takes the XOR of each bit of the message with each bit of the secret
key, yielding the ciphertext
Now Alice sends this ciphertext to Bob over the internet. If someone intercepts it
along the way, like Eve the eavesdropper, then she will not be able to determine the
original plaintext since she does not have the secret key. Now Bob has the ciphertext,
and he takes the XOR of it with the secret key, which he knows:
What is the plaintext binary string, and what does it encode in ASCII?
A classical way send a secret message, such as to establish a secret key, is the RSA
cryptosystem, which is an acronym for its inventors Rivest, Shamir, and Adleman.1
It is an example of a public-key cryptosystem, where each user reveals some public
information while still maintaining some private (secret) information.
Say Alice wants to send a message to Bob. To do this securely, Bob prepares
some public and private information. The public information allows anyone to send
him encrypted messages. The private information allows Bob, and only Bob, to
decrypt the message. To do this, Bob begins by choosing two distinct prime numbers
p and q, with some conditions that are beyond the scope of this textbook. A prime
number is any whole number greater than 1 whose only factors are 1 and itself.
Prime numbers can be listed using Mathematica or SageMath:
• In Mathematica, the first ten prime numbers can be listed using
1Actually, RSA was invented a few earliers by Clifford Cocks, a mathematician working for a
British intelligence agency, but his work was classified and not revealed until decades later.
256 6 Entanglement and Quantum Protocols
The output is
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29}.
• In SageMath, the prime numbers less than 30 can be listed using
sage: list(primes(30))
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
For example, say Bob chooses p = 17 and q = 41. Bob keeps these two numbers a
secret, but he does reveal their product. That is, bob computes n = pq = 17·41 = 697
and makes the number 697 known to Alice and the world. This product n is one of
Bob’s two public keys, and he publishes it so that Alice can use it to send him a
secret message. The length of n in bits is called the key length, and at the time of
this writing, the recommended key length of RSA is 2048-bits. For example, 697 in
binary is 1010111001, so we are using a key length of 10, which is much too short
in practice.
Bob then computes the product φ = (p−1)(q−1). Continuing our example, Bob
computes φ = 16 · 40 = 640. Using this, he then finds some integer e between 1 and
φ that is relatively prime or coprime to φ , meaning the greatest common divisor of
e and φ is 1, i.e., gcd(e, φ ) = 1, or e and φ share no common factors except 1. We
can find relatively prime numbers using Mathematica or SageMath:
• In Mathematica, we can list all the numbers between 2 and 639, inclusive, that
are relatively prime to 640 using the following:
Table[If[GCD[i, 640] == 1, i, Nothing], {i, 2, 639}]
6.6 Quantum Key Distribution 257
{3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39, 41, 43, 47, 49, 51, 53, 57, 59,
61, 63, 67, 69, 71, 73, 77, 79, 81, 83, 87, 89, 91, 93, 97, 99, 101, 103, 107, 109,
111, 113, 117, 119, 121, 123, 127, 129, 131, 133, 137, 139, 141, 143, 147, 149,
151, 153, 157, 159, 161, 163, 167, 169, 171, 173, 177, 179, 181, 183, 187, 189,
191, 193, 197, 199, 201, 203, 207, 209, 211, 213, 217, 219, 221, 223, 227, 229,
231, 233, 237, 239, 241, 243, 247, 249, 251, 253, 257, 259, 261, 263, 267, 269,
271, 273, 277, 279, 281, 283, 287, 289, 291, 293, 297, 299, 301, 303, 307, 309,
311, 313, 317, 319, 321, 323, 327, 329, 331, 333, 337, 339, 341, 343, 347, 349,
351, 353, 357, 359, 361, 363, 367, 369, 371, 373, 377, 379, 381, 383, 387, 389,
391, 393, 397, 399, 401, 403, 407, 409, 411, 413, 417, 419, 421, 423, 427, 429,
431, 433, 437, 439, 441, 443, 447, 449, 451, 453, 457, 459, 461, 463, 467, 469,
471, 473, 477, 479, 481, 483, 487, 489, 491, 493, 497, 499, 501, 503, 507, 509,
511, 513, 517, 519, 521, 523, 527, 529, 531, 533, 537, 539, 541, 543, 547, 549,
551, 553, 557, 559, 561, 563, 567, 569, 571, 573, 577, 579, 581, 583, 587, 589,
591, 593, 597, 599, 601, 603, 607, 609, 611, 613, 617, 619, 621, 623, 627, 629,
631, 633, 637, 639}
• In SageMath, we can list all the numbers less than 640 that are relatively prime
to 640 using the coprime integers function:
sage: phi = 640
sage: phi.coprime_integers(phi)
[1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39,
41, 43, 47, 49, 51, 53, 57, 59, 61, 63, 67, 69, 71, 73, 77,
79, 81, 83, 87, 89, 91, 93, 97, 99, 101, 103, 107, 109, 111,
113, 117, 119, 121, 123, 127, 129, 131, 133, 137, 139, 141,
143, 147, 149, 151, 153, 157, 159, 161, 163, 167, 169, 171,
173, 177, 179, 181, 183, 187, 189, 191, 193, 197, 199, 201,
203, 207, 209, 211, 213, 217, 219, 221, 223, 227, 229, 231,
233, 237, 239, 241, 243, 247, 249, 251, 253, 257, 259, 261,
263, 267, 269, 271, 273, 277, 279, 281, 283, 287, 289, 291,
293, 297, 299, 301, 303, 307, 309, 311, 313, 317, 319, 321,
323, 327, 329, 331, 333, 337, 339, 341, 343, 347, 349, 351,
353, 357, 359, 361, 363, 367, 369, 371, 373, 377, 379, 381,
383, 387, 389, 391, 393, 397, 399, 401, 403, 407, 409, 411,
413, 417, 419, 421, 423, 427, 429, 431, 433, 437, 439, 441,
443, 447, 449, 451, 453, 457, 459, 461, 463, 467, 469, 471,
473, 477, 479, 481, 483, 487, 489, 491, 493, 497, 499, 501,
503, 507, 509, 511, 513, 517, 519, 521, 523, 527, 529, 531,
533, 537, 539, 541, 543, 547, 549, 551, 553, 557, 559, 561,
563, 567, 569, 571, 573, 577, 579, 581, 583, 587, 589, 591,
593, 597, 599, 601, 603, 607, 609, 611, 613, 617, 619, 621,
623, 627, 629, 631, 633, 637, 639]
Bob can choose any of these numbers. Say he chooses e = 3. This number e is
Bob’s second public key, and he publishes it. It is called e because later, Alice will
use it as an exponent. Note there is a fast method called Euclid’s algorithm from
300 BC/BCE for calculating the greatest common divisor of two integers, and the
number of steps is at most five times the number of digits of the smaller integer. See
Exercise 6.19 for a description of the algorithm.
Finally, Bob computes d = e−1 mod φ , where mod refers to the modulus or re-
mainder when dividing by φ .2 For example, the 12-hour clock is modulo 12, since
7 + 8 = 15 = 3 mod 12. Continuing our example, we can compute d using Mathe-
matica or SageMath:
• In Mathematica, the first ten prime numbers can be listed using
ModularInverse[3,640]
So, Bob computed d = 3−1 mod 640 = 427. Since d is the inverse of e modulo φ , if
we multiply e and d modulo φ , we should get 1, i.e., ed = 1 mod φ . The number d is
Bob’s private key. It will allow him to decrypt messages sent to him, so he keeps it
a secret. No one else knows d because even though Bob published e, one also needs
to know φ in order to calculate d. Bob may now throw away p and q, but keeping
it does allow him to decrypt a little faster using the Chinese remainder theorem, but
that is beyond the scope of this book.
Alice wants to send Bob a plaintext message that is encoded as a number M, such
that 0 < M < n. She finds Bob’s public keys n and e and computes the ciphertext
C = M e mod n, and then she sends C to Bob. For example, Alice wants to send
M = 104 to Bob, which in binary is 1101000. She can compute the ciphertext using
Mathematica or SageMath:
• In Mathematica,
PowerMod[104,3,697]
2From number theory, the inverse of e mod φ exists precisely because e was chosen to be relatively
prime to φ .
6.6 Quantum Key Distribution 259
Thus, C = 1043 mod 697 = 603, and alice sends the number 603 to Bob.
When Bob receives the ciphertext C, he computes Cd mod n = (M e )d mod n =
M mod n = M 1 mod n = M, receiving the message.3 Continuing our example,
ed
Bob computes 603427 mod 697 = 104, decrypting the message. In Mathematica, the
code and output would be
• In Mathematica,
PowerMod[603,427,697]
Alice Bob
Chooses primes p = 17, q = 41.
Calculates n = pq = 697.
Publishes n.
Computes φ = 16 · 40 = 640.
Chooses e = 3 since gcd(3, 640) = 1.
Publishes e.
Computes d = 3−1 mod 640 = 427.
Chooses message M = 104.
Computes C = 1043 mod 697 = 603.
Alice Sends to C to Bob.
Computes 603427 mod 697 = 104 = M.
Decryption is hard for an eavesdropper, Eve, to do, because she does not know
the secret key d. She only knows Bob’s public keys, n and e. To find the secret key
d = e−1 mod φ , she needs to know φ = (p − 1)(q − 1), but this requires knowing
p and q, which involves factoring n. There is no known efficient, classical algo-
rithm for factoring large numbers, although a quantum computer can efficiently fac-
tor numbers using Shor’s algorithm. As described in Section 1.7.2 on complexity
classes, this is evidence, but not proof, that BQP is larger than P.
Exercise 6.19. Euclid’s algorithm is a method for finding the greatest common divisor of two
integers by converting the problem into finding the greatest common divisor of successively smaller
and smaller pairs of integers until we get one that is easy to find.
For example, say we want to find gcd(1122, 422). We begin by dividing the larger number by
the smaller number. That is, 1122/442 is 2 with a remainder of 238, which we can write as
3 The plaintext and ciphertext are modulo n, but the exponents are modulo φ . It can be proved
using Fermat’s Little Theorem that with these moduli, M ed mod n = M mod n, but the proof is
beyond the scope of this textbook.
260 6 Entanglement and Quantum Protocols
The claim is that the greatest common divisor of the dividend 1122 and divisor 442 is equal to
the greatest common divisor of the divisor 442 and the remainder 238, i.e., gcd(1122, 442) =
gcd(442, 238). This is because they have the same common divisors, including their greatest com-
mon divisor.4
Using this fact, let us instead find gcd(442, 238). Dividing these numbers, 442/238 is 1 with a
remainder of 204, so we can write:
Using the same fact from before, the greatest common divisor of the dividend and divisor is equal
to the greatest common divisor of the divisor and remainder, i.e., gcd(442, 238) = gcd(238, 204).
So, we instead find gcd(238, 204). Again, we divide these numbers. Since 238/204 is 1 with a
remainder of 34, we write,
238 = 1 · 204 + 34.
Again using the fact, gcd(238, 204) = gcd(204, 34).
Now, we instead find gcd(204, 34). Dividing, 204/34 is 6 with no remainder, so
204 = 6 · 34 + 0.
Exercise 6.20. You are Bob, and you and Alice are communicating using RSA cryptography. You
picked two prime numbers p = 59 and q = 127. Find
(a) Your public key n.
(b) A valid public key e.
(c) Your private key given your choice in part (b).
Exercise 6.21. You are Alice, and you and Bob are communicating using RSA cryptography. Bob
has secret keys n = 2 035 153 and e = 5. You want to send him a message M = 1 234 567 using
RSA. What ciphertext C do you send him?
Exercise 6.22. You are Bob, and you and Alice are communicating using RSA cryptography. Alice
sends you some ciphertext C = 1 873 198. Your public keys are n = 2 035 153 and e = 5, and your
secret key is d = 1 219 277. What is the plaintext message (number) that Alice sent?
Exercise 6.23. In 1991, the RSA Factoring Challenge was created to encourage research into fac-
toring by giving values of n of increasing sizes and offering prize money for people who factored
them. Visit
https://en.wikipedia.org/wiki/RSA_Factoring_Challenge
4 As a proof, let us show that any divisor of 1122 and 422 is also a divisor of their remainder
238, and any divisor of 422 and 238 is also a divisor of 1122. First, say d is a common divisor of
1122 and 422. Then, d is is also a divisor of 1122 − 2 · 442, since division is distributive. Since
238 = 1122 − 2 · 442, d is also a divisor of 238. Going the other direction, say e is a common
divisor of 422 and 238. Then, e is also a divisor of 2 · 2 · 442 + 238, since division is distributive.
Since 2 · 442 + 238 = 1122, e is also a divisor of 1122. So, gcd(1122, 442) = gcd(442, 238).
6.6 Quantum Key Distribution 261
A quantum method for establishing a shared secret key was introduced by Bennett
and Brassard in 1984, and it is called the BB84 protocol. It does not use entangle-
ment, but it is a quantum protocol, so it’s included in this chapter. There do exist
quantum key distribution protocols that utilize entanglement, such as E91, which
is named after Ekert and discovered in 1991, but they are beyond the scope of this
textbook.
Alice begins with a bunch of random bits, and for each bit, she randomly chooses
either the Z-basis {|0i, |1i} or the X-basis {|+i, |−i}. For example,
Alice’s Bits 0 1 0 1 1 0 1 1 1
Alice’s Bases Z Z X Z X X X Z Z
If the bit she wants to send is a 0, and she picked the Z-basis, then she sends Bob
|0i, and if she picked the X-basis, then she sends Bob |+i. If Alice instead wants to
send the bit 1, and she picked the Z-basis, then she sends Bob |1i, and if she picked
the X-basis, then she sends Bob |−i. Continuing the example,
Alice’s Bits 0 1 0 1 1 0 1 1 1
Alice’s Bases Z Z X Z X X X Z Z
Alice Sends |0i |1i |+i |1i |−i |+i |−i |1i |1i
Bob receives the qubits, and he randomly measures each one in either the Z-basis
or X-basis. If the basis he picked was the same as Alice’s, then he will get the
same result as Alice. If he picked the opposite basis, however, then he will get
each possible result with probability 1/2. For example, if Alice sent Bob |0i and
he measures in the Z-basis, he is certain to get |0i. But if he measures in the X-
basis, he gets |+i with probability 1/2 or |−i with probability 1/2. He interprets
|0i and |+i as 0, and |1i and |−i as 1. Continuing the example,
Alice’s Bits 0 1 0 1 1 0 1 1 1
Alice’s Bases Z Z X Z X X X Z Z
Alice Sends |0i |1i |+i |1i |−i |+i |−i |1i |1i
Bob’s Bases Z X X Z Z X Z X Z
Bob’s Measurement |0i |−i |+i |1i |0i |+i |1i |+i |1i
Bob’s Bits 0 1 0 0 0 0 1 0 1
262 6 Entanglement and Quantum Protocols
Now Alice and Bob call each other and openly (publicly) share what basis they
used for each measurement. If they used the same basis, then they know that their
measurement outcomes should agree, and they have a shared secret bit. If they used
different basis, then their measurement outcomes might agree or disagree, and they
discard these bits. Continuing the example.
Alice’s Bits 0 1 0 1 1 0 1 1 1
Alice’s Bases Z Z X Z X X X Z Z
Alice Sends |0i |1i |+i |1i |−i |+i |−i |1i |1i
Bob’s Bases Z X X Z Z X Z X Z
Bob’s Measurement |0i |−i |+i |1i |0i |+i |1i |+i |1i
Bob’s Bits 0 1 0 0 0 0 1 0 1
Public Discussion of Basis
Shared Secret Key 0 0 1 0 1
To ensure Eve did not measure the qubits along the way, Alice and Bob can
reveal a fraction of their shared secret key and make sure they agree. For example,
if Alice and Bob want 256 bits in their shared secret key, they can generate 306 bits
using BB84, reveal 50 of them to ensure there was no eavesdropper, and then use
the remaining 256 for their shared secret key.
If Alice and Bob reveal 50 bits of their shared secret key, what is the probability
they will catch Eve, if Eve is measuring every qubit along the way? To answer this,
let us start with revealing one bit. Say Alice and Bob are revealing a bit where
they both used the Z-basis, and say Alice sent qubit in the state |0i. Then, Alice will
reveal that her bit is 0, while Bob could reveal that his bit is 0 or 1, and we determine
the probabilities of these outcomes using the following diagram:
Eve Bob 1
1/2 Z, |0i Z, |0i 2
3
Alice 4 undetected
Z, |0i Eve
Bob 1
1/2
1/2
Eve Z, |0i 4
X, |±i Bob 1 1
1/2 detected
Z, |1i 4 4
On the left side of the diagram, Alice is using the Z-basis, and she sends a qubit in
the |0i state. In the middle of the diagram, Eve intercepted the qubit and measured
it in either the Z-basis or the X-basis, each with probability 1/2. If Eve measured
in the Z-basis, she got |0i, and then forwarded the qubit to Bob. Bob measured the
qubit in the Z-basis, so he also got |0i. This is the top row of the above diagram. In
this scenario, which occurs with probability 1/2, Alice and Bob both reveal that they
got the bit 0, and Eve’s eavesdropping was undetected. Now, if Eve measured in the
X-basis instead, then she collapsed the state to |+i or |−i and forwarded it to Bob. In
the above figure, this is the bottom row. Bob then measured the qubit in the Z-basis,
6.7 Summary 263
getting |0i with probability 1/2 or |1i with probability 1/2. Overall, each of these
outcomes occur with probability 1/4. If Bob got |0i, Eve is undetected, but if he
got |1i, Alice and Bob will realize there was an eavesdropper when they reveal their
results. Overall, Eve has a probability of 3/4 of being undetected and a probability
of 1/4 of being detected, as indicated by the curly brace in the above figure, when
Alice and Bob reveal this bit of their shared secret key. In other words, there is a
probability of 3/4 that Alice and Bob both have 0 as their bits, and probability 1/4
that Alice has 0 and Bob has 1.
If Alice and Bob share n bits of their shared secret key, the probability that Eve is
undetected for all n bits is (3/4)n . Then, the probability that Eve is detected is one
minus this, or
n
3
Probability Alice and Bob detect Eve = 1 − .
4
Thus, if Alice and Bob share 50 bits of their shared secret key, the probability that
they detect Eve is 1 − (3/4)50 = 0.99999943, which is very close to certainty.
In order to use BB84 in practice, we need the ability to send qubits to each other
through a network. This is called a quantum network, and building a quantum net-
work is an area of active research.
Exercise 6.24. You are Alice, and you and Bob are establishing a secret key using BB84. You have
the following random bits and random bases. What qubits do you send to Bob?
Alice’s Bits 1 0 0 1 0 0 0 1 1
Alice’s Bases X X Z Z Z X X X X
Alice Sends ? ? ? ? ? ? ? ? ?
Exercise 6.25. You are Bob, and you and Alice are establishing a secret key using BB84. You
choose the following random bases to measure each qubit in, and you got the following results.
Bob’s Bases X X Z X Z Z X X Z
Bob’s Measurement |+i |−i |0i |−i |0i |1i |1i |+i |1i
Bob’s Bits 0 1 0 1 0 1 1 0 1
Next, you call Alice and learn that she used the following bases:
Alice’s Bases Z Z Z Z Z X X Z Z
Exercise 6.26. Alice and Bob want to catch a possible eavesdropper with a probability of 99%.
How many bits of their shared secret key should they reveal?
6.7 Summary
Entanglement is a feature of quantum states that does not exist with classical bits.
Entangled qubits can influence each other faster than the speed of light, but this
264 6 Entanglement and Quantum Protocols
The most precise way to quantify the complexity of a quantum circuit is to count the
least number of quantum gates required to implement it, relative to some universal
set of quantum gates. This is called its circuit complexity. For example, if we permit
any one- and two-qubit quantum gates, then recall from Problem 4.23 that the Toffoli
gate can be decomposed into
H T† T T† T H
• = • • T† T† S
• • • • • T
This has sixteen one- and two-qubit gates, but it is not the circuit complexity of the
Toffoli gate. In the top row, the last T and H gates can be combined into a single
one-qubit gate, reducing the number of one- and two-qubit gates to fifteen. Yet this
265
266 7 Quantum Algorithms
is still not the circuit complexity. A circuit that uses even fewer one- and two-qubit
gates is
H P P† P H
• = • •
• • • •
where P is some one-qubit gate. This only uses seven one- and two-qubit gates. It
is an active area of research to determine whether Toffoli can be simplified further.
If only CNOT and one-qubit gates are permitted, however, it has been proved that
Toffoli requires at least six CNOT gates (plus one-qubit gates). Suffice it to say that
circuit complexity is generally hard to find.
In terms of circuit complexity, an efficient quantum algorithm is one with a poly-
nomial circuit complexity. For example, the quantum adder is efficient since its
4n − 2 Toffoli gates can each be decomposed into seven one- and two-qubit gates.
Adding the 4n − 2 CNOT gates, this results in 7(4n − 2) + (4n − 2) = 32n − 16 one
and two-qubit gates, which is polynomial (linear) in n.
It is also difficult to find the circuit complexity of classical circuits. It can be
hard to determine if a logic circuit has been fully simplified, and it depends on what
gates are allowed. For example, should the final circuit only consist of AND, OR,
and NOT, or can it also include XOR? Should it only consist of NAND gates? Are
three-bit logic gates allowed, or only one- and two-bit gates?
Since circuit complexity can be hard to find, we often turn to query complexity
instead. The query complexity of a problem is the number of calls to a function, or
queries to an oracle or black box needed to solve the problem. We give an input to
the function or oracle or black box, and it returns an output, without us knowing
its inner workings. Hence, it is opaque or black. It is significantly easier to find the
query complexity of a problem, or when that is not possible, mathematically prove
upper or lower bounds on it.
For example, a problem we will explore later this chapter is brute-force search-
ing. Say we are searching a database of 100 items for one particular item, and we
have an oracle that tells us whether an item is the correct one or not. We can query,
“Oracle, is item number 1 correct?” If the oracle replies, “Yes,” we have found our
item. If the oracle replies “No,” we can inquire about another item: “Oracle, is item
number 2 correct?” We can continue in this manner until the oracle says “Yes.”
Mathematically, the oracle is just a function f (x) that outputs 0 (no) or 1 (yes). So,
evaluating f (1) is inquiring whether item number 1 is correct. If f (1) = 1, we have
found the correct item, but if f (1) = 0, we can inquire about another item, like f (2),
and so on. The query complexity is the number of times we need to evaluate f (x)
7.1 Circuit vs Query Complexity 267
in order to find the item. As we will see later in Section 7.6, if the database has N
entries,
√ a classical computer takes O(N) queries, but a quantum computer only takes
O( N) using Grover’s algorithm. We call such an improvement or speedup in the
number of oracle queries an oracle separation.
The first part of this chapter will cover quantum algorithms with oracle separa-
tions, meaning they take fewer queries than classical computers to solve problems.
These are generally easier algorithms to understand. Then, the second part of this
chapter we will cover quantum algorithms with better circuit complexities, which
are generally more advanced. Before we start looking an oracular problems, i.e.,
problems with an oracle, let us discuss next how an oracle f (x) acts in a quantum
computer.
An oracle is simply a boolean function, meaning a function that acts on bits. Then,
it can be defined using a truth table, and it can be constructed using logic gates.
For it to be a quantum oracle, however, it needs to be reversible. Fortunately, from
Section 1.5.4, we can turn it into a reversible circuit by XORing its output with an
extra bit. For example, if the function is f (x), the reversible circuit is
y
y ⊕ f (x)
Oracle f (x)
x x
Since this entire circuit is reversible, it is a quantum gate. Let us call the gate U f to
emphasize that it is unitary. We can draw it as
|yi |y ⊕ f (x)i
Uf
|xi |xi
That is, the quantum oracle U f acts as
Uf
|xi|yi −→ |xi|y ⊕ f (x)i.
The extra qubit |yi is called an answer qubit or target qubit, and |xi is called the
input qubit.
268 7 Quantum Algorithms
Exercise 7.1. Consider a classical oracle f (x) = x, where x is a bit, and x is the NOT of x. We want
to turn this into a quantum oracle U f that acts according to
Uf
|xi|yi −→ |xi|y ⊕ f (x)i.
Exercise 7.2. Go to https://bit.ly/3m3Zcei. By following this link, you should have ac-
cess to a custom gate called U f .
If we query a quantum oracle the standard way described above, the input qubit
|xi is unchanged while the answer qubit |yi becomes |y ⊕ f (x)i. There is a way to
query the quantum oracle, however, that causes the answer qubit |yi to be unchanged
while multiplying |xi by a phase. It works by setting |yi = |−i, which can be done by
initializing the answer qubit to |0i, applying X to turn it into |1i, and then applying
H to turn it into |−i. That is, writing both the input and answer qubits,
I⊗X I⊗H
|xi|0i −−→ |xi|1i −−→ |xi|−i.
7.1 Circuit vs Query Complexity 269
Note this is possible because qubits can be in superpositions of |0i and |1i. Now, let
us expand |0i|−i and see what happens when we query the oracle:
1
|xi|−i = |xi √ (|0i − |1i)
2
1
= √ (|xi|0i − |xi|1i)
2
Uf 1
−→ √ (|xi|0 ⊕ f (x)i − |xi|1 ⊕ f (x)i)
2
( 1
√ (|xi|0i − |xi|1i) , f (x) = 0
= 12
√ (|xi|1i − |xi|0i) , f (x) = 1
2
(
|xi|−i, f (x) = 0
=
−|xi|−i, f (x) = 1
= (−1) f (x) |xi|−i.
We can interpret this as the answer qubit staying in the |−i state while the input
qubit goes from |xi to (−1) f (x) |xi. That is, the input qubit acquires a phase. This
is called phase kickback. Often, we drop the answer qubit, since it stays in the |−i
state, and only write the input qubit:
Uf
|xi −→ (−1) f (x) |xi.
This is called a phase oracle, where the qubit |xi is multiplied by a phase (−1) f (x) .
The phase oracle will be very useful for the oracular problems we will cover.
Exercise 7.3. Suppose an input qubit and answer qubit are in the state |xi|+i, where x is a bit. If
we apply the quantum oracle U f to this, which maps |xi|yi to |xi|y ⊕ f (x)i, what do we get if
(a) f (x) = 0?
(b) f (x) = 1?
(c) How do your answers in parts (a) and (b) compare to the initial state |xi|+i?
Exercise 7.4. Quantum oracles are quantum gates, so they act across superpositions. For example,
applying the phase oracle to |+i,
1 Uf 1 h i
√ (|0i + |1i) −→ √ (−1) f (0) |0i + (−1) f (1) |1i .
2 2
(a) Show that if f (0) = f (1), the above state is |+i, times an overall phase.
(b) Show that if f (0) 6= f (1), the above state is |−i, times an overall phase.
270 7 Quantum Algorithms
7.2 Parity
For the first algorithm, we have two unknown bits b0 and b1 , and we want to find
the parity of the two bits. That is, we want to find b0 ⊕ b1 , or equivalently, whether
the number of 1’s is even or odd. To do this, we are given an oracle f (x) = bx that
takes as input an index x ∈ {0, 1} and returns the corresponding bit. That is,
f (0) = b0 , f (1) = b1 .
We will show that to find the parity of b0 and b1 , we must query this oracle twice
classically, but only once quantumly.
Classically, we need to know both bits in order to find b0 ⊗ b1 . So, we need to query
the oracle twice, once to find b0 and again to find b1 :
b0 = f (0), b1 = f (1).
Then we take the XOR of b0 and b1 to find the parity. Thus, the classical query
complexity is 2.
Quantumly, it only takes 1 query using Deutsch’s algorithm. The idea was already
seen in Exercise 7.4, where applying the phase oracle to |+i yields either |+i or |−i,
with overall phases, depending on whether f (0) and f (1) are equal or not. Deutsch’s
algorithm starts by applying the Hadamard gate to turn |0i into |+i. Then, it queries
the oracle to get |+i or |−i, with overall phases, depending on the parity of b0 and
b1 . These states can be distinguished by a measurement in the X-basis {|+i, |−i},
but let us instead apply the Hadamard gate to turn the state into |0i or |−i, with
overall phases, and distinguish them using a measurement in the Z-basis {|0i, |1i}.
If we include the answer qubit, the quantum circuit for Deutsch’s algorithm is
|−i |−i
Uf
|0i H H Parity
Let us also work through the math from start to finish. Ignoring the answer qubit,
7.2 Parity 271
H 1
→ √ (|0i + |1i)
|0i −
2
Uf 1 h i
−→ √ (−1) f (0) |0i + (−1) f (1) |1i
2
1 h i
= √ (−1)b0 |0i + (−1)b1 |1i
2
1 h i
= (−1)b0 √ |0i + (−1)b1 −b0 |1i
2
(
(−1) 0 √12 (|0i + |1i) , b0 = b1
b
=
(−1)b0 √12 (|0i − |1i) , b0 6= b1
(
(−1)b0 |+i, b0 = b1
=
(−1)b0 |−i, b0 6= b1
(
H (−1)b0 |0i, b0 = b1
−
→ .
(−1)b0 |1i, b0 6= b1
If we measure this, we either get |0i or |1i, since the overall phase of (−1)b0 does
not matter. We get |0i if b0 = b1 , but if the bits are equal, that means their parity
is 0 (even). On the other hand, we get |1i if b0 6= b1 , but if the bits are opposite,
that means their parity is 1 (odd). Thus, depending on whether we get |0i or |1i,
we know whether the parity of the two bits is 0 or 1, and we determined this with
just one query to the oracle. Thus, the quantum query complexity is 1, which is an
improvement over the classical query complexity of 2.
Note in Deutsch’s algorithm, we never learned the values of b0 and b1 them-
selves, which would require two oracle queries. Instead, we only learned whether
they are equal or opposite, which corresponds to even or odd parity, respectively.
While the improvement in query complexity from 2 to 1 may be small, it is our
first concrete example of a quantum computer outperforming a classical computer,
in this case in the number of queries it needs to an oracle to determine the parity of
two bits.
Exercise 7.5. There are two unknown bits b0 and b1 , and you want to find the parity of the two bits
by querying an oracle. Go to https://bit.ly/2ILe3cF. By following this link, you should
have access to a custom gate called U f . This is the oracle, and it acts on two qubits by
|yi |y ⊕ f (x)i
Uf
|xi |xi
For this problem, the function f (x) returns bit x, so f (x) = bx .
(a) In Quirk, use Deutsch’s algorithm to find the parity of b0 and b1 using just one query to U f .
Note you will need to prepare the answer qubit so that it is in the minus state.
(b) In Quirk, query U f in such a way as to find b0 .
(c) In Quirk, query U f in such a way as to find b1 .
(d) Since you now know b0 and b1 from parts (b) and (c), find their parity. Verify that it agrees
with your result from part (a).
(e) In the worst case, how many queries does it take to solve the problem classically?
272 7 Quantum Algorithms
Exercise 7.6. There are two unknown bits b0 and b1 , and you want to find the parity of the two bits
by querying an oracle. Go to https://ibm.co/3GxSWTT. By following this link, you should
have access to a custom gate called U f :
|yi |y ⊕ f (x)i
Uf
|xi |xi
For this problem, the function f (x) returns bit x, so f (x) = bx .
(a) Program Deutsch’s algorithm in IBM Quantum Experience, and use the quantum simulator to
find the parity of b0 and b1 .
(b) Run the circuit on an actual quantum processor using IBM Quantum Experience. Which pro-
cessor did you use, and what histogram of results do you get?
Exercise 7.7. Say you are trying to use Deutsch’s algorithm, but you neglect the last Hadamard
gate. That is, you apply
H Uf 1 h i
→ |+i −→ √ (−1) f (0) |0i + (−1) f (1) |1i .
|0i −
2
If you measure the system now, what possible states do you get, and with what probabilities?
What if we have n bits b0 , b1 , . . . , bn−1 , and we want to find their parity? Classically,
we need to know all n bits, so it takes n queries. Quantumly, we can use Deutsch’s
algorithm to find the parity of pairs of bits:
b , b , b , b , . . . bn−2 , bn−1 .
| 0{z }1 | 2{z }3 | {z }
parity parity parity
This takes n/2 queries. Then we can take the XOR of all these parities to get the
parity of all the bits. This takes no additional queries. So the quantum query com-
plexity is n/2, which is half classical query complexity. Note both the classical and
quantum runtimes are O(n), however, so there is no improvement in their asymptotic
scaling.
7.3 Constant vs Balanced Functions 273
Exercise 7.8. You have eight bits, and using Deutsch’s algorithm, you have found the parities of
pairs of bits, shown below:
b0 , b1 , b2 , b3 , b4 , b5 , b6 , b7 .
| {z } | {z } | {z } | {z }
1 1 0 1
(a) What is the parity of all eight bits?
(b) How many queries to the oracle did it take to find the parity of all eight bits?
(c) In the worst case, how many queries does it take to solve the problem classically?
Exercise 7.9. You have nine bits, and using Deutsch’s algorithm, you have found the parities of
the first four pairs of bits. Then you queried the oracle for the last bit, revealing whether it’s a 1 or
a 0. This is shown below:
b0 , b1 , b2 , b3 , b4 , b5 , b6 , b7 , b8 .
| {z } | {z } | {z } | {z } |{z}
0 1 0 0 1
(a) What is the parity of all nine bits?
(b) How many queries to the oracle did it take to find the parity of all nine bits?
(c) In the worst case, how many queries does it take to solve the problem classically?
This does not depend on n. So, we can bound the error by checking an appropriate
number of inputs. For example, if we want the probability of error to be less than
1%, we only need to query f for c = 8 different inputs, and this is a constant number
of queries regardless of n. Thus, this randomized algorithm takes O(1) queries of f .
Problems that are efficiently solved with bounded error by such randomized algo-
rithms are contained in the complexity class bounded-error probabilistic polynomial
time (BPP). It is believed that P = BPP, but it is not proven. Since we gave an effi-
cient randomized algorithm for determining whether f is constant or balanced, this
problem is in BPP.
To review, for a classical computer to determine with certainty whether f is con-
stant or balanced, it needs 2n−1 + 1 queries to f in the worst case, which is O(2n ).
For a probabilistic classical computer to guess the answer with bounded error, it
only needs a constant number of queries to f , which is O(1).
Exercise 7.10. When determining if an oracle f is constant or balanced,
(a) What is the probability of an error if you evaluate f for c = 7 different inputs?
(b) What is the probability of an error if you evaluate f for c = 8 different inputs?
(c) How many times should f be evaluated to reduce the error probability to less than 0.1%?
A quantum computer using the Deutsch-Jozsa algorithm can determine with cer-
tainty whether f is constant or balanced using just 1 query to f . This is an exponen-
tial speedup over the exact classical algorithm, but no speedup over the bounded-
error probabilistic classical algorithm.
The Deutsch-Jozsa algorithm is very similar to Deutsch’s algorithm, but we now
have n qubits (plus an answer qubit, which we ignore by using a phase oracle). These
n qubits are initially each in the |0i state, and we apply Hadamards to put them in
a superposition of all n-bit strings. Then we query the oracle on this superposition.
Finally, we apply Hadamards to all the qubits to create a state that we measure,
and whose measurement outcome allows us to distinguish whether the function is
constant or balanced. Including the answer qubit, the Deutsch-Jozsa algorithm as a
quantum circuit is
|−i
|0i H H
|0i Uf
H H
n .. .. .. ..
. . . .
|0i
H H
Let us work out the math to show why this determines whether f is constant or
balanced. Ignoring the answer qubit, we begin with n qubits, all in the |0i state. First
276 7 Quantum Algorithms
H ⊗n
|0i⊗n −−→ |+i⊗n
1
= √ (|0i + |1i)⊗n
2n
1
=√ ∑ |xi. (7.1)
2n x∈{0,1}n
So applying Hadamards to the all zero state creates a uniform superposition over all
binary strings. Next, we query the phase oracle:
1 Uf 1
√ ∑ |xi −→ √2n ∑ n (−1) f (x) |xi.
2n x∈{0,1}n x∈{0,1}
This is the final state of the algorithm before the measurement, and to interpret this,
let us focus on just one H ⊗n |xi, where |xi is a single n-bit string:
H ⊗n |xi = H ⊗n |xn−1 . . . x1 x0 i
= H|xn−1 i . . . H|x1 iH|x0 i.
1 h i
H|xi i = √ |0i + (−1)xi |1i .
2
This way, when xi = 0, (−1)xi = 1, and the result is |+i, and when xi = 1, (−1)xi =
−1, and the result is |−i. Writing each H|xi i like this, we get
1 h
√ |0 . . . 000i + (−1)x0 |0 . . . 001i + (−1)x1 |0 . . . 010i
2n
+ (−1)x1 +x0 |0 . . . 011i + (−1)x2 |0 . . . 100i + (−1)x2 +x0 |0 . . . 101i
i
+ (−1)x2 (−1)x1 |0 . . . 110i + (−1)x2 +x1 +x0 |0 . . . 111i + . . . .
1
√ ∑ (−1)∑i:zi =1 xi |zi.
2n z∈{0,1}n
For the negative sign, the power is the sum of the values of xi such that zi = 1. We
can also write this sum using the dot product x · z,
In this dot product, the only xi ’s that survive are those where zi = 1. Using this
notation, we get
1
H ⊗n |xi = √ ∑ (−1)x·z |zi. (7.3)
2n z∈{0,1}n
Plugging this into Eq. (7.2), the final state of the algorithm before measurement is
1 1 1
√ ∑ (−1) f (x) H ⊗n |xi = √ ∑ (−1) f (x) √ ∑ (−1)x·z |zi
n
2 x∈{0,1}n n
2 x∈{0,1}n 2n z∈{0,1}n
!
1
= ∑ ∑ n (−1) f (x)+x·z |zi. (7.4)
2n x∈{0,1}
z∈{0,1}n
To see how measuring this state lets us determine whether the function is constant or
balanced, let us calculate the probability of getting all zeros |0 . . . 00i. The amplitude
of |0 . . . 00i (right before measurement) is
1
∑ n (−1) f (x) .
2n x∈{0,1}
If f (x) is constant, then f (x) always outputs the same value, so f (x) = f (0 . . . 00)
for all x, and the amplitude is
1 1
∑ n (−1) f (0...00) = 2n (−1) f (0...00) 2n = (−1) f (0...00) .
2n x∈{0,1}
and −1 the other half the time, so the amplitude is 0. Hence, if f (x) is a balanced, the
probability of measuring |0 . . . 00i is 0, so we are guaranteed to get something other
than |0 . . . 00i when we measure. Thus, to determine if f is constant or balanced, we
measure the n qubits, and if we get |0 . . . 00i, the function is constant, and if we get
anything else, the function is balanced.
Exercise 7.11. Apply H ⊗ H ⊗ H to |000i, and show that the resulting state is a uniform superpo-
sition of all binary strings of length 3. If you measure the qubits, what possible outcomes can you
get, and with what probabilities?
Exercise 7.12. There is a function on three bits f (b0 , b1 , b2 ), with the promise that the function
is constant or balanced. You want to determine which by querying an oracle. Go to https:
//bit.ly/38P0Nig. By following this link, you should have access to a custom gate called
U f . This is the oracle, and it acts on four qubits by
|yi |y ⊕ f (b0 , b1 , b2 )i
|b0 i |b0 i
Uf
|b1 i |b1 i
|b2 i |b2 i
(a) In Quirk, use the Deutsch-Jozsa algorithm to determine whether f (x) is constant or balanced
using just one query to U f . Note you will need to prepare the answer qubit so that it is in the
minus state.
(b) In Quirk, query U f in various ways to determine f (b2 , b1 , b0 ):
b2 b1 b0 f (b2 , b1 , b0 )
0 0 0 ?
0 0 1 ?
0 1 0 ?
0 1 1 ?
1 0 0 ?
1 0 1 ?
1 1 0 ?
1 1 1 ?
(c) Since you now know f (b2 , b1 , b0 ) completely from part (b), verify that it agrees with your
result from part (a).
(d) In the worst case, how many queries does it take to solve the problem classically?
Exercise 7.13. There is a function on three bits f (b0 , b1 , b2 ), with the promise that the function
is constant or balanced. You want to determine which by querying an oracle. Go to https:
//ibm.co/3EWbltg. By following this link, you should have access to a custom gate called
U f . This is the oracle, and it acts on four qubits by
|yi |y ⊕ f (b0 , b1 , b2 )i
|b0 i |b0 i
Uf
|b1 i |b1 i
|b2 i |b2 i
(a) Program the Deutsch-Jozsa algorithm in IBM Quantum Experience, and use the quantum
simulator to determine if f (b0 , b1 , b2 ) is constant or balanced.
(b) Run the circuit on an actual quantum processor using IBM Quantum Experience. Which pro-
cessor did you use, and what histogram of results do you get?
7.4 Secret Dot Product String 279
Deutsch’s algorithm and the Deutsch-Jozsa algorithm both followed the same steps:
apply Hadamard gate(s), query the oracle, apply Hadamard gate(s) again, and then
measure. Since this worked so well, are there any other problems that can be solved
by this procedure?
The answer is yes. There is another problem that a quantum computer can solve
using this procedure, and it is finding a secret n-bit string that by querying an oracle
that takes the dot product of the string with the input. That is, we again have a
function f that takes as input a binary string of length n and outputs 0 or 1, so
f : {0, 1}n → {0, 1}, where {0, 1}n . But now the promise is that f (x) = s · x, where
s is some n-bit string sn−1 . . . s1 s0 , and the dot product of s and x is the sum of the
products of their elements, i.e.,
s · x = sn−1 xn−1 + . . . s1 x1 + s0 x0 .
Since we need to determine all n bits of s, and this requires n classical queries, one
to learn each bit of s. For example, if n = 4, then
Quantumly, we only need one query using the Bernstein-Vazirani algorithm, which
is a polynomial speedup over classical computers. It follows the exact same steps
as the Deutsch-Jozsa algorithm, where we apply Hadamards, query the oracle, and
then apply Hadamards again, so as a quantum circuit, it is
280 7 Quantum Algorithms
|−i
|0i H H
|0i Uf
H H
n .. .. .. ..
. . . .
|0i
H H
Let us work out the math to show that this works. Ignoring the answer qubit,
from Eq. (7.4), the state of the n qubits before measurement is
!
1 f (x)+x·z
∑ 2n ∑ n (−1) |zi.
z∈{0,1}n x∈{0,1}
For the problem of the secret dot product string, f (x) = s · x. Plugging this in, we
get !
1
∑ 2n ∑ n (−1)(s+z)·x |zi.
z∈{0,1}n x∈{0,1}
where s + z denotes bitwise addition (no carry), also knowns as bitwise XOR. Now
we measure this, and to determine the possible measurement outcomes, let us con-
sider the amplitude of getting |si. When z = s, s + z is a bit string of all zeros. Then
the amplitude of |si is
1 1 1
n ∑(−1)0 = n ∑ 1 = n 2n = 1.
2 x 2 x 2
Thus, normalization implies that the amplitude of all other states is 0, so the final
state of the qubits is
|si.
Measuring this is certain to yield |si, and we have determine s with just one query
to the oracle.
This is a polynomial speedup over the O(n) queries needed by a classical com-
puter. . The above speedup holds for bounded error, so it yields an oracle separa-
tion between the complexity classes P and BQP. However, the problem is efficient
for both classical and quantum computers. The next algorithm, Simon’s algorithm,
gives the first “true” exponential speedup, where the problem is inefficient for a
classical computer, but efficient for a quantum computer, in the number of oracle
queries.
Exercise 7.14. There is a function on six bits f (b5 , b4 , b3 , b2 , b1 , b0 ) = s5 b5 + · · · + s1 b1 + s0 b0 .
Find s = s5 . . . s1 s0 by querying an oracle. Go to https://bit.ly/31YCBZu. By following
this link, you should have access to a custom gate called U f . This is the oracle, and it acts on seven
qubits by
7.4 Secret Dot Product String 281
|yi |y ⊕ f (b5 , . . . , b0 )i
|b0 i |b0 i
|b1 i |b1 i
|b2 i Uf |b2 i
|b3 i |b3 i
|b4 i |b4 i
|b5 i |b5 i
(a) In Quirk, use the Bernstein-Vazirani algorithm to determine s using just one query to U f . Note
you will need to prepare the answer qubit so that it is in the minus state.
(b) In Quirk, query U f in various ways to determine each bit of s. Verify that it agrees with your
result from part (a).
(c) In the worst case, how many queries does it take to solve the problem classically?
|yi |y ⊕ f (x3 , . . . , x0 )i
|x0 i |x0 i
|x1 i Uf |x1 i
|x2 i |x2 i
|x3 i |x3 i
(a) Program the Bernstein-Vazirani algorithm in IBM Quantum Experience, and use the quantum
simulator to find s = s3 s2 s1 s0 .
(b) Run the circuit on an actual quantum processor using IBM Quantum Experience. Which pro-
cessor did you use, and what histogram of results do you get?
Exercise 7.16. In the Bernstein-Vazirani algorithm, recall the final state of the qubits (before mea-
surement) is !
1 (s+z)·x
∑ n ∑ n (−1) |zi.
2n z∈{0,1} x∈{0,1}
Say n = 3 and consider z 6= s such that s+z = 001 (using bitwise addition). Show that the amplitude
of this choice of |zi is zero by filling in the following table, and then computing the sum of the last
column.
x (s + z) · x (−1)(s+z)·x
000 ? ?
001 ? ?
010 ? ?
011 ? ?
100 ? ?
101 ? ?
110 ? ?
111 ? ?
∑(−1)(s+z)·x : ?
x
282 7 Quantum Algorithms
The problem of finding a hidden dot product string can be made recursive, meaning
we embed the problem in a bigger instance of the problem, which is embedded in a
bigger instance of the problem, and so forth. The details are beyond the scope of this
textbook, but if we have k levels, then a classical computers takes Ω (nk ) queries to
solve the problem (recall from Section 1.7.1 that big-Ω is a lower bound, so a clas-
sical computer takes at least this many queries). In contrast, a quantum computer
using recursive version of the Bernstein-Vazirani algorithm, however, can solve this
problem with 2k queries. If we have k = log2 (n) levels, then the classical algorithm
takes Ω (nlog n queries, which is bigger than any polynomial but less than exponen-
tial. We call this superpolynomial. The quantum computer, however, only takes n
queries, which is linear and efficient. Thus, the recursive version of the hidden dot
product string problem shows that a quantum computer can yield a superpolynomial
speedup in queries. Next, we will see a problem with a true exponential speedup.
In this problem, the oracle takes as input an n-bit string x = xn−1 . . . x1 x0 and outputs
an n-bit string f (x) = fn−1 . . . f1 f0 . That is, f : {0, 1}n → {0, 1}n . We are promised
that
f (x) = f (y)
if and only if the two inputs x and y are related by
x = y ⊕ s, and y = x⊕s
for some “secret” n-bit string s = sn−1 . . . s1 s0 6= 0 . . . 00, where ⊕ denotes the bitwise
XOR. That is, f (x) = f (y) if and only if
xi = yi ⊕ si , and yi = xi ⊕ si .
The goal is to find the secret n-bit string s = sn−1 . . . s1 s0 . The secret bitstring is
called a mask, and since it is used to XOR the inputs, it is called an XOR mask. The
problem is to find the secret XOR mask s = sn−1 . . . s1 s0
For example, say n = 3, and s = 110. Then, for each value of x, y = x ⊕ s is shown
in the following table:
7.5 Secret XOR Mask 283
x y
000 110
001 111
010 100
011 101
100 010
101 011
110 000
111 001
Notice these come in pairs. That is, 000 and 110 are a pair, 001 and 111 are a pair,
010 and 100 are a pair, and 011 and 101 are a pair. This is because taking the XOR
mask of x with s yields y, and taking the XOR mask of y with s yields x. Put another
way, if x = y ⊕ s, then it is automatically true that y = x ⊕ s. As a proof, we start with
x = y ⊕ s.
x ⊕ s = y ⊕ s ⊕ s.
Since s ⊕ s = 0, this is
x ⊕ s = y ⊕ 0.
Thus,
x ⊕ s = y,
or reversing the two sides, y = x ⊕ s. Now, from the promise about the oracle, for
each pair x and y, f (x) and f (y) must be the same. For example, here are two possi-
ble truth tables for f (x), satisfying that f (x) = f (y) if and only if y = x ⊕ s:
x f (x) x f (x)
000 011 000 110
001 101 001 001
010 001 010 111
011 000 011 000
100 001 100 111
101 000 101 000
110 011 110 110
111 101 111 001
Notice that in both examples, f (000) = f (110), f (001) = f (111), f (010) = f (100),
and f (011) = f (101). Also note there are 1680 different possible truth tables for
f (x). This is because we have four pairs that we need to assign outputs to, and there
are 23 = 8 different outputs. For the first pair, we have 8 choices of outputs. For the
second pair, we have 7 choices of outputs. For the third pair, we have 6 choices of
outputs. And for the fourth pair, we have 5 choices of outputs. Altogether, we have
8 · 7 · 6 · 5 = 1680 possible permutations.
284 7 Quantum Algorithms
Exercise 7.17. Say n = 3 and s = 010. Find the pairs of n-bit strings x and y such that y = x ⊕ s.
Give a possible truth table for f (x) that satisfies the promise that f (x) = f (y) if and only if y = x⊕s.
Classically, we can find the secret XOR mask s by finding a collision, meaning a
pair x and y such that f maps them to the same string, i.e., f (x) = f (y). From the
promise about f , this implies that x = y ⊗ s and y = x ⊗ s, and we can take the XOR
of x and y to find s:
x ⊗ y = x ⊗ (x ⊗ s) = (x ⊗ x) ⊗s = s.
| {z }
0
One approach is to trying the inputs one-by-one until we find a collision. In the
worse case, we could try half of the inputs without yet seeing a collision. We are
guaranteed, however, that trying one more input will yield a collision, so the query
complexity with this approach is O(2n−1 + 1).
We can do better, however. If we query f with random inputs. This prevents f
from being designed to be as worse as possible as previously described, where half
the inputs, plus 1, must be queried to find a collision. Now, say we have queried
f a total of k times, so we have k values of f . The probability of there being a
collision in these k values of f is given by the number of pairs of values, which is
the combination kC2 = k(k − 1)/2 = O(k2 ). Since this√ grows quadratically with the
number of queries, one expects to query f roughly 2n = 2n/2 times in order to find
a collision. Although this is an improvement, it is still exponential in n.
Exercise 7.18. We have an oracle f : {0, 1}n → {0, 1}n with a secret XOR mask s. Say n = 4.
Querying the oracle with some various inputs, we find that f (1011) = 0010 and f (0111) = 0010.
What is s?
Exercise 7.19. The task of finding a collision is closely related to a famous problem called the
birthday problem, which is to find the probability that in a room of n people, at least two of them
share the same birthday. We ignore leap years, so there are 365 days in a year. We also assume that
people’s birthdays are randomly distributed. In reality, this is not true, as some birthdays are more
common than others, but this only makes a shared birthday more likely.
To solve this problem, we find the probability that the n people do not share any birthdays. Then,
the probability that at least two people share the same birthday is 1 minus this. To calculate the
probability that no one shares a birthday, we add people to the room one-by-one. The first person
in the room does not share a birthday with anyone else because there is no one else. The second
person in the room has 364 possible birthdays so as to not share a birthday with the first person,
and the probability of this is 364/365. The third person in the room has 363 possible birthdays
so as to not share a birthday with the first two people, and the probability of this is 363/365. The
fourth person has 362 possible birthdays to avoid sharing, which has a probability of 362/365.
Continuing this, the probability of no one sharing a birthday is
365 364 363 362 365 − (n − 1) 365 · 364 · 363 · 362 · (365 − (n − 1))
... = .
365 365 365 365 365 365n
Thus, the probability that at least two people share the same birthday is
Simon’s algorithm follows the pattern we have seen so far: apply Hadamards, query
the oracle, and apply Hadamards again. But now we have n input qubits and n an-
swer qubits, and we start each of the answer qubits in the |0i state (so we are using
the regular quantum oracle, not the phase oracle). That is, the oracle maps
Uf
|xi|yi −→ |xi|y ⊕ f (x)i,
where
|xi = |xn−1 . . . x1 x0 i,
|yi = |yn−1 . . . y1 y0 i,
|y ⊕ f (x)i = |yn−1 ⊕ f (x)n−1 i . . . |y0 ⊕ f (x)0 i.
Let us work through the math of what this does. Initially, we have two n-qubit reg-
isters, one for the input qubits, and another for the answer qubits.
|0 . . . 00i|0 . . . 00i.
Now, we apply the Hadamard gate to each of the input qubits, resulting in
|+ · · · + +i|0 . . . 00i.
Multiplying out the |+i states, we get a uniform superposition over n-bit strings:
1
√ ∑ |xi|0 . . . 00i.
2n x∈{0,1}n
Now, we again apply the Hadamard gate to each of the input qubits, resulting in
1
√ ∑ H ⊗n |xi| f (x)i.
2n x∈{0,1}n
From Eq. (7.3), H ⊗n |xi is a uniform superposition of bit strings |zi multiplied by a
phase of (−1)x·z , so we get
1 1
√ ∑ √ ∑ (−1)x·z |zi| f (x)i.
2 x∈{0,1}n 2n z∈{0,1}n
n
7.5 Secret XOR Mask 287
Now, let us measure the answer qubits. We will get one particular value of f (x). Let
us call the value f 0 . There are two values of x for which f (x) = f 0 . Let us call them
x0 and x00 . That is, f (x0 ) = f (x00 ) = f 0 . So, x0 and x00 are a pair of inputs for which
there is a collision. Then, the state will collapse to these two values of x:
1 1 h
x0 ·z x00 ·z
i
√ √ ∑ (−1) + (−1) |zi f 0 .
2 2n z∈{0,1}n
√ √
Note the first coefficient went from 1/ 2n to 1/ 2 because the number of possible
outcomes for x went from 2n (all possible bitstrings) to 2 (|x0 i and |x00 i). Combining
the coefficients,
1 h
x0 ·z x00 ·z
i
√ ∑ (−1) + (−1) |zi f 0 .
2n+1 z∈{0,1}n
Next, we measure the input qubits. To determine the possible results, note that
0 00
(−1)x ·z = ±1 and (−1)x ·z = ±1 depending on what x0 and x00 are. Then, their
sum is either ±2 or 0:
(
x0 ·z x00 ·z ±2, x0 · z = x00 · z mod 2,
(−1) + (−1) =
0, x0 · z 6= x00 · z mod 2.
Thus, when measuring the input qubits, we only get a value of |zi where
x0 · z = x00 · z mod 2.
The right-hand side of this equation is 0 because if we add any bit to itself modulo
2, we get 0. Thus,
x0 · z + x00 · z = 0 mod 2.
Factoring the left-hand side,
Since x0 and x00 are a pair of inputs for which there is a collision, x0 ⊕ x00 = s. Then,
we have
s · z = 0 mod 2.
Thus, when measuring the input qubits, we get a value of |zi = |zn−1 . . . z1 z0 i such
that its dot product with s is 0 mod 2. Writing out the dot product,
This gives us one equation containing all n of our unknowns, the si ’s. The probability
of measuring any such |zi is
±2 2
√ 4 1
n+1 = 2n+1 = 2n−1 .
2
Or, put another way, there are 2n−1 possible |zi’s whose dot product with s is zero,
and we have the same probability of getting each one.
If we repeat this process, we will a |zi = |zn−1 . . . z1 z0 i that satisfies Eq. (7.5) and
is likely different from the first because there are an exponential number of them.
Repeating this O(n) times, we can get n different |zi’s, each satisfying Eq. (7.5).
Together, they are a system of n equations and n unknowns, which we can solve for
the si ’s. Thus, we can find s with O(n) queries to the oracle, and this was the first
exponential oracle separation between classical and quantum computers.
Exercise 7.20. You are using Simon’s algorithm to find an n = 3 bit string s = s2 s1 s0 . You run the
quantum circuit three times, and you get the following values for |zi, such that s · z = 0 mod 2:
What is s?
Exercise 7.21. You are using Simon’s algorithm to find an n = 3 bit string s = s2 s1 s0 .
(a) How many different values of |zi are there, such that s · z = 0 mod 2?
(b) If you run the quantum circuit three times, what is the probability that all three values of |zi
are different?
7.5.4 Summary
We have examined several quantum algorithms that all follow the same proce-
dure: apply Hadamards, query the oracle, and apply Hadamards again. The follow-
ing table summarizes the problems, query complexities, and asymptotic quantum
speedups:
We started with the parity problem. The quantum algorithm does offer an improve-
ment in that it takes half as many queries, but asymptotically, both the classical and
quantum algorithms are O(n), so there is no speedup in that sense. Then, we looked
at determining whether the oracle is constant or balanced. Although the quantum
algorithm yields an exponential improvement over the exact classical algorithm, it
is no improvement over the bounded algorithm that is often acceptable in practice.
For a true asymptotic speedup, the problem of finding a secret dot product string
is solved by a quantum computer using polynomially fewer queries, and a recur-
sive version of the problem is solved with superpolynomially fewer queries. Finally,
finding a secret XOR mask takes exponentially fewer queries on a quantum com-
puter, which shows that for oracular problems, quantum computers can yield an
exponential speedup.
Before moving on to problems where we can calculate the circuit complexity, let us
discuss one more oracular problem where we count the number of oracle queries.
It is the problem of brute-force searching. We again have a function f that takes as
input a binary string of length n and outputs 0 or 1, so f : {0, 1}n → {0, 1}. This
function, however, only outputs 1 for one input, so it outputs 0 for all other inputs.
The problem is to find this one special input, which we will call w (for winner).
A common motivation for this problem is searching a telephone book, which is
a list of people in alphabetical order along with their telephone number:1
Name Phone Number
Alice 314-1592
Bob 271-8281
Charlie 105-4571
Dave 885-4187
Eve 125-6637
Frank 299-7924
Grace 729-7352
.. ..
. .
Zoe 200-2319
Given a name, it is easy to find the corresponding phone number, since the names
are sorted in alphabetical order. For example, we can start in the middle of the list
and determine if the person we are looking for is in the first half or the last half of
the phone book. Say it is the first half. Then we can look at the middle entry of this
1 These phone numbers are inspired by the mathematical constants π and e, and the physical
half of the phone book and determine if the person we are looking for is in the first
quarter or second quarter of the list. Repeating this, we reduce the number entries by
one-half each time until we find the entry we are looking for. This process is called
binary search, and if the number of entries is N, it takes at most log2 N steps.
The inverse problem, however, is harder to do. Given a phone number, say 299-
7924, finding the name it corresponds to is harder since the numbers are unsorted.
In fact, we might need to look though every phone number until we find a match.
This inverse problem is the brute-force searching problem that we want to solve, and
a classical computer needs O(N) queries to solve it. This problem is also phrased
as searching an unordered database, and it is also called unstructured searching. In
terms of a function, we have f (name) = number, and we want to find the name. So,
this is also the problem of inverting a function, i.e., of starting with an output and
trying to find the input.
It is possible to have a function or oracle that recognizes the correct answer, even
if it does not know what the correct answer is. For example, it is generally hard to
factor numbers (see Section 6.6.2 on RSA cryptography), but it is easy to verify
if the product of numbers equals the number we are trying to factor. Recall from
Section 1.7.2 that the problems whose possible solutions are easy to check comprise
the complexity class NP. So, brute-force searching includes trying to solve problems
in NP by checking each input to see which output is correct.
Classically, we must query all N = 2n possible bit strings in the worst case. Or on
average, we must query half the bit strings, or N/2, since it is equally likely that
the winner is the first input we check as the last. Either way, the classical runtime is
O(N).
A√ quantum computer can solve the brute-force searching problem using only
O( N) queries using Grover’s algorithm. Ignoring the answer qubit, the algorithm
begins with the qubits all in the |+i state. Let us call this starting state |si. From
Eq. (7.1), this is a uniform superposition over all n-bit strings:
1
|si = |+i⊗n = √ ∑ |xi,
N x∈{0,1}n
where N = 2n . We can create this initial state by applying Hadamard gates to the all-
zeros state, i.e., |+i⊗n = H ⊗n |0i⊗n . Since the initial state is a uniform superposition
over all n-bit strings, it includes the binary string |wi we are trying to find, and all
7.6 Brute-Force Searching 291
where |ri is defined as the uniform superposition over all n-bit strings that are not
|wi, and θ is defined such that
r
1 N −1
sin θ = √ , cos θ = .
N N
Drawing the initial state in a coordinate plane with |ri and |wi as the x- and y-axes,
we get
|wi
|si
θ |ri
Next, we query the phase oracle U f . Since f (x) = 1 only when x = w, the qubits
become
Thus, the amplitude of |wi is inverted. Drawn in the coordinate plane, this is equiv-
alent to a reflection across |wi:
292 7 Quantum Algorithms
|wi
|si
θ |ri
θ
Uf |si
Next, we apply a quantum gate Rs that reflects about |si (more on how to do this,
and how to interpret it, later). Drawn in the rw-plane,
|wi
Rs Uf |si
2θ |si
θ |ri
θ
Uf |si
We see that the net effect of these two reflections is a rotation by 2θ . If we apply U f
and Rs again, we rotate by 2θ again:
|wi
(Rs Uf )2 |si
Rs Uf |si
2θ
2θ |si
θ |ri
θ
Uf |si
In this manner, we keep rotating by 2θ by applying U f and Rs until the final state is
close to |wi. Say this takes a total of t rotations. Then since the angle between |ri
and |wi is 90◦ , or π/2 radians,
π
θ + t(2θ ) =
2
π
t(2θ ) = − θ
2
π 1
t= −
4θ 2
7.6 Brute-Force Searching 293
Assuming N is large,
−1 1 1
θ = sin √ ≈√ ,
N N
and so
π√ 1 π√
t≈
N− ≈ N.
4 2 4
√
Thus, the number of queries is O( N), which is a quadratic speedup over the clas-
sical computer’s O(N).
The angle of the final state may not be exactly π/2, so the success probability
may not be exactly 1. This is not an issue, however, for a couple reasons. First,
for large N, the angle θ is small. So, the final state may only miss |wi by a small
amount. Second, there are ways to adjust this algorithm so that the last step rotates
by a different angle, causing the final state to be exactly aligned with |wi. This is
beyond the scope of this textbook.
Including the answer qubit, as a quantum circuit, Grover’s algorithm is
|−i
|+i
|+i
Uf Uf Uf
n .. Rs Rs ··· Rs ..
. .
|+i
|+i
Exercise 7.22. Prove that when N = 4 (n = 2), the final state of Grover’s algorithm is exactly |wi.
|yi |y ⊕ f (x3 , . . . , x0 )i
|x0 i |x0 i
|x1 i Uf |x1 i
|x2 i |x2 i
|x3 i |x3 i
294 7 Quantum Algorithms
Now let us explore the reflection about |si, which we denoted Rs . Since we want |si
to be unchanged, but states perpendicular to |si to be reflected (i.e., take on a minus
sign), we can write Rs as
Rs = 2|sihs| − I.
Recall from Section 3.4 that the outer product |sihs| is a matrix, and I is the identity
matrix that acts on n qubits, so it is N × N in size. Let us show that this keeps |si
the same, but flips any state |s⊥ i that is orthogonal to |si, as we expect a reflection
about |si to do:
Rs = 2H ⊗n |0n ih0n |H ⊗n − I.
I = I ⊗ · · · ⊗ I = HH ⊗ · · · ⊗ HH = (H ⊗ · · · ⊗ H)(H ⊗ · · · ⊗ H) = H ⊗n H ⊗n .
Rs = 2 H ⊗n |0n i h0n |H ⊗n − H ⊗n H ⊗n
= H ⊗n (2|0n ih0n | − I) H ⊗n
| {z }
R0
⊗n ⊗n
=H R0 H ,
R0 = 2|0n ih0n | − I.
H H
Rs = .. R0 ..
. .
H H
H H
Now for R0 , let us calculate how it acts on the all 0’s state, and how it acts on
anything else:
R0 |0 . . . 0i = |0 . . . 0i,
R0 |x0 6= 0, . . . , xn−1 6= 0i = −|x0 . . . xn−1 i.
Thus, R0 is a reflection about the all zeros state |0i⊗n . To create a circuit for R0 ,
recall Z|0i = |0i and Z|1i = −|1i. Then the following circuit flips the sign of the all
ones state |1 . . . 1i only:
296 7 Quantum Algorithms
•
•
..
.
•
If we multiply this on both sides by X gates, the resulting circuit will flip the sign of
the all zeros state |0 . . . 0i state only:
X • X
X • X
.. .. ..
. . .
X • X
X Z X
But, we want the all zeros state to be unchanged, while all other states are flipped.
Using Exercise 2.26, we use ZXZX to flip the sign of the top qubit, which flips the
sign of the entire state.
X • X X Z X Z
X • X
.. .. .. .. .. .. ..
. . . . . . .
X • X
X Z X
Using X 2 = I, R0 is
X • Z X Z
X • X
R0 = .. .. ..
. . .
X • X
X Z X
7.6.5 Optimality
It is proven
√ that a quantum computer cannot solve the brute-force problem faster
than O( N), so Grover’s algorithm is optimal, the best that a quantum computer can
do. Then, if it takes a classical computer an exponential number of queries to solve
a problem in NP, a quantum computer also takes an exponential number of queries
(albeit a smaller exponential).
√ This is because the square root of an exponential is
still an exponential, e.g., 2n = 2n/2 . Thus, quantum computers cannot brute-force
solve NP problems by simply checking all the answers in superposition. If quantum
computers can solve NP problems efficiently, they would have to exploit some other
structure of the problems besides the fact that their potential solutions are efficiently
checked. All evidence, however, suggests that quantum computers cannot solve NP
problems.
We have finished exploring oracular algorithms. For the rest of this chapter, we
will explore problems where quantum computers have a better gate complexity than
classical computers, meaning the number of elementary gates/steps is less.
In this section, we will explore a method of analyzing data that has wide appli-
cations in science, engineering, and technology: the discrete Fourier transform. To
introduce it, let us look at a specific application: analyzing music and sound.
Sound is vibration. The pluck of a guitar string, flutter of our vocal chords, or
pulse of a speaker causes air molecules to vibrate. These vibrations reverberate
through subsequent air molecules, eventually reaching our ears. A diagram of an
ear is shown below:
298 7 Quantum Algorithms
The air in the ear canal carries these vibrations to the eardrum, a stretchy membrane,
causing it to vibrate. The vibrations continue into through three bones, the smallest
in the human body, called ossicles. The third ossicle rests on the oval window, an-
other stretchy membrane, and the vibrations transmit through it into the cochlea, a
spiral-shaped hollow bone. Inside the cochlea are tiny hair receptors that convert
the vibrations into nerve signals that are sent to the brain’s hearing center and inter-
preted as sound.2
The following waveform shows the vibrations of a piano playing a C major chord
(made of middle C and the E and G notes above it) for one second:
1
0.5
Amplitude
-0.5
-1
0 0.2 0.4 0.6 0.8 1
Time (s)
The details of the vibrations are hard to see, so let us zoom in to the first 0.05
seconds:
2 In the 2013 Academy Award-winning film Gravity, many of the scenes are silent because there
Amplitude 0.5
-0.5
-1
0 0.01 0.02 0.03 0.04 0.05
Time (s)
Now, say we want to find the frequencies that make up the previous C chord, which
correspond to the pitches or notes that make up the sound. If we let the num-
ber of samples be N = 44100 and label the previous amplitudes a0 = −0.46933,
a1 = −0.46011, . . . , aN−1 = 0.13571, then the discrete Fourier transform of the
waveform is a sequence of N points φ0 , φ1 , . . . , φN−1 defined to be
1 N−1
φk = √ ∑ a j e2πi jk/N . (7.6)
N j=0
For example,
1
φ0 = √ − 0.46933e2πi(0)(0)/44100 − 0.46011e2πi(1)(0)/44100 + . . .
44100
+ 0.13571e2πi(44099)(0)/44100 = −0.0973861,
1
φ1 = √ − 0.46933e2πi(0)(1)/44100 − 0.46011e2πi(1)(1)/44100 + . . .
44100
300 7 Quantum Algorithms
+ 0.13571e2πi(44099)(1)/44100 = −0.118737 + 0.136405i,
1
φ2 = √ − 0.46933e2πi(0)(2)/44100 − 0.46011e2πi(1)(2)/44100 + . . .
44100
+ 0.13571e2πi(44099)(2)/44100 = −0.106039 + 0.0597867i,
..
.
1
φ44098 = √ − 0.46933e2πi(0)(44098)/44100 − 0.46011e2πi(1)(44098)/44100 + . . .
44100
+ 0.13571e2πi(44099)(44098)/44100 = −0.106039 − 0.0597867i
1
φ44099 = √ − 0.46933e2πi(0)(44099)/44100 − 0.46011e2πi(1)(44099)/44100 + . . .
44100
+ 0.13571e2πi(44099)(44099)/44100 = −0.118737 − 0.136405i.
|φ0 | = 0.097386
|φ1 | = 0.180844
|φ2 | = 0.121732
..
.
|φ44098 | = 0.121732
|φ44099 | = 0.180844.
Due to the symmetry of the discrete Fourier transform, |φ1 | = |φ44099 |, |φ2 | =
|φ44098 |, etc. Plotting k on the x-axis and |φk | on the y-axis with k = 0, 1, . . . , 22050,
we get the frequency spectrum of the waveform:
7.7 Discrete Fourier Transform 301
14
12
Amplitude 10
8
6
4
2
0
0 5000 10000 15000 20000
Frequency (Hz)
This is hard to read, so let us zoom into the first 1000 points of the x-axis:
14
12
10
Amplitude
8
6
4
2
0
0 100 200 300 400 500 600 700 800 900 1000
Frequency (Hz)
Here, the x-axis corresponds to the frequency, which is measured in Hertz (Hz) and
corresponds to the pitch, so a higher frequency is a higher pitch note. The y-axis
corresponds to the strength of note, so there are several frequencies that are stronger
than the rest. The biggest is around 262 Hz. This corresponds to Middle C on the
piano, which has a frequency of 261.6256 Hz. There is another large spike around
330 Hz, and this corresponds to the E key on the piano above Middle C, and this has
a frequency of 329.6276 Hz. Beyond that, there is another spike at 392 Hz, and this
corresponds to the next G key on the piano, which has a frequency of 391.9954 Hz.
These three piano keys were pressed in order to create the music, so they contribute
strongly to the sound. Note the other prominent frequencies are resonances of these
three fundamental frequencies, and they occur at integer multiples of the aforemen-
tioned frequencies. For example, the spike at 522 Hz is twice Middle C’s 262 Hz,
the spike at 660 Hz is twice the E key’s 330 Hz, the spike at 784 Hz is roughly three
times Middle C’s 262 Hz and twice the G key’s 392 Hz, and the spike at 990 Hz is
three times the E key’s 330 Hz.
Exercise 7.26. Consider a sequence of four points
a0 = 0.841,
a1 = 0.909,
a2 = 0.141,
a3 = −0.757.
Calculate the discrete Fourier transform of this (i.e., φ0 , φ1 , and φ2 ) using Eq. (7.6). You may use
a calculator, but not a computer algebra system.
302 7 Quantum Algorithms
Calculating just one φk using Eq. (7.6) requires adding together N terms. Since there
are N different φk ’s, altogether, this is a total of N 2 terms. Although this O(N 2 ) run-
time is efficient in the language of computational complexity, since it is a polynomial
in N, in practice, it can be quite slow because of the large number of points that a
long audio recording can contain.
Another way to interpret Eq. (7.6) is as a matrix-vector multiplication. To write
it more cleanly, let us define ω = e2πi/N . Then, Eq. (7.6) becomes
1 N−1
φk = √ ∑ a j ω jk .
N j=0
For example,
1
φ0 = √ (a0 + a1 + a2 + · · · + aN−1 ) ,
N
1
φ1 = √ a0 + a1 ω + a2 ω 2 + · · · + aN−1 ω N−1 ,
N
1
φ2 = √ a0 + a1 ω 2 + a2 ω 4 + · · · + aN−1 ω 2(N−1) ,
N
..
.
1 2
φN−1 = √ a0 + a1 ω N−1 + a2 ω 2(N−1) + · · · + aN−1 ω (N−1) .
N
These equations can be written as
1 1 1 ... 1
φ0 a0
φ1 1 ω ω2 . . . ω N−1 a1
1 ω 2 ω4 . . . ω 2(N−1)
φ2= a2 . (7.7)
.. .. .. .
.. .. .. ...
. .
. . .
N−1 2(N−1) (N−1)2
φN−1 1ω ω ... ω aN−1
Thus, the discrete Fourier transform can be interpreted as a matrix that acts on a
vector of amplitudes. Although this is useful conceptually, it is just as slow as using
7.7 Discrete Fourier Transform 303
Eq. (7.6) directly, since we must calculate all N 2 terms of the matrix, and then do
the matrix-vector multiplication.
Fortunately, faster classical algorithms for the discrete Fourier transform exist
that only take O(N log N) steps. These are called fast Fourier transform (FFT) al-
gorithms. The precise workings of these algorithms are beyond the scope of this
textbook, but they are used by computer algebra systems, like Mathematica and
SageMath.
First, download the waveform from url. It is a csv file, which stands for comma-
separated values. Its contents are the 44100 (x, y) points of the waveform:
0.00000,-0.46933
0.00002,-0.46011
0.00005,-0.44931
0.00007,-0.41455
0.00009,-0.38632
0.00011,-0.34164
0.00014,-0.28851
...
0.99993,0.12177
0.99995,0.12454
0.99998,0.13571
In the variable wave, the first column of numbers is the time of each sample,
and the second column is the amplitude. We can just get the amplitudes using
wave[[;;,2]], and we can take the discrete Fourier transform of these ampli-
tudes using the Fourier command:
ft = Fourier[wave[[;;,2]]];
Finally, we can plot the first 1000 points of the frequency spectrum using the
following command:
ListPlot[Transpose[{Table[i, {i, 0, 999}], Abs[ft
,→ [[1;;1000]]]}],
PlotRange -> All, Joined -> True]
Exercise 7.28. A one-second recording of a piano playing a triad (a three-note chord), sampled
at 44100 Hz, is available at https://tinyurl.com/43yzv7s3, and the waveform can be
downloaded at https://tinyurl.com/hebe2tj9. Using a computer algebra system, an-
swer the following.
(a) Plot the first 1000 points of the frequency spectrum of the waveform.
(b) Determine the frequencies of the three keys that make up the chord. You can estimate them
off the plot.
(c) Visit https://en.wikipedia.org/wiki/Piano_key_frequencies. Using your
answers to part (b), what are the scientific names of the three keys?
In the last section, we showed in Eq. (7.7) that the discrete Fourier transform can
be written as a matrix-vector multiplication. It turns out that the N × N matrix is
7.7 Discrete Fourier Transform 305
unitary, so it is a valid quantum gate, which we call the quantum Fourier transform
(QFT). Later, we will show how to implement this using single-qubit and two-qubit
quantum gates, which proves that the QFT is unitary. Alternatively, Exercise 7.29
outlines the proof that QFT† QFT = I. Then, if
a0
a1
|ψi = a2 = a0 |0i + · · · + aN−1 |N − 1i
..
.
aN−1
is a normalized quantum state, then applying the QFT yields another normalized
quantum state
φ0
φ1
|φ i = φ2 = φ0 |0i + · · · + φN−1 |N − 1i.
..
.
φN−1
We call |φ i the quantum Fourier transform of |ψi. Put another way, using Eq. (7.6),
the QFT transforms the state
N−1 N−1
1 N−1 N−1 2πi jk/N
|ψi = ∑ a j | ji −→ |φ i = ∑ k φ |ki = √ ∑ ∑ a je |ki.
j=0 k=0 N k=0 j=0
Examining the above equation, the QFT transforms basis states from
1 N−1
| ji −→ √ ∑ e2πi jk/N |ki. (7.8)
N k=0
The QFT is a large quantum gate acting on n qubits, whose general state has
N = 2n amplitudes. We want to implement it, however, using single-qubit and two-
qubit gates. From Section 4.6, the Solovay-Kitaev theorem says we can decompose
a n-qubit gate into a universal gate set up to precision ε using Θ (2n logc (1/ε))
gates, for some constant c. Or, since N = 2n , this is Θ (N logc (1/ε)). Compared to
the classical fast Fourier transform algorithms, which run in O(N log N) time, the
quantum implementation could be a little better or worse by logarithmic factors,
depending on the constant c. This is not the kind of speedup we desire.
Fortunately, there is a more clever way to implement the QFT using single-qubit
and two-qubit gates, and it only takes O(log2 N) of them, which is an exponential
speedup in circuit complexity over the classical fast Fourier transform algorithms.
To construct this implementation, we express j as an n-bit binary number:
j = jn−1 jn−2 . . . j1 j0
= jn−1 2n−1 + jn−2 2n−2 + · · · + j1 2 + j0 .
306 7 Quantum Algorithms
Then, j/N can be represented using a binary point, which is like a decimal point,
but in base 2:
j jn−1 2n−1 + jn−2 2n−2 + · · · + j1 2 + j0
=
N 2n
jn−1 jn−2 j1 j0
= + 2 + · · · + n−1 + n
2 2 2 2
= 0. jn−1 jn−2 . . . j1 j0 .
k = kn−1 kn−2 . . . k1 k0
= kn−1 2n−1 + kn−2 2n−2 + · · · + k1 2 + k0 .
n−1 n−2
= e2πi(0. jn−1 jn−2 ... j1 j0 )kn−1 2 e2πi(0. jn−1 jn−2 ... j1 j0 )kn−2 2 ...
× e2πi(0. jn−1 jn−2 ... j1 j0 )k1 2 e2πi(0. jn−1 jn−2 ... j1 j0 )k0
= e2πi( jn−1 jn−2 ... j1 . j0 )kn−1 e2πi( jn−1 jn−2 ... j2 . j1 j0 )kn−2 . . .
× e2πi( jn−1 . jn−2 ... j1 j0 )k1 e2πi(0. jn−1 jn−2 ... j1 j0 )k0 .
We can drop all the bits left of the binary point. To see why, take for example the
first exponential in the previous line:
n−2 + j n−3 ... j + j /2)k
e2πi( jn−1 jn−2 ... j1 . j0 )kn−1 = e2πi( jn−1 2 n−2 2 1 0 n−1
The exponentials are 1 because they are either e0 = 1 or e2πim = 1 for some positive
integer m. Dropping all the bits left of the binary point, we have
1 N−1
| ji → √ ∑ e2πi jk/N |ki
N k=0
7.7 Discrete Fourier Transform 307
1 N−1
= √ ∑ e2πi(0. j0 )kn−1 e2πi(0. j1 j0 )kn−2 . . .
N k=0
× e2πi(0. jn−2 ... j1 j0 )k1 e2πi(0. jn−1 jn−2 ... j1 j0 )k0 |ki.
Since we are summing over all n-bit binary numbers k, each bit kn−1 , kn−2 , . . . , k0
sums through 0 and 1, so this becomes
1 1
1
√ ∑ . . . ∑ e2πi(0. j0 )kn−1 e2πi(0. j1 j0 )kn−2 . . .
N kn−1 =0 k0 =0
× e2πi(0. jn−2 ... j1 j0 )k1 e2πi(0. jn−1 jn−2 ... j1 j0 )k0 |kn−1 . . . k0 i.
Since |kn−1 . . . k0 i is shorthand for |kn−1 i . . . |k0 i, we can move the terms to get
1 1
1
√ ∑ . . .
N kn−1 =0 k0 =0
∑ e2πi(0. j0 )kn−1 |kn−1 ie2πi(0. j1 j0 )kn−2 |kn−2 i . . .
× e2πi(0. jn−2 ... j1 j0 )k1 |k1 ie2πi(0. jn−1 jn−2 ... j1 j0 )k0 |k0 i.
1 1
√ |0i + e2πi(0. j0 ) |1i √ |0i + e2πi(0. j1 j0 ) |1i . . . (7.9)
2 2
1 1
× √ |0i + e2πi(0. jn−2 ... j1 j0 ) |1i √ |0i + e2πi(0. jn−1 jn−2 ... j1 j0 ) |1i .
2 2
This is another way of stating the definition of the QFT, but in binary. If we can
create a quantum circuit that converts | ji = | jn−1 . . . j0 i to Eq. (7.9), we will have a
quantum circuit for the QFT.
Let us now prove that we can create a circuit for the QFT using Hadamard gates
and controlled rotations. Consider the rightmost term of Eq. (7.9). To begin con-
308 7 Quantum Algorithms
1 1
H| jn−1 i = √ |0i + (−1) jn−1 |1i = √ |0i + (eiπ ) jn−1 |1i
2 2
1 1
= √ |0i + e2πi jn−1 /2 |1i = √ |0i + e2πi(0. jn−1 ) |1i .
2 2
Next, consider a single-qubit gate that rotation about the z-axis of the Bloch sphere
by 2π/2r radians, which we call Rr . It acts on basis states by
Rr |0i = |0i,
r
Rr |1i = e2πi/2 |1i,
Note the order of the outputs is reversed, so we need to reverse the order, such as by
using SWAP gates
j0 ... • ... • ... • H × k0
j1 ... • ... • ... H R2 × k1
.. ..
. .
jn−2 • ... H ... Rn−2 Rn−1 ... × kn−2
jn−1 H R2 ... Rn−1 Rn ... ... × kn−1
This is our quantum circuit for the QFT. For example, with n = 4 qubits,
j0 • • • H × k0
j1 • • H R2 × k1
j2 • H R2 R3 × k2
j3 H R2 R3 R4 × k3
Let us add up the total number of gates in the QFT circuit with n qubits, beginning
with the Hadamard and controlled-Rr gates. The bottom row of the circuit uses n
gates, the row above it uses n − 1 gates, and so fourth, until we get to one gate at the
top row. So, the total number of Hadamard and controlled-Rr gates is
n(n + 1)
n + (n − 1) + (n − 2) + · · · + 3 + 2 + 1 = .
2
The first equality can be obtained by pairing up terms from the outside in. That is,
the first term n and the last term 1 add up to n + 1. Similarly, the second term (n − 1)
and the second-to-last term 2 add up to n + 1. Next, the third term (n − 2) and the
third-to-last term 3 add up to n + 1. Altogether, there are n/2 pairs, so they total
(n/2)(n + 1) = n(n + 1)/2. There are also n/2 swap gates to reverse the order of the
outputs. Altogether, the total number of single-qubit and two-qubit gates is
n(n + 1) n
+ = O(n2 ) = O(log2 N).
2 2
This runtime of O(log2 N) is an exponential speedup over the classical fast Fourier
transform algorithms, which run in O(N log N) time. This speedup, however, comes
310 7 Quantum Algorithms
with a major caveat. With the classical algorithm, we get all the terms of the discrete
Fourier transform. In contrast, with the QFT, we get a quantum state whose ampli-
tudes correspond to the discrete Fourier transform, and we cannot access these am-
plitudes all at once. We can only measure the qubits, which yields a bitstring with
a probability given by the norm-square of the amplitude. Thus, obtaining actual
speedups using the QFT requires clever application of it. Draper’s adder from Exer-
cise 4.28 is one example, and we will see two more examples in the next sections:
phase estimation and factoring.
Exercise 7.29. The quantum Fourier transform was given as a N × N matrix in Eq. (7.7). In this
problem, we will show that the matrix is unitary, so it is a valid quantum gate. Let M = QFT† QFT
and let Mrs denote the element of matrix M at row r and column s. We want to prove that Mrs = 1
when r = s and Mrs = 0 when r 6= s, so M is equal to the identity matrix.
(a) Show that
1 N−1 −kr ks 1 N−1 k(s−r)
Mrs = ∑ ω ω = ∑ω .
N k=0 N k=0
(b) Show that Mrs = 1 when r = s.
(c) Show that Mrs = 0 when r 6= s. Hint: When r 6= s, Mrs is a geometric series. You may need
to look up the geometric series formula from Algebra II. Also note that ω mN = e2πim = 1 for
integer m.
Exercise 7.30. In this exercise, we will use Quirk to simulate the quantum Fourier transform on
six qubits. Go to https://bit.ly/3DNKSxl to access the following quantum circuit:
The first part of the QFT circuit, which calculates the Fourier transform of the bottom qubit, has
been done for you. Note the Z gate is a rotation about the x-axis by π radians, so we have the
following relations:
Exercise 7.31. Using the IBM Quantum Lab, use the following code to create a quantum circuit
for the QFT:
# Number of qubits.
n = 4
qc = QuantumCircuit(n)
# Swap qubits.
for qubit in range(n//2):
qc.swap(qubit, n - qubit - 1)
The inverse quantum Fourier transform (IQFT) does undoes the QFT. Since the
QFT performs the mapping in Eq. (7.8), the IQFT does the reverse:
1 N−1
√ ∑ e2πi jk/N |ki −→ | ji. (7.10)
N k=0
As a quantum circuit, the IQFT can be performed by reversing the order of the gates
the QFT and replacing them with their inverses:
k0 × H • ... • ... • ... j0
†
kn−1 × ... ... Rn† Rn−1 R2† ... H jn−1
Since quantum gates are unitary, the inverses are their conjugate transposes. Note
SWAP† = SWAP, H † = H, and R†r is a rotation about the z-axis of the Bloch sphere
by −2π/2r radians. The IQFT has the same gate complexity as the QFT, which is
O(n2 ).
312 7 Quantum Algorithms
Insert the IQFT circuit in the space provided, and verify that it cancels out the QFT gate that is
already present, so the qubits are unchanged. Use single-qubit gates and controls to construct the
circuit, not the QFT† gate that comes with Quirk. Also, note R†4 = Z −1/8 , and this can be made
with the “Formula Z Rotation” in Quirk, which has a label Z f (t) .
Exercise 7.33. Modify the code in Exercise 7.31 so that it creates a circuit for the IQFT.
Most of the time, when the X-gate is applied to a vector, we get a different vector
as the result. There are some special vectors, however, called eigenvectors, where
applying the X-gate results in the exact same vector, multiplied by a number called
an eigenvalue. For example, |+i is an eigenvector of the X-gate, since if we apply
the X-gate to it, we get |+i multiplied by 1, so its eigenvalue is 1:
√ √
01 1/√2 1/√2
X|+i = = = |+i.
10 1/ 2 1/ 2
Similarly, |−i is an eigenvector of the X-gate with eigenvalue −1, meaning when
we apply the X-gate to it, we get |−i multiplied by −1:
√ √ √
01 1/ √2 −1/√ 2 1/ √2
X|−i = = =− = −|−i.
10 −1/ 2 1/ 2 −1/ 2
When the eigenvector is the state of a quantum system, it is often called an eigen-
state. So, |+i and |−i are eigenstates of the X-gate.
7.8 Phase / Eigenvalue Estimation 313
Since we are promised that |vi is an eigenvector of U. We want to find its eigenvalue,
which takes the form eiθ . Then, we know that multiplying |vi by U will result in |vi
multiplied by eiθ , i.e.,
U|vi = eiθ |vi.
If |vi is an N-dimensional vector and U is an N × N matrix, we can write out this
equation as
U11 U12 . . . U1N v1 v1
U21 U22 . . . U2N v2 v2
iθ
.. .. = e .. .
.. .. . .
. . . . . .
UN1 UN2 . . . UNN vN vN
314 7 Quantum Algorithms
We can use any row to find eiθ . For example, using the first row,
|vi .
|{z}
n qubits
|0 . . . 000i |vi .
| {z } |{z}
m qubits n qubits
So, the total number of qubits in our circuit is m + n. Let us refer to these groupings
as the “eigenvalue register” and the ”eigenstate register,” since the m qubits will
eventually contain an m-bit approximation of the phase of the eigenvalue, and the
n qubits are in the eigenstate |vi. To estimate the phase of the eigenvalue, we apply
the following quantum circuit:
7.8 Phase / Eigenvalue Estimation 315
...
.. .. m−1
..
n qubits |vi . U U2 U4 . U2 . |vi
...
...
|0i H • jm
|0i H • ... jm−1
m qubits |0i H • ... IQFT jm−2
.. .. .. .. .. ..
.
. . . . .
...
|0i • j1
H
Let us go through each step of this circuit to see how it works. First, we apply the
Hadamard gate to each qubit of the eigenvalue register, and we get
1 1 1
|+ + · · · +i|vi = √ (|0i + |1i) √ (|0i + |1i) . . . √ (|0i + |1i) |vi
2 2 2
1
= √ (|0i + |1i) (|0i + |1i) . . . (|0i + |1i) |vi.
2m
Next, we apply a controlled-U gate, where the rightmost qubit of the eigenvalue
register is the control, and the eigenstate register is the target. Since U|vi = eiθ |vi,
this causes the state to acquire a phase of eiθ when the control qubit is |1i:
1
√ (|0i + |1i) . . . (|0i + |1i) (|0i + |1i) |0i + eiθ |1i |vi.
2m
Then, we apply the controlled-U 2 gate, which cause the second-to-rightmost qubit
of the eigenvalue register to acquire a phase of eiθ twice, which is a phase of e2iθ ,
when the control qubit is |1i:
1
√ (|0i + |1i) . . . (|0i + |1i) |0i + e2iθ |1i |0i + eiθ |1i |vi.
2m
From Section 7.7.3, we can ignore the bits to the left of the binary point because
they contribute to multiples of e2πi = 1, so the state is equivalent to
1
√ |0i + e2πi(0. jm ) |1i . . . |0i + e2πi(0. j3 ... jm ) |1i
2m
× |0i + e2πi(0. j2 ... jm ) |1i |0i + e2πi(0. j1 ... jm ) |1i |vi.
Comparing this to Eq. (7.9), this is precisely the QFT of | j1 j2 . . . jm i, so we can find
| j1 j2 . . . jm i by taking the IQFT of the eigenvalue register, resulting in:
| j1 j2 . . . jm i|vi.
This completes the quantum circuit for phase estimation. After measuring these
qubits and obtaining j1 , j2 , . . . , jm , we do a little postprocessing. We calculate
j = 0. j1 j2 . . . jm
j1 j2 jm
= + +···+ m .
2 4 2
Then, the phase of the eigenvalue is θ = 2π j, and the eigenvalue is eiθ .
To estimate the eigenvalue to m bits of precision, we need m Hadamard gates,
m controlled-U p operations, and an IQFT on m qubits that takes O(m2 ) gates. Al-
together, the number of gates is O(m2 ). The classical method takes O(N) = O(2n )
elementary arithmetic operations, so depending on the number of bits of precision
m, the quantum method can be faster, although it assumes we can create |vi and do
controlled-U p operations.
Exercise 7.36. Go to https://tinyurl.com/fdtm5fas to access the following quantum
circuit:
7.8 Phase / Eigenvalue Estimation 317
There are two custom three-qubit gates, v and U. The v gate turns |000i into |vi, which is an
eigenstate of U whose eigenvalue we want to estimate to m = 8 bits. The beginning of the phase
estimation circuit has been started for you.
(a) Using two copies of U, use Quirk’s “Make Gate” ”From Circuit” to create the gate U 2 . Then
using two copies of U 2 , make U 4 . Continuing, make U 8 , U 16 , U 32 , U 64 , and U 128 .
(b) Fill in the rest of the phase estimation circuit. Hint: Use Quirk’s IQFT function, which is
called QFT† , rather than constructing it from scratch.
(c) What is j = 0. j1 j2 . . . j7 as a binary number?
(d) What is j as a decimal number?
(e) What is θ , the phase of the eigenvalue eiθ ?
(f) What is the eigenvalue eiθ .
(g) Explain why your value for eiθ is only an estimate, and the actual value may be slightly
different.
Say we have two eigenstates of U, which we call |v1 i and |v2 i, with corresponding
eigenvalues e2πi j1 and e2πi j2 . Say we are using the previous phase estimation algo-
rithm but prepare the eigenstate register in the following superposition of |v1 i and
|v2 i: √
3 1
|v1 i + |v2 i.
2 2
We also have the m qubits that each start in the |0i state, so the initial state of the
phase estimation circuit is
318 7 Quantum Algorithms
√ ! √
3 1 3 1
|0 . . . 000i |v1 i + |v2 i = |0 . . . 000i|v1 i + |0 . . . 000i|v2 i.
2 2 2 2
Following the same calculation as the previous section, the final state of the phase
estimation circuit is
√
3 1
| j1 j2 . . . jm i|v1 i + j10 j20 . . . jm
0
|v2 i,
2 2
where 0. j1 j2 . . . jm is an m-bit approximation of j1 and 0. j10 j20 . . . jm
0 is an m-bit ap-
proximation of j2 . Then, when we measure the qubits at the end of the circuit, we
get an approximation of j1 with probability 3/4 or an approximation of j2 with
probability 1/4.
Exercise 7.37. Consider three eigenstates of U, |v1 i, |v2 i, and |v3 i, with corresponding eigenvalues
e2πi j1 , e2πi j2 , and e2πi j3 . If we use the phase estimation algorithm but prepare the eigenstate register
in the following state, √
3 1 1
√ |v1 i + √ |v2 i + √ |v3 i,
2 2 2 2 2
what is the probability that we get an approximation to j1 , j2 , and j3 ?
Recall from Section 6.6.2 that “mod” refers to modulus, or the remainder when
dividing. For example, 15 = 3 mod 12 because 15 divided by 12 has a remainder
of 3. This is also how a twelve-hour clock works, as 15 o’clock corresponds to 3
o’clock.
Modular exponentiation is taking powers of a number modulo some other num-
ber. For example, consider powers of 2 taken modulo 7:
20 mod 7 = 1 mod 7,
21 mod 7 = 2 mod 7,
22 mod 7 = 4 mod 7,
23 mod 7 = 8 mod 7 = 1 mod 7,
24 mod 7 = 16 mod 7 = 2 mod 7,
25 mod 7 = 32 mod 7 = 4 mod 7,
26 mod 7 = 64 mod 7 = 1 mod 7,
27 mod 7 = 128 mod 7 = 2 mod 7,
28 mod 7 = 256 mod 7 = 4 mod 7,
7.9 Period of Modular Exponentiation 319
Notice the results are 1, 2, 4, . . . repeated. The period or order r of the modular
exponential is the length of the repeating sequence, so in this example, r = 3. Next,
let us consider another example: powers of 3 taken modulo 10:
Now, the pattern is 1, 3, 9, 7 repeated, and the period is r = 4. In both of these ex-
amples, the repeated sequences started with a 1. This is always true because a0 = 1
for any positive integer a. Furthermore, the modular exponential ax mod N always
follows a repeated pattern as long as a and N are relatively prime (i.e., their greatest
common divisor is 1, so they share no common factors except 1). This fact comes
from a branch of mathematics called called number theory.
Since the repeated sequence always starts with 1, another way to define the period
is as the smallest positive exponent r such that ar mod N = 1 mod N. For example,
with 2x mod 7, r = 3 was the smallest positive exponent to yield 1 mod 7, so it
takes r = 3 terms for the pattern to repeat to 1. For the second example, r = 4 is the
smallest exponent so that 3x mod 10 = 1 mod N. More generally, since the numbers
repeat every r powers, ax+r mod N = ax mod N.
The problem is to find the period of the modular exponential. Since this is a
mouthful, we often just call this problem period finding or order finding. Note the
period r must be less than N, and so the challenge is to find the period for large N.
Exercise 7.38. Calculate enough terms of 4x mod 5, where x = 1, 2, . . . , to see a pattern.
(a) Confirm that 4 and 5 are relatively prime.
(b) What is the sequence that is repeated?
(c) What is the period?
Exercise 7.39. Calculate enough terms of 4x mod 13, where x = 1, 2, . . . , to see a pattern.
(a) Confirm that 4 and 13 are relatively prime.
(b) What is the sequence that is repeated?
(c) What is the period?
320 7 Quantum Algorithms
Finding a single modular exponent is fast using the repeated squaring method. For
example, say we want to find
9143 mod 131.
We do not want to calculate 9143 , as this is a very big number. Instead, we want
to calculate it in pieces, taking it modulo 131 as we go. To do this, we express the
exponent in binary:
43 = 1010112
= 1 · 25 + 0 · 24 + 1 · 23 + 0 · 22 + 1 · 21 + 1 · 20
= 1 · 32 + 0 · 16 + 1 · 8 + 0 · 4 + 1 · 2 + 1 · 1.
This consists of square powers of 91 modulo 131, and we can calculate them by
starting with 911 , then squaring it to get 912 , then squaring it to get 914 , then squar-
ing it to get 918 , and so forth:
By repeatedly squaring, we were able to calculate these using relatively small num-
bers. Plugging these into Eq. (7.11), we get
9143 mod 131 = (125)1 (16)0 (4)1 (129)0 (28)1 (91)1 mod 131
= 125 · 4 · 28 · 91 mod 131
= 1 274 000 mod 131
= 25 mod 131.
To go from the second to the third line, we used 2548 mod 131 = 59 mod 131. Al-
together, the repeated squaring method allows us to compute modular exponentials
we showed that 9143 mod 131 = 25 mod 131, and we were able to calculate this
using relatively small numbers, as opposed to trying to calculate 9143 from the start.
Repeated squaring and other similar methods for calculating modular exponen-
tials have been implemented in computer algebra systems like Mathematica and
SageMath:
• In Mathematica, 9143 mod 131 can be computed using:
PowerMod[91,43,131]
For the computational complexity of the repeated square method, say we are
calculating ax mod N, where x is an n-bit binary number. Then, we start with a and
square it n − 1 times, modulo N. Once we have these, we may have to multiply
them together, which following the progressive approach above takes up to n − 1
multiplications, modulo N. Together, this is (n − 1) + (n − 1) = 2(n − 1) = O(n)
decimal arithmetic operations modulo N. We may be interested in the number of bit
operations, however, rather than decimal operations. Recall from elementary school
that you can multiply two d-digit numbers by multiplying O(d)2 pairs of digits. For
example, to multiply 123 and 456,
322 7 Quantum Algorithms
123
× 456
738
6150
+ 49200
56088
That is, we multiplied each digit of 123 by 6, then multiplied each digit of 123
by 5, and then multiplied each digit of 123 by 4, doing the carries along the way.
Altogether, we multiplied 9 pairs of numbers. Then, we added 9 digits together,
ignoring the zeros that we padded on the right. So, the total number of operations
on digits is 9 + 9 = d 2 + d 2 = 2d 2 = O(d 2 ). Similarly, to multiply two n-bit strings,
this method takes O(n2 ) multiplications of pairs of bits and additions. For example,
to multiply 101 and 110,
101
× 110
000
1010
+ 10100
11110
Exercise 7.40. Use the repeated squares algorithm to calculate 9153 mod 131. Hint: Many of the
numbers were calculated for you in the text. Check your answer using a computer algebra system.
Exercise 7.41. Use the repeated squares algorithm to calculate 8738 mod 197. Check your answer
using a computer algebra system.
U 2 |1i = a2 mod N ,
U 3 |1i = a3 mod N ,
..
.
U r |1i = |ar mod Ni = a0 mod N = 1 mod N ,
1 r−1 E
= √ ∑ e−2πisk/r ak mod N .
r k=0
1 r−1 E
U|vs i = √ ∑ e−2πisk/rU ak mod N
r k=0
1
= √ e−2πis(0)/rU a0 mod N + e−2πis(1)/rU a1 mod N + . . .
r
+ e−2πis(r−2)/rU ar−2 mod N + e−2πis(r−1)/rU ar−1 mod N
1
= √ e−2πis(0)/r a1 mod N + e−2πis(1)/r a2 mod N + . . .
r
+ e−2πis(r−2)/r ar−1 mod N + e−2πis(r−1)/r |ar mod Ni
| {z }
|a0 mod N i
1
= √ e−2πis(r−1)/r a0 mod N + e−2πis(0)/r a1 mod N
r
+ e−2πis(1)/r a2 mod N + · · · + e−2πis(r−2)/r ar−1 mod N .
Note the first coefficient is e−2πis(r)/r = e−2πis = 1 since s is an integer, and since
e−2πis(0)/r = 1, the is equation can be written as
1
U|vs i = e2πis/r √ e−2πis(0)/r a0 mod N + e−2πis(1)/r a1 mod N
r
+ e−2πis(2)/r a2 mod N + · · · + e−2πis(r−1)/r ar−1 mod N
= e2πis/r |vs i.
This way, when z = 0, the target remains unchanged as y, and when z = 1, the target
j
is multiplied by the modular exponential a2 mod N. From the previous subsection,
we have a fast classical method for computing ax mod N that takes O(n2 ) , and we
can convert this into a reversible circuit and hence a quantum gate. In Section 4.5.2,
we discussed methods for this by converting a classical adder into a quantum adder.
The process would be similar, but we would need to discuss how to square integers
and take the modulo using a classical computer first, so to avoid the lengthy discus-
sion, the details are beyond the scope of this textbook. Also, the best way to do this
is also an open research question.
For the second item, we need to prepare an eigenvector of U. A trick is, instead
of preparing a single eigenvector of U, we prepare the following equal superposition
of them:
1 r−1
√ ∑ |vs i.
r s=0
In a moment, we will see that this superposition is easy to construct. First, the
broader picture is that we will use this superposition in the phase estimation al-
gorithm. Since the eigenvalue of |vs i is e2πis/r , the phase estimation will yield an
m-bit approximation to s/r for one s = 0, 1, . . . , r − 1, where each value of s has a
probability of 1/r.
Now, let us show that the equal superposition is easy to construct. Plugging in
the definition of |vs i, the equal superposition becomes
Let us show why the term in parenthesis is r when k = 0 and why it is 0 when k 6= 0.
First, when k = 0, the term in parenthesis is
r−1 r−1 r−1
∑ e−2πisk/r = ∑ e0 = ∑ 1 = r.
s=0 s=0 s=0
This is a geometric series. We could look up the formula from Algebra II, but let us
quickly derive it. Let us call the series S:
S = 1 + ω + · · · + ω r−1 .
If we multiply S by ω, we get
ωS = ω + ω 2 + · · · + ω r .
S − ωS = 1 + ω + · · · + ω r−1 − ω + ω 2 + · · · + ω r
= 1 − ωr.
(1 − ω)S = 1 − ω r .
1 − ωr
S= .
1−ω
Using this formula, let us plug in for ω:
1 − e−2πisk 1−1
S= = = 0,
1 − e−2πisk/r 1 − e−2πisk/r
1 r−1 1
√ ∑ |vs i = ra0 mod N = |1 mod Ni.
r s=0 r
Thus, the equal superposition of the eigenstates |vs i is precisely equal to |1 mod Ni,
which is easily prepared by starting all the qubits as |00 . . . 00i and then applying
an X-gate to the rightmost qubit to yield |00 . . . 01i = |1 mod Ni. Then, from Sec-
tion 7.8.4, if we use the phase estimation algorithm, we get an approximation to the
phase of one |vs i, with s ∈ {0, . . . , r − 1}, with probability 1/r. That is, since |vs i is
an eigenstate of U with eigenvalue e2πis/r , the phase estimation yields 0. j1 j2 . . . jm ,
which is an m-bit approximation to j = s/r.
For example, let us implement this in Quirk to find the order of 3x mod 7. See
https://bit.ly/3otwt4n:
7.9 Period of Modular Exponentiation 327
Quirk has a built-in modular multiplication gate under the “Modular” toolbox. It is
×A
labeled mod R . To use this, we need to specify the values of A and R. We can do
A=# R=#
this using a tool under the “Inputs” toolbox, and the tools look like default and default .
On the above circuit, they look like large gray squares with squared off corners.
Next, gates that multiply by modular powers of A can created using the “Make
Gate” feature, i.e., ×A2 is two copies of ×A, and ×A4 is two copies of ×A2 , etc.
The output of the quantum circuit can be hard to read, so we added a “Chance”
display that we resized across all five qubits of the eigenvalue register. By hovering
the mouse cursor over the Chance display, we can see the probability of various
outcomes for the eigenvalue register:
Here are the most likely outcomes for the eigenvalue register:
328 7 Quantum Algorithms
These are the likely values for our approximation of s/r. For example, we have an
11.4759% chance of measuring the qubits to be |00101i, so 0.00101 is a binary
approximation of s/r. Converting this to decimal, we get that s/r is approximately
0.1562.
Now for the third item, how to we take an approximation to s/r, like 0.1562 from
above, and find s and r? We use a method called continued fractions. A continued
fraction has the form
1
a0 +
1
a1 +
1
a2 +
.. 1
.+
a`
for some non-negative integer `. For example, from the table above, consider the
number 0.1562. To express this as a continued fraction, we begin by expressing
0.1562 as 1562/10000, which we express as a mixed number, i.e., a whole number
0 and fractional part 1562/10000:
1562 1562
0.1562 = = 0+ .
10000 10000
Next, we invert the fractional part to get
1
0+ .
10000
1562
Then, we express this improper fraction as a mixed number, resulting in
1
0+ .
628
6+
1562
Again, we invert the fractional part and then express it as a mixed number:
7.9 Period of Modular Exponentiation 329
1 1
0+ = 0+ .
1 1
6+ 6+
1562 306
2+
628 628
Continuing this, we eventually arrive at
1
0.1562 = 0 + .
1
6+
1
2+
1
2+
1
19 +
8
By listing all the whole numbers, plus the very last denominator, we can write the
continued fraction as [a0 , a1 , . . . , a5 ] = [0, 6, 2, 2, 19, 8].
A computer algebra system can quickly write a number as a continued fraction:
• In Mathematica, we can convert 0.1562 to a continued fraction using the
ContinuedFraction function:
ContinuedFraction[1562/10000]
The reason why we care about continued fractions is they allow us to find rational
approximations to numbers by truncating the continued fraction. These are called
convergents. For example, for 0.1562, the convergents are:
1 97
4rd convergent = [0, 6, 2, 2, 19] = 0 + = ,
1 621
6+
1
2+
1
2+
19
1 781
5th convergent = [0, 6, 2, 2, 19, 8] = 0 + = .
1 5000
6+
1
2+
1
2+
1
19 +
8
In this example, the 5th convergent contains all the terms of the continued fraction,
and so the 5th convergent is exactly 0.1562 = 781/5000 = 1562/10000. We can
also calculate the convergents using a computer algebra system:
• In Mathematica, we can find a convergent using the FromContinuedFraction
function:
FromContinuedFraction[{0}]
FromContinuedFraction[{0, 6}]
FromContinuedFraction[{0, 6, 2}]
FromContinuedFraction[{0, 6, 2, 2}]
FromContinuedFraction[{0, 6, 2, 2, 19}]
FromContinuedFraction[{0, 6, 2, 2, 19, 8}]
must be less than N. Then, looking at the convergents, the best approximation to s/r
such that r < N = 7 is 1/6. Thus, using the convergents of continued fractions, we
were able to guess that s = 1 and r = 6. To check whether our guess is correct, we
can calculate 3r mod 7 and see if we get 1 mod 7:
36 mod 7 = 1 mod 7.
Thus, with this measurement result, we successfully found the period r = 6. Note it
is known that the continued fraction algorithm yields a guess for s and r in O(n3 )
steps, if s and r are n-bit numbers.
From the previous table of significant measurement outcomes of the Quirk circuit
for phase estimation, some other likely estimates for s/r are 0, 0.3438, 0.5, 0.6562,
and 0.8438. For 0, we get s = 0 and no guess for r, so if we get this value, we need
to run the quantum circuit again in hopes for a better outcome. For the other values,
we can use the continued fraction algorithm and get the following guesses for s and
r:
Probability Binary Approx. of s/r Decimal Approx. of s/r Guess of s/r 3r mod 7
16.7963% |00000i 0 N/A N/A
11.4759% |00101i 0.1562 1/6 1
11.4760% |01011i 0.3438 1/3 6
16.7963% |10000i 0.5 1/2 2
11.4759% |10101i 0.6562 2/3 6
11.4760% |11011i 0.8438 5/6 1
For example, for 0.3438, the continued fraction algorithm yields s = 1 and r = 3.
Checking if this guess for the period is correct, we calculate 3r mod 7 = 33 mod 7 =
6 mod 7 6= 1 mod 7, so 3 is not the period. Then, we run the quantum circuit again,
hoping to get a better guess for r. The guess for s and r, and the value of 3r mod 7
for each guess for r, is shown in the above table (see Exercise 7.42). The number
of times we may have to repeat repeat the quantum circuit is small enough that it
does not affect the overall runtime of the algorithm, although a proof of this fact is
beyond the scope of this textbook (see Nielsen and Chuang for it).
Speaking of the overall runtime, a detailed analysis of the errors shows that we
can take m = O(n). Then, the quantum algorithm takes one X-gate to prepare the
eigenvector register in the state |00 . . . 01i, m Hadamard gates, and m controlled-
U power gates, and an IQFT on m qubits. Each of the m controlled-U power gates takes
O(n2 ) gates for a total of O(mn2 ) = O(n3 ) gates. The IQFT takes O(m2 ) = O(n2 )
gates. Finally, the continued fraction algorithm takes O(n3 ) gates. Thus, the gate
complexity of the quantum period algorithm is O(n3 ), which is a polynomial in n,
so it is efficient.
Exercise 7.42. In the text, we considered the example of 2x mod 7, and we found several approxi-
mations to s/r. For each of the approximations 0.3438, 0.5, 0.6562, and 0.8438, do the following:
(a) Express the decimal as a continued fraction.
(b) Find the convergents of the continued fraction.
(c) What is the fractional guess for s/r, and hence, what are the guesses s and r?
332 7 Quantum Algorithms
7.10 Factoring
Say we are given a number N that is the product of two prime numbers p and q.
The goal is to factor N, i.e., to find its factors p and q. In Section 6.6.2, we learned
that the believed difficulty of factoring for classical computers is the basis of RSA
cryptography.
The best known classical algorithm for factoring is the general number field sieve.
Its workings are beyond the scope of this textbook, but to factor an n-bit number, its
runtime is √3 1/3 2/3
e( 64/9+o(1))(ln n) (ln ln n) .
This is an example of a sub-exponential function. It grows faster than polynomial,
so factoring is not efficient for classical computers, but it is also not exponential
because of the natural logarithms.
An efficient quantum algorithm for factoring was invented by Peter Shor in 1994.
(This is the same Peter Shor who invented the Shor code from Section 4.7.4 in
1995.) This means quantum computers, if they can built at scale, can break RSA
cryptography from Section 6.6.2. Historically, this greatly increased the amount of
money for research in quantum computing and is one of the reasons why quantum
computing has developed into the field it is today.
To factor N = pq, Shor’s algorithm consists of the following three steps:
7.10 Factoring 333
1. Pick any number 1 < a < N. See if we were extraordinarily lucky and picked a
multiple of p or q by calculating gcd(a, N). If the gcd is not 1, then the gcd is a
nontrivial common factor of a and N, and so we have found one of the factors
of N. Let us call it p = gcd(a, N). Then, q = N/p, and we are done factoring. If
gcd(a, N) = 1, we continue to the next step.
2. Find the period r of ax mod N. Note this is believed to be hard for classical
computers, but it is efficient for quantum computers using the period finding
algorithm from the previous section. Make sure the period r is even; if it is odd,
go back to step 1 and pick a different a. Also, calculate ar/2 mod N and make
sure it does not equal N − 1; if it equals N − 1, go back to step 1 and pick a
different a. It is known that there is at least a 50% chance of picking a “good”
a that meets both criteria, so we will not have to try too many times. The proof
of this is beyond the scope of this textbook, but Theorem 5.3 of Nielsen and
Chuang has details.
3. Since we calculated the period r in the previous step, we know that ar =
1 mod N. Subtracting 1 from both sides, this means
ar − 1 = 0 mod N.
ar − 1 = kN.
ar − 1 = kpq.
From Step 2, we know that r is even. So, ar/2 is an integer, and ar/2 ± 1 are also
integers. Now, for the product of ar/2 − 1 and ar/2 + 1 to equal kpq, at least one
of the terms ar/2 − 1 or ar/2 + 1 must contain p and/or q as a factor. That is, for
some integers c and d such that cd = k, we have three possibilities for ar/2 − 1
and ar/2 + 1:
Let us show that the first and third cases are not possible, i.e., ar/2 −1 and ar/2 +
1 are not multiples of N. (ar/2 − 1) mod N 6= 0 mod N and (ar/2 + 1) mod N 6=
0 mod N, so neither has N as a factor.
Let us start with (ar/2 − 1) mod N = 0 mod N and show that this equation is
not true. If we add 1 to both sides, we get ar/2 = 1 mod N. We know that r
is the period of ax mod N, however, which means r is the smallest value of x
such that ax = 1 mod N. Thus, it cannot be that ar/2 = 1 mod N, otherwise r/2
would be a smaller value of x such that ax = 1 mod N. Therefore, the equation
(ar/2 −1) mod N = 0 mod N is incorrect, and it must be that (ar/2 −1) mod N 6=
0 mod N, so (ar/2 − 1) does not have N as one of its factors.
Next, let us show that (ar/2 + 1) mod N = 0 mod N is not true. If we subtract 1
from both sides, we get ar/2 mod N = −1 mod N. Recall that the modulus works
in a “cyclical” fashion. For example, with a 12-hour clock, 15 o’clock corre-
sponds to 3 o’clock. Similarly, −1 o’clock corresponds to 11 o’clock. Thus, our
modular equation becomes ar/2 mod N = N − 1 mod N. This is not true, how-
ever, because in Step 2, we made sure that ar/2 mod N 6= N − 1 mod N. Thus,
ar/2 + 1 also does not have N as one of its factors.
Thus, only the second case is possible:
This means ar/2 − 1 and ar/2 + 1 each share a nontrivial factor with N = pq, and
we can obtain them using the greatest common divisor:
p = gcd(ar/2 − 1, N),
q = gcd(ar/2 + 1, N).
Exercise 7.46. Use Shor’s algorithm to factor N = 209. Say we pick a = 22.
(a) Show that gcd(a, N) 6= 1.
(b) What are the factors of N?
7.11 Summary
For many algorithms in classical and quantum computing, it is easier to find the
query complexity of the algorithms, which is the number of calls/queries of a func-
tion/oracle. Some problems are naturally framed in query complexity, like brute-
force searching, for which Grover’s algorithm provides a quadratic speedup. The
most accurate way to quantify the complexity of an algorithm, however, is counting
the number of elementary gates, but this circuit complexity can differ depending on
what gates are permitted, and even then it is difficult to know if a circuit can be
simplified. Despite this, quantum computers are known to provide speedups in gate
complexity for several problems, such as discrete Fourier transforms, estimating the
phase of eigenvalues of unitary matrices, finding the period of modular exponents,
and factoring. Factoring, in particular, is very relevant to the real-world, as it under-
pins RSA cryptography.
Chapter 8
Next Steps
337
338 8 Next Steps
computers can be used for each of their businesses. If they wait for fault-tolerant
quantum computers to be built before investigating their uses, they will be left
behind by competitors.
These companies are desperately trying to hire qualified individuals. Some of the
jobs are quantumly technical, such as building quantum computers and developing
quantum algorithms. Other jobs are classically technical. For example, web pro-
grammers and software engineers were needed to create the IBM Quantum Expe-
rience website, and these jobs require little or no prior experience with quantum
computing. As another example, electrical engineers with experience with radio-
frequency devices can easily pivot to helping to build superconducting qubits, where
radio frequency interactions are very important. Still, other jobs are non-technical.
Companies need accountants, marketers, experts in human resources, business ad-
ministrators, and more who may not need any knowledge of quantum computing at
all, although a general understanding may be useful. All this is to say that if you
want a job related to quantum computing but do not have the quantum skills, yet,
there are job opportunities that utilize non-quantum skills.
More universities are also hiring professors in quantum computing, recognizing
the growth of the field. In 2017, the Division of Quantum Information was formed
in the American Physical Society, placing it alongside well-established areas of
physics like astrophysics, condensed matter physics, and particles and fields.
While many students are aware of industrial and academic jobs, often, students
have little exposure to government careers. To ignore government jobs, however,
is to ignore a large sector of the quantum computing ecosystem. For example, the
U.S. Department of Defense is the largest employer of scientists and engineers in
the United States, and many of these are civilian jobs in research laboratories. In-
creasingly, government laboratories are hiring people to investigate how quantum
computers affect the missions of their organizations. Besides technical roles, experts
in quantum computing are also needed for program management, policy, and advis-
ing roles to help the government prioritize quantum computing research and work-
force development. Government jobs typically come with good non-salary benefits,
including retirement pensions and work-life balance. There are also many jobs at
national laboratories. Although national laboratories are funded by the government,
they are managed by contractors, so employees at national laboratories are typically
employees of the contractors and are not government employees. But, many their
jobs are similar in that they can be mission-focused.
If you want a quantumly technical job, you will likely want to study quantum
computing, mathematics, and physics beyond this introductory textbook, and some
suggestions for possible next steps are next.
As stated in the preface and throughout this textbook, Nielsen and Chuang’s text-
book, Quantum Computation and Quantum Information is the standard advanced
8.2 Technical Next Steps 339
textbook and will dive deeply into many of the results that were out of the scope of
this introductory textbook, such as proving the Solovay-Kitaev theorem about uni-
versal sets of quantum gates, and calculating the probability that the quantum phase
estimation algorithm will yield the wrong answer. There are plenty of resources
on the internet as well, including lecture notes from professors, video lectures, and
tutorials.
A major concept that one should learn are mixed states. In this entire textbook,
the state of a qubit was |0i, |1i, or some superposition of |0i and |1i. For all of these,
the state can be known with certainty, even though the measurement outcome may
be probabilistic. These states were visualized as points on the Bloch sphere. These
are called pure states. In contrast, if we are not sure if a qubit is on one pure stare
or another pure state, then the state itself and not just its outcome is probabilistic.
These are called mixed states, and they can be visualized as a point inside the Bloch
sphere, so in this context, it is actually a Bloch ball, which also contains the inside
and not just the surface. While a pure state was written using vectors (with “kets”
being column vectors and “bras” being row vectors), a mixed state is written using a
matrix called an density matrix. Some of the topics that we covered in this textbook
should actually be done in terms of mixed states, namely quantum error correction,
the no-signaling principle, and aspects of entanglement. Again, Nielsen and Chuang
goes into detail about all this.
If you are a student, you may wonder what courses or majors to consider. Physics,
computer science, mathematics, and engineering are all fine. If you are interested in
quantum hardware, experimental physics or electrical engineering are good choices.
If you are interested in the theoretical side of quantum computing and quantum algo-
rithms, then theoretical physics, computer science, or mathematics are good choices.
Personally, I work on quantum algorithms. My PhD was in theoretical physics, but
my PhD advisor was a mathematician. Then, I did two postdoctoral research fellow-
ships in computer science before landing a tenure-track job as a physics professor.
So, my own story is a good illustration of the interplay between physics, mathemat-
ics, and computer science.
For students, a great way to learn quantum computing is to do research with a
professor. Look around at your university to see if any professors work on quantum
computing, and do not be afraid to look outside of your department. Remember my
story, that I was a physics student who was advised by a math professor. If there
are no professors at your university who are interested in quantum computing, see if
any professors are interested in learning about quantum computing, and if you can
do an independent study with them, where you read Nielsen and Chuang or some
other educational resource and teach the professor what you have been learning.
This way, if you apply for graduate school, there is a professor who can write you
a letter of recommendation and explain that you have been self-learning quantum
computing and even teaching them. Another way to gain research experience is to
apply for summer internships and research fellowships. Many quantum computing
companies, as well as government and national labs, host students for summer re-
search. There is also the Research Experiences for Undergraduates (REU) program,
340 8 Next Steps
and some of the universities may have quantum computing research projects for
students.
There are some make broad statements like, “You don’t need to know so and so
to do quantum computing.” For example, a common critique among some computer
scientists is that you do not need to know Schrödinger’s equation, the fundamental
physics equation of quantum mechanics, to do quantum computing. In some sense,
that is true, as this is literally the first time I have mentioned Schrödinger’s equation
in this textbook. But, that is because this textbook builds up to quantum circuits
and algorithms. There are many other parts of quantum computing where know-
ing Schrödinger’s equation is necessary, such as for physical quantum hardware,
for analog quantum algorithms like continuous-time quantum walks, and for many
optimization algorithms like those based on quantum annealing or the quantum ap-
proximate optimization algorithm (QAOA). You may want to learn Schrödinger’s
equation yourself, depending on what aspects of quantum computing you wish to
pursue. If you major in physics, you will certainly come across it in a Modern
Physics course and in a Quantum Mechanics course. I bring this up to say there
is no one right path to be come a quantum information scientist. If you want to take
a traditional physics approach through Schrödinger’s equation, that is fine. If you
want to take an alternative approach, that is also fine. Just because your path does
not look like someone else’s does not mean it is the wrong path for you.
8.3 Questions
I regret that I do not have the capacity to respond to individual questions, nor do I
have the expertise to answer most questions beyond my specific research area. So,
if you have any questions, I suggest submitting them on the Quantum Computing
Stack Exchange at
https://quantumcomputing.stackexchange.com/
Many members of our community volunteer their expertise on the website to help
others, including those who are newer to the field.
As we end our journey together, I again want to celebrate your completion of this
textbook. You did it! I hope you will consider quantum computing as a potential
career. If you do become a quantum information scientist, please let me know. I
would be delighted to hear that I played a role in your journey. Also, I wrote this
textbook for my students, that it might help them in their learning. By completing
this textbook, you have, in some way, also become one of my students, and so this
textbook is also dedicated to you.
Answers to Exercises
Exercises of Chapter 1
341
342 Answers to Exercises
A AB
AB + AB
B AB
A
A
B
B
C
C
ABC
ABC
ABC
ABC
.
1.20 Answer varies. One answer is ABC + ABC = ABCABC:
344 Answers to Exercises
A
A
B
B
C
C
ABC
ABC
1.21 In the text, the OR gate was implemented using three NOT gates and one AND
gate. Each NOT gate can be implemented using a single NAND gate, and the AND
gate can be implemented by one NAND gate and one NOT gate. The last two NOT
gates cancel out, however, yielding the following:
A
A+B
B
.
1.22 Start with {NOT, AND, OR}:
A
B
AB + AB
A
B
AB + AB
1.23
A
AB
B
ABC
C
1.24
A B A B A+B A+B
0 0 1 1 1 0
0 1 1 0 1 0
1 0 0 1 1 0
1 1 0 0 0 1
1.25 (a) (b)
A
A Output Output
B
1.26 Changing the OR to an XOR does not change the logic, so the truth table stays
the same.
1.27 S = 0000 and Cout = 1. Together, this is 10000, or 16, which is correct. In
decimal, we expect 9 + 7 = 16.
1.28 11100110.
1.29 The outputs are all 1. Makes sense because B + B = 1, and A + 1 = 1.
1.30 A + B.
1.31 A + B +C.
1.32 ABC + ABC + ABC = B(A +C).
1.33 (a) ABC + ABC + ABC + ABC. (b) AB + AC.
1.34 (a) Irreversible. (b) Irreversible. (c) Irreversible. (d) Irreversible.
1.35 Reversible.
346 Answers to Exercises
Gate f (A)
f (A) ⊕ B
B
A⊕B⊕C
C
AB
AB ⊕ D
D
1.41
Cin Cin
A A
B B
S
FA
Cout
S⊕D
D
Cout ⊕ E
E
1.42 (a) 213 = 8192. (b) single event upset. (c) miniaturized. (d) cosmic rays, parti-
cles, black holes. (e) cascade, transistor. (f) error correction code. (g) month, cosmic
rays. (h) 10 to 30. (i) 161. (j) flash, every star.
1.43 (a) Even. (b) No. (c) Yes. The parity of the first seven bits doesn’t match the
parity bit. (d) No. If two bits flipped, the parity would be unchanged.
348 Answers to Exercises
1.44 (a) Yes, the middle bit has flipped. (b) Yes, the left bit has flipped. (c) 0.128.
Decrease, since 0.128 < 0.2. (d) 0.544. Increase, since 0.544 < 0.2.
1.45 (a) Yes, b1 was flipped. (b) Yes, b2 and b1 were flipped. (c) 10p3 + 5p4 + p5 .
(d) 0 < p < 0.294. (e) 0.0105, decreases. (f) For 3-bit code, the probability of an
uncorrectable error is 0.031, which is more likely than the 5-bit code’s 0.0105.
1.46 (a) f (100). (b) g(100). (c) They are equal when n = 477, after which g(n) is
greater than f (n). (d) true, true, false, false, false.
1.47 Possibilities are (a) iii or v, (b) i or v, (c) ii or v, (d) v, (e) i or iv. Thus, the only
correct answer is (a) iii, (b) i, (c) ii, (d) v, (e) iv.
1.48 (a) Bounded-Error Probabilistic Polynomial-Time. (b) “As the class of feasible
problems for a computer with access to a genuine random-number source.”
1.49 Many possible answers, including factoring, graph isomorphism, n × n Su-
doku, traveling salesman, Hamiltonian path, and bin packing.
1.50 (a) Yang–Mills and Mass Gap, Riemann Hypothesis, P vs NP Problem,
Navier–Stokes Equation, Hodge Conjecture, Poincaré Conjecture, and Birch and
Swinnerton-Dyer Conjecture. Only the Poincaré Conjecture is solved. (b) Stephen
Cook and Leonid Levin in 1971.
1.51 Answers vary.
1.52 Answers vary.
1.53 (a) . 0 0 1. (b) . 0 1 1. (c) . 1 0 1. (d) . 1 1 0. (e) NAND. (f) Calculates the
NAND of all the bits.
1.54 Answers may vary. Here is one:
1.55 (a) Run forever. (b) Halt. (c) By returning true, H is saying that Z halts. But
then Z responds to this by running forever. Z can’t both halt and run forever. That’s
a contradiction. (d) By returning false, H is saying that Z runs forever. But then Z
responds to this by halting. Z can’t both run forever and halt. That’s a contradiction.
1.56 (a) complete, consistent, decidable. (b) programs, themselves. (c) Can every
even number greater than 2 be written as the sum of two primes? (d) Runs forever.
(e) Halts. (f) Runs forever. Yes, it’s a contradiction. (g) Runs forever. Yes, it’s a
contradiction. (h) undecidable, can’t solve.
Answers to Exercises 349
Exercises of Chapter 2
2.1 (a) 1. (b) 1. (c) 0. (d) +. (e) 0. (f) −i. (g) 0. (h) 0.
2.2 (a) −i. (b) i. (c) −i. (d) −i. (e) −. (f) 0. (g) i. (h) 0 or 1 depending on die roll.
2.3 (a) The north pole. (b) The south pole. (c) The equator. (d) The northern hemi-
sphere. (e) The southern hemisphere.
2.4 (a) 1. (b) 2. (c)
Im
Re
Re
1
√ (|0i − |1i)
2 y
x
√ √ √ √
2.15 (a) (θ , φ ) = (120◦ , 45◦ ) = (2π/3, π/4). (b) (x, y, z) = ( 3/2 2, 3/2 2, −1/2).
(c)
z
y
x
2.17 (a) polarization, decoherence. (b) electric fields. (c) laser beams. (d) molecule,
radio-frequency, one-qubit. (e) discrete energy levels, atomic nucleus. (f) Quantum
information, hyperfine. (g) spin, microwave, optical. (h) charge, flux, phase.
2.18 Answers vary.
2.19 Answers vary.
2.20 Answers vary. One is the Josephson junction is used to create a superconduct-
ing flux qubit, which uses currect as its qubit. Clockwise current corresponds to |0i,
and counterclockwise current corresponds to |1i.
2.21 (a) (α + β )|0i + (α − β )|1i. (b) No, the total probability is 2, which is not
possible.
√ √ √ √
− 3−3i
2.22 (a) α32 + 3+i 4 β |0i+
3+i
4 α+ 4 β |1i. (b) No, the total probability
is 2, which is not possible.
2.23 (a) Yes, reversible. (b) No, not reversible.
2.24 (a) Yes, reversible. (b) No, not reversible.
2.25 ZX(α|0i + β |1i) = Z(β |0i + α|1i) = β |0i − α|1i
2.26 (a) XZXZ|0i = −|0i, XZXZ|1i = −|1i. (b) ZXZX|0i = −|0i, ZXZX|1i =
−|1i.
2.27 (a) α|0i + β eiθ |1i. (b) |α|2 + |β eiθ |2 = |α|2 + |β |2 = 1.
√ 2 α+β
2
√ |0i + α+β
2.28 (a) α+β √ |1i. (b) α+β + √ = |α|2 + |β |2 = 1.
2 2 2 2
2.29 Answers vary.
2.30 Y H|0i = Y |+i = −i|−i.
2.31 HXH|0i = Z|0i = |0i and HXH|1i = Z|1i = −|1i.
h √ √ i
2.32 (a) 2√1 2 (1 − i + eiπ/4 )|0i + (1 + i)|1i = 2√1 2 1 + 2 + (1 − 2)i |0i + (1 + i)|1i .
If γ = π/8, get U|0i = |0i and U|1i = eiπ/4 |1i, which is the T gate.
352 Answers to Exercises
y y
x x
√ √ √
(c) (1/ 3, 1/ 3, 1/ 3). (d) −ieiγ √13 (X +Y + Z). (e) −ieiγ √13 [|0i + (1 + i)|1i]. (f)
−ieiγ √13 [(1 − i)|0i − |1i].
2.35 (a,b)
(c) 85.4% probability of getting |0i, and 14.6% probability of getting |1i.
Exercises of Chapter 3
!
1
3.1 √2 .
3
2
3.2 |0i with probability 3/4 and |1i with probability 1/4.
√ √
3.3 (a) 23 h0| + 12 h1|. (b) 23 12 . (c) 32 h0| + 1+2i 2 1+2i
3 h1|. (d) 3 3 .
√ √ √ √
3.4 (a) (3 + 2 15 − i 3)/16. (b) (3 + 2 15 + i 3)/16. (c) Complex conjugates.
√
3.5 (a) 13|A|2 . (b) A = 1/ 13.
0
3.6 (a) h+|−i = 1 0 = 0, so orthogonal.
1
√
(b) h0|+i = 1/ 2 6= 0, so not orthogonal.
(c) The inner product is 0, so orthogonal.
√ √
3.7 (a) x = (−3 + i 3)/8. (b) x = eiθ 15/4. (c) none.
3.8
Answers to Exercises 353
ha|bi = cos(θa /2) cos(θb /2) + ei(θb −θa ) sin(θa /2) sin(θb /2)
= cos(θa /2) cos(θb /2) − sin(θa /2) sin(θb /2)
= cos((θa + θb )/2) = cos(π/2) = 0.
√ √ √ √
3.9 (a) ( 3 − i)/2 2. (b) ( 3 + √i)/2 2. (c) |ii with probability 1/2, |−ii with
probability 1/2. (d) (3 − i)/4. (e) 3(1 − i)/4. (f) |ai with probability 5/8, |bi with
probability 3/8.
3.10 (a) |0i with probability 3/4 and |1i with probability 1/4. (b) √ |+i with proba-
bility 7/8 and |−i with probability
√ 1/8. (c) |ii with probability (4 + 3)/8 ≈ 0.717,
|−ii with probability (4 − 3)/8 = 0.283.
1−i
3.11 (a) √ |+i − √i |−i.
3 3
(b) 1−3i
√ |ii − 1−i
2 3
√ |−ii.
2 3
3+i 1−i
3.12 2√1 3 .
−(1 + i) 3 − i
3+i 1−i
3.13 (a) U = 2√1 3 . (b) See https://bit.ly/3qR5HnR. |0i with
−(1 + i) 3 − i
probability 83.3%, |1i with probability 16.7%.
1 1 −1
3.14 √2 .
1 1
3.15
√
1 1 1 1 0 1 2−i √ 1 1
HTU|0i = √
2 1 −1 0 eiπ/4 2 −1 2+i 0
iπ/4
√
1 2−i−e
= √ √ .
2 2 2 − i + eiπ/4
3.16 (a) XY |0i = i|0i, iZ|0i = i|0i. XY |1i = −i|1i, iZ|1i = −i|1i. (b)
10 0 −i i 0 1 0 i 0
XY = = , iZ = i = .
01 i 0 0 −i 0 −1 0 −i
1 i
3.17 U †U = 6= I, so no.
−i 1
3.18 U †U = I, so yes. U|0i = |ii, and U|1i = |−ii.
3.19 (a) √ √ √ √ !
1+√ 3 −1+
− i 1−√3 √ 3 − i 1−√ 3
U −1 = U † = 2 √2 2 6
√ 2√6 2 √6
1−√ 3
− i 1−√ 3 1+√ 3 − i −1+
√ 3
2 6 2 6 2 2 2 6
√
3 1
(b) |ψi = 2 |0i + 2 |1i.
354 Answers to Exercises
1 1 −1
3.20 (a) 2 . (b) No, not unitary.
i −i
1 1
3.21 (a) √12 . (b) Yes, it is unitary.
1 −1
3.22
1 1 1 1 1 1 10
√ √ 11 +√ √ 1 −1 = .
2 1 2 2 −1 2 01
3.23
1 1 1 1 1 31
10 +√ √ 11 = .
0 2 1 2 2 11
Exercises of Chapter 4
4.1 (a) When the other player is on ONE. (b) To planet Phi Minus. (c) When the
blue player uses an H engine card.
4.2 (a) 5. (b) Conceptual. (c) 50-60%. (d) Entanglement.
4.3 (a) 0. (b) −1/2. (c) 0.
0 0 1
4.4 |1i ⊗ |1i ⊗ |0i = ⊗ ⊗ .
1 1 0
1/2
0 √ √
√
4.5 (a) |ψi = i/ 2 . (b) hψ| = 1/2 0 −i/ 2 ( 3 − i)/4 .
√
( 3 + i)/4
4.6
1 0 0 0
0 1 0 0
1 0 0 0 + 0 1 0 0 + 0 0 1 0 + 0 0 0 1
0 0 1 0
0 0 0 1
1000
0 1 0 0
=0 0 1 0 .
0001
4.7 |00i with probability 1/10, |01i with probability 1/2, |10i with probability
1/10, or |11i with probability 3/10.
√
4.8 A = 2/ 17.
Answers to Exercises 355
√ q
4.9 √15 |00i + √25 |01i with probability 5/16 or 2√112 |10i + 11
3
|11i with probability
11/16.
√ √ √ √
4.10 (|000i + 6|010i)/ 7 with probability
√ 7/36, ( 2|001i + 3|011i)/√ 11 with
probability 11/36, (|100i+|110i)/ 2 with probability 1/18, or (|101i+ 3|111i)/2
with probability 4/9.
4.11 (a) Entangled state. (b) Product state. |1i ⊗ √12 (|0i + i|1i) = |1i ⊗ |ii.
√ √
4.12 (a) Product state. 23 |0i + 12 |1i ⊗ 23 |0i − 12 |1i . (b) Entangled state.
1000 0001 0 0 −1 0 0 0 1 0
4.16 (a) (b) It is the same.
A BC A0 B0 C0
0 0 0 0 0 0
0 0 1 0 0 1
0 1 0 0 1 1
0 1 1 0 1 0.
1 0 0 1 0 1
1 0 1 1 0 0
1 1 0 1 1 0
1 1 1 1 1 1
0 1 0 0
1 0 0 0
4.17 As matrices, both circuits are equal to
0
.
0 1 0
0 0 0 1
4.18 CNOT|+i|−i = |−i|−i, CNOT|−i|+i = |−i|+i, CNOT|−i|−i = |+i|−i.
4.19
356 Answers to Exercises
1 1 0
1 0 1 1 0 1 1 1 1
√ √ 1 0 0 1 + √ √ 1 0 0 −1 + √ √ 0 1 1 0
2 0 2 2 0 2 2 1 2
1 −1 0
0 1000
1 1 √1 0 1 −1 0 = 0 1 0 0 .
+√
2 −1 2 0 0 1 0
0 0001
1 0 0 0
0 1 0 0
4.20 cZ =
0
.
0 1 0
0 0 0 −1
4.21 (a) SWAP|ω0 i = |ω3 i. (b) (X ⊗ I)|ω1 i = |ω3 i. (c) CNOT01 |ω2 i = |ω0 i. (d)
CNOT|ω3 i = |ω2 i.
1 0 0 i
0 1 −i 0 8
4.22 (a) MS = √12
0 −i 1 0. (b) MS = I.
i 0 0 1
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
4.23
.
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
4.24 (a) Yes, since they are orthogonal. (b) Yes, since they are orthogonal. (c) No,
since they are not orthogonal.
4.25 See https://bit.ly/30LBtIi:
Answers to Exercises 357
4.27 (a) After all the carries, but before the CNOT, the bottom four qubits are
c03 , a3 , a3 ⊕ b3 , and s4 . Taking the CNOT of c03 and a3 ⊕ b3 changes the target to
s3 = a3 ⊕ b3 ⊕ c03 .
(b) The number of C gates is n, and the number of C† gates is (n − 1). Both C and C†
each have two Toffoli gates, so there is a total of 2n+2(n−1) = 4n−2 Toffoli gates.
The C and C† gates each have one CNOT gate, and the n − 1 S gates each have two
CNOT gates, plus the extra CNOT, for a total of n + (n − 1) + 2(n − 1) + 1 = 4n − 2
CNOT gates.
4.28 The final state should be |si|bi = |1010i|0011i. The circuit can be viewed in
Quirk at https://bit.ly/3omghlU, and a picture is below:
358 Answers to Exercises
4.29 (a) 14 Toffoli gates and 16 CNOT gates. (b) 30 Toffoli gates and 32 CNOT
gates.
4.30 3n + 1.
4.31 (a) Tom Wong, the author of the textbook. (b) hands, quantum, some, no faster.
4.32 (a) Missing complex amplitudes.
(b) Cannot generate entanglement (no 2-qubit gates).
(c) Cannot generate entanglement. SWAP is 2-qubit, but cannot generate entangle-
ment.
4.33 Possible answers are replacing CNOT with Toffoli, H with Rπ/8 , or S with T .
4.34
CNOT2,1 CNOT2,0
|ψ00i = α|000i + β |100i −−−−−→ α|000i + β |110i −−−−−→ α|000i + β |111i.
4.35 (a) Nothing. (b) Apply X then Z to the rightmost qubit. (c) Apply X then Z to
the leftmost qubit. (d) Apply X then Z to the middle qubit.
4.36
CNOT2,1 CNOT2,0
|ψ00i = α|000i + β |100i −−−−−→ α|000i + β |110i −−−−−→ α|000i + β |111i
H ⊗3
−−→ α|+ + +i + β |− − −i.
4.37 (a) |0 + +i. (b) |1 − +i. (c) |1 + −i. (d) |0 − −i. (e) It outputs 0 when there is
an even number of |−i’s, and outputs 1 when there is an odd number of |−i’s.
4.38 In the top row, the Hadamard gate can be moved to the end of the circuit. In
the next row, H 2 = I, so we can remove the Hadamard gates in the middle. In the
next row, we can move the Hadamard gate to the beginning of the circuit.
4.39 (a) Probability 1 − ε 2 , and the resulting state is α|+ + +i + β |− − −i, so there
is no error to correct.
(b) Probability ε 2 , and the resulting state is α|− + +i − β |+ − −i, so we apply
Z ⊗ I ⊗ I to get α|+ + +i − β |− − −i, then X ⊗ I ⊗ I to get α|+ + +i + β |− − −i.
(c) Probability zero.
(d) Probability zero.
4.40 (a)
Answers to Exercises 359
(b)
H ,H ,H
−−8−−5 2
−→ α|+i|00i|+i|00i|+i|00i + β |−i|00i|−i|00i|−i|00i
1 1 1
= α √ (|0i + |1i) |00i √ (|0i + |1i) |00i √ (|0i + |1i) |00i
2 2 2
1 1 1
+ β √ (|000i − |100i) √ (|000i − |100i) √ (|000i − |100i)
2 2 2
α
= √ (|000i + |100i) (|000i + |100i) (|000i + |100i)
2
β
+ √ (|000i − |100i) (|000i − |100i) (|000i − |100i)
2
(c)
CNOTs α
−−−−→ √ (|000i + |111i) (|000i + |111i) (|000i + |111i)
2
β
+ √ (|000i − |111i) (|000i − |111i) (|000i − |111i)
2
= α|0L i + β |1L i.
4.45 (a) Qubits |q7 i, |q3 i, and |q1 i flipped. Fix by applying the X-gate to each one.
(b) Triplet0 has a phase flip. Fix by applying a Z-gate to any one of |q2 i, |q1 i, or
|q0 i.
Exercises of Chapter 5
5.4 (a)
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
Answers to Exercises 361
h q[2];
cx q[2], q[1];
cx q[0], q[1];
h q[0];
cx q[1], q[2];
cz q[0], q[2];
measure q -> c;
(b)
Exercises of Chapter 6
6.1 |0i with probability 3/4, and the state collapses to √1 (|00i + |01i) = |0i √1 (|0i + |1i).
√ 2 √ 2
3 1 3 1
|1i with probability 1/4, and the state collapses to 2 |10i+ 2 |11i = |1i 2 |0i + 2 |1i .
Partially entangled.
6.2 |0i with probability 1/2, and the state collapses to |01i. |1i with probability
1/2, and the state collapses to |10i. Maximally entangled.
6.3 (a) right now. (b) space, space.
6.4 (a)
1 h √ i 1 + √2 + i
iθ
HT HS p √ (1 + 2)|0i + |1i = p √ |0i = e |0i,
4+2 2 4+2 2
1
where θ = tan−1 √ ,
1+ 2
1 h √ i 1 − √2 + i
iθ
HT HS p √ (1 − 2)|0i + |1i =p √ |1i = e |1i,
4−2 2 4−2 2
−1
√
where θ = π − tan 1+ 2 .
(b)
362 Answers to Exercises
1 h √ i −1 − √2 + i
† iθ
HT HS p √ (−1 − 2)|0i + |1i = p √ |0i = e |0i,
4+2 2 4+2 2
√
where θ = π + tan−1 1 − 2 ,
1 h √ i −1 + √2 + i
HT † HS p √ (−1 + 2)|0i + |1i = p iθ
√ |1i = e |1i,
4−2 2 4−2 2
−1
√
where θ = tan 1+ 2 .
√ √ √ √
2− 2 2+ 2 2+ 2 2− 2 −1
6.5 (a) 8 . (b) 8 . (c) 8 . (d) 8 . (e) √
2
.
6.6 (a) photons. (b) −2 ≤ S ≤ 2. (c) S = 2.70 ± 0.05. (d) S = 2.697 ± 0.015. (e)
disagree.
6.7 (a) electron. (b) detection loophole. (c) 1.3 kilometers. (d) CHSH-Bell inequal-
ity, S ≤ 2. (e) 2.42 ± 0.20. (f) 0.039.
6.8 √1 (|++i + |−−i).
2
6.9 (a) realism. (b) superposition. (c) incomplete, hidden variables. (d) locality, Lo-
cality, speed of light. (e) entangled pair, instantly, local hidden variables (f) forces
(g) hidden variables, Nothing, no correlation (h) defined values, spooky action (i)
Bell inequalities, (j) violated, faster than (k) local hidden variables (l) realism, lo-
cality (m) consistent, information (n) Copenhagen
6.10 (b) |000i or |111i, each with probability 1/2.
(c) No. |000i and |111i are product states.
6.11 (b) √1 (|001i + |010i) with probability 2/3 or |100i with probability 1/3.
2
(c) √1
(|001i + |010i) = |0i|Ψ + i, so there is entanglement between the right two
2
qubits. |100i is not entangled.
6.12 hΦ + |Φ + i = 1, hΦ + |Ψ + i = 0, hΦ + |Φ − i = 0, hΦ + |Ψ − i = 0, hΨ + |Ψ + i = 1,
hΨ + |Φ − i = 0, hΨ + |Ψ − i = 0, hΦ − |Φ − i = 1, hΦ − |Ψ − i = 0, hΨ − |Ψ − i = 1.
6.13 (a) 4. (b) 2. (c) 4.
6.14 (a) |11i. (b) Answers vary. (c) In the middle section, instead of applying X and
Z, Alice simply applies X.
6.15 (b)
(c)
Answers to Exercises 363
6.16 (b)
(c)
6.17 (b)
(d)
Exercises of Chapter 7
7.1 (a) (b) Yes, since the outputs are unique. (c)
x y x y ⊕ f (x) 0 1 0 0
0 00 1 1 0 0 0
Uf =
.
0 10 0 0 0 1 0
1 01 0 0 0 0 1
1 11 1
(d) U f†U f = I.
7.2 (a) f (0) = 1:
(b) f (1) = 0:
7.3 (a) |xi|+i. (b) |xi|+i. (c) They are equal to the initial state, so the oracle does
nothing to |xi|+i.
h i h i
7.4 √12 (−1) f (0) |0i + (−1) f (1) |1i = (−1) f (0) √12 |0i + (−1) f (1)− f (0) |1i . (a) (−1) f (0) |+i.
(a) (−1) f (0) |−i.
7.5 (a) The parity is 0 or even. See https://bit.ly/3s9rmsa:
s = 100101. (b) Set |yi = |0i. Then, |y ⊕ f (b5 , . . . , b0 )i = | f (b5 , . . . , b0 )i. Get s0 =
f (000001) = 1, s1 = f (000010) = 0, s2 = f (000100) = 1, s3 = f (001000) = 0, s4 =
f (010000) = 0, s5 = f (100000) = 1. (c) 6.
7.15 (a) See https://ibm.co/3m0WPZB:
7.17 The x, y pairs are 000 and 010, 001 and 011, 100 and 110, and 101 and 111.
There are many different possible truth tables for f (x) that satisfies f (x) = f (y).
One is:
Answers to Exercises 367
x y
000 100
001 101
010 100
011 101
100 011
101 000
110 011
111 000
7.18 s = 1100.
7.19 (a) 0.706316. (b) 0.891232. (c) 0.970374. (d) 0.994123.
7.20 s = 110.
7.21 (a) 128. (b) 8001/8192 = 0.977.
√
7.22 Geometrically, the initial angle is θ = sin−1 (1/ N) = sin−1 (1/2) = π/6.
Applying RsU f to this adds 2θ to this, resulting in 3θ = π/2, which is perfectly
aligned with |wi.
7.23 (a) 1/N. (b) 9/N − 24/N 2 + 16/N 3 .
7.24 (a) 4. (b) 16. (c) 3. (d) See https://bit.ly/3E3WnzZ:
368 Answers to Exercises
w = 1011.
(e)
x f (x)
0000 0
0001 0
0010 0
0011 0
0100 0
0101 0
0110 0
0111 0
1000 0
1001 0
1010 0
1011 1
1100 0
1101 0
1110 0
1111 0
7.25 (a) 5. (b) 32. (c) 4. (d,e,f) See https://bit.ly/3q4ExrK
w = 10110.
7.26 φ0 = 0.567, φ1 = 0.35 + 0.833i, φ2 = 0.415, φ3 = 0.35 − 0.833i.
7.27 (a) G4 . (b) 440 Hz.
7.28 (a)
Answers to Exercises 369
Amplitude 6
0
0 100 200 300 400 500 600 700 800 900 1000
Frequency (Hz)
1 −0r 0s
Mrs = ω ω + ω −1r ω 1s + ω −2r ω 2s + · · · + ω −(N−1)r ω (N−1)s
N
1 N−1 −kr ks 1 N−1 k(s−r)
= ∑ ω ω = ∑ω .
N k=0 N k=0
(b) If r = s,
1 N−1 0 1 N−1 1 1
Mrs = ∑ ω = ∑ 1 = N (1 + · · · + 1) = N = 1.
N k=0 N k=0 | {z } N
N times
7.31
The Hadamard gate on q2 can be swapped with the P(π/8) gate, so the circuit is
equivalent to the textbook’s.
7.32 See https://bit.ly/3nJIPoB:
7.33
# Number of qubits.
n = 4
# Swap qubits.
for qubit in range(n//2):
qc.swap(qubit, n - qubit - 1)
qc.h(target)
(c) 0.0001101. (d) 0.1133. (e) 0.7118. (f) e0.7118i = 0.7572 + 0.6532i. (g) We only
estimated j to eight binary places, and the actual value may need more bits.
7.37 3/8, 1/2, 1/8.
7.38 (a) gcd(4, 5) = 1. (b) 4, 1. (c) 2.
7.39 (a) gcd(4, 13) = 1. (b) 4, 3, 12, 9, 10, 1. (c) 6.
7.40 49 mod 131.
7.41 33 mod 197.
372 Answers to Exercises
7.42 (a) 0.3438 = [0, 2, 1, 9, 1, 18, 1, 1, 1, 2]. (b) Its convergents are 0, 1/2, 1/3, 10/29,
11/32, 208/605, 219/637, 427/1242, 646/1879, 1719/5000. (c) Best s/r is 1/3, so
s = 1 and r = 3. (d) 33 mod 7 = 6 mod 7. (e) Not the period.
(a) 0.5 = [0, 1]. (b) Its convergents are 0, 1/2. (c) Best s/r is 1/2, so s = 1 and
r = 2. (d) 32 mod 7 = 2 mod 7. (e) Not the period.
(a) 0.6562 = [0, 1, 1, 1, 9, 1, 18, 1, 1, 1, 2]. (b) Its convergents are 0, 1, 1/2, 2/3,
19/29, 21/32, 397/605, 418/637, 815/1242, 1233/1879, 3281/5000. (c) Best s/r is
2/3, so s = 2 and r = 3. (d) 33 mod 7 = 6 mod 7. (e) Not the period.
(a) 0.8438 = [0, 1, 5, 2, 2, 19, 8]. (b) Its convergents are 0, 1, 5/6, 11/13, 27/32,
524/621, 4219/5000. (c) Best s/r is 5/6, so s = 5 and r = 6. (d) 36 mod 71 mod 7.
(e) The period.
7.43 See https://bit.ly/31E6M8h:
Probability Binary Approx. of s/r Decimal Approx. of s/r Guess of s/r 7r mod 13
33.3974% |00000i 0 N/A N/A
5.7377% |01010i 0.3125 1/3 1
22.8399% |01011i 0.3438 1/3 1
22.8399% |10101i 0.6562 2/3 1
5.7377% |10110i 0.6875 2/3 1
The period is r = 3.
7.44 See https://bit.ly/3y1rmLX:
Answers to Exercises 373
Probability Binary Approx. of s/r Decimal Approx. of s/r Guess of s/r 2r mod 15
25% |00000000i 0 N/A N/A
25% |01000000i 0.25 1/4 1
25% |10000000i 0.5 1/2 4
25% |11000000i 0.75 3/4 1
The period is r = 4.
7.45 Answers vary.
7.46 (a) gcd(22, 209) = 11. (b) p = 11, q = 209/11 = 19.
Index
Symbols B
375
376 Index