Deutsch-Jozsa and Bernstein-Vazirani are the two smallest algorithms that show a quantum computer extracting global information about a function with fewer queries than any classical method. Neither is useful as software. I keep them in the notes because they contain every ingredient that Grover, phase estimation and Shor reuse later: an oracle, phase kickback, and a Hadamard transform that turns phases into bit strings.

One question to the box instead of many: interference reveals the answer in a single try. Image: User:Fran4004, CC BY-SA 3.0, via Wikimedia Commons.

Mental Model

  • An oracle is a black box that computes a Boolean function reversibly. The algorithms are judged by how often they call it, not by their gate count.
  • With the ancilla in , the oracle does not change the ancilla and instead multiplies each input by the phase (phase kickback).
  • maps between the computational basis and the “parity” basis. Phases of the form become the bit string .
  • One query lets all inputs contribute at once, and the final Hadamards make the contributions interfere so that one global property survives.
  • The speedups are relative to an oracle. They say nothing about how hard it is to build the oracle in the first place.

1. Oracles and Phase Kickback

The standard oracle acts on input qubits and one target qubit:

Put the target in . Then either leaves the target alone () or swaps its two components, which gives a factor (). So

The target is untouched and the information about has moved into the phase of the input register. From here on I drop the ancilla and treat the oracle as the diagonal operator .

The Hadamard transform is needed in both algorithms:

where .

2. Deutsch-Jozsa

The promise: is either constant or balanced (exactly half of the inputs map to 1). Decide which.

The circuit is , then the oracle, then again, then measure the input register. After the first layer and the oracle the state is

and after the second layer the amplitude of is

For a constant this is , so the measurement returns all zeros with certainty. For a balanced the sum cancels exactly to 0, so all zeros never appears. Any other outcome means balanced.

Query counts for input bits:

MethodQueries
Classical deterministic, worst case
Classical randomized, error at most about , independent of
Quantum (exact)1

The honest reading of this table is that the exponential gap is against deterministic classical algorithms only. A randomized classical algorithm that samples random inputs and says “constant” if all outputs agree is wrong with probability about . So Deutsch-Jozsa is an exact-versus-deterministic separation, not a proof that quantum computers beat randomized classical ones.

Worked example: n = 2

Take the balanced function . After the state is . The oracle gives the signs , which is . The final Hadamards map to , a non-zero result, so is balanced. The same computation with gives up to a global phase.

3. Bernstein-Vazirani

The promise: for a hidden string . Find .

The circuit is identical to Deutsch-Jozsa. After the first Hadamard layer and the oracle the register holds

by the Hadamard formula above. The second Hadamard layer undoes the first, because is its own inverse, so the register is exactly and one measurement reveals every bit.

Classically you need queries: ask for on each unit vector to read off . Even randomization does not help, since each query returns one bit and has unknown bits. That is an honest linear-versus-one separation, and a bounded-error one. Bernstein and Vazirani’s paper also has a recursive version of the problem that gives a super-polynomial gap, which is the more interesting result, though it is not what the single-level algorithm shows.

4. A Qiskit Circuit for BV

The oracle for is a CNOT from input qubit to the target for each . Qiskit orders bits right to left, so qubit corresponds to the character s[n-1-i].

from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
 
s = "1011"; n = len(s)
qc = QuantumCircuit(n + 1)
qc.x(n); qc.h(range(n + 1))              # target in |->, inputs in |+>^n
for i, bit in enumerate(reversed(s)):
    if bit == "1":
        qc.cx(i, n)                      # phase kickback of s_i * x_i
qc.h(range(n))
 
probs = Statevector(qc).probabilities_dict(qargs=list(range(n)))
print({k: round(float(v), 6) for k, v in probs.items() if v > 1e-9})

Running this prints {'1011': 1.0} on the statevector simulator. The oracle has three CNOTs, one per 1 in , and the whole algorithm calls it once.

Common mistake

Reading the output string in the wrong order. Qiskit prints qubit on the left, so if you build the oracle with cx(i, n) for s[i] instead of s[n-1-i], you get reversed, and a palindromic test string like 101 hides the bug.

5. What These Algorithms Teach

Both problems are contrived, but the pattern is general. Build a superposition over all inputs, imprint information as phases, and use a Fourier-type transform so that the answer sits in a single basis state. Simon’s problem replaces the linear function by a two-to-one function with a hidden XOR period, and Shor’s algorithm replaces the Hadamard transform by the QFT to find a hidden period over the integers. The query advantage always lives inside the oracle model. Whether it survives into a real task depends on whether the oracle can be built cheaply, which is exactly the hard part for Shor.