r/QuantumComputing • u/Weekly-Maize-3414 • 20d ago
Question Could anyone explain how does the Bernstein-Vazarani Algorithm actually works?
I'm new into the QC community and recently started learning about the most importants algorithms. I'm doing all by myself with free yt content and blogs from internet.
Today i was learning about this Algorithm that helps to find a string s in the function
f : {0 , 1}^n -> {0, 1}
f(x) = x * s (mod2)
And after aplying the Algorithm the resulkt of the measure is Prob. = 1 .
But with that how can i really find the string s ?
10
Upvotes
4
u/ollpu 20d ago edited 1d ago
The best way to understand it I've found:
Take the B-V circuit, like in https://commons.wikimedia.org/wiki/File:Bernstein-Vazirani_algorithm_circuit_in_Qiskit.png
The secret oracle may do some more complicated operations, but the definition guarantees that its net effect is just "some CNOT gates with control on some input qubit and target on the output qubit". There is a CNOT if the corresponding bit in s is set. The oracle is then surrounded by walls of Hadamard gates on both sides.
There's this gate identity that when a CNOT gate is surrounded by Hadamards, it flips around. See A here: https://www.researchgate.net/publication/335196503/figure/fig2/AS:11431281379143372@1744756696738/Identities-for-two-qubit-quantum-gates-A-The-effect-of-a-quantum-CNOT-in-the-Hadamard_W640.jpg. You can prove it by calculating the matrix product or using the Hadamard basis (|+> and |->). See also phase kickback.
So we can move each CNOT gate across the wall of Hadamards, and they all flip around. Also remember that two Hadamard gates with nothing in between cancel each other out. We're left with a circuit with a bunch of CNOTs that control on the output and target the inputs! Simply setting the "input" qubit to |1> will activate each CNOT gate and reveal the secret in one run.
1
u/david-marqov Working in Industry 19d ago
The measurement result is s. You don’t calculate anything afterwards.
After the oracle (with the ancilla in |−⟩), the input register holds Σₓ (−1)^(s·x) |x⟩. The last layer of Hadamards makes every basis state cancel except |s⟩. So just before measurement the register is exactly |s⟩, and “probability 1” means you’ll read out s every time. If s = 1011, you measure 1011.
This matters because classically you’d need n queries (f(100…0) gives the first bit of s, f(010…0) the second, and so on). Quantum needs one.
If you’re using Qiskit, note that it prints bitstrings with qubit 0 on the right, so the histogram label may look reversed compared with your s. If it's useful, I can put this in Marqov as an example you can run on different sims/qpus, so you can get more hands-on than those videos.
1
u/Longjumping-Ad514 18d ago edited 18d ago
It’s a handcrafted toy problem. The way it’s described makes it seem more mysterious than it is. Algebraic derivation IMO hides the main gist of the problem.
Once you push (or multiply out) a superposition of all 1s through the oracle, the oracle will flip some 1 to -1. Which ones? I’ll flip just enough make the state vector match exactly one row of the Haddamard matrix. The problem is hand-crafted to do exactly this. State vector changes from all ones to being equal one of the rows of the Haddamard matrix.
What happens when multiply a row (as a vector) of a Haddamard by a Haddamard, at the end? All rows of the Haddamard matrix are orthogonal. This means any row multiplied by itself is 1, any row multiplied by every other row is zero.
Now take a careful look at how matrix multiplication is defined. State vector equal to one row, is multiplied by every row of the matrix, top to bottom. Every index of the output, contains the result of that multiplication, top to bottom.
Where the state vector and matrix row are equal, you get one, otherwise you get zero, due to orthogonality as mentioned above.
If you look at the output, the index (of the state vector / result) that ends up as 1, corresponds to the “secret code” the oracle contained, if you read the index as binary instead of decimal.
Ta da.
Flipping of the sign of -1 is called phase kickback. Is more easily understood on the Deutches problem.
1
u/[deleted] 20d ago
[removed] — view removed comment