QSYS2022 09 EarlyQC JD Notes
QSYS2022 09 EarlyQC JD Notes
Quantum Computing
John M. Donohue
Institute for Quantum Computing, University of Waterloo
(Dated: June 29, 2022)
In these notes, we will discuss quantum computing through the Deutsch-Josza algorithm.
These notes are based heavily on Kaye, Laflamme, and Mosca, An Introduction to Quantum
Computing [1].
Over the past many decades, computing power has increased dramatically. In the 1960s, com-
puters took up entire rooms and were only used for specialty applications. Today, we carry much
more powerful computers around in our pocket. However, even though we use them for drastically
different tasks, early computers, modern computers, and even ancient methods like the abacus all
work by the same rules. They’ve gotten faster, but they have the same power.
Quantum computers represent a paradigm shift, working by entirely different rules than clas-
sical computers. While there is a lot of science to be done to find which problems quantum
computing can be useful for, we already know that computing based on quantum rules would have
advantages for problems like factoring, optimization, and simulation. But where does this power
come from?
Here, we’ll go through a historically and educationally relevant example: The Deutsch-Josza
algorithm, proposed by David Deutsch and Richard Josza in 1992 [2, 3] and improved to its pre-
sented form by Cleve, Ekert, Macchiavello, and Mosca in 1998 [4]. While quantum computing
was originally proposed by Paul Benioff and Richard Feynman in the early 1980s as a method to
simulate other quantum systems, the Deutsch-Josza algorithm was a milestone in that it showed
that quantum computers could have speedups for problems phrased in computer-science terms
rather than physics terms. The problem itself is not a terribly practical one, but the line of thinking
it inspired led to Shor’s factoring algorithm but two years later [5] and many others
2
ORACLE PROBLEMS
Before bringing in the quantum solution, we’ll need to define the problem. The Deutsch prob-
lem is a “black-box” or “oracle” problem.
Imagine you have a question and a device that knows all the answers. You could open the
device, figure out exactly how it works, and use that knowledge to answer your question. However,
that could take a lot of physical and mental effort, and you may only be interested in a small
fraction of that information.
For example, a phone book contains thousands of numbers. However, I only need one of them.
I could read and memorize every single number to get the answer I need, or, I could use the fact
that the phone book is sorted by name to immediately get the answer I’m interested in. In this
example, the phone book is the “oracle”, and the challenge we have is the ask it the right questions
to get the right information. Each time we ask a question, we call it a “query” to the oracle.
To analyze this problem mathematically, we’ll need to think about information in a more ab-
stract sense. Without loss of generality, we can break the problem down to a binary representation.
We ask the oracle a question with an input bit string x and it responds with an output bit string that
depends on x, f (x). In the phone book example, x is the name of the person whose number I want
(e.g. RAY LAFLAMME) and f (x) is their phone number (e.g. 5198884567) (breaking each down to a
binary or ASCII representation). In this case, there is an easy one-to-one correspondence between
an input I have and an output I want, which is just an element of a list.
There are other times where I want to know a collective property about the oracle. For example,
maybe I want to know how many numbers have the area code 519. In this case, providing one
input string x is not enough. I’ll need to know every output f (x) for all possible inputs x to know
the answer. This Deutsch problem is this sort of problem.
3
The problem
We’re given an oracle with f (x) programmed into it. The oracle accepts one bit as input (0 or
1) and returns one bit as output (0 or 1). We can straightforwardly write down all four possible
functions fi (x), as in the table below:
To identify which function the oracle has, we obviously need to make two queries to the oracle.
For example, if we ask the oracle about f? (0) and it returns 0, we would know we have f0 (x) or
f1 (x), but would need to know f (1) to distinguish between the two. However, like in the phone
book example, if our question was “What is f (0)?”, we would have our answer.
What if our question is about a collective property? The question the Deutsch problem is
concerned with is whether the function is constant or balanced. “Constant” means that, no matter
what input x is, f (x) provides the same output. “Balanced” means that there are an equal number
of inputs x that provide f (x) = 0 and f (x) = 1. From inspection, f0 (x) and f3 (x) are constant
functions, and f1 (x) and f2 (x) are balanced. Another way to think about the question is that we
don’t particularly want to know f (0) or f (1), but we do want to know f (0) ⊕ f (1), which is 0 for
constant functions and 1 for balanced functions.
Using a classical oracle, there is no strategy better than simply querying the oracle twice to
find both values of f (x). However, let’s say that we get to choose how the oracle is built. If we’re
allowed to query it with a quantum state and measure the result with a quantum measurement, how
does that change the problem?
A quantum solution
We program f (x) into the two-qubit unitary U f , and send in the qubit states |x⟩ and |y⟩, where
x and y can take the values 0 or 1. The unitary is designed such that
where ⊕ is the XOR operation. Put simply, the unitary will flip the second qubit if f (x) is one,
and leave it alone if f (x) = 0, depending on the value of the first qubit. We sometimes call the
first qubit the “control” qubit and the second the “target”, since the value of the first qubit controls
whether or not the second is flipped, depending on the function f (x).
What happens when we use this unitary? If we send in a separable state, we find:
By measuring the second qubit, we can implement to classical two-query method using this quan-
tum implementation.
However, what happens if we send in a superposition state?
|0⟩ + |1⟩ 1
Uf √ |0⟩ = √ (|0⟩| f (0)⟩ + |1⟩| f (1)⟩) .
2 2
We see that both f (0) and f (1) are in the resulting state, implying that we’ve probed both pieces of
f (x) simultaneously. However, when we make a measurement on the second qubit, we’ll randomly
get either the answer to f (0) or f (1). Simply providing a superposition state is not enough to get
a computational advantage; the input and measurement must both be carefully considered.
What if both the control and target are in superposition states? Consider the quantum circuit
below:
|0⟩ H H
Uf
|0⟩ X H
|0⟩+|1⟩
If we trace through it, our first step is to convert the (top) control qubit to the state |+⟩ = √
2
|0⟩−|1⟩
using the Hadamard gate (H) and the (bottom) target qubit to the state |−⟩ = √
2
using the X
gate followed by the Hadamard gate. The unitary then does the following:
1
U f |+⟩|−⟩ = U f (|0⟩ + |1⟩) (|0⟩ − |1⟩) (2)
2
1 1
= U f |0⟩ (|0⟩ − |1⟩) + U f |1⟩ (|0⟩ − |1⟩) (3)
2 2
1 1
= |0⟩ (|0 ⊕ f (0)⟩ − |1 ⊕ f (0)⟩) + |1⟩ (|0 ⊕ f (1)⟩ − |1 ⊕ f (1)⟩) (4)
2 2
5
Isolating step (4), we can re-arrange the terms on the target qubit depending on the value of
f (x) as
1 1
If f (x) = 0, √ (|0 ⊕ 0⟩ − |1 ⊕ 0⟩) = √ (|0⟩ − |1⟩) = |−⟩
2 2
(5)
1 1
If f (x) = 1, √ (|0 ⊕ 1⟩ − |1 ⊕ 1⟩) = √ (|1⟩ − |0⟩) = −|−⟩
2 2
So we see that, depending on the value of f (x), the second qubit can pick up a π phase or not.
Since we probe both x = 0 and x = 1 in superposition, this is not a global phase.
Using this, we return to step (4) and simplify to
1 1
U f |+⟩|−⟩ = √ (−1) f (0) |0⟩|−⟩ + √ (−1) f (1) |1⟩|−⟩ (7)
2 2
1
= √ (−1) f (0) |0⟩ + (−1) f (1) |1⟩ |−⟩ (8)
2
1
= (−1) f (0) √ |0⟩ + (−1) f (0)⊕ f (1) |1⟩ |−⟩. (9)
2
We can remove the (−1) f (0) on the outside as a global phase, and we find that
Therefore, by measuring the first (control) qubit in the superposition basis, we can determine
whether f (x) is constant or balanced with a single query.
So why did the quantum algorithm work? There are a couple of important things to note about
the Deutsch algorithm. As mentioned, the two-qubit example is rudimentary and the problem is
not a terribly practical one. However, these same sorts of tricks (along with a plethora of others)
form the bedrock for other quantum algorithms. Additionally, as we’ll see in the next section, this
algorithms scales nicely. While the advantage is only one query at this level (one query quantumly
6
vs. two queries classically), we’ll see that it persists as the input size grows (one query quantumly
vs n/2 queries classically).
Firstly, it’s important to note that superposition alone was not enough. While “quantum paral-
lelism”, where all possible solutions are probed in superposition, is important to how the Deutsch
algorithm works, it is also essential to measure in the superposition basis (the final Hadamard
before the measurement). Without this, no gains over classical are made.
Secondly and interestingly, the qubit that is normally measured in the classical-emulating strat-
egy (the “target” qubit) is not measured in the quantum strategy. Rather, we set the target qubit to
|−⟩, where it may or may not pick up a π phase. By probing those possibilities in superposition,
that phase is “kicked back” to the control qubit. Phase kickback is an essential concept for many
other quantum algorithms.
Finally, when we run the quantum algorithm, we get information about f (0) ⊕ f (1), and can
use that to separate f0 (x) and f3 (x) from f1 (x) and f2 (x). However, even if we know the function
is balanced (or constant), we cannot tell whether it is f0 (x) or f3 (x) (or, f1 (x) or f2 (x)). With
one query, we still get one bit of information out, and we would need two queries to get full
information about the system. So quantum algorithms don’t pack more information into a smaller
circuit or anything like that; what they allow us to do is ask questions and manipulate logic in
ways that classical computers cannot. Finding problems that quantum computers can help with is
highly non-trivial, but the tools of the Deutsch algorithm inform the art of quantum algorithms.
We next consider the same problem with more possible inputs. Once again, f (x) can take only
one of two values (0 or 1). However, now x is an n-bit number, or equivalently, is any number
between 0 and 2n − 1. To use another example, if we have a deck of playing cards, I can pull any
of the 52 cards and ask in the one-bit questions “Are you red or black”? In this example, only two
outputs are possible (“RED” or “BLACK”), but I can ask to any of 52 inputs (the cards).
f0 (x) is constant, since all the outputs are the same regardless of the input, and f3 (x) is balanced,
since there are an equal number of 0’s and 1’s. However, the other two functions are neither
constant or balanced. For this particular problem, we assume that we are promised that these are
not allowed for the oracle.
Eliminating these other functions, we are left with 2 possible constant functions (all zero or
n
2n !
all one) and 22n−1 = (2n−1 !)2
possible balanced functions (the number of possible ways to arrange
half the possibilities as zero and the other as one).
How many queries to I need to be certain whether the function is constant or balanced? If I
query half (2n−1 ) of the possible x values and see that they are all 0, I might think it likely that
the function is constant. However, given the promise made at the start, it’s equally likely that I’ve
gotten unlucky with a specific balanced function, and every other input would give f (x) = 1. So,
classically, I need to make test at least half of the possible inputs plus one (2n−1 + 1 queries) to be
certain if f (x) is constant or balanced.
The quantum solution is a straightforward extension of the 1-bit version, with an example
circuit for the four-bit problem seen below:
|0⟩ H H
|0⟩ H H
|0⟩ H Uf H
|0⟩ H H
|0⟩ X H
We start by using n Hadamard gates to put n qubits into the superposition state
1 n
H ⊗n |0⟩⊗n = |+⟩⊗n = √ ∑ = 02 −1 |x⟩,
2n x
where the exponentiated tensor product implies n copies of the same gate or state. To be explicit,
8
1
(H ⊗ H)|00⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩) .
2
In the same way as before, we use an X gate and a Hadamard to put the control qubit (bottom) into
the state |−⟩.
The oracle U f functions in the same way as in the one-bit case, such that U f |x⟩|y⟩ = |x⟩|y ⊕
f (x)⟩. As seen in Eq. (6), this provides U f |x⟩|−⟩ = (−1) f (x) |x⟩|−⟩. When applied to the superpo-
sition state, we obtain
1
U f |+⟩⊗x |−⟩ = ∑ √ (−1) f (x) |x⟩|−⟩. (11)
x 2n
To analyze the effect of the unitary, we want to measure back in the superposition basis by
applying another n Hadamard transformations. To see the main point clearly, we note that the
Hadamard is reversible by applying it twice, such that
∑(−1) f (x)|x⟩.
x
If and only if f (x) is constant, we can move the (−1) f (x) outside the sum. Applying the Hadamard
transformations then provides
⊗n f (x) ifconstant f (x) ⊗n 1
H ∑(−1) |x⟩ = (−1) H √ |x⟩
x 2n
(12)
(−1) f (x) H ⊗n |−⟩⊗n
= ±|0⟩⊗n .
Therefore, if f (x) is constant, all of the qubits will be measured in the 0 state.
We still need to prove that we will never measure the all-zero state if f (x) is balanced. To start,
9
we’ll consider the possibilities when applying Hadamard transforms on the two-qubit basis states:
1
H ⊗ H|00⟩ = (|00⟩ + |01⟩ + |10⟩ + |11⟩)
2
1
H ⊗ H|01⟩ = (|00⟩ − |01⟩ + |10⟩ − |11⟩)
2 (13)
1
H ⊗ H|10⟩ = (|00⟩ + |01⟩ − |10⟩ − |11⟩)
2
1
H ⊗ H|11⟩ = (|00⟩ − |01⟩ − |10⟩ + |11⟩)
2
1
H ⊗ H|x⟩ = √ ∑(−1)x1 z1 +x2 z2 |z⟩,
22 z
1
H ⊗n |x⟩ = √ ∑(−1)⃗x·⃗z |z⟩, (14)
2n z
0
where ⃗x is a vector representing x’s binary representation (e.g. ⃗x = 1 for the state |x⟩ = |011⟩).
1
1
H ⊗nU f |+⟩⊗n |−⟩ = √ ∑(−1) f (x) H ⊗n |x⟩|−⟩
2n x
(15)
1
= n ∑ ∑(−1) f (x) (−1)⃗x·⃗x |z⟩|−⟩.
2 x z
The probability of measuring the all-zero state |0⟩⊗n is, by Born’s rule,
2 2
⊗n
⊗n ⊗n 2 1 f (x) ⃗x·⃗z
⟨−|⟨0| H U f |+⟩ |−⟩ = ∑ ∑(−1) (−1) ⟨0|z⟩⟨−|−⟩ (16)
2n x z
Knowing that ⟨−|−⟩ = 1 and that ⟨0|z⟩ will pick out only the part of the sum where |z⟩ = |0⟩⊗n ,
10
we can simplify to
2 2
1
Prob(all zero) =
2n ∑(−1) f (x)
x (17)
= 1 if constant, 0 if balanced.
Note that this algorithm only works with the promise that the function is either constant or balanced
(if it is neither, there will be some probability of measuring all zero). However, we suddenly go
from needing 2n−1 + 1 queries to a single query, even in the worst case scenario.
[1] P. Kaye, R. Laflamme, and M. Mosca, An Introduction to Quantum Computing (Oxford University
Press, 2007).
[2] D. Deutsch, Proceedings of the Royal Society of London. A. Mathematical and Physical Sciences 400,
97 (1985).
[3] D. Deutsch and R. Jozsa, Proceedings of the Royal Society of London. Series A: Mathematical and
Physical Sciences 439, 553 (1992).
[4] R. Cleve, A. Ekert, C. Macchiavello, and M. Mosca, Proceedings of the Royal Society of London.
Series A: Mathematical, Physical and Engineering Sciences 454, 339 (1998).
[5] P. W. Shor, in Foundations of Computer Science, 1994 Proceedings., 35th Annual Symposium on (IEEE,
1994) pp. 124–134.