Deutsch–Jozsa Algorithm
The Deutsch–Jozsa algorithm decides whether a promised Boolean function is constant or balanced. One coherent oracle call turns its values into relative signs; interference then tests whether their average vanishes. It is a useful first example because the entire correctness proof can be followed through the amplitudes.
The comparison depends on the required accuracy. One exact quantum query replaces exact deterministic classical queries, but two randomized classical queries already suffice if error is allowed. This is a statement about oracle calls, with oracle construction and gate costs separate.
Helpful background. Quantum oracles defines a reversible Boolean query. Phase kickback explains why a minus-state answer qubit converts a bit flip into a phase. Query complexity compares fixed query caps and error requirements.
The constant-or-balanced promise
Section titled “The constant-or-balanced promise”Fix , write , and let
The legal functions belong to one of two disjoint classes:
Equivalently, regard the truth table as an -bit string. Its promised Hamming weight is , , or , and the required output is the class label constant or balanced. There are
legal functions: two constant truth tables and balanced ones.
This is a promise on which oracle has been supplied, not a guarantee the circuit verifies. An arbitrary off-promise function need not receive a meaningful class label. The input-size conventions also matter: the address length is , while the truth table contains values.
One oracle call and two Hadamard layers
Section titled “One oracle call and two Hadamard layers”The data register has qubits and the answer register has one. In that tensor order, the supplied query is
It is defined on every basis state and satisfies . Starting with , apply Hadamards to all qubits, call , and apply Hadamards to the data alone:
Measure the data. Return constant for and balanced otherwise. The sum over addresses still contains only one invocation of .
The answer qubit returns to after the oracle, while the data acquires relative phases . The final Hadamards turn their mean into the amplitude of . Here denotes computational-basis measurement.
Why the answer qubit returns
Section titled “Why the answer qubit returns”The answer state
is a eigenstate of the bit flip . Applying the oracle to one data basis state gives
Linearity then gives the post-query state
The answer register returns exactly, carries no residual function value, and factors from the data. What remains is a relative-phase pattern. This is not a readable list of all values: measuring immediately would still return only one uniformly distributed address.
Complementing the function multiplies the entire data state by :
That global sign is unobservable. The two constant functions must therefore lead to the same class decision, as the promise requires.
Walsh–Hadamard interference
Section titled “Walsh–Hadamard interference”For bit strings , define the mod-two inner product
The -qubit Hadamard transform acts as
Applying it to the oracle state produces the normalized Walsh coefficients
The factor is , not : one factor of comes from the uniform query state and the other from the final Hadamard transform.
Character orthogonality verifies normalization. Expanding the squared amplitudes gives
The algorithm uses only , but the other coefficients explain the nonzero outcomes. A generic balanced function can distribute probability over several Walsh labels; balance alone does not encode a unique hidden word.
Exact classification and off-promise inputs
Section titled “Exact classification and off-promise inputs”At the zero word, the character factor is one, so
If everywhere, then and the final data state is . If everywhere, then and the state is . In either constant case, measurement returns with certainty. If is balanced, the two cardinalities are equal, , and that outcome is impossible. Therefore
on the promise, with any certifying balance.
Both promise classes are nonempty, so an ordinary zero-query procedure has no information about the label and cannot achieve worst-case success greater than . Under the site’s zero-error convention, a never-wrong zero-query procedure cannot emit either class label, because the same output distribution must serve inputs from both classes. It must therefore report ? with probability one, exceeding the allowed inconclusive probability . The one-query circuit is exact, so with the fixed query caps defined in query complexity,
Outside the promise, if , then
This formula describes the circuit’s response to an off-promise truth table. It does not make one shot a test of arbitrary Hamming weight, and it does not certify that the supplier respected the promise.
Classical queries: exact answers versus bounded error
Section titled “Classical queries: exact answers versus bounded error”The deterministic exact bound
Section titled “The deterministic exact bound”A deterministic exact algorithm queries distinct truth-table entries. It stops and returns balanced as soon as two different values have appeared. If the first answers are identical, the function cannot be balanced, so it returns constant. This gives the upper bound .
The bound is necessary. After any transcript containing at most identical answers, two legal completions remain possible: the queried value can fill the entire table, producing a constant function, or exactly entries can take that value while the unqueried positions supply the opposite value, producing a balanced function. No deterministic decision tree can label both completions correctly from that transcript. Consequently,
This is exponential in the address length and linear in the truth-table size . It is an exact-query theorem. The phrase “classical algorithms require exponentially many queries” is misleading unless the deterministic or zero-error requirement is stated.
The randomized comparison
Section titled “The randomized comparison”Bounded error changes the picture. For a fixed cap , query distinct uniformly random addresses. A mixed transcript proves the function balanced. For a balanced function, the probability of an all-equal transcript is
where when . On an all-equal transcript, return constant with probability
A constant instance then has error , while a balanced instance has error . The choice of equalizes them:
This risk is minimax optimal. Average any candidate algorithm over permutations of the addresses and over complementation of every truth-table bit. Symmetrization cannot increase its worst-case error. Repeated addresses add no information, and after distinct queries the symmetrized transcript is either mixed, which identifies balance, or all equal, which is compatible with both classes. The only remaining decision probability is ; minimizing gives the value above.
For one query, and . For two,
for every . Hence
The honest comparison is now visible: exact quantum versus exact deterministic queries separates from , whereas bounded-error quantum versus bounded-error randomized queries separates from . The latter has no asymptotic query advantage.
For , the following values show how a small allowed error changes the query count. At there is no observed transcript; guessing the class with a fair coin has error .
| Query cap | Balanced all-equal probability | Optimal worst-case error |
|---|---|---|
| No queries | ||
For example, query two distinct uniform addresses. Unequal answers establish balance. After equal answers, return constant with probability . A constant function has error ; a balanced function has error .
A nonlinear balanced example
Section titled “A nonlinear balanced example”Take , , and order truth-table positions as 000, 001, 010, 011, 100, 101, 110, 111. Enumerating all promised functions verifies the exact decision criterion. A concrete nonlinear balanced instance is majority on three bits,
with truth-table word 00010111. Its unnormalized Walsh vector, ordered by , is
Because , the final data state is
Each displayed word has probability , and has probability zero. The two constant functions instead give and .
The following integer calculation checks the complete three-bit promise and its Walsh normalization. Bit of mask stores the value at address , so the majority truth table is hexadecimal 0xe8 in this encoding.
const N = 8;const parity = (z) => { let p = 0; while (z) { p ^= z & 1; z >>= 1; } return p;};
let promised = 0;let majorityWalsh;for (let mask = 0; mask < 256; mask++) { const f = (x) => (mask >> x) & 1; const weight = Array.from({ length: N }, (_, x) => f(x)) .reduce((a, b) => a + b, 0); if (![0, 4, 8].includes(weight)) continue; promised++; const W = Array.from({ length: N }, (_, y) => Array.from({ length: N }, (_, x) => (f(x) ^ parity(x & y)) ? -1 : 1) .reduce((a, b) => a + b, 0)); if (W.reduce((a, v) => a + v * v, 0) !== N * N) throw Error('norm'); if ((weight === 4) !== (W[0] === 0)) throw Error('decision'); if (mask === 0xe8) majorityWalsh = W;}if (promised !== 72) throw Error('promise count');if (majorityWalsh.join(',') !== '0,4,4,0,4,0,0,-4') throw Error('spectrum');console.log({ promised, majorityWalsh });Gates, history and related problems
Section titled “Gates, history and related problems”With the initial answer supplied, the circuit uses visible qubits, Hadamards, one oracle call and one measurement of the data register. Starting all qubits in zero adds an on the answer. Any workspace inside the oracle must also be counted. The Hadamards can run in parallel within each layer, but total depth includes the oracle, preparation and readout.
An arbitrary truth table has entries, so constructing its reversible circuit may be expensive. A compiled small example can demonstrate phase control and interference without establishing an asymptotic runtime advantage. Nor does the measurement reveal all the truth-table entries: it gives one promised class label.
Deutsch’s original 1985 one-bit procedure could be inconclusive with probability . Deutsch and Jozsa’s 1992 generalization used two function-controlled-NOT calls. The exact one-query circuit above is the refinement by Cleve, Ekert, Macchiavello and Mosca, published in 1998 (preprint 1997).
For the affine functions , the same calculation gives . Recovering the hidden word is the Bernstein–Vazirani problem. Balance alone does not imply this affine form. Simon’s algorithm instead uses a many-bit value oracle and repeated samples to find a hidden XOR period. Statements about BQP and BPP require the definitions in quantum complexity classes, beyond this fixed promise problem.
Exercises
Section titled “Exercises”Count the promised oracle family
Section titled “Count the promised oracle family”For , count the constant and balanced Boolean truth tables. Explain why this count describes a promise on the selected oracle rather than a restriction on the states the algorithm may query.
Solution
There are two constant truth tables, all zero and all one. A balanced truth table is specified by choosing the positions assigned one, giving choices. Thus the promised family has
members. The supplier chooses one unitary from this family. Once chosen, the complete XOR map acts on every computational-basis state and hence on every superposition by linearity; the promise does not confine the query state to a smaller subspace.
Verify Boolean phase kickback
Section titled “Verify Boolean phase kickback”Apply to . Prove target return and factorization for a general data superposition, then determine the effect of replacing by .
Solution
For a basis state,
Therefore
The common answer state factors exactly. Complementing multiplies every data amplitude by , which is one global phase and cannot affect any measurement probability.
Derive the Walsh–Hadamard law
Section titled “Derive the Walsh–Hadamard law”Starting from the post-query data state, derive , prove Parseval normalization with character orthogonality, and obtain the zero-word probability for a truth table of arbitrary weight .
Solution
Insert
into . Collecting the coefficient of gives
Since , expanding leaves diagonal terms divided by , hence one. At , there are positive and negative contributions, so
Enumerate the three-bit promise
Section titled “Enumerate the three-bit promise”Reproduce the exhaustive check and compute the spectrum of the nonlinear balanced truth table 00010111. Why does this example refute the claim that every balanced function produces one deterministic nonzero word?
Solution
There are promised masks. For each mask, compute
The two constant masks have ; all seventy balanced masks have . For 00010111, direct summation gives
Dividing by eight gives amplitudes on , , and , amplitude on , and zero elsewhere. Four different outcomes therefore occur with probability each. Balance forbids only the zero word; it does not determine the rest of the spectrum.
Prove the deterministic exact bound
Section titled “Prove the deterministic exact bound”Give an executable classical schedule using queries and an indistinguishable-transcript argument showing that no exact deterministic schedule can use fewer in the worst case.
Solution
Query distinct addresses until a mismatch occurs or identical values have been seen. A mismatch proves balance; more than half the table having one value proves constancy under the promise.
For the lower bound, consider any transcript of at most identical answers. It can be completed to a constant table, but it can also be completed to a balanced table by placing the opposite bit in exactly unqueried positions. Both completions follow the same decision-tree path, so no leaf at that depth can be correct on both. Thus
Randomization cannot reduce the worst-case fixed cap if every run must return the correct class: an all-equal transcript of at most answers remains compatible with both classes.
Solve the randomized minimax problem
Section titled “Solve the randomized minimax problem”Derive , , and . Evaluate them for and , then prove for every .
Solution
For distinct uniform addresses, a balanced table gives all zeros with probability and all ones with the same probability. Hence
If is the probability of returning constant after an all-equal transcript, the two worst-case errors are for a constant and for a balanced function. Equalizing them gives
For and through , the balanced all-equal probabilities are
the constant-decision probabilities are
and the minimax errors are
Symmetrization over addresses and bit complementation reduces any algorithm to these transcript classes, proving optimality. One query has error , while two have error for every . Therefore .
Separate affine structure from balance
Section titled “Separate affine structure from balance”For , compute the entire Walsh spectrum. Explain why its deterministic output cannot be generalized to every balanced function.
Solution
Substitution gives
Thus measurement returns with certainty. When , the function is constant; when , it is balanced. For , only of the balanced truth tables are affine, so most can have support on several Walsh labels, as the majority example shows. (For , every balanced truth table happens to be affine.) The hidden-word inference belongs to Bernstein–Vazirani, not to the general Deutsch–Jozsa promise.
Diagnose a noisy zero-word test
Section titled “Diagnose a noisy zero-word test”Suppose the ideal data output is measured after an additional channel which, with probability , replaces the entire data state by . Compute the error for each promised class. Does one observation still certify balance?
Solution
The replacement contributes probability to every word. For a constant function the zero-word probability becomes , so its classification error is . For a balanced function the ideal zero-word probability vanishes, but replacement contributes ; this is its error. For , neither outcome provides the ideal certainty. Repeated independent trials can estimate the probabilities, but interpreting them requires a specified noise model. This example assumes replacement of the whole data register, not independent depolarization of each qubit.
References
Section titled “References”- R. Cleve, A. Ekert, C. Macchiavello, and M. Mosca, “Quantum Algorithms Revisited,” Proceedings of the Royal Society A 454, 339–354 (1998), doi:10.1098/rspa.1998.0164.
- D. Deutsch, “Quantum Theory, the Church–Turing Principle and the Universal Quantum Computer,” Proceedings of the Royal Society A 400, 97–117 (1985), doi:10.1098/rspa.1985.0070.
- D. Deutsch and R. Jozsa, “Rapid Solution of Problems by Quantum Computation,” Proceedings of the Royal Society A 439, 553–558 (1992), doi:10.1098/rspa.1992.0167.
- M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th Anniversary Edition, Cambridge University Press (2010), doi:10.1017/CBO9780511976667.