QuIC Seminar 10
The Bernstein-Vazirani Algorithm
Contents
10.1 The Bernstein-Vazirani Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
10.2 Classical Query Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
10.3 The Bernstein-Vazirani Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
10.4 Concluding Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Last time we introduced the query model of quantum computing and saw one problem which a quantum
computer could solve twice as fast as a classical computer, with respect to the same oracle. Twice as fast is
pretty good. If it took an hour originally, you could do it an a half hour. Two weeks? One week. This could
make a huge difference!
But we’re not always concerned about the constant factors of algorithms1 . Often, we’re concerned with
how the problem scales in the input to the algorithm. For example, if I give you a list of n numbers, how
long does the algorithm take to run, roughly? Does the runtime t scale polynomially, i.e. t = O(nk ) for
some integer k? Or does it scale exponentially, i.e., t = O(bt ) for some base exponent b?
This is often the metric that computer scientists use to determine whether an algorithm is efficient.
Definition 10.1 (Efficient algorithm.). An efficient algorithm is one in which the number of steps scales
(at most) polynomially in the input size.
In the Deustch-Jozsa problem, there is necessarily no scaling—the function of interest only acts on one
bit! We can’t scale the problem up to larger bits and see how the complexity scales.
The Bernstein-Vazirani (BV) algorithm presents us with a quantum algorithm whose complexity scales
better than the best classical algorithm. Namely, the quantum algorithm will give us a linear speedup in the
number of queries relative to the best classical algorithm. (If “linear speedup” doesn’t make sense to you
now, it will by the end of the seminar.)
First, note that the complexity of the BV algorithm is in terms of the number of queries to an oracle, so
the speedup is also in terms of the number of queries. We talked about justifying the query model in the
last seminar. If you’re still losing sleep over the query model setting, maybe the following analogy will help2 .
Imagine there’s two graduate students, Alice and Bob, working for you. They both sit in the same room,
and every time you come by you find out they haven’t done anything! They’re hardly doing any work, not
making any progress, etc. Then someone asks you: Who’s the better graduate student, Alice or Bob?
1 Well, computational scientists emphatically do care about constant factors—parallelizing an algorithm to get even a 35%
speedup, say, is a huge deal. On the other hand, computer scientists largely don’t care about these constant factors. The
coarse-grained measure of polynomial vs exponential, say, is the key characteristic here.
2 I didn’t come up with this, but I forgot where I heard it. When I remember, I’ll add a citation.
45
QUIC SEMINAR 10. THE BERNSTEIN-VAZIRANI ALGORITHM 46
How could you possibly answer this?! You can’t say anything about either—as you haven’t seen them
do anything!
Then you have an idea. You give them each a computer, the same computer, and you let them get back
to work. When you come back in a week, you find that Alice has made enormous progress, while Bob is still
spinning his wheels.
So you go back to the person who asked you the question and say, With respect to the computer, Alice is
the better graduate student.
This seems natural, doesn’t it? This is the same thing we’re saying in the query model setting. It’s hard
to answer general questions about “which is better: quantum or classical?” So we give them each the same
tool, namely an oracle, and ask which is better with respect to the oracle. (Or, as a computer scientist would
say, relative to an oracle.)
Alright, end analogy! It’s time to see the real deal.
10.1 The Bernstein-Vazirani Problem
The problem that Bernstein and Vazirani considered in order to show their quantum speedup is the following.
Definition 10.2 (Bernstein-Vazirani problem.). Let f be a function from bit strings of length n to a
single bit,
f : {0, 1}n → {0, 1}, (10.1)
such that for all input bit strings x ∈ {0, 1}n , there exists a secret string s ∈ {0, 1}n such that
f (x) = x · s. (10.2)
Here, · denotes the inner product modulo two. The problem is to find s by querying f as few times as
possible.
To be absolutely clear about the problem, let’s consider the following examples.
Example 1: Inner products modulo 2.
Let
x1 = 100, x2 = 001, x3 = 111, (10.3)
and consider s = 101. Then,
x1 · s = (1)(1) + (0)(0) + (0)(1) (mod 2) = 1. (10.4)
Here, we multiply the jth bit of x1 with the jth bit of s and sum up the terms, taking the answer modulo
two. This is exactly the same as the standard dot product of vectors over the field Zn2 . For example, we
could have written the dot product above as
x1 · s = (1, 0, 0) · (1, 0, 1) = 1. (10.5)
It’s easy to verify that x2 · s = 1 and x3 · s = 0.
Now that we’re clear on what the inner product means, let’s consider an example of such a function f
with this property.
Example 2: Function with the Bernstein-Vazirani property.
Consider the following function f on two bits which obeys the Berstein-Vazirani property f (x) = s · x.
x f(x)
00 0
01 0
10 1
11 1
46
QUIC SEMINAR 10. THE BERNSTEIN-VAZIRANI ALGORITHM 47
Given just this table of values, can you determine what the secret string s is to solve the Bernstein-
Vazirani problem?
10.2 Classical Query Complexity
There’s a method for solving the BV problem in n classical queries. Here, in the classical setting, we send
one input string x into our “black box” oracle and learn the value f (x). How can we do this with n queries?
Consider what happens when we send in the bit string x = 100 · · · 0. (That is, the bit string with a one
as its first bit and zeros elsewhere.) The query then sends us back
f (x) = x · s = s1 , (10.6)
where s1 is the first bit of s.
Exercise 48: Prove that
xj · s = s j (10.7)
here xj is the bit string with a one in the jth component and zeroes elsewhere.
Thus, we have an algorithm for determining s explicitly with n queries. Can we do better than this? The
answer is no, simply because each bit of s is independent. It takes at least n bits of information to specify
s, and we learn exactly one bit from each query. Thus, n classical queries is a lower bound.
10.3 The Bernstein-Vazirani Algorithm
The circuit for the Bernstein-Vazirani algorithm is shown below.
|0⟩ H H ✌✌✌
|0⟩ H H ✌✌✌
Qf
|0⟩ H H ✌✌✌
|0⟩ H H ✌✌✌
Figure 10.1: Quantum circuit for the Bernstein-Vazirani algorithm. Here, four qubits are shown, but the
general construction is with n qubits. The query used is a phase query.
The algorithm uses n qubits and outputs each bit of the secret string s once measurements are made,
thereby solving the BV problem in one query—a linear speedup! How does this work?
We start in the all zero state, as we always do, then perform a Hadamard gate on each qubit:
∑
|0⟩⊗n 7−→ |+⟩⊗n = |x⟩. (10.8)
x∈{0,1}n
At this point, we make a query to our function f , which writes an appropriate phase into the state, as
follows:
∑ ∑
|x⟩ 7−→ (−1)f (x) |x⟩ (10.9)
x∈{0,1}n x∈{0,1}n
Using the BV property that f (x) = x · s, we can of course write
∑ ∑
(−1)f (x) |x⟩ = (−1)s·x |x⟩. (10.10)
x∈{0,1}n x∈{0,1}n
47
QUIC SEMINAR 10. THE BERNSTEIN-VAZIRANI ALGORITHM 48
What does this state do for us? Note that we can rewrite this state in the following way:
∑
(−1)s·x |x⟩ = (|0⟩ + (−1)s1 |1⟩) ⊗ (|0⟩ + (−1)s2 |1⟩) ⊗ (|0⟩ + (−1)sn |1⟩). (10.11)
x∈{0,1}n
Exercise 49: Prove the equality (10.11) by expanding the right-hand side and using (10.7). It may be
instructive to start with a small number of qubits, say n = 2, to see what’s going on, then generalize it.
Note that the state (10.11) is extremely useful for us. It tells us that the jth qubit is in the plus state |+⟩
if the jth bit of s is 0, otherwise the j qubit is in the minus state |−⟩. Thus, all we need to ask is whether
each qubit is in the plus state or the minus state, and this tells us s exactly!
Of course, we can’t directly measure in the Hadamard basis—we only have access to computational basis
measurements. That is, we can only ask if the qubits are |0⟩ or |1⟩. This isn’t much of a problem though—we
can just change bases to the Hadamard basis and then make a computational basis measurement there. This
is what the latter part of the circuit (after the query) is doing.
If this doesn’t make sense to you, we can still carry out the calculations explicitly. Let’s write the state
(10.11) as
n
⊗
(|0⟩ + (−1)s1 |1⟩) ⊗ (|0⟩ + (−1)s2 |1⟩) ⊗ (|0⟩ + (−1)sn |1⟩) = (|0⟩ + (−1)sj |1⟩). (10.12)
j=1
By performing a Hadamard gate on all the qubits, we get the state
n
⊗ n
⊗ n
⊗ n
⊗
(|0⟩ + (−1)sj |1⟩) 7−→ H ⊗n (|0⟩ + (−1)sj |1⟩) = H(|0⟩ + (−1)sj |1⟩) = |sj ⟩. (10.13)
j=1 j=1 j=1 j=1
Here, we use the fact that H|+⟩ = |0⟩ and H|−⟩ = |1⟩. Thus, by measuring the jth qubit, we get the
value sj exactly. By measuring all the qubits, we get the secret string s exactly, thereby solving the BV
problem with exactly one query in the quantum setting!
10.4 Concluding Remarks
One thing to note is that, although we used only one query, we used O(n) gates. That is, we used a number
of gates that scales linearly in the size of the input (which is the number of bits in the secret string s, or
equivalently the domain of the function f .) Apparently, Daniel Simon didn’t like this feature and set out
to prove there couldn’t be a “real” quantum speedup. He discovered a quantum algorithm that obtains an
exponential speedup in the query model setting, however.
Exercise 50: State the Deustch-Jozsa problem and algorithm as a particular case of the Bernstein-
Vazirani algorithm.
Exercise 51: For the Deustch-Jozsa algorithm, we presented a classical simulation using standard
optical equipment to see exactly how the interference comes into play. What would an optical simulation of
the Bernstein-Vazirani algorithm look like for general n > 1?
48