Deutsch–Jozsa step by step: one query to tell constant from balanced

Follow the amplitudes through the five steps, see why the chance of 00…0 is exactly 1 or 0 under the promise, and what happens when the promise is broken.

10 min read · reviewed December 2026

The problem

A function f takes n bits to one bit. It is promised to be constant, giving the same output for every input, or balanced, giving 0 for exactly half the inputs and 1 for the other half. A classical algorithm must query inputs one at a time. To be certain, it needs 2^(n−1) + 1 queries in the worst case, because the first 2^(n−1) answers could all agree even for a balanced function. The quantum circuit asks once.

The circuit, five steps

  • Start with n input qubits in |0⟩ and one answer qubit in |0⟩.
  • Flip the answer qubit with X so that it is |1⟩.
  • Apply Hadamard to every qubit. The inputs are an equal superposition of all x, and the answer qubit is |−⟩.
  • Call the oracle once. By phase kickback, each x picks up the sign (−1)^f(x).
  • Apply Hadamard to the n input qubits and measure them.

Use Back and Next in the lab’s Deutsch–Jozsa tab to walk through these steps. The table shows the input amplitudes at each stage.

Why the answer is clean

After the last Hadamards the amplitude of the all-zeros string is the average of the signs: (1/N) Σ_x (−1)^f(x), where N = 2^n. If f is constant, every sign is the same and the average is +1 or −1, so the chance of reading 00…0 is 100 percent. If f is balanced, half the signs are +1 and half are −1, the average is 0, and the chance of 00…0 is exactly 0. Any other reading means balanced.

That is the whole algorithm. The decision rule is: read the input register; if it is all zeros the function is constant, otherwise balanced.

amplitude of 00…0 = (1/N) Σ_x (−1)^f(x)

chance of 00…0 = | (N₀ − N₁) / N |², where N₀, N₁ count inputs with f = 0, 1

Worked example with three bits

The table is computed by the same simulator as the lab. The first two rows are constant and give 100 percent. The other rows are balanced and give 0 percent. A balanced function can be very different from another one, and the circuit does not care.

Three input bits. Computed by the lab’s simulator.
Function ff(000) … f(111)ClassChance of 000
Constant 00 0 0 0 0 0 0 0constant100.0%
Constant 11 1 1 1 1 1 1 1constant100.0%
Balanced: first bit0 0 0 0 1 1 1 1balanced0.0%
Balanced: parity of all bits0 1 1 0 1 0 0 1balanced0.0%
Balanced: random half (seed 7)0 1 1 0 1 0 1 0balanced0.0%

When the promise is broken

Click cells in the lab’s truth table so that f has, say, three ones among eight inputs. The chance of 000 is now neither 0 nor 1. It equals ((N₀ − N₁)/N)², here (2/8)² = 6.25 percent. The circuit still runs, but its answer no longer settles anything, because the algorithm was only designed to decide between the two promised cases.

This is a general feature of promise problems. A quantum algorithm can be far better than a classical one on the promised inputs and give no useful guarantee outside them.

History note

Deutsch posed the one-bit version in 1985, and Deutsch and Jozsa generalised it in 1992. The compact one-query circuit used here is usually credited to Cleve, Ekert, Macchiavello and Mosca in 1998. See the sources at the foot of the lab page.

Try it

Questions

Why 2^(n−1) + 1 classical queries?

A balanced function has 2^(n−1) zeros and 2^(n−1) ones. You could see 2^(n−1) equal answers and still be unsure. The next query settles it.

Does the circuit work for any balanced function?

Yes. The amplitude of 00…0 depends only on how many inputs map to 0 and how many to 1, and balanced means those counts are equal.

Read next

Sources and further reading

Check the original sources for exact citations.