Lesson 27 · 75 min · core
The Deutsch–Jozsa Algorithm
Given a black box promised to be either constant or balanced, the Deutsch–Jozsa algorithm decides which with a single query to the reversible oracle , whereas every deterministic classical algorithm needs queries in the worst case. This lesson proves that is unitary, derives phase kickback, proves the Hadamard-transform formula by induction, computes the full output state, works through Deutsch's algorithm () vector by vector, and proves the classical lower bound with an adversary argument, together with the honest caveat that a randomized classical algorithm with queries already errs with probability at most .
Prerequisites: Controlled Gates: CNOT, CZ, SWAP, Hadamard, S and T Gates
Objectives and key results
Prove that the bit oracle is a permutation matrix with , hence unitary and self-inverse.
Derive the phase-kickback identity from the two cases and .
Prove by induction on .
Compute the Deutsch–Jozsa output state and show that the outcome has probability for constant and for balanced .
Prove that a deterministic classical algorithm needs queries in the worst case, and that random classical queries decide the problem with one-sided error at most .
- definition · Bit oracle
- identity · Phase kickback
- theorem · Hadamard transform
- formula · Deutsch–Jozsa output
- theorem · Query complexity
The promise problem and the bit oracle#
Definition
Constant and balanced functions
A Boolean function is constant if for all inputs (), and balanced if for exactly inputs and for the other . The Deutsch–Jozsa problem is a promise problem: we are guaranteed that is one of the two kinds and must decide which, using as few evaluations (queries) of as possible. For there are functions; are constant, are balanced, and the remaining (those with one or three ones) violate the promise.
Definition
Bit oracle
A quantum algorithm may access only through the bit oracle on qubits, defined on basis states by and extended linearly. The map would discard and therefore could not be invertible (both and would have the same image); adding to an ancilla bit modulo keeps the input and makes the map reversible. One application of counts as one query.
is an involutive permutation, hence unitary
For every the operator permutes the computational basis states, satisfies , and is therefore unitary with .
Proof (5 steps)
- Step 1:Each basis state is mapped to a basis state, so every column of the matrix of contains exactly one entry and all other entries .
- Step 2:Apply the definition twice: the input register is unchanged by the first application, so the second one adds the same bit , and for every bit .
- Step 3:agrees with on a basis, so by linearity. A map that is its own inverse is bijective; in particular two different basis states never have the same image, so every row also contains exactly one : is a permutation matrix.
- Step 4:For a permutation matrix , columns and are the standard basis vectors and , which are orthonormal. The entries are real, so .
- Step 5:Unitarity gives and the involution gives ; hence is also Hermitian, a real symmetric permutation matrix.
∎
For the truth table is . On the four blocks with fixed input , acts on the ancilla as , so . The same operator is realised by two CNOTs onto the ancilla, , because they map . Every linear function is realised the same way with one CNOT from each qubit with .
Unitary verified: (max deviation 0.0e+0)
Phase kickback#
- Step 1:Expand , use linearity, and apply the definition with () and .
- Step 2:Case : the ancilla is untouched.
- Step 3:Case : the oracle applies to the ancilla, and because is the eigenvector of .
- Step 4:Both cases read ; by linearity the oracle acts on any input superposition as the diagonal phase oracle , and the ancilla stays in the product state .
Result
Quick check
The ancilla is prepared in instead of . What does do to ?
The Hadamard transform on n qubits#
For every and every , In particular is the uniform superposition.
Proof (5 steps)
- Step 1:Base case. : column is and column is . The coefficient of is , since only for .
- Step 2:Inductive step: assume the claim for and split into its first bits and its last bit . Then , , and .
- Step 3:Insert the induction hypothesis for the first factor and the base case for the second.
- Step 4:The tensor product is bilinear, so the product of two sums is the double sum of the products; the prefactors multiply to and the signs multiply to .
- Step 5:The pairs run exactly once over all , , and , which is all that sees. This is the claim for ; by induction it holds for all .
∎
For the lemma gives the four columns of at once: the coefficient of in is , so with rows and columns in the order . The matrix is real and symmetric and squares to ; its entry is .
The Deutsch–Jozsa algorithm#
The state after each layer of the Deutsch–Jozsa circuit
- Step 1:Start from and apply to the ancilla.
- Step 2:The lemma with gives the uniform superposition on the inputs (every sign is ), and on the ancilla.
- Step 3:Phase kickback, applied term by term with : one query writes the whole truth table of into the signs of the amplitudes.
- Step 4:Apply the lemma to each ; the ancilla is untouched.
- Step 5:Exchange the two finite sums and collect the coefficient of each ; .
- Step 6:Born rule on the input register (the ancilla factor has norm ). For every is .
Result
Constant gives with certainty, balanced never gives
Measuring the input register of yields with probability if is constant and with probability if is balanced. Hence one query decides the Deutsch–Jozsa problem with certainty: answer "constant" if and only if the outcome is .
Proof (4 steps)
- Step 1:All terms equal .
- Step 2:is a unit vector (unitaries preserve the norm), so once all other amplitudes vanish: the full state after the second is the basis state times a global sign.
- Step 3:Each input with contributes and each input with contributes ; balanced means both counts are .
- Step 4:So a balanced function always produces a non-zero string ; which one depends on , but any certifies "balanced".
∎
For a linear balanced function with the full output is a single basis state: , because . The character sum factorises over the bits, since a single factor with is . Hence for and otherwise. For and the inputs therefore read with certainty. The circuit below adds a final on the ancilla, which turns back into , so the whole register ends in .
Circuit · 3 qubits
Deutsch–Jozsa for and the balanced function , with the oracle realised as followed by . The statevector is exactly and the histogram shows the single outcome : the inputs read , never , so the function is balanced.
- |000⟩0
- |001⟩0
- |010⟩0
- |011⟩0
- |100⟩0
- |101⟩0
- |110⟩0
- |111⟩1
Quick check
For the function with truth table is neither constant nor balanced. Running the Deutsch–Jozsa circuit anyway, what is the probability that the inputs read ?
Quick check
For and the balanced function , which outcome does the Deutsch–Jozsa circuit produce on the inputs?
Worked example: Deutsch's algorithm (n = 1)#
For there are four functions : the constants , and the balanced , . The two-qubit register is on qubits and vectors are listed in the order . The oracles are , , and .
Every state vector of Deutsch's algorithm
- Step 1:Prepare the ancilla in .
- Step 2:, , and multiplied out; this is column of .
- Step 3:Phase kickback on each of the two input branches.
- Step 4:Constant : both branches get the same sign , which factors out as a global phase.
- Step 5:Balanced : the two branches get opposite signs, so the input qubit turns from into .
- Step 6:The final Hadamard on the input uses and .
- Step 7:Collecting the four cases: , , , . The measured input bit is the parity , a global property of obtained from one query, while neither nor individually is learned.
Result
Classical versus quantum query complexity#
Deterministic algorithms need queries
Every deterministic classical algorithm that solves the Deutsch–Jozsa problem on all constant and balanced makes at least queries on some input, and queries suffice.
Proof (5 steps)
- Step 1:Suppose an algorithm always halts after at most queries. Run it against an adversary who answers to every query. Because is deterministic, each query point and the final output are functions of the earlier answers only; against the all-zero answers queries a fixed set with and outputs a fixed answer.
- Step 2:The constant zero function gives exactly the answers the adversary gave.
- Step 3:Since we can enlarge to a set of exactly inputs. Then takes the value on inputs and on the other , so it is balanced, and it is on every queried point.
- Step 4:On and on the algorithm sees the same answers, hence (by determinism, inductively over the queries) asks the same questions and produces the same output. That output is wrong for one of them, contradicting correctness. Therefore some input forces at least queries.
- Step 5:Query any distinct inputs. A balanced function has only zeros and ones, so it cannot return equal answers; seeing two different values rules out a constant function. Under the promise the answer is always correct.
∎
random classical queries err with probability at most
The classical randomized algorithm that queries inputs chosen independently and uniformly at random and answers "constant" exactly when all answers agree is always correct on constant and errs on balanced with probability exactly .
Proof (3 steps)
- Step 1:A constant function always returns equal answers, so there is no error on constant inputs (one-sided error).
- Step 2:Each is uniform over and exactly half the inputs map to each value.
- Step 3:The algorithm errs on a balanced exactly when the answers all agree; the two events are disjoint and each has probability by independence.
∎
import { Statevector, matrix } from '@/lib/quantum-simulator';
// Deutsch-Jozsa on n = 2 input qubits (0, 1; qubit 0 = most significant bit) and the ancilla qubit 2,
// run for all 16 functions f: {0,1}^2 -> {0,1}. The truth table is written f(00) f(01) f(10) f(11).
function oracle(f) {
// U_f |x, y> = |x, y XOR f(x)>: column 2x + y has its single 1 in row 2x + (y XOR f(x))
const rows = Array.from({ length: 8 }, () => Array(8).fill(0));
for (let col = 0; col < 8; col++) rows[(col & 6) | ((col & 1) ^ f[col >> 1])][col] = 1;
return matrix.fromRows(rows);
}
for (let m = 0; m < 16; m++) {
const f = [3, 2, 1, 0].map((k) => (m >> k) & 1);
const w = f.reduce((a, b) => a + b, 0);
const kind = w === 0 || w === 4 ? 'constant' : w === 2 ? 'balanced' : 'neither';
const sv = Statevector.zero(3)
.applyGate('X', [2]).applyGate('H', [0]).applyGate('H', [1]).applyGate('H', [2])
.applyMatrix(oracle(f), [0, 1, 2])
.applyGate('H', [0]).applyGate('H', [1]);
const a = sv.amplitudes();
// P(inputs read 00) = |amp(000)|^2 + |amp(001)|^2, summed over the ancilla
const p00 = a[0].re ** 2 + a[0].im ** 2 + a[1].re ** 2 + a[1].im ** 2;
const predicted = ((4 - 2 * w) / 4) ** 2;
console.log('f = ' + f.join('') + ' ' + kind.padEnd(8) + ' P(00) = ' + p00.toFixed(4) + ' (1 - w/2)^2 = ' + predicted.toFixed(4));
}Expected output
f = 0000 constant P(00) = 1.0000 (1 - w/2)^2 = 1.0000 f = 0001 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 0010 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 0011 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 0100 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 0101 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 0110 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 0111 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 1000 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 1001 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 1010 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 1011 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 1100 balanced P(00) = 0.0000 (1 - w/2)^2 = 0.0000 f = 1101 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 1110 neither P(00) = 0.2500 (1 - w/2)^2 = 0.2500 f = 1111 constant P(00) = 1.0000 (1 - w/2)^2 = 1.0000
References
- D. Deutsch and R. Jozsa, Rapid solution of problems by quantum computation, Proceedings of the Royal Society of London A 439, 553–558 (1992).doi:10.1098/rspa.1992.0167
- R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, Quantum algorithms revisited, Proceedings of the Royal Society of London A 454, 339–354 (1998).doi:10.1098/rspa.1998.0164
- M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th anniversary ed., Cambridge University Press (2010), §1.4.3 (Deutsch’s algorithm) and §1.4.4 (the Deutsch–Jozsa algorithm).doi:10.1017/CBO9780511976667
Hands-on labs
Deutsch–Jozsa with one oracle query
1 step · auto-gradedOpen labSurround a black-box oracle with gates from so that a single query decides whether is constant or balanced. The grader runs your circuit four times, once for each of the oracles , , and , and inspects the measurement distribution of the two input qubits.