The query model
The first quantum algorithms were not designed to factor numbers or simulate molecules. They were designed to answer a cleaner question: can a quantum computer learn a property of a black-box function with fewer evaluations than any classical computer? To make "fewer" precise we fix a model.
A function is available only through an oracle, a gate we may apply but not look inside. Because gates must be reversible, the oracle writes into an extra target qubit instead of overwriting the input:
This bit oracle permutes basis states, so it is unitary. The cost of an algorithm is its number of queries, i.e. applications of . Everything else (other gates, classical processing, the time to build ) is not counted. The classical counterpart may evaluate on inputs of its choice, and each evaluation is one query. We compare query counts, and we must also say what kind of classical algorithm we compare with: deterministic and always correct, or randomised with bounded error (correct with probability at least, say, 2/3). Background: Nielsen and Chuang, Sections 1.4 and 6.1.
Phase kickback
Set the target to . If nothing happens. If the target becomes . Either way,
The target is unchanged and the value of has been "kicked back" as a sign on the input register. Discarding the target, one query implements the phase oracle . On a superposition of inputs, the phase oracle marks every term with its own sign in a single query; interference then turns these signs into a measurable answer. Kickback is also why controlled gates make global phases matter: a phase that is global for the target becomes a relative phase on the control.
A tool: Hadamards on qubits
For a basis state on qubits,
For one qubit this is ; the general case is the tensor product of such factors. In particular is the uniform superposition, and the amplitude of in is the uniform average of the amplitudes of , scaled by .
Deutsch–Jozsa
Problem (a promise problem). We are promised that is either constant (the same value on all inputs) or balanced (0 on exactly half of the inputs, 1 on the other half). Decide which.
Algorithm. Apply to , query the phase oracle once, apply again, and measure.
Analysis. After the first layer and the oracle the state is . By the Hadamard identity, the amplitude of after the second layer is
If is constant, every term has the same sign and : the outcome is with certainty. If is balanced, half the terms are and half , so : the outcome is never . So one query decides the problem with certainty. This is the algorithm of Deutsch and Jozsa (1992) in the improved single-query form of Cleve, Ekert, Macchiavello and Mosca (1998).
Classical comparison, stated carefully.
- Deterministic, always correct: queries are necessary and sufficient in the worst case. Sufficient: among distinct inputs a balanced function must show both values. Necessary: an adversary can answer 0 to the first queries, and then both the constant-0 function and a balanced function that is 0 exactly on the queried inputs are still possible. The exercise confirms both halves by brute force for .
- Randomised, bounded error: query independent uniformly random inputs and answer "constant" if all values agree. A constant function is never misclassified; a balanced one gives all-equal answers with probability . Two queries already give error at most , and ten give error below , independently of .
So the exponential gap (1 query versus ) holds only against exact deterministic classical algorithms. Against randomised algorithms that may err with small probability, the advantage is a constant number of queries.
Worked example
Take and equal to 1 only on input . This is neither constant nor balanced, so the promise fails, but the formula still applies: and the probability of measuring is . With the promise in force the outcome is always 0 or 1, never in between.
Bernstein–Vazirani
Problem. for an unknown string . Find .
Algorithm. The same circuit as Deutsch–Jozsa. After the oracle the state is , so the final (which is its own inverse) returns exactly . One query reveals all bits of with certainty (Bernstein and Vazirani, 1997).
Classical comparison. Each classical query returns one bit, and is one of possibilities. A deterministic algorithm needs exactly queries (querying the unit vectors returns ; with fewer than answers at least two strings remain consistent). A randomised algorithm that succeeds with probability at least 2/3 still needs queries, by an information-counting argument. The gap is therefore versus : a linear separation, not an exponential one. (Bernstein and Vazirani also defined a recursive version of the problem with a larger gap; we do not cover it.)
import numpy as np
from functools import reduce
n = 3
H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
Hn = reduce(np.kron, [H] * n)
zero = np.eye(2 ** n)[0]
def dj_p_zero(f_values):
oracle = np.diag([(-1.0) ** v for v in f_values]) # phase oracle from kickback
return abs((Hn @ oracle @ Hn @ zero)[0]) ** 2
print(round(dj_p_zero([1, 1, 1, 1, 1, 1, 1, 1]), 12)) # constant: 1.0
print(round(dj_p_zero([0, 1, 1, 0, 1, 0, 0, 1]), 12)) # balanced (parity): 0.0
s = 0b101 # Bernstein-Vazirani with hidden s = 101
f_bv = [bin(s & x).count("1") % 2 for x in range(2 ** n)]
out = Hn @ np.diag([(-1.0) ** v for v in f_bv]) @ Hn @ zero
print(format(int(np.argmax(abs(out) ** 2)), "03b"), round(abs(out[s]) ** 2, 12)) # 101 1.0
What these results do and do not show
Deutsch–Jozsa and Bernstein–Vazirani are theorems about query complexity in an oracle model. They show that interference can extract a global property of a function from fewer evaluations than classical evaluation allows. They do not show a practical speed-up on any problem anyone needs solved, for three reasons.
- The oracle is assumed, not built. In practice someone must implement as a circuit. If you are given that circuit, you can often read the answer off it classically: for Bernstein–Vazirani, a circuit computing essentially contains .
- The classical baseline matters. The Deutsch–Jozsa gap disappears (up to a constant) against randomised classical algorithms with small error.
- Query counts are not running time. Real cost includes gates, error correction and the classical control around them.
Their value is conceptual: they are the smallest complete examples of the pattern "superpose, query in phase, interfere", which reappears in Simon's algorithm, phase estimation and Shor's algorithm (the Algorithms course covers these, with their own precise conditions).
Quantum teleportation
Teleportation answers a different question: Alice holds a qubit in an unknown state , and Alice and Bob share one Bell pair . Using only local operations and two classical bits, can Bob end up holding ? Yes (Bennett et al., 1993); the protocol was first demonstrated with photons by Bouwmeester et al. (1997).
The first two gates prepare the Bell pair (in practice it is distributed in advance). Derivation. Write the three-qubit state as . Alice's CX from qubit 0 to qubit 1 gives
and her on qubit 0 gives, after regrouping by the values of her two qubits,
Each of Alice's four outcomes has probability , whatever and are, and leaves Bob with . Alice sends to Bob over an ordinary channel; Bob applies if and then if , which undoes exactly. His qubit is now , with fidelity 1 on every branch.
What teleportation does not do.
- It does not send information faster than light. Before the two bits arrive, Bob does not know which branch occurred, so his qubit is described by the average over branches: for every (averaging over maps every Bloch vector to the centre). Nothing Bob can measure depends on until the classical message, limited by the speed of light, arrives.
- It does not copy. Alice's qubits end in , which carry no trace of ; the original is destroyed, consistent with no-cloning.
- It does not move matter or energy, and it consumes a Bell pair per qubit: one shared Bell pair plus two classical bits buy one transmitted qubit.
Checking the protocol with the KetQat SDK
The SDK's quantum-teleportation product computes all four branches exactly and
reports, for each, the probability, Bob's correction, and the fidelity with and
without it:
from ketqat_runner import catalog
record = catalog.run("[email protected]", seed=11, theta=2.0, phi=1.0, shots=1000)
result = record.result
for branch in result["branches"]:
print(branch["outcome"], branch["probability"], branch["correction"],
round(branch["fidelity"], 9), round(branch["fidelity_without_correction"], 4))
print(result["average_fidelity_without_correction"], result["counts"])
For the input with Bloch angles , this printed (SDK commit 2cedabb, NumPy 2.5) probability 0.25 and fidelity 1.0 for all four outcomes, uncorrected fidelities 1.0, 0.2414, 0.1732 and 0.5855 for outcomes 00, 01, 10 and 11, an average uncorrected fidelity of exactly 0.5, and seeded counts 242, 276, 226 and 256. The uncorrected values have a simple explanation: on branch Bob holds , whose fidelity with is the square of a Bloch component, , or . For this input . The exercise asks you to reproduce every one of these numbers with your own NumPy simulation and to confirm that Bob's state before the bits arrive is .
Common mistakes
- Quoting "exponential speed-up" for Deutsch–Jozsa without the conditions. The gap is against exact deterministic algorithms in the query model; against bounded-error randomised algorithms it is a constant.
- Counting the wrong thing. Query complexity counts oracle calls only.
- Forgetting the target in . With the target in the bit oracle writes into the target and entangles it with the input; no phase is kicked back.
- Thinking teleportation is instantaneous. Bob's qubit is useless until the two classical bits arrive.
- Applying Bob's corrections in the wrong pairing. (Alice's half of the pair) controls , (the message qubit) controls .
Summary
- In the query model, cost is the number of oracle calls; phase kickback turns a bit oracle with target into a phase oracle.
- Deutsch–Jozsa: 1 query, exact, versus for deterministic exact classical algorithms but for bounded-error randomised ones.
- Bernstein–Vazirani: 1 query versus classical queries.
- These are oracle separations, not practical speed-ups.
- Teleportation transfers an unknown qubit using one Bell pair and two classical bits; Bob's state is until the bits arrive, so no faster-than-light signalling.
The two exercises implement the oracle algorithms with a brute-force check of the classical bound, and teleportation checked branch by branch against the KetQat SDK. Next, Noise, limitations and modern hardware asks what happens when gates are not perfect.