Skip to content

Interference and Deutsch's algorithm

Deutsch’s algorithm solves a problem nobody needs solved. It is worth learning anyway, because it is the smallest complete example of how a quantum algorithm gets its answer, and the same two ideas reappear in Shor’s and Grover’s algorithms.

It assumes you know the material in the Quantum Information track up to entanglement.

Someone gives you a function ff that takes one bit and returns one bit. There are only four such functions:

f(0)f(0) f(1)f(1) Kind
always 0 0 0 constant
always 1 1 1 constant
identity 0 1 balanced
negation 1 0 balanced

You cannot see inside ff. You can only call it. The question is: is ff constant or balanced?

Classically you must call it twice. One value, say f(0)=0f(0) = 0, is consistent with both a constant function and a balanced one. We count the cost of an algorithm here by the number of calls, or queries, it makes.

A quantum computer can answer with one query.

Gates must be reversible, and a constant function is not, so we cannot use ff as a gate directly. The standard fix is a two-qubit gate that keeps the input and writes the output onto a second qubit:

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

where ⊕\oplus is addition modulo 2. This gate is called an oracle. For the identity function f(x)=xf(x) = x, the oracle flips the second qubit exactly when the first is 1. That is CNOT.

Here is the first idea. Put the second qubit in the state ∣−⟩=12(∣0⟩−∣1⟩)\ket{-} = \tfrac{1}{\sqrt2}(\ket{0} - \ket{1}) before calling the oracle.

If f(x)=0f(x) = 0, the second qubit is untouched. If f(x)=1f(x) = 1, it is flipped, and flipping ∣−⟩\ket{-} gives 12(∣1⟩−∣0⟩)=−∣−⟩\tfrac{1}{\sqrt2}(\ket{1} - \ket{0}) = -\ket{-}. Either way:

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

The second qubit ends as it began. The value of f(x)f(x) has turned into a sign on the first qubit’s state. This is called phase kickback.

For a single basis state ∣x⟩\ket{x} that sign is a global phase and means nothing. For a superposition of ∣0⟩\ket{0} and ∣1⟩\ket{1} it becomes a relative phase, and a relative phase can be measured.

PuzzleKick a phase back

The top qubit starts in ∣0⟩\ket{0} and the bottom in ∣1⟩\ket{1}. Make the state ∣−⟩∣−⟩\ket{-}\ket{-}, which written out is 12(∣00⟩−∣01⟩−∣10⟩+∣11⟩)\tfrac12\bigl(\ket{00} - \ket{01} - \ket{10} + \ket{11}\bigr).

This puzzle needs JavaScript.

Hint

First make ∣+⟩∣−⟩\ket{+}\ket{-} with one HH on each wire. Then apply the oracle for f(x)=xf(x) = x, which is CNOT.

Look at what the CNOT did. It was controlled by the top qubit, yet the bottom qubit did not change and the top one went from ∣+⟩\ket{+} to ∣−⟩\ket{-}. Which qubit is “the control” depends on the basis you look in.

Now the second idea: interference. Start the first qubit in ∣+⟩\ket{+}, so the oracle is queried on both inputs in superposition. After kickback the first qubit is in

12((−1)f(0)∣0⟩+(−1)f(1)∣1⟩).\frac{1}{\sqrt2}\Bigl((-1)^{f(0)}\ket{0} + (-1)^{f(1)}\ket{1}\Bigr).
  • If ff is constant, the two signs are equal and this is ±∣+⟩\pm\ket{+}.
  • If ff is balanced, the two signs differ and this is ±∣−⟩\pm\ket{-}.

A final HH turns ∣+⟩\ket{+} into ∣0⟩\ket{0} and ∣−⟩\ket{-} into ∣1⟩\ket{1}. Measure the first qubit: 0 means constant, 1 means balanced. The oracle was called once.

PuzzleRun Deutsch's algorithm

The Oracle gate is UfU_f for f(x)=xf(x) = x: a CNOT from the top wire to the bottom wire, which you cannot turn around. Using it and HH, leave the top qubit in ∣1⟩\ket{1} with certainty, showing that ff is balanced. The target is ∣1⟩∣−⟩\ket{1}\ket{-}.

This puzzle needs JavaScript.

Try the constant functions in this sandbox. Build the same circuit, but replace the oracle: for f(x)=0f(x) = 0 the oracle does nothing, and for f(x)=1f(x) = 1 it is an XX on the bottom wire. The top qubit should come out as 0 both times.

SandboxOther oracles

Press Measure ×100 to see the outcome statistics for your circuit.

This puzzle needs JavaScript.

It is tempting to say the quantum computer “evaluated ff on both inputs at once”. That is misleading. If you measured straight after the oracle you would learn about one input at most, the same as classically.

What the algorithm does is narrower and cleverer. It never learns f(0)f(0) or f(1)f(1). It arranges for the amplitudes to interfere so that a joint property of the two values, whether they are equal, lands in a place where one measurement can read it.

Check

After running Deutsch’s algorithm once, what do you know about ff?

This is the pattern for quantum algorithms generally. Prepare a superposition, let the problem imprint phases on it, then interfere the paths so that wrong answers cancel. Saving one query out of two is a small win. The next lesson scales the same circuit to nn qubits, where the saving over any classical algorithm that must be certain of its answer becomes exponential.

RecallAnswer from memory, then check
LessonFinish this lesson