Bernstein-Vazirani algorithm
Above, we discussed how to use the Hadamard transform to solve an interesting problem.
Given a function and the promise that is either constant or balanced, the Deutsch-Jozsa algorithm was able to determine which of these two options was the case.
In this section we will discuss another interesting problem that one can solve by a slight variant of the same procedure.
As before, we will start with a promise about the unknown function that we are dealing with.
This time, instead of assuming that it is constant or balanced, we will assume that it is of the following special form:
|
|
|
(5.14) |
where is some fixed bitstring that defines the function.
If then there are only two such functions:
-
•
, corresponding to , and
-
•
, corresponding to .
If then there are already four such functions:
-
•
, corresponding to ,
-
•
, corresponding to ,
-
•
, corresponding to , and
-
•
, corresponding to .
Note that each of these functions computes the sum modulo two of some subset of the variables .
Which subset? Whenever , the corresponding variable is included in the subset.
In general, there are choices of the bitstring and hence functions of the special form (5.14).
In fact, we may think of Eq. 5.14 as a way of hiding the bitstring
in the function .
How many evaluations of the function do we need to uncover it?
Since
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
we conclude that evaluations of the function are certainly enough.
For any classical algorithm, this is also optimal: each time you evaluate the function you only learn a single bit.
Since the unknown bits are completely arbitrary and there are of them, you need to evaluate the function at least times to learn them all.
We will now see that we can do much better using a quantum algorithm.
To start, let us compute how the sign oracle for the function in Eq. 5.14 acts on the basis states:
|
|
|
|
|
|
|
|
(5.15) |
In the second step we used that only depends on modulo two (i.e., on whether is even or odd), so it does not matter whether we use addition modulo 2 (‘’) or the ordinary addition. How can this sign oracle be implemented?
We do not really care, since our algorithm will treat the oracle as a black box.
But since it is a nice exercise, you can try to figure this out in the following problem.
(Implementing the sign oracle (optional)).
In this problem, you will implement the sign oracle for functions of the form (5.14).
-
1.
When there are four such functions, as we discussed above.
Can you find for each of them a Quirky circuit that implements the sign oracle?
-
2.
Explain in words or pictures how you can implement the sign oracle in the general case (i.e., when and the bits are arbitrary).
Solution.
-
1.
Here are the four functions and their sign oracles:
-
•
For , , so the sign oracle does nothing at all.
-
•
For , , which is the same as .
-
•
For , , which is the same as .
-
•
For , , which is the same as .
It is clear what these four operations look like in Quirky.
-
2.
The general pattern is now clear. For an arbitrary function of the form (5.14):
|
|
|
where we write and .
In other words, we apply a Z gate on the -th qubit if and only if .
We now present the Bernstein-Vazirani algorithm, which uncovers the hidden bitstring using a single evaluation of the sign oracle for :
-
1.
Start with .
-
2.
Apply the Hadamard transform .
-
3.
Apply the sign oracle corresponding to the function .
-
4.
Apply the Hadamard transform again.
-
5.
Measure all qubits. The measurement outcome is precisely the bitstring .
The algorithm is identical to the Deutsch-Jozsa algorithm in Section 5.2.3 except for the last step, which is even simpler.
(Run Bernstein-Vazirani).
The orange Oracle box in Quirky implements the sign oracle for a function of the form (5.14) with .
Implement the Bernstein-Vazirani algorithm in Quirky and use it to determine the hidden bitstring .
.
Quirky shows the following result:
Thus the hidden bitstring is (hover the green ‘100%’ entry with your mouse to see which outcome it corresponds to).
Why does this algorithm work?
Now it is your turn to do the analysis!
(Verify Bernstein-Vazirani).
The function corresponds to the bitstring , as we saw earlier.
Show that when you run the Bernstein-Vazirani algorithm for this function, the measurement outcome is indeed always .
Don’t just verify this using Quirky, but write down the state after each step yourself.
Solution.
In step 1 we start with:
|
|
|
In step 2 we apply and obtain:
|
|
|
|
|
|
|
|
In step 3 we apply the sign oracle for :
|
|
|
In step 4 we apply again , resulting in:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In step 5 we measure both qubits and always obtain the result .
In the following homework problem you can analyze the general case:
(Verify Bernstein-Vazirani (challenging)).
Show that, when you run the Bernstein-Vazirani algorithm for a function of the form (5.14), the measurement outcome is with 100% probability.
Hint: Since the first four steps of the algorithm are identical to the Deutsch-Jozsa algorithm, the state right before the measurement is given by Eq. 5.13.
.
Following the hint, we know that the state right before the measurement is the following:
|
|
|
Since the function in this problem is now of the form (as defined in Eq. 5.14), this is the same as
|
|
|
To understand the probability of getting the measurement outcome , we need to analyze the amplitude of the state in the above expression.
Since this corresponds to , …, , the amplitude is given by
|
|
|
|
|
|
|
|
|
|
|
|
In the first step we used that , …, .
In the second step we used that .
Since the amplitude of is 1, we conclude that we obtain outcome with 100% probability.