Deutsch–Jozsa and Bernstein–Vazirani
Deutsch’s algorithm saved one query out of two. This lesson runs the same circuit on qubits. The saving grows, and you will also see why the first version of the result is less impressive than it sounds, and what a more convincing one looks like.
Many inputs at once
Section titled “Many inputs at once”A function of bits takes a string and returns one bit. Its oracle works as before, with input wires and one output wire:
Phase kickback carries over unchanged. With the output wire in ,
for every one of the strings .
We also need to know what a row of Hadamards does. One sends to . Applying one to each of qubits and multiplying out gives
The sum runs over all -bit strings . In particular is the equal superposition of every string, with all signs positive. And because is its own inverse, a row of Hadamards turns the state on the right back into . That second fact does most of the work in this lesson.
The Deutsch–Jozsa problem
Section titled “The Deutsch–Jozsa problem”You are given on bits with a promise: it is either constant (the same output for every input) or balanced (output 0 for exactly half of the inputs and 1 for the other half). Which is it?
The circuit is Deutsch’s, widened:
- Start with qubits in and the output qubit in .
- Apply to every qubit.
- Call the oracle once.
- Apply to the first qubits and measure them.
After step 3 the first qubits are in the state . Step 4 asks how much of that state lies along the all-positive superposition, since that is what the Hadamards send to . The amplitude of at the end is
- If is constant, every term has the same sign and the amplitude is . You measure all zeros with certainty.
- If is balanced, half the terms are and half are . The amplitude is exactly 0, so you never measure all zeros.
All zeros means constant. Anything else means balanced. One query, for any .
For , a function outputs 1 on two of the eight inputs and 0 on the other six. You run the circuit many times and see all zeros about a quarter of the time. What do you conclude?
The amplitude of all zeros is , which is neither nor 0. The algorithm only gives a meaningful answer when the promise holds.
How big is the speedup?
Section titled “How big is the speedup?”A classical algorithm that must be certain of its answer is in trouble. It could query half of all inputs, see the same output every time, and still not know. In the worst case it needs queries, against one. That is an exponential gap.
But it is a gap against a very demanding opponent. Suppose the classical algorithm is allowed a small chance of error. It queries inputs chosen at random. If it ever sees two different outputs, the function is balanced for sure. If a balanced function is being tested, the chance that random outputs all agree is about . Twenty queries already bring the error below one in a hundred thousand, whatever is.
So Deutsch–Jozsa shows an exponential advantage only for exact, deterministic computation. Against randomised algorithms, which are what we use in practice, the advantage is a small constant. The result matters historically, as the first sign that quantum queries behave differently, and less as a speedup in its own right.
A hidden string
Section titled “A hidden string”The Bernstein–Vazirani problem uses the same circuit and gives a cleaner result. This time the promise is that
for some secret string . The task is to find .
Classically the natural approach is to query the strings with a single 1 in them: , , and so on. That takes queries, and no classical method does better, randomised or not, because each query returns one bit and contains bits of information.
First, see what this oracle looks like as a circuit. The output wire must be flipped once for each position where both and have a 1. That is a CNOT from each input wire where has a 1.
Build the oracle for , so that the bottom wire ends up holding . You are shown the input , and your circuit is also tested on four other inputs.
This puzzle needs JavaScript.
Hint
The output wire should flip when and again when . It should ignore .
Now run the Deutsch–Jozsa circuit on such an oracle. After the kickback the input qubits hold
Compare this with the formula for a row of Hadamards. It is exactly . So the final Hadamards turn it into , and measuring the input qubits gives the secret string, every bit of it, with certainty.
In the next puzzle the oracle is a sealed box. It hides a three-bit string that is not . The target state is not shown: finding it is the point.
Find the hidden string with one use of the Oracle. The puzzle is solved when the top three qubits hold and the bottom qubit is left in . Read from the first three bits of your state.
This puzzle needs JavaScript.
Hint
Put an on all four wires, then the oracle, then an on each of the top three wires.
One query instead of . The gap is modest, linear and not exponential, but it is honest: it survives when the classical algorithm is allowed to use randomness and to make errors.
If you look closely at the solved circuit, you may notice that one of the top wires did not need its two Hadamards. Where has a 0 the oracle never touches the wire, and the two Hadamards cancel. You could not have known which wire in advance, which is why the algorithm puts them everywhere.
Why does one query suffice for Bernstein–Vazirani?
The output wire is never measured, and amplitudes cannot be read directly. The sign pattern happens to be exactly what a row of Hadamards produces from , so a row of Hadamards undoes it.
What these algorithms teach
Section titled “What these algorithms teach”Neither problem is useful in itself. What they establish is a method:
- Put the input register into an equal superposition.
- Use kickback to write the function into the phases.
- Apply a transform that turns a pattern in the phases into a basis state you can measure.
In this lesson the pattern was a set of signs and the transform was a row of Hadamards. The Hadamard transform can detect only one kind of pattern, the kind given by . The next step historically was Simon’s problem, in which a similar circuit finds a hidden string with exponentially fewer queries than any classical algorithm, randomised ones included. Simon’s algorithm in turn led Peter Shor to ask what a more powerful transform could detect.
That transform is the quantum Fourier transform, and the patterns it detects are periods.
Remember it
Section titled “Remember it”What is the promise in the Deutsch–Jozsa problem, and what must you decide?
The function is either constant or balanced (0 on exactly half of the inputs). Decide which.
In the Deutsch–Jozsa algorithm, what does measuring all zeros mean?
The function is constant. Any other outcome means it is balanced.
Against which classical algorithms is Deutsch–Jozsa exponentially faster?
Only deterministic ones that must be certain. A randomised algorithm gets the answer with a tiny error from a handful of queries.
What does the Bernstein–Vazirani algorithm find, and with how many queries?
The hidden string in , with one query. Classically it takes .
What does a row of Hadamards do to the basis state of qubits?
.