Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Deutsch and Deutsch–Jozsa algorithms

Clear, exam-focused notes on the Deutsch and Deutsch–Jozsa algorithms: definitions, step-by-step derivations, formulas, worked exam-style solutions, common errors and practice questions.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Quantum algorithms can sometimes solve decision problems with fewer queries to a black-box function (oracle) than any classical deterministic algorithm. The Deutsch and Deutsch–Jozsa algorithms are the simplest examples that show how quantum superposition and interference give a query advantage. An engineer uses these ideas to understand oracle-based speedups, basic circuit design patterns (Hadamard, phase-oracle), and how to reason about multi-qubit interference before moving to Grover or Shor.

Key ideas

Define terms before use:

  • Qubit: the basic quantum information unit with states in a 2-dimensional complex Hilbert space spanned by |0> and |1>. Measurement in the computational basis yields 0 or 1 with probabilities given by squared amplitudes.
  • Oracle (black-box function): a device that computes a classical boolean function f but is only accessible through a specified quantum unitary. We treat f as unknown and count how many queries to the oracle are needed.
  • Constant function: a boolean function f: {0,1}^n -> {0,1} that returns the same value (0 or 1) for every input.
  • Balanced function: a boolean function f: {0,1}^n -> {0,1} that returns 0 for exactly half of the inputs and 1 for the other half (so exactly 2^{n-1} zeros and 2^{n-1} ones).
  • Phase oracle form: a unitary that applies a phase (-1)^{f(x)} to |x> (we implement this via a standard oracle that flips a target qubit when f(x)=1 and using the target prepared in |->).

Exact statement (Deutsch, n=1): Given an unknown boolean function f:{0,1}->{0,1}, the Deutsch quantum algorithm determines whether f is constant or balanced (i.e., whether f(0)=f(1) or f(0)≠f(1)) with one query to a quantum oracle, whereas a deterministic classical algorithm needs two queries in the worst case.

Exact statement (Deutsch–Jozsa, n >= 1): Given an unknown boolean function f:{0,1}^n->{0,1} with the promise that f is either constant or balanced, the Deutsch–Jozsa quantum algorithm determines which case holds with a single query to the quantum oracle U_f (and with certainty). A classical deterministic algorithm needs at least 2^{n-1}+1 queries in the worst case to decide with certainty.

Assumptions behind both algorithms:

  • The oracle U_f is available and implements the classical mapping on basis states: |x>|y> -> |x>|y ⊕ f(x)> (⊕ is XOR). This is reversible.
  • The promise in Deutsch–Jozsa: f is either constant or balanced (no other functions allowed). Without the promise, the algorithm cannot decide reliably.
  • Ideal gates, no noise, perfect measurement.

Step-by-step derivation (Deutsch, n=1):

  1. Start with two qubits in |0>|1>.
  2. Apply Hadamard H to both qubits. Recall H|0> = (|0>+|1>)/√2, H|1> = (|0>-|1>)/√2. After H⊗H the state is 1/2 (|0>+|1>) ⊗ (|0>-|1>).
  3. Apply oracle U_f: |x>|y> -> |x>|y ⊕ f(x)>. Note: when the target qubit is |-> = (|0>-|1>)/√2, the oracle action introduces a phase (-1)^{f(x)} on the control qubit component |x>, because U_f(|x>|->) = |x> (|0 ⊕ f(x)> - |1 ⊕ f(x)>)/√2 = (-1)^{f(x)} |x>|->. So after the oracle the two-qubit state is (1/2) Σ_{x=0,1} (-1)^{f(x)} |x> ⊗ (|0>-|1>) = (1/√2) (Σ_x (-1)^{f(x)}|x>) ⊗ |->.
  4. Apply H to the first qubit only. Use H|x> = (1/√2) Σ_{y=0,1} (-1)^{x·y} |y>, where x·y is bitwise AND modulo 2 (here just xy). The amplitude for |0> on the first qubit becomes (1/2) Σ_x (-1)^{f(x)}. If f is constant (f(0)=f(1)=c), this sum = 2(-1)^c /2 = (-1)^c so amplitude magnitude 1 → measurement yields |0> with certainty. If f is balanced (f(0)≠f(1)), the sum = 0 → amplitude 0 → measurement never yields |0>. Thus a single query suffices to decide.

Step-by-step derivation (Deutsch–Jozsa, general n):

  1. Prepare n input qubits in |0>^{⊗n} and one target qubit in |1>.
  2. Apply H to all n+1 qubits. The n input qubits become an equal superposition: (1/2^{n/2}) Σ_{x∈{0,1}^n} |x>. The target becomes |->.
  3. Apply oracle U_f. Because the target is |->, U_f maps each |x>|-> to (-1)^{f(x)} |x>|->, giving the state (1/2^{n/2}) Σ_x (-1)^{f(x)} |x> ⊗ |->.
  4. Apply H^{⊗n} to the n input qubits. Use the identity H^{⊗n} |x> = (1/2^{n/2}) Σ_{y∈{0,1}^n} (-1)^{x·y} |y|, where x·y = Σ_i x_i y_i (mod 2). The amplitude for |0^n> is (1/2^n) Σ_{x} (-1)^{f(x)}.
    • If f is constant with value c, the sum = 2^n (-1)^c so amplitude = (-1)^c → measurement yields |0^n> with certainty.
    • If f is balanced, the sum = 0 → amplitude 0 → measurement never yields |0^n>. Thus a single oracle query and a final measurement of the first n qubits distinguishes constant vs balanced with certainty. Specifically, observing |0^n> indicates f is constant; observing any other n-bit string indicates f is balanced.

