5 Selecting outcomes with quantum oracles
This chapter introduces how quantum oracles let us specify “good” outcomes inside a quantum computation, a capability that underpins search and probability-estimation tasks. Unlike classical databases—where writing a WHERE clause is easy but scanning is costly—quantum workflows make the search step cheap (for example with Grover’s algorithm) once an efficient oracle is available, making oracle design the hard and crucial part. The chapter motivates oracles with practical contexts such as unstructured search, optimization, and sampling, and frames them as a universal tagging mechanism: they identify outcomes that satisfy a desired predicate so later algorithmic steps can amplify, isolate, or measure them.
Two oracle styles are developed. Phase oracles flip the phase of marked amplitudes, while bit oracles entangle marked outcomes with an extra “tag” qubit that reads 1 for good and 0 for bad. The text first builds classical, state-vector-level versions to build intuition, then constructs true quantum circuits in a Python simulator. Readers learn to assemble reusable subcircuits with append and controlled-append, employ multi-controlled gates, and manage ancilla qubits. Practical helper routines create phase and bit oracles from a set of target items, and examples show their effects on uniform and random states, reinforcing how amplitude signs or tag-bit populations encode the predicate without measuring the data qubits.
The chapter also shows how to convert between oracle types: a bit oracle can be realized from a phase oracle by controlling it with an ancilla prepared in superposition, and a phase oracle can be synthesized from a bit oracle by sandwiching a phase rotation on the tag between two calls to the bit oracle. A thematic example marks outcomes whose binary strings have no consecutive ones, revealing that the count of “good” states follows the Fibonacci sequence and that carefully chosen rotations can concentrate probability on exactly those outcomes, with probability ratios tied to the golden ratio. By the end, readers have both the conceptual model and concrete Python circuits needed to define, implement, and interchange oracles—foundational pieces for Grover’s algorithm and many optimization and machine-learning workflows that follow.
A dependency diagram of concepts introduced in this chapter. Oracles are also an essential building block of Grover’s algorithm, covered in the next chapter.
The effect of a phase oracle on the amplitudes of a state with good outcomes \(g\) and bad outcomes \(b\).
The effect of a phase oracle on a \(n = 3\) qubit state prepared with amplitudes in equal superposition and a single good outcome 3.
The effect of a bit oracle on the amplitudes of a state with good outcomes \(g\) and bad outcomes \(b\).
The effect of a bit oracle on an \(n = 3\) qubit state prepared with amplitudes in equal superposition and a single good outcome 3.
The effect of a bit oracle on a random \(n = 3\) qubit state with a single good outcome 3.
The phase oracle circuit for \(n = 3\) qubits and a single good outcome 3.
The state tables before and after applying a circuit that applies a Hadamard gate to each of \(n = 3\) qubits followed by the phase oracle for a single good outcome 3.
The state tables before and after applying a circuit that applies a Hadamard gate to each of \(n = 3\) qubits followed by the phase oracle for good outcomes 1, 3, and 5.
The state tables before and after applying a circuit that applies a Hadamard gate to each of \(n = 3\) qubits followed by the bit oracle for a single good outcome 3.
The state tables before and after applying a circuit that applies a Hadamard gate to each of \(n = 3\) qubits followed by the phase oracle for good outcomes 1, 3, and 5.
Circuit diagram of a bit oracle using the corresponding phase oracle \(O_P\).
Circuit diagram of the phase oracle \(O_P\) for \(n = 3\) qubits and good outcomes 1, 3, and 5.
Circuit diagram of the bit oracle created using the phase oracle \(O_P\) for \(n = 3\) qubits and good outcomes 1, 3, and 5 in figure 5.12.
The state tables before and after applying the bit oracle in figure 5.15 to a random \(n = 3\) qubit state.
Circuit diagram of a phase oracle using the corresponding bit oracle \(O_B\).
Circuit diagram of the bit oracle \(O_B\) for \(n = 3\) qubits and good outcomes 1, 3, and 5.
Circuit diagram of the phase oracle created using the bit oracle \(O_B\) for \(n = 3\) qubits and good outcomes 1, 3, and 5 in figure 5.19.
The state tables before and after applying the phase oracle in figure 5.20 to a random \(n = 3\) qubit state.
Table 5.2 A single-qubit state with good outcomes \(G_1\).
Table 5.3 A two-qubit state with good outcomes \(G_2\).
Table 5.4 A three-qubit state with good outcomes \(G_3\).
Summary
- To solve problems that search for specific outcomes or estimate the probability of specific outcomes, we need mechanisms to identify outcomes of interest.
- The quantum circuits used to identify certain outcomes, which we call "good" outcomes, are called oracles.
- Phase oracles rotate the amplitudes of the good outcomes by \(180^\circ\).
- Bit oracles entangle good outcomes with an additional qubit in such a way that the good ones are associated with its measurement of 1, and the bad ones with its measurement of 0.
- We can convert from a bit oracle to a phase oracle and vice versa.
FAQ
What is a quantum oracle, and why is it important for search and estimation tasks?
A quantum oracle is a circuit that recognizes “good” outcomes and tags them so downstream algorithms can find or amplify them. It plays the role of a predicate (like a SQL WHERE clause) but in quantum form. In unstructured search, specifying the oracle efficiently is the hard part, while the search or amplification (for example with Grover’s algorithm) is comparatively easy.What’s the difference between a phase oracle and a bit oracle?
- Phase oracle: multiplies the amplitudes of good outcomes by −1 (a 180° phase flip).- Bit oracle: entangles good outcomes with an extra “tag” qubit so that good outcomes correlate with measuring the tag qubit as 1, and bad ones with 0.
How do I specify “good” outcomes programmatically?
Define a classical predicate over the integers 0..2^n−1 that returns True for good items and False otherwise (for example,lambda k: k in [1, 3, 5]). This predicate is used both in classical simulations (for intuition) and to construct the corresponding quantum oracle circuits.How does a classical phase-oracle simulator work?
Given a state vector and a predicate, it scans all indices and multiplies by −1 the amplitudes whose indices satisfy the predicate. This mirrors the 180° phase rotation that a quantum phase oracle applies to good outcomes.How does a classical bit-oracle simulator work, and how are indices arranged after adding a tag qubit?
It appends amplitudes for the extra qubit, doubling the state length from N to 2N. Outcomes 0..N−1 correspond to tag=0; outcomes N..2N−1 correspond to tag=1. For each good item k, it moves the amplitude from index k to index N+k (setting the original to 0), so measuring the tag qubit as 1 points to good outcomes.How do I assemble oracle circuits from reusable building blocks in the simulator?
UseQuantumCircuit.append to apply a reusable subcircuit to a register of matching size, and QuantumCircuit.c_append to append it controlled on an external qubit. Multi-controlled gates are available via methods like mcx (multi-controlled X) and mcp (multi-controlled phase), following Qiskit conventions.How do I implement a phase oracle circuit from a list of good outcomes?
For each good item m: (1) X-flip qubits whose corresponding bits in m are 0 to align the all-ones pattern, (2) apply a multi-controlled phase of π (e.g.,mcp(pi,...)) on the most significant target, and (3) uncompute the X flips. Repeating this for every item yields a circuit that flips the sign of all tagged amplitudes.How do I implement a bit oracle circuit from a list of good outcomes?
Add an ancilla (tag) qubit. For each good item m: (1) X-flip the “0” bits of m, (2) apply a multi-controlled X (mcx) targeting the ancilla, and (3) uncompute the X flips. Measuring the ancilla as 1 identifies the good subset.How do I convert between phase and bit oracles?
- Phase → Bit: Prepare an ancilla with H, apply the phase oracle controlled on the ancilla (c_append), then H the ancilla again. This turns the phase mark into a measurable tag bit.- Bit → Phase: Apply the bit oracle, then a Z/phase-π on the ancilla, then uncompute with the bit oracle. The phase “kicks back” to good items, producing a phase oracle.
Why do Fibonacci numbers and the golden ratio appear in this chapter?
Define good outcomes Gₙ as n-bit strings with no consecutive ones. Their count satisfies |Gₙ| = |Gₙ₋₁| + |Gₙ₋₂|, so |Gₙ| = Fₙ₊₂ (Fibonacci). A tailored circuit (fib_circuit) prepares states where bad outcomes are suppressed and the ratio between probabilities of good outcomes starting with 1 versus 0 approaches the golden ratio (1+√5)/2.
Building Quantum Software with Python ebook for free