Skip to content

Deutsch–Jozsa and Bernstein–Vazirani

Deutsch’s algorithm saved one query out of two. This lesson runs the same circuit on nn 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.

A function of nn bits takes a string x=x0x1…xn−1x = x_0x_1\dots x_{n-1} and returns one bit. Its oracle works as before, with nn input wires and one output wire:

Uf∣x⟩∣y⟩=∣x⟩∣y⊕f(x)⟩.U_f\ket{x}\ket{y} = \ket{x}\ket{y \oplus f(x)}.

Phase kickback carries over unchanged. With the output wire in ∣−⟩\ket{-},

Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩U_f\ket{x}\ket{-} = (-1)^{f(x)}\ket{x}\ket{-}

for every one of the 2n2^n strings xx.

We also need to know what a row of Hadamards does. One HH sends ∣x⟩\ket{x} to 12(∣0⟩+(−1)x∣1⟩)\tfrac{1}{\sqrt2}\bigl(\ket{0} + (-1)^{x}\ket{1}\bigr). Applying one to each of nn qubits and multiplying out gives

H⊗n∣x⟩=12n∑z(−1)x⋅z∣z⟩,x⋅z=x0z0+x1z1+⋯+xn−1zn−1(mod2).H^{\otimes n}\ket{x} = \frac{1}{\sqrt{2^n}}\sum_{z} (-1)^{x\cdot z}\ket{z}, \qquad x\cdot z = x_0z_0 + x_1z_1 + \dots + x_{n-1}z_{n-1} \pmod 2.

The sum runs over all nn-bit strings zz. In particular H⊗n∣00…0⟩H^{\otimes n}\ket{00\dots0} is the equal superposition of every string, with all signs positive. And because HH is its own inverse, a row of Hadamards turns the state on the right back into ∣x⟩\ket{x}. That second fact does most of the work in this lesson.

You are given ff on nn 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:

  1. Start with nn qubits in ∣0⟩\ket{0} and the output qubit in ∣1⟩\ket{1}.
  2. Apply HH to every qubit.
  3. Call the oracle once.
  4. Apply HH to the first nn qubits and measure them.

After step 3 the first nn qubits are in the state 12n∑x(−1)f(x)∣x⟩\tfrac{1}{\sqrt{2^n}}\sum_x (-1)^{f(x)}\ket{x}. Step 4 asks how much of that state lies along the all-positive superposition, since that is what the Hadamards send to ∣00…0⟩\ket{00\dots0}. The amplitude of ∣00…0⟩\ket{00\dots0} at the end is

12n∑x(−1)f(x).\frac{1}{2^n}\sum_x (-1)^{f(x)}.
  • If ff is constant, every term has the same sign and the amplitude is ±1\pm1. You measure all zeros with certainty.
  • If ff is balanced, half the terms are +1+1 and half are −1-1. The amplitude is exactly 0, so you never measure all zeros.

All zeros means constant. Anything else means balanced. One query, for any nn.

Check

For n=3n = 3, 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?

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 2n−1+12^{n-1} + 1 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 kk 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 kk random outputs all agree is about 2×(12)k2 \times (\tfrac12)^k. Twenty queries already bring the error below one in a hundred thousand, whatever nn 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.

The Bernstein–Vazirani problem uses the same circuit and gives a cleaner result. This time the promise is that

f(x)=s⋅x=s0x0+s1x1+⋯+sn−1xn−1(mod2)f(x) = s\cdot x = s_0x_0 + s_1x_1 + \dots + s_{n-1}x_{n-1} \pmod 2

for some secret string ss. The task is to find ss.

Classically the natural approach is to query the strings with a single 1 in them: f(100…0)=s0f(100\dots0) = s_0, f(010…0)=s1f(010\dots0) = s_1, and so on. That takes nn queries, and no classical method does better, randomised or not, because each query returns one bit and ss contains nn 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 ss and xx have a 1. That is a CNOT from each input wire where ss has a 1.

PuzzleBuild the oracle

Build the oracle for s=101s = 101, so that the bottom wire ends up holding y⊕(x0+x2)y \oplus (x_0 + x_2). You are shown the input x=100x = 100, and your circuit is also tested on four other inputs.

This puzzle needs JavaScript.

Hint

The output wire should flip when x0=1x_0 = 1 and again when x2=1x_2 = 1. It should ignore x1x_1.

Now run the Deutsch–Jozsa circuit on such an oracle. After the kickback the input qubits hold

12n∑x(−1)s⋅x∣x⟩.\frac{1}{\sqrt{2^n}}\sum_x (-1)^{s\cdot x}\ket{x}.

Compare this with the formula for a row of Hadamards. It is exactly H⊗n∣s⟩H^{\otimes n}\ket{s}. So the final Hadamards turn it into ∣s⟩\ket{s}, 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 101101. The target state is not shown: finding it is the point.

PuzzleRead the hidden string

Find the hidden string with one use of the Oracle. The puzzle is solved when the top three qubits hold ss and the bottom qubit is left in ∣−⟩\ket{-}. Read ss from the first three bits of your state.

This puzzle needs JavaScript.

Hint

Put an HH on all four wires, then the oracle, then an HH on each of the top three wires.

One query instead of nn. 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 ss 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.

Check

Why does one query suffice for Bernstein–Vazirani?

Neither problem is useful in itself. What they establish is a method:

  1. Put the input register into an equal superposition.
  2. Use kickback to write the function into the phases.
  3. 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 s⋅xs\cdot x. 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.

RecallAnswer from memory, then check
LessonFinish this lesson