What to draw for an exam diagram:

  • Draw n wires for input qubits, one wire for the target qubit. On inputs place |0> states on each input wire and |1> on the target wire. Draw H gates on all wires, a box labeled U_f acting on all n+1 wires, then H gates on the n input wires only, and finally measurement boxes on the n input wires. Label the lines with states at key points: after the first H layer (equal superposition), after U_f (phased superposition), after final H (show amplitude expression for |0^n>).

Where I am unsure / caveats: I have presented the usual textbook oracle form and the usual classical deterministic lower bound 2^{n-1}+1 — please verify you want to state that exact worst-case classical bound as I wrote it, and confirm any phrasing you prefer for x·y (bitwise inner product mod 2) in your syllabus.

Formulas

H = (1/√2) [[1, 1], [1, -1]] — Hadamard gate matrix. Applies to a single qubit. No SI units. U_f: |x>|y> -> |x>|y ⊕ f(x)> — oracle unitary mapping basis states. x is n-bit string, y is 1 bit. Reversible operation. |-> = (|0> - |1>)/√2 — the minus state of a single qubit. No units. U_f(|x>|->) = (-1)^{f(x)} |x>|-> — shows oracle acting as a phase oracle when target is |->. H^{⊗n} |x> = (1/2^{n/2}) Σ_{y∈{0,1}^n} (-1)^{x·y} |y> — action of n-fold Hadamard. x·y = Σ_i x_i y_i (mod 2). Amplitude(|0^n>) = (1/2^n) Σ_{x∈{0,1}^n} (-1)^{f(x)} — amplitude of the all-zero basis state after the full DJ circuit (before measuring the inputs). Probability(|0^n>) = |(1/2^n) Σ_x (-1)^{f(x)}|^2 — probability to observe |0^n> on the input register.

Worked examples

Model exam question 1 (Deutsch, n=1): Question: For f:{0,1}->{0,1} with f(x)=x (so f(0)=0, f(1)=1), show the Deutsch algorithm evolution and state the measured result on the first qubit. Model answer (stepwise):

  1. Initial state: |0>|1>.
  2. After H⊗H: (1/2)(|0>+|1>) ⊗ (|0>-|1>). Write as (1/2) Σ_{x=0,1} |x> ⊗ (|0>-|1>).
  3. Oracle U_f applies phase (-1)^{f(x)} to |x> because target is |->. For f(0)=0,f(1)=1 the state becomes (1/2)(+1·|0> + (-1)·|1>) ⊗ (|0>-|1>) = (1/2)(|0> - |1>) ⊗ (|0>-|1>).
  4. Apply H to the first qubit: H(|0>-|1>)/√2 = |1> (compute: H|0> = (|0>+|1>)/√2, H|1>=(|0>-|1>)/√2; combine gives |1>). After normalization the input register is |1> and the target is |->.
  5. Measurement of the first qubit yields |1> with certainty. Final answer (bold): The first qubit will be measured as 1 with probability 1; hence f is balanced.

Model exam question 2 (Deutsch–Jozsa, n=2): Question A: Let f be constant with f(x)=0 for all x∈{00,01,10,11}. Show the amplitude for |00> after the algorithm. Model answer A:

  1. Amplitude(|00>) = (1/2^2) Σ_x (-1)^{f(x)} = (1/4)·(1+1+1+1) = 1.
  2. So measurement yields |00> with certainty. Final: Measurement result is |00>, so f is constant.

Question B: Let f(x) = x0 XOR x1 (parity of the two bits). This is balanced. Compute the same amplitude. Model answer B:

  1. Compute (-1)^{f(x)} for x in (00,01,10,11): (+1, -1, -1, +1). Sum = 0.
  2. Amplitude(|00>) = (1/4)·0 = 0, so |00> is never observed. Final: Measurement will not yield |00>; therefore f is balanced.

Common mistakes

  • Confusing the oracle forms: writing U_f as |x> -> (-1)^{f(x)}|x> without explaining the role of the target qubit. The phase form is derived only because the target is |->.
  • Thinking Deutsch–Jozsa gives exponential speedup for practical unpromised functions. The algorithm relies crucially on the promise (constant vs balanced).
  • Sign and parity mistakes in the inner product x·y (remember it is modulo 2).

For your exam

Typical question types:

  • Definitions and short notes: define oracle, constant vs balanced, explain the phase trick (target in |->).
  • Derivations: show step-by-step evolution for n=1 Deutsch and general n Deutsch–Jozsa; derive amplitude formula for |0^n>.
  • Proof/argument: show why amplitude is ±1 for constant and 0 for balanced, and explain classical query complexity required to decide deterministically.
  • Circuit drawing: draw the n+1 wire circuit with H, U_f and final H and measurement; label intermediate states. What a full-marks answer includes: correct state vectors at each stage, correct normalization, explicit use of U_f action on |->, final amplitude expression (1/2^n Σ_x (-1)^{f(x)}), and a clear statement linking measurement outcome to constant/balanced.

Quick check

  1. In Deutsch (n=1) why do we put the target qubit in |1> before the first Hadamard?
  2. Write the oracle mapping U_f on basis states.
  3. In Deutsch–Jozsa, what is the amplitude of |0^n> when f is balanced? Answers: 1) So that after H the target is |-> and oracle action becomes a phase (-1)^{f(x)} on |x>. 2) U_f: |x>|y> -> |x>|y ⊕ f(x)>. 3) Zero (amplitude = 0).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?