5.2.4 Bernstein-Vazirani algorithm

Above, we discussed how to use the Hadamard transform to solve an interesting problem. Given a function f:{0,1}n{0,1}f\colon\{0,1\}^{n}\to\{0,1\} and the promise that ff 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 ff 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:

f(x1,,xn)=x1a1xnan,\displaystyle f(x_{1},\dots,x_{n})=x_{1}a_{1}\oplus\ldots\oplus x_{n}a_{n}, (5.14)

where a1,,an{0,1}a_{1},\dots,a_{n}\in\{0,1\} is some fixed bitstring that defines the function.

If n=1n=1 then there are only two such functions:

  • f(x1)=0f(x_{1})=0, corresponding to a1=0a_{1}=0, and

  • f(x1)=x1f(x_{1})=x_{1}, corresponding to a1=1a_{1}=1.

If n=2n=2 then there are already four such functions:

  • f(x1,x2)=0f(x_{1},x_{2})=0, corresponding to [a1,a2]=[0,0][a_{1},a_{2}]=[0,0],

  • f(x1,x2)=x2f(x_{1},x_{2})=x_{2}, corresponding to [a1,a2]=[0,1][a_{1},a_{2}]=[0,1],

  • f(x1,x2)=x1f(x_{1},x_{2})=x_{1}, corresponding to [a1,a2]=[1,0][a_{1},a_{2}]=[1,0], and

  • f(x1,x2)=x1x2f(x_{1},x_{2})=x_{1}\oplus x_{2}, corresponding to [a1,a2]=[1,1][a_{1},a_{2}]=[1,1].

Note that each of these functions computes the sum modulo two of some subset of the variables xix_{i}. Which subset? Whenever ai=1a_{i}=1, the corresponding variable xix_{i} is included in the subset.

In general, there are 2n2^{n} choices of the bitstring a1,,ana_{1},\dotsc,a_{n} and hence 2n2^{n} functions ff of the special form (5.14). In fact, we may think of Eq. 5.14 as a way of hiding the bitstring

[a1,,an][a_{1},\dots,a_{n}]

in the function ff. How many evaluations of the function do we need to uncover it? Since

a1\displaystyle a_{1} =f(1,0,,0,0),\displaystyle=f(1,0,\dots,0,0),
a2\displaystyle a_{2} =f(0,1,,0,0),\displaystyle=f(0,1,\dots,0,0),
\displaystyle\qquad\qquad\vdots
an\displaystyle a_{n} =f(0,0,,0,1),\displaystyle=f(0,0,\dots,0,1),

we conclude that nn evaluations of the function ff 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 a1,,ana_{1},\dots,a_{n} are completely arbitrary and there are nn of them, you need to evaluate the function at least nn 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:

Of|x1,,xn\displaystyle O_{f}\left|x_{1},\dots,x_{n}\right\rangle =(1)x1a1xnan|x1,,xn\displaystyle=(-1)^{x_{1}a_{1}\oplus\ldots\oplus x_{n}a_{n}}\left|x_{1},\dots,% x_{n}\right\rangle
=(1)x1a1++xnan|x1,,xn.\displaystyle=(-1)^{x_{1}a_{1}+\ldots+x_{n}a_{n}}\left|x_{1},\dots,x_{n}\right\rangle. (5.15)

In the second step we used that (1)a(-1)^{a} only depends on aa modulo two (i.e., on whether aa is even or odd), so it does not matter whether we use addition modulo 2 (‘\oplus’) 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.

Exercise 5.5 (Implementing the sign oracle (optional)).

In this problem, you will implement the sign oracle for functions of the form (5.14).

  1. 1.

    When n=2n=2 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. 2.

    Explain in words or pictures how you can implement the sign oracle in the general case (i.e., when n1n\geq 1 and the bits a1,,an{0,1}a_{1},\dots,a_{n}\in\{0,1\} are arbitrary).

