Deutsch–Jozsa algorithm

On this page 7

The Deutsch–Jozsa algorithm is a quantum algorithm that determines whether a hidden Boolean function on n bits is constant or balanced using a single query to a quantum oracle, whereas a deterministic classical algorithm needs up to 2^(n-1) + 1 queries in the worst case. Proposed by David Deutsch and Richard Jozsa in 1992, it was among the first quantum algorithms to demonstrate an exponential separation between quantum and deterministic classical query complexity.

Among quantum algorithms, it has little practical value on its own, but it introduced the core pattern later refined by Bernstein–Vazirani, Simon and Shor: superposition over all inputs, a phase oracle, and interference that concentrates the answer into a single measurement outcome.

The problem the Deutsch–Jozsa algorithm solves

The algorithm addresses a promise problem. A function f maps n-bit strings to a single bit, and it is guaranteed to be either constant, returning the same value for every input, or balanced, returning 0 for exactly half of the inputs and 1 for the other half. The task is to decide which case holds while treating f as a black box that can only be queried.

The promise is essential. If f could be neither constant nor balanced, the algorithm would still return an answer, but that answer would carry no reliable meaning. The table below lists the two allowed cases for a small input size.

Function classOutputs over 00, 01, 10, 11Number of such functions
Constant0, 0, 0, 0 or 1, 1, 1, 12
BalancedTwo zeros and two ones in any order6
Excluded by the promiseOne or three ones8
Allowed function classes for n = 2 input bits

How the circuit works step by step

The circuit uses n input qubits prepared in |0⟩ and one ancilla qubit prepared in |1⟩. Hadamard gates on every qubit place the input register in a uniform superposition of all 2^n strings and put the ancilla in the |-⟩ state. The oracle then computes f into the ancilla, and because the ancilla is in |-⟩, the result appears as a phase of (-1)^f(x) on each input string, an effect called phase kickback.

  1. Prepare |0…0⟩|1⟩ and apply a Hadamard gate to every qubit.
  2. Apply the oracle U_f, which maps |x⟩|y⟩ to |x⟩|y XOR f(x)⟩.
  3. Apply Hadamard gates again to the n input qubits.
  4. Measure the input register in the computational basis.

The final Hadamard layer makes all paths interfere. The amplitude of the all-zero outcome equals the average of (-1)^f(x) over all inputs. For a constant function this average is plus or minus 1, so the register reads all zeros with certainty. For a balanced function the positive and negative terms cancel exactly, so the probability of all zeros is exactly zero.

Function classAmplitude of all-zero stringMeasured resultConclusion
ConstantPlus or minus 1Always 00…0f is constant
Balanced0Any nonzero stringf is balanced
Measurement outcome by function class

Query complexity compared with classical approaches

The algorithm is measured by query complexity, the number of oracle calls, not by total running time. A deterministic classical algorithm must check more than half of the inputs before it can be certain, because the first 2^(n-1) answers might all agree even for a balanced function. The quantum circuit always needs exactly one query.

The advantage shrinks sharply once classical randomness and a small error are allowed. A randomized classical algorithm that queries k random inputs and answers “constant” if they all agree fails with probability at most about 2^(1-k), so roughly ten queries give an error below one in five hundred, independent of n. The comparison below shows why the separation is often described as exponential only in the exact, deterministic setting.

ApproachQueries requiredError allowedScaling with n
Deterministic classical2^(n-1) + 1 in the worst caseNoneExponential
Randomized classicalAbout k for error 2^(1-k)Small, boundedConstant
Deutsch–Jozsa quantum1None in the ideal caseConstant
Oracle queries needed to solve the problem

Relation to other early quantum algorithms

The Deutsch–Jozsa algorithm generalizes Deutsch’s algorithm, the n = 1 case from 1985, and its structure was reused almost unchanged by later work. The Bernstein–Vazirani algorithm applies the same circuit to recover a hidden bit string, and Simon’s algorithm adds repeated runs and classical post-processing to obtain an exponential separation even against randomized classical algorithms, which directly inspired Shor’s algorithm.

AlgorithmProblemQuantum queriesSeparation achieved
DeutschIs f on one bit constant or balanced12 versus 1 queries
Deutsch–JozsaConstant or balanced on n bits1Exponential, deterministic only
Bernstein–VaziraniFind hidden string s in f(x) = s·x1Polynomial, n versus 1
SimonFind hidden XOR periodAbout nExponential, also against randomized
Deutsch–Jozsa and its immediate successors

Limitations and role on real hardware

The algorithm has no known practical application, because the promise problem is artificial and randomized classical methods solve it cheaply. Its value is pedagogical and historical: it shows clearly how phase kickback and interference produce a quantum advantage in the oracle model.

  • Oracle cost is hidden: query complexity ignores how many gates the oracle itself needs.
  • Noise breaks certainty: on real devices, balanced functions sometimes yield the all-zero string.
  • Small instances only: demonstrations typically use 2 to 10 input qubits.
  • Benchmark use: it serves as a simple test of gate fidelity and interference on new processors.

Because the ideal output is deterministic, any deviation measured on hardware is a direct signal of error, which makes the circuit a convenient first exercise in quantum software frameworks and in courses on quantum oracles.

Put simply

Imagine a sealed box with many switches, each linked to a light that is either on or off. You are told that either every light behaves the same way or exactly half are on. Checking switch by switch could take a very long time if you insist on being certain.

The Deutsch–Jozsa algorithm lets a quantum computer ask the box about every switch at once and combine the answers so that the two cases produce clearly different results. One question is enough. The catch is that the puzzle is artificial, so the algorithm is mainly a clear demonstration of how quantum interference works.

In practice

A university course on quantum programming uses the Deutsch–Jozsa algorithm as its first hardware assignment. Students build a four-qubit oracle for a balanced function using CNOT gates, add the Hadamard layers, and run the circuit 4,000 times on a cloud-accessible superconducting processor and on an ideal simulator.

The simulator returns the all-zero string in exactly zero percent of shots, as theory predicts. The real device returns it in a few percent of shots because of gate and readout errors. Students then compare a constant oracle, where the device returns all zeros in most but not all shots. The exercise teaches phase kickback, interference and the effect of hardware noise in one compact experiment, and it gives a simple baseline for comparing processors before moving on to Grover’s algorithm.