Building the oracle: the part that decides the real cost

How a yes/no checker becomes a phase flip, how the circuit marks a bitstring, and why oracle cost matters more than the iteration count.

9 min read · reviewed December 2026

From a checker to a phase oracle

You start with a classical function f(x) that returns 1 for a correct answer and 0 otherwise, such as “does this assignment satisfy the formula” or “does this key decrypt the message to readable text”. Turning f into a reversible quantum circuit is mechanical: every classical circuit can be rewritten with reversible gates such as the Toffoli gate.

The reversible circuit computes f(x) into a spare helper qubit. If the helper is prepared in the minus state, the computation produces a phase of (−1)^f(x) on the input instead of changing the helper. This is called phase kickback, and it is the same trick used in Deutsch–Jozsa and Bernstein–Vazirani. The helper is then uncomputed so that it does not stay entangled with the rest.

Marking a known bitstring

In this site’s lab the marked items are fixed in advance, so the oracle is simple. A multi-controlled Z gate flips the sign of the single state |11…1⟩. To mark another bitstring, sandwich the gate between X gates on every qubit where the target has a 0. Those X gates temporarily turn the target into |11…1⟩.

For n = 3 and target 101, the oracle is: X on qubit 1, then a controlled-controlled-Z across all three qubits, then X on qubit 1 again. The controlled-controlled-Z is built from H on the last qubit, a Toffoli gate and H again.

  • Target 111: no X gates are needed.
  • Target 000: X on every qubit before and after.
  • Several targets: apply this sandwich once for each marked item.

A fixed oracle like this “knows” the answer, so it is only a teaching device. In a real search the oracle is a checker circuit that does not know the answer.

The diffusion operator as a circuit

Diffusion is a reflection about the uniform state. In circuit form: H on every qubit, X on every qubit, a multi-controlled Z, X on every qubit, H on every qubit. The middle part reflects about |0…0⟩ and the surrounding H gates move that reflection onto the uniform state.

What the circuit costs

A multi-controlled Z on n qubits can be built from O(n) Toffoli-type gates if extra helper qubits are available, and from more gates if none are. In the Circuit Builder you can build the three-qubit version yourself and read off its depth. The number of two-qubit gates is the number that tends to set the error on real hardware.

For a real problem, the oracle is the expensive part. It might evaluate a hash function, a cipher or a satisfiability formula, and it must run once per iteration, forward and then backward to clean up. The total cost is roughly (number of iterations) × (cost of the oracle). This is why a √N speedup in query count does not automatically translate into a √N speedup in time.

Checking an oracle before trusting it

  • Run it on a single basis state and confirm it flips the sign only for marked items.
  • In the Circuit Builder, use the State vector tab to read off the sign of each amplitude after the oracle.
  • Check that the helper qubits return to |0⟩, otherwise they stay entangled and spoil the interference.

Try it

Questions

Does the oracle know the answer?

In a real application it does not. It only checks a candidate. In demonstrations like the lab, the oracle is built from the known answer for teaching.

Why must helper qubits be returned to |0⟩?

If they hold information about the candidate, they become entangled with it and the interference that boosts the answer no longer works.

Read next

Sources and further reading