Imagine I have a secret number and you are to guess what that number is. How many tries do you think you will need?
To put it into context, say we have the binary string "1011001" with 7 bits. Classically, a computer would require 7 queries to get that right. First, for the 7th bit, it will perform an AND operation between "1" and the 7th bit. If the result is 1, the computer will be sure that the 7th bit is 1; whereas, if the result is 0, the computer will know that the last bit is 0. Similarly, for every bit starting from the last, the computer will have to make as many queries as the number of bits (n) in the string to finally fetch the secret value. However, quantum computer promises to guess the number with a single shot! The quantum algorithm, named after Ethan Bernstein and Umesh Vazirani, put forth in 1992, explores more on how this can be made possible.
In this tutorial, we will walk you through the Qiskit implementation of Bernstein-Vazirani algorithm.
First, let's import all of the necessary libraries:
Bernstein-Vazirani algorithm can be referred to as an extension of Deutsch-Jozsa algorithm. In both cases, we depend on an oracle based model.
Let us first solve a number-specific example. As secret_number = '1011001' (7-bit long string), we need to initialize a quantum circuit with 7+1 qubits and 7 classical bits where we will store the results.
As usual, we need to create a superposition (|+> state) of all input quantum bits to leverage interference of qubits. The last (extra) qubit is, however, put into a |-> state:
Now, for each '1' we see in the bit string, we will add a CX-gate with control on the succeeding qubit:
The series of CX-gates bounded by two barriers is analogous to Oracle operation- exclusively for the provided bit-string. (Generalizing later). Next, we will again have to apply Hadamard gate on all qubits and finally measure.
Now, let us run the circuit on a simulator to see if it really matches with the number presumed:
As we can see, the algorithm guessed the secret number with a single shot! But, we cannot always know beforehand what the number is. So we need a more generalized circuit that would work independently of the bit-string. So, the oracle function would take the following input:
As we initialize the qubits in |+> state through applying n Hadamard gates, we get the following state:
We will apply an X-gate on the last qubit before Hadamard gate.
Now let us construct the oracle: U_f which performs the following transformation:
Here, similar to the previous example, the Oracle performs a CX-operation on the qubit for each "1" in the bit-string. However, in this case we ourselves will not know when to apply C-NOT gate, rather we will code for a black-box that will do the task for us. In this case, the CX gate denotes a mod 2 operation. The oracle function, this time unlike Deutsch's algorithm, does not require to be balanced.
After the oracle query, we need to apply Hadamard gates for unitary operation. To wrap-up, add measurement gates:
Let us check if our generalized circuit works! (Make sure you run the circuit on the simulator with a single shot)
If you want to run the circuit on a real quantum device:
>> least busy backend: ibmq_lima
Turns out the string assigned to secret_number, in fact, has the highest probability of outcome!