Solution.
  1. 1.

    Here are the four functions and their sign oracles:

    • For [a1,a2]=[0,0][a_{1},a_{2}]=[0,0], Of|x1,x2=|x1,x2O_{f}\left|x_{1},x_{2}\right\rangle=\left|x_{1},x_{2}\right\rangle, so the sign oracle does nothing at all.

    • For [a1,a2]=[0,1][a_{1},a_{2}]=[0,1], Of|x1,x2=(1)x2|x1,x2O_{f}\left|x_{1},x_{2}\right\rangle=(-1)^{x_{2}}\left|x_{1},x_{2}\right\rangle, which is the same as IZI\otimes Z.

    • For [a1,a2]=[1,0][a_{1},a_{2}]=[1,0], Of|x1,x2=(1)x1|x1,x2O_{f}\left|x_{1},x_{2}\right\rangle=(-1)^{x_{1}}\left|x_{1},x_{2}\right\rangle, which is the same as ZIZ\otimes I.

    • For [a1,a2]=[1,1][a_{1},a_{2}]=[1,1], Of|x1,x2=(1)x1+x2|x1,x2=(1)x1(1)x2|x1,x2O_{f}\left|x_{1},x_{2}\right\rangle=(-1)^{x_{1}+x_{2}}\left|x_{1},x_{2}\right% \rangle=(-1)^{x_{1}}(-1)^{x_{2}}\left|x_{1},x_{2}\right\rangle, which is the same as ZZZ\otimes Z.

    It is clear what these four operations look like in Quirky.

  2. 2.

    The general pattern is now clear. For an arbitrary function of the form (5.14):

    Of|x1,,xn=(1)x1a1++xnan|x1,,xn=(Za1Zan)|x1,,xn,\displaystyle O_{f}\left|x_{1},\dots,x_{n}\right\rangle=(-1)^{x_{1}a_{1}+% \ldots+x_{n}a_{n}}\left|x_{1},\dots,x_{n}\right\rangle=(Z^{a_{1}}\otimes\dotsb% \otimes Z^{a_{n}})\left|x_{1},\dots,x_{n}\right\rangle,

    where we write Z1=ZZ^{1}=Z and Z0=IZ^{0}=I. In other words, we apply a Z gate on the jj-th qubit if and only if aj=1a_{j}=1.

We now present the Bernstein-Vazirani algorithm, which uncovers the hidden bitstring [a1,,an][a_{1},\dots,a_{n}] using a single evaluation of the sign oracle for ff:

  1. 1.

    Start with |00\left|0\dots 0\right\rangle.

  2. 2.

    Apply the Hadamard transform HHH\otimes\dotsb\otimes H.

  3. 3.

    Apply the sign oracle OfO_{f} corresponding to the function ff.

  4. 4.

    Apply the Hadamard transform HHH\otimes\dotsb\otimes H again.

  5. 5.

    Measure all qubits. The measurement outcome is precisely the bitstring [a1,,an][a_{1},\dots,a_{n}].

The algorithm is identical to the Deutsch-Jozsa algorithm in Section 5.2.3 except for the last step, which is even simpler.

Homework 5.3 (Run Bernstein-Vazirani).

The orange Oracle box in Quirky implements the sign oracle for a function of the form (5.14) with n=4n=4. Implement the Bernstein-Vazirani algorithm in Quirky and use it to determine the hidden bitstring [a1,a2,a3,a4][a_{1},a_{2},a_{3},a_{4}].

Hack.

Quirky shows the following result:

[Uncaptioned image]

