Quantum Algorithms Database
Deutsch-Jozsa Algorithm
The first algorithm to prove a quantum computer could solve a problem exponentially faster than any classical deterministic algorithm.
Last reviewed September 30, 2026. Check the linked sources for anything that may have changed since.
Year
1992 (Deutsch's original 1985)
Inventor(s)
David Deutsch and Richard Jozsa (with later refinements by Cleve, Ekert, Macchiavello, and Mosca)
Speedup Type
Exponential Speedup
Difficulty
★★★☆☆
Overview
The Deutsch-Jozsa algorithm answers a simple question about a hidden function with a single query. The function takes n bits and returns one bit, and you are promised that it is either constant (the same output for every input) or balanced (output 0 for exactly half of the inputs and 1 for the other half). A deterministic classical algorithm may need up to 2 to the n minus 1, plus 1, queries to be sure. A quantum computer needs exactly one.
The problem itself has no practical use. The algorithm matters because it was one of the first demonstrations that quantum computers can do something provably better than classical ones in the query model, and because its circuit structure, with Hadamard gates before and after an oracle, appears again in Bernstein-Vazirani, Simon's, and Shor's algorithms.
The problem
Determining whether a given function is 'constant' (same output for all inputs) or 'balanced' (different outputs for half the inputs).
History
David Deutsch described the first quantum algorithm in 1985 in a paper that introduced the idea of a universal quantum computer. That version solved the one-bit case and succeeded only with probability one half. In 1992, Deutsch and Richard Jozsa generalized it to n bits, and the resulting speedup was exponential relative to deterministic classical algorithms.
In 1998, Richard Cleve, Artur Ekert, Chiara Macchiavello, and Michele Mosca reformulated the algorithm in the clean form used in textbooks today, which always succeeds. Small versions of the algorithm were among the first to be run on real quantum hardware, including NMR machines in 1998 and 2000 and later on superconducting and ion-trap devices. It is still a common first program used to test a new quantum chip.
Intuition
A quantum computer can feed a superposition of all inputs into the oracle at once. The oracle does not return the output directly. Instead it encodes each output in the phase of the corresponding input, multiplying its amplitude by plus or minus 1 depending on the function value. This trick, called phase kickback, is a core technique in quantum algorithms.
After the oracle, a second layer of Hadamard gates converts the phase pattern into measurement outcomes. If the function is constant, all amplitudes have the same sign, and the Hadamard layer interferes them all back into the all-zeros state. If the function is balanced, the positive and negative amplitudes cancel exactly at the all-zeros state, so that outcome has probability zero. A single measurement therefore tells you which case holds: all zeros means constant, anything else means balanced.
The speedup is real but limited. A randomized classical algorithm can solve the problem with a handful of queries and a tiny chance of error, which is why the advantage appears only against deterministic algorithms that must always be correct.
How it works
Uses superposition to evaluate the function on all inputs simultaneously, then uses interference so a single measurement reveals whether the function is constant or balanced.
- Prepare n input qubits in the state 0 and one ancilla qubit in the state 1.
- Apply a Hadamard gate to all n plus 1 qubits. The input register becomes an equal superposition of all 2 to the n inputs, and the ancilla becomes the minus state.
- Apply the oracle U sub f, which maps x and y to x and y XOR f of x. Because the ancilla is in the minus state, this puts the phase minus 1 to the f of x on each input state.
- Apply Hadamard gates to the n input qubits again.
- Measure the n input qubits.
- If every measured bit is 0, the function is constant. If any bit is 1, the function is balanced.
Worked example
Take n = 2 and the balanced function f of 00 = 0, f of 01 = 0, f of 10 = 1, f of 11 = 1, which depends only on the first bit. After the first Hadamards and the oracle, the phases are plus, plus, minus, minus on the four inputs. The second Hadamards turn this pattern into the state 10, because the signs vary only along the first bit. Measuring 10 is not all zeros, so the function is balanced.
Now take the constant function f of x = 0. The oracle does nothing, the two Hadamard layers cancel, and the measurement gives 00 with certainty. A classical deterministic algorithm would need three queries in the worst case to be sure for n = 2, and 2 to the n minus 1 plus 1 queries in general.
Complexity and resources
| Quantum queries | 1 (exactly, with certainty) |
|---|---|
| Classical deterministic queries | 2 to the n minus 1, plus 1, in the worst case |
| Classical randomized queries | O(1) for a small constant error probability, so there is no exponential gap against randomized algorithms |
| Qubits | n plus 1 |
| Gates | 2n Hadamards, one oracle call, and one ancilla preparation |
Hardware needs
A few qubits and a handful of gates, so it runs on any current hardware for small n.
An oracle implementation. For a general function the oracle can be as costly as the problem it is meant to test.
Real-world impact
Mainly historical and educational — it proved quantum speedup was possible in principle, paving the way for Shor's and Grover's algorithms, though the problem itself has limited practical use.
Applications
Teaching phase kickback, interference, and the query model of quantum computation.
Benchmarking new quantum hardware as a simple, verifiable circuit.
Serving as the blueprint for Bernstein-Vazirani, Simon's, and Shor's algorithms.
Limitations
- The promise that the function is constant or balanced is artificial and rarely arises in practice.
- The advantage disappears against randomized classical algorithms.
- The oracle cost is not counted in the query model, so a fair comparison needs care.
Common misconceptions
- It does not show that quantum computers are exponentially faster at everything. It shows an exponential gap against deterministic algorithms on a contrived problem.
- It does not evaluate the function on all inputs and read them all out. It uses interference so that one measurement reveals a global property.
How it compares
Bernstein-Vazirani uses the same circuit but extracts a hidden bit string.
Simon's algorithm extends the idea to a problem where even randomized classical algorithms need exponentially many queries.
Compared with Grover's algorithm, it solves a promise problem exactly instead of searching with probability.
Why it matters
Deutsch-Jozsa is the first example most students meet in which a quantum computer beats a classical one in a mathematically precise way. Understanding why it works gives you the vocabulary of superposition, oracles, phase kickback, and interference that every later algorithm builds on.
It also teaches an important lesson in being careful about comparisons. The result is only an exponential speedup when you compare with the right class of classical algorithm, a habit worth keeping when reading advantage claims.
The problem and the promise
The Deutsch-Jozsa problem gives you a black-box function f that takes n bits and returns one bit, with a promise: f is either constant (the same output for every input) or balanced (output 0 for exactly half the inputs and 1 for the other half). Your task is to decide which. Classically, a deterministic algorithm may need to check just over half of the 2^n inputs, 2^(n-1) + 1 queries in the worst case, to be certain. The quantum algorithm answers with a single query.
The promise is essential. If the function is allowed to be anything, the clean separation disappears. This is the first thing a careful reader should note: the algorithm solves a problem designed to show a difference, not a problem anyone needed solved.
How a single query suffices
Prepare n qubits in an equal superposition with a Hadamard on each, put the output qubit in a state that turns the oracle into a phase flip, call the oracle once, and apply Hadamards again. If f is constant, every path interferes so that the all-zeros outcome has probability 1. If f is balanced, the all-zeros amplitude cancels exactly, so any other outcome signals balanced. Measuring the n qubits therefore decides the question with certainty.
Cleve, Ekert, Macchiavello and Mosca's 'Quantum Algorithms Revisited' (arXiv quant-ph/9708016) presents algorithms of this type as multi-particle interference, and shows how they relate to phase estimation. The paper's improved version removes some of the shortcomings of the original formulation, and the interference view is the clearest way to understand why the amplitudes cancel.
- Quantum queries: 1 (exact, no error).
- Classical deterministic queries: 2^(n-1) + 1 in the worst case.
- Classical randomized: a few queries give a very small error probability, which weakens the practical separation.
Why its importance is historical rather than practical
A randomized classical algorithm can tell constant from balanced with a handful of random queries and an error probability that shrinks exponentially, so the exponential gap holds only against deterministic algorithms with zero error. That is why researchers treat Deutsch-Jozsa as a proof that quantum query complexity can differ from classical, not as evidence of a useful speedup.
Its value is as a teaching example and as a stepping stone. Bernstein-Vazirani and Simon's problem refine the idea into separations against randomized algorithms, and Shor's algorithm then moves the same interference pattern to a problem of real consequence.
Running it on hardware
The algorithm is small enough that it is a common first test on real devices, which makes it a good way to see noise at work: on a noiseless simulator the all-zeros result is certain for constant functions, while on hardware you see a distribution with leakage into other outcomes. The size of that leakage is a quick, rough check on circuit fidelity, not a benchmark of computational power.
Frequently asked questions
What does the Deutsch-Jozsa algorithm do?
It decides whether a hidden function is constant or balanced using a single query, whereas a deterministic classical algorithm may need exponentially many.
Is the speedup real?
Yes, against deterministic classical algorithms. Randomized classical algorithms can solve the problem with few queries, so the advantage is much smaller in that comparison.
What is phase kickback?
It is a technique in which the result of a function evaluation is written into the phase of the input state instead of into an output register, which allows interference to reveal global properties.
How many qubits are needed?
n plus 1, where n is the number of input bits.
Why is it important if it has no practical use?
It introduced the circuit pattern and the oracle model that later algorithms such as Simon's and Shor's use, and it clarified what makes a quantum advantage possible.
Related algorithms
References and further reading
Sources opened and checked on December 1, 2026. Resource estimates and claims change, so check the originals.
- Quantum Algorithms Revisited (arXiv (Cleve, Ekert, Macchiavello, Mosca)) Opened. Source for the interference framing and the link to phase estimation.
- Polynomial-Time Algorithms for Prime Factorization and Discrete Logarithms on a Quantum Computer (arXiv (P. W. Shor)) Opened. Shows how the same interference idea reaches a practical problem.