An implementation of the Bernstein-Vazirani quantum algorithm.
- The 1st section uses for n=4 qubits with the secret string 1011
- The 2nd section is applicable to any secret string of any length
The Bernstein-Vazirani algorithm is an example of a quantum algorithm that outperforms classical methods.
- Say you have a secret string of 1's and 0's. Then put the secret string in a box (function).
- Now the computer wants to determine what's inside the box by guessing the secret number.
Classically,
- the comp can apply AND operations by guessing n attempts
- or exponentially: for n-bit secretNum, tries from 0 to 2^n-1
Using Bernstein-Vazirani's alg
- finds secretNum in 1 attempt (regardless of secretNum size)
- initialize 1st n qubits in |0> state, & last qubit in |1> state
- apply Hadamard gates to all qubits
- build oracle (box containing secret number)
- measure the 1st n qubits in the Bell basis (applying h gates before measurements)
n=4 qubits, secret string: 1011
output: {'1011': 1}
n=11 qubits, secret string: 10111100101
The histogram shows how 100% of the results contain the secret number (this example uses 1 shot).
output: {'10111100101': 1}