Thus the hidden bitstring is [a1,a2,a3,a4]=[1,1,0,1][a_{1},a_{2},a_{3},a_{4}]=[1,1,0,1] (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!

Exercise 5.6 (Verify Bernstein-Vazirani).

The function f(x1,x2)=x2f(x_{1},x_{2})=x_{2} corresponds to the bitstring [a1,a2]=[0,1][a_{1},a_{2}]=[0,1], as we saw earlier.

Show that when you run the Bernstein-Vazirani algorithm for this function, the measurement outcome is indeed always [a1,a2]=[0,1][a_{1},a_{2}]=[0,1]. Don’t just verify this using Quirky, but write down the state after each step yourself.

Solution. In step 1 we start with:
|00\displaystyle\left|00\right\rangle
In step 2 we apply HHH\otimes H and obtain:
12(|0+|1)12(|0+|1)\displaystyle\frac{1}{\sqrt{2}}\left(\left|0\right\rangle+\left|1\right\rangle% \right)\otimes\frac{1}{\sqrt{2}}\left(\left|0\right\rangle+\left|1\right% \rangle\right)
=\displaystyle= 12(|00+|01+|10+|11)\displaystyle\frac{1}{2}\left(\left|00\right\rangle+\left|01\right\rangle+% \left|10\right\rangle+\left|11\right\rangle\right)
In step 3 we apply the sign oracle OfO_{f} for f(x1,x2)=x2f(x_{1},x_{2})=x_{2}:
12(|00|01+|10|11)\displaystyle\frac{1}{2}\left(\left|00\right\rangle-\left|01\right\rangle+% \left|10\right\rangle-\left|11\right\rangle\right)
In step 4 we apply again HHH\otimes H, resulting in:
12((HH)|00(HH)|01+(HH)|10(HH)|11)\displaystyle\frac{1}{2}\Bigl{(}(H\otimes H)\left|00\right\rangle-(H\otimes H)% \left|01\right\rangle+(H\otimes H)\left|10\right\rangle-(H\otimes H)\left|11% \right\rangle\Bigr{)}
=\displaystyle= 14((|00+|01+|10+|11)(|00|01+|10|11)\displaystyle\frac{1}{4}\Bigl{(}\left(\left|00\right\rangle+\left|01\right% \rangle+\left|10\right\rangle+\left|11\right\rangle\right)-\left(\left|00% \right\rangle-\left|01\right\rangle+\left|10\right\rangle-\left|11\right% \rangle\right)
+(|00+|01|10|11)(|00|01|10+|11))\displaystyle+\left(\left|00\right\rangle+\left|01\right\rangle-\left|10\right% \rangle-\left|11\right\rangle\right)-\left(\left|00\right\rangle-\left|01% \right\rangle-\left|10\right\rangle+\left|11\right\rangle\right)\Bigr{)}
=\displaystyle= |01.\displaystyle\left|01\right\rangle.
In step 5 we measure both qubits and always obtain the result [0,1][0,1].

In the following homework problem you can analyze the general case:

Homework 5.4 (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 [a1,,an][a_{1},\dots,a_{n}] 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.

Hack.

Following the hint, we know that the state right before the measurement is the following:

y1,,yn{0,1}12n(x1,,xn{0,1}(1)x1y1++xnyn(1)f(x1,,xn))|y1,,yn.\displaystyle\sum_{y_{1},\dots,y_{n}\in\{0,1\}}\frac{1}{2^{n}}\left(\sum_{x_{1% },\dots,x_{n}\in\{0,1\}}(-1)^{x_{1}y_{1}+\ldots+x_{n}y_{n}}(-1)^{f(x_{1},\dots% ,x_{n})}\right)\left|y_{1},\dots,y_{n}\right\rangle.

Since the function f(x1,,xn)f(x_{1},\dots,x_{n}) in this problem is now of the form f(x1,,xn)=x1a1xnanf(x_{1},\dots,x_{n})=x_{1}a_{1}\oplus\cdots\oplus x_{n}a_{n} (as defined in Eq. 5.14), this is the same as

y1,,yn{0,1}12n(x1,,xn{0,1}(1)x1y1++xnyn(1)x1a1++xnan)|y1,,yn.\displaystyle\sum_{y_{1},\dots,y_{n}\in\{0,1\}}\frac{1}{2^{n}}\left(\sum_{x_{1% },\dots,x_{n}\in\{0,1\}}(-1)^{x_{1}y_{1}+\ldots+x_{n}y_{n}}(-1)^{x_{1}a_{1}+% \ldots+x_{n}a_{n}}\right)\left|y_{1},\dots,y_{n}\right\rangle.

To understand the probability of getting the measurement outcome [a1,,an][a_{1},\dots,a_{n}], we need to analyze the amplitude of the state |a1,,an\left|a_{1},\dots,a_{n}\right\rangle in the above expression. Since this corresponds to y1=a1y_{1}=a_{1}, …, yn=any_{n}=a_{n}, the amplitude is given by

12n(x1,,xn{0,1}(1)x1y1++xnyn(1)x1a1++xnan)\displaystyle\frac{1}{2^{n}}\left(\sum_{x_{1},\dots,x_{n}\in\{0,1\}}(-1)^{x_{1% }y_{1}+\ldots+x_{n}y_{n}}(-1)^{x_{1}a_{1}+\ldots+x_{n}a_{n}}\right)
=\displaystyle= 12n(x1,,xn{0,1}(1)x1a1++xnan(1)x1a1++xnan)\displaystyle\frac{1}{2^{n}}\left(\sum_{x_{1},\dots,x_{n}\in\{0,1\}}(-1)^{x_{1% }a_{1}+\ldots+x_{n}a_{n}}(-1)^{x_{1}a_{1}+\ldots+x_{n}a_{n}}\right)
=\displaystyle= 12n(x1,,xn{0,1}1)=2n2n=1.\displaystyle\frac{1}{2^{n}}\left(\sum_{x_{1},\dots,x_{n}\in\{0,1\}}1\right)=% \frac{2^{n}}{2^{n}}=1.

In the first step we used that y1=a1y_{1}=a_{1}, …, yn=any_{n}=a_{n}. In the second step we used that (1)(1)=(+1)(+1)=1(-1)(-1)=(+1)(+1)=1.

Since the amplitude of |a1,,an\left|a_{1},\dots,a_{n}\right\rangle is 1, we conclude that we obtain outcome [a1,,an][a_{1},\dots,a_{n}] with 100% probability.