Path: blob/main/notebooks/ch-algorithms/bernstein-vazirani.ipynb
3855 views
Bernstein-Vazirani Algorithm
In this section, we first introduce the Bernstein-Vazirani problem, its classical solution, and the quantum algorithm to solve it. We then implement the quantum algorithm using Qiskit and run it on both a simulator and a device.
1. The Bernstein-Vazirani Algorithm
The Bernstein-Vazirani algorithm, first introduced in Reference [1], can be seen as an extension of the Deutsch-Jozsa algorithm we covered in the last section. It showed that there can be advantages in using a quantum computer as a computational tool for more complex problems than the Deutsch-Jozsa problem.
1.1 The Bernstein-Vazirani Problem
We are again given a black-box function , which takes as input a string of bits (), and returns either or , that is:
Instead of the function being balanced or constant as in the Deutsch-Jozsa problem, now the function is guaranteed to return the bitwise product of the input with some string, . In other words, given an input , . We are expected to find . As a classical reversible circuit, the Bernstein-Vazirani oracle looks like this:

1.2 The Classical Solution
Classically, the oracle returns: given an input . Thus, the hidden bit string can be revealed by querying the oracle with the sequence of inputs:
| Input(x) |
|---|
| 100...0 |
| 010...0 |
| 001...0 |
| 000...1 |
Where each query reveals a different bit of (the bit ). For example, with x = 1000...0 one can obtain the least significant bit of , with x = 0100...0 we can find the next least significant, and so on. This means we would need to call the function , times.
1.3 The Quantum Solution
Using a quantum computer, we can solve this problem with 100% confidence after only one call to the function . The quantum Bernstein-Vazirani algorithm to find the hidden bit string is very simple:
Initialize the inputs qubits to the state, and output qubit to .
Apply Hadamard gates to the input register
Query the oracle
Apply Hadamard gates to the input register
Measure

To explain the algorithm, let’s look more closely at what happens when we apply a H-gate to each qubit. If we have an -qubit state, , and apply the H-gates, we will see the transformation:
Explain Equation (Click to Expand)
We remember the Hadamard performs the following transformations on one qubit:
Using summation notation, we could rewrite it like this:
For two qubits, applying a Hadamard to each performs the following transformations:
We can express this using the summation below:
You will hopefully now see how we arrive at the equation above.
In particular, when we start with a quantum register and apply Hadamard gates to it, we have the familiar quantum superposition:
In this case, the phase term disappears, since , and thus .
The classical oracle returns for any input such that , and returns otherwise. If we use the same phase kickback trick from the Deutsch-Jozsa algorithm and act on a qubit in the state , we get the following transformation:
The algorithm to reveal the hidden bit string follows naturally by querying the quantum oracle with the quantum superposition obtained from the Hadamard transformation of . Namely,
Because the inverse of the Hadamard gates is again the Hadamard gates, we can obtain by
2. Example
Let's go through a specific example for qubits and a secret string . Note that we are following the formulation in Reference [2] that generates a circuit for the Bernstein-Vazirani quantum oracle using only one register.
- The register of two qubits is initialized to zero:
Use the widget bv_widget below. Press the buttons to apply the different steps, and try to follow the algorithm through. You can change the number of input qubits and the value of the secret string through the first two positional arguments.
We first set the number of qubits used in the experiment, and the hidden bit string to be found by the algorithm. The hidden bit string determines the circuit for the quantum oracle.
We then use Qiskit to program the Bernstein-Vazirani algorithm.
We can see that the result of the measurement is the hidden string 011.
As we can see, most of the results are 011. The other results are due to errors in the quantum computation.
The above implementation of Bernstein-Vazirani is for a secret bit string . Modify the implementation for a secret string . Are the results what you expect? Explain.
The above implementation of Bernstein-Vazirani is for a secret bit string . Modify the implementation for a secret string . Are the results what you expect? Explain.
5. References
Ethan Bernstein and Umesh Vazirani (1997) "Quantum Complexity Theory" SIAM Journal on Computing, Vol. 26, No. 5: 1411-1473, doi:10.1137/S0097539796300921.
Jiangfeng Du, Mingjun Shi, Jihui Wu, Xianyi Zhou, Yangmei Fan, BangJiao Ye, Rongdian Han (2001) "Implementation of a quantum algorithm to solve the Bernstein-Vazirani parity problem without entanglement on an ensemble quantum computer", Phys. Rev. A 64, 042306, 10.1103/PhysRevA.64.042306, arXiv:quant-ph/0012114.