QuantumCore

Lesson 27 · 75 min · core

The Deutsch–Jozsa Algorithm

Loading progress…

Given a black box f:{0,1}n→{0,1}f:\{0,1\}^n\to\{0,1\} promised to be either constant or balanced, the Deutsch–Jozsa algorithm decides which with a single query to the reversible oracle Uf∣x⟩∣y⟩=∣x⟩∣y⊕f(x)⟩U_f\lvert x\rangle\lvert y\rangle=\lvert x\rangle\lvert y\oplus f(x)\rangle, whereas every deterministic classical algorithm needs 2n−1+12^{n-1}+1 queries in the worst case. This lesson proves that UfU_f is unitary, derives phase kickback, proves the Hadamard-transform formula H⊗n∣x⟩=2−n/2∑z(−1)x⋅z∣z⟩H^{\otimes n}\lvert x\rangle=2^{-n/2}\sum_z(-1)^{x\cdot z}\lvert z\rangle by induction, computes the full output state, works through Deutsch's algorithm (n=1n=1) vector by vector, and proves the classical lower bound with an adversary argument, together with the honest caveat that a randomized classical algorithm with kk queries already errs with probability at most 21−k2^{1-k}.

Prerequisites: Controlled Gates: CNOT, CZ, SWAP, Hadamard, S and T Gates

Objectives and key results
  • Prove that the bit oracle Uf∣x,y⟩=∣x,y⊕f(x)⟩U_f\lvert x,y\rangle=\lvert x,y\oplus f(x)\rangle is a permutation matrix with Uf2=IU_f^2=I, hence unitary and self-inverse.

  • Derive the phase-kickback identity Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩U_f\lvert x\rangle\lvert-\rangle=(-1)^{f(x)}\lvert x\rangle\lvert-\rangle from the two cases f(x)=0f(x)=0 and f(x)=1f(x)=1.

  • Prove H⊗n∣x⟩=2−n/2∑z∈{0,1}n(−1)x⋅z∣z⟩H^{\otimes n}\lvert x\rangle=2^{-n/2}\sum_{z\in\{0,1\}^n}(-1)^{x\cdot z}\lvert z\rangle by induction on nn.

  • Compute the Deutsch–Jozsa output state ∑z(2−n∑x(−1)f(x)+x⋅z)∣z⟩∣−⟩\sum_z\big(2^{-n}\sum_x(-1)^{f(x)+x\cdot z}\big)\lvert z\rangle\lvert-\rangle and show that the outcome 0n0^n has probability 11 for constant ff and 00 for balanced ff.

  • Prove that a deterministic classical algorithm needs 2n−1+12^{n-1}+1 queries in the worst case, and that kk random classical queries decide the problem with one-sided error at most 21−k2^{1-k}.

definition · Bit oracle
Uf∣x⟩∣y⟩=∣x⟩∣y⊕f(x)⟩,Uf2=I,Uf†=UfU_f\lvert x\rangle\lvert y\rangle=\lvert x\rangle\lvert y\oplus f(x)\rangle,\qquad U_f^2=I,\qquad U_f^\dagger=U_f
identity · Phase kickback
Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩,∣−⟩=12(∣0⟩−∣1⟩)U_f\lvert x\rangle\lvert-\rangle=(-1)^{f(x)}\lvert x\rangle\lvert-\rangle,\qquad\lvert-\rangle=\tfrac1{\sqrt2}\big(\lvert0\rangle-\lvert1\rangle\big)
theorem · Hadamard transform
H⊗n∣x⟩=12n/2∑z∈{0,1}n(−1)x⋅z∣z⟩,x⋅z=⨁kxkzkH^{\otimes n}\lvert x\rangle=\frac1{2^{n/2}}\sum_{z\in\{0,1\}^n}(-1)^{x\cdot z}\lvert z\rangle,\qquad x\cdot z=\textstyle\bigoplus_k x_kz_k
formula · Deutsch–Jozsa output
(H⊗n⊗I) Uf H⊗(n+1)∣0n⟩∣1⟩=∑z(12n∑x(−1)f(x)+x⋅z)∣z⟩∣−⟩(H^{\otimes n}\otimes I)\,U_f\,H^{\otimes(n+1)}\lvert0^n\rangle\lvert1\rangle=\sum_{z}\Big(\frac1{2^n}\sum_x(-1)^{f(x)+x\cdot z}\Big)\lvert z\rangle\lvert-\rangle
theorem · Query complexity
Qexact=1,D=2n−1+1,Rε≤k  with  ε=21−kQ_{\mathrm{exact}}=1,\qquad D=2^{n-1}+1,\qquad R_{\varepsilon}\le k\ \text{ with }\ \varepsilon=2^{1-k}

The promise problem and the bit oracle#

Definition

Constant and balanced functions

A Boolean function f:{0,1}n→{0,1}f:\{0,1\}^n\to\{0,1\} is constant if f(x)=cf(x)=c for all 2n2^n inputs (c∈{0,1}c\in\{0,1\}), and balanced if f(x)=0f(x)=0 for exactly 2n−12^{n-1} inputs and f(x)=1f(x)=1 for the other 2n−12^{n-1}. The Deutsch–Jozsa problem is a promise problem: we are guaranteed that ff is one of the two kinds and must decide which, using as few evaluations (queries) of ff as possible. For n=2n=2 there are 222=162^{2^2}=16 functions; 22 are constant, (42)=6\binom42=6 are balanced, and the remaining 88 (those with one or three ones) violate the promise.

Definition

Bit oracle

A quantum algorithm may access ff only through the bit oracle on n+1n+1 qubits, defined on basis states by Uf∣x⟩∣y⟩=∣x⟩∣y⊕f(x)⟩,x∈{0,1}n, y∈{0,1},U_f\lvert x\rangle\lvert y\rangle=\lvert x\rangle\lvert y\oplus f(x)\rangle,\qquad x\in\{0,1\}^n,\ y\in\{0,1\}, and extended linearly. The map ∣x⟩∣y⟩↦∣x⟩∣f(x)⟩\lvert x\rangle\lvert y\rangle\mapsto\lvert x\rangle\lvert f(x)\rangle would discard yy and therefore could not be invertible (both ∣x⟩∣0⟩\lvert x\rangle\lvert0\rangle and ∣x⟩∣1⟩\lvert x\rangle\lvert1\rangle would have the same image); adding f(x)f(x) to an ancilla bit yy modulo 22 keeps the input and makes the map reversible. One application of UfU_f counts as one query.

Proposition

UfU_f is an involutive permutation, hence unitary

For every f:{0,1}n→{0,1}f:\{0,1\}^n\to\{0,1\} the operator UfU_f permutes the 2n+12^{n+1} computational basis states, satisfies Uf2=IU_f^2=I, and is therefore unitary with Uf†=Uf−1=UfU_f^\dagger=U_f^{-1}=U_f.

Proof (5 steps)
  1. Step 1:
    Uf∣x,y⟩=∣x, y⊕f(x)⟩∈{∣x′,y′⟩: x′∈{0,1}n, y′∈{0,1}}U_f\lvert x,y\rangle=\lvert x,\,y\oplus f(x)\rangle\in\big\{\lvert x',y'\rangle:\ x'\in\{0,1\}^n,\ y'\in\{0,1\}\big\}
    Each basis state is mapped to a basis state, so every column of the matrix of UfU_f contains exactly one entry 11 and all other entries 00.
  2. Step 2:
    Uf2∣x,y⟩=Uf∣x, y⊕f(x)⟩=∣x, y⊕f(x)⊕f(x)⟩=∣x,y⟩U_f^2\lvert x,y\rangle=U_f\lvert x,\,y\oplus f(x)\rangle=\lvert x,\,y\oplus f(x)\oplus f(x)\rangle=\lvert x,y\rangle
    Apply the definition twice: the input register xx is unchanged by the first application, so the second one adds the same bit f(x)f(x), and b⊕b=0b\oplus b=0 for every bit bb.
  3. Step 3:
    Uf2=I ⟹ Uf is a bijection of the basis with Uf−1=UfU_f^2=I\ \Longrightarrow\ U_f\ \text{is a bijection of the basis with}\ U_f^{-1}=U_f
    Uf2U_f^2 agrees with II on a basis, so Uf2=IU_f^2=I by linearity. A map that is its own inverse is bijective; in particular two different basis states never have the same image, so every row also contains exactly one 11: UfU_f is a permutation matrix.
  4. Step 4:
    (UfTUf)jk=∑iPijPik=δjk ⟹ Uf†Uf=UfTUf=I(U_f^TU_f)_{jk}=\sum_iP_{ij}P_{ik}=\delta_{jk}\ \Longrightarrow\ U_f^\dagger U_f=U_f^TU_f=I
    For a permutation matrix PP, columns jj and kk are the standard basis vectors eπ(j)e_{\pi(j)} and eπ(k)e_{\pi(k)}, which are orthonormal. The entries are real, so Uf†=UfTU_f^\dagger=U_f^T.
  5. Step 5:
    Uf†=Uf−1=UfU_f^\dagger=U_f^{-1}=U_f
    Unitarity gives Uf†=Uf−1U_f^\dagger=U_f^{-1} and the involution gives Uf−1=UfU_f^{-1}=U_f; hence UfU_f is also Hermitian, a real symmetric permutation matrix.

∎

For f(x0x1)=x0⊕x1f(x_0x_1)=x_0\oplus x_1 the truth table is (0,1,1,0)(0,1,1,0). On the four 2×22\times2 blocks with fixed input xx, UfU_f acts on the ancilla as Xf(x)X^{f(x)}, so Uf=∣00⟩⟨00∣⊗I+∣01⟩⟨01∣⊗X+∣10⟩⟨10∣⊗X+∣11⟩⟨11∣⊗IU_f=\lvert00\rangle\langle00\rvert\otimes I+\lvert01\rangle\langle01\rvert\otimes X+\lvert10\rangle\langle10\rvert\otimes X+\lvert11\rangle\langle11\rvert\otimes I. The same operator is realised by two CNOTs onto the ancilla, Uf=CNOT1→2 CNOT0→2U_f=\mathrm{CNOT}_{1\to2}\,\mathrm{CNOT}_{0\to2}, because they map ∣x0,x1,y⟩↦∣x0,x1,y⊕x0⟩↦∣x0,x1,y⊕x0⊕x1⟩\lvert x_0,x_1,y\rangle\mapsto\lvert x_0,x_1,y\oplus x_0\rangle\mapsto\lvert x_0,x_1,y\oplus x_0\oplus x_1\rangle. Every linear function f(x)=s⋅xf(x)=s\cdot x is realised the same way with one CNOT from each qubit kk with sk=1s_k=1.

Ux0⊕x1=(1000000001000000000100000010000000000100000010000000001000000001)=(I0000X0000X0000I)U_{x_0\oplus x_1}=\begin{pmatrix}1&0&0&0&0&0&0&0\\0&1&0&0&0&0&0&0\\0&0&0&1&0&0&0&0\\0&0&1&0&0&0&0&0\\0&0&0&0&0&1&0&0\\0&0&0&0&1&0&0&0\\0&0&0&0&0&0&1&0\\0&0&0&0&0&0&0&1\end{pmatrix}=\begin{pmatrix}I&0&0&0\\0&X&0&0\\0&0&X&0\\0&0&0&I\end{pmatrix}
U_f for f(x0 x1) = x0 ⊕ x18×8

Unitary verified: U†U=IU^\dagger U = I (max deviation 0.0e+0)

Phase kickback#

Derivation

Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩U_f\lvert x\rangle\lvert-\rangle=(-1)^{f(x)}\lvert x\rangle\lvert-\rangle

  1. Step 1:
    Uf∣x⟩∣−⟩=12(Uf∣x⟩∣0⟩−Uf∣x⟩∣1⟩)=12(∣x⟩∣f(x)⟩−∣x⟩∣1⊕f(x)⟩)U_f\lvert x\rangle\lvert-\rangle=\tfrac1{\sqrt2}\big(U_f\lvert x\rangle\lvert0\rangle-U_f\lvert x\rangle\lvert1\rangle\big)=\tfrac1{\sqrt2}\big(\lvert x\rangle\lvert f(x)\rangle-\lvert x\rangle\lvert1\oplus f(x)\rangle\big)
    Expand ∣−⟩=12(∣0⟩−∣1⟩)\lvert-\rangle=\tfrac1{\sqrt2}(\lvert0\rangle-\lvert1\rangle), use linearity, and apply the definition with y=0y=0 (0⊕f(x)=f(x)0\oplus f(x)=f(x)) and y=1y=1.
  2. Step 2:
    f(x)=0:12(∣x⟩∣0⟩−∣x⟩∣1⟩)=∣x⟩∣−⟩=(−1)0∣x⟩∣−⟩f(x)=0:\quad\tfrac1{\sqrt2}\big(\lvert x\rangle\lvert0\rangle-\lvert x\rangle\lvert1\rangle\big)=\lvert x\rangle\lvert-\rangle=(-1)^0\lvert x\rangle\lvert-\rangle
    Case f(x)=0f(x)=0: the ancilla is untouched.
  3. Step 3:
    f(x)=1:12(∣x⟩∣1⟩−∣x⟩∣0⟩)=−∣x⟩12(∣0⟩−∣1⟩)=(−1)1∣x⟩∣−⟩f(x)=1:\quad\tfrac1{\sqrt2}\big(\lvert x\rangle\lvert1\rangle-\lvert x\rangle\lvert0\rangle\big)=-\lvert x\rangle\tfrac1{\sqrt2}\big(\lvert0\rangle-\lvert1\rangle\big)=(-1)^1\lvert x\rangle\lvert-\rangle
    Case f(x)=1f(x)=1: the oracle applies XX to the ancilla, and X∣−⟩=−∣−⟩X\lvert-\rangle=-\lvert-\rangle because ∣−⟩\lvert-\rangle is the −1-1 eigenvector of XX.
  4. Step 4:
    Uf(∑xαx∣x⟩)∣−⟩=(∑x(−1)f(x)αx∣x⟩)∣−⟩U_f\Big(\sum_x\alpha_x\lvert x\rangle\Big)\lvert-\rangle=\Big(\sum_x(-1)^{f(x)}\alpha_x\lvert x\rangle\Big)\lvert-\rangle
    Both cases read (−1)f(x)∣x⟩∣−⟩(-1)^{f(x)}\lvert x\rangle\lvert-\rangle; by linearity the oracle acts on any input superposition as the diagonal phase oracle Of=∑x(−1)f(x)∣x⟩⟨x∣O_f=\sum_x(-1)^{f(x)}\lvert x\rangle\langle x\rvert, and the ancilla stays in the product state ∣−⟩\lvert-\rangle.

Result

Uf∣x⟩∣−⟩=(−1)f(x)∣x⟩∣−⟩\boxed{U_f\lvert x\rangle\lvert-\rangle=(-1)^{f(x)}\lvert x\rangle\lvert-\rangle}

Quick check

The ancilla is prepared in ∣+⟩\lvert+\rangle instead of ∣−⟩\lvert-\rangle. What does UfU_f do to ∣x⟩∣+⟩\lvert x\rangle\lvert+\rangle?

Select one answer

The Hadamard transform on n qubits#

Lemma

H⊗n∣x⟩=2−n/2∑z(−1)x⋅z∣z⟩H^{\otimes n}\lvert x\rangle=2^{-n/2}\sum_z(-1)^{x\cdot z}\lvert z\rangle

For every n≥1n\ge1 and every x∈{0,1}nx\in\{0,1\}^n, H⊗n∣x⟩=12n/2∑z∈{0,1}n(−1)x⋅z∣z⟩.H^{\otimes n}\lvert x\rangle=\frac1{2^{n/2}}\sum_{z\in\{0,1\}^n}(-1)^{x\cdot z}\lvert z\rangle . In particular H⊗n∣0n⟩=2−n/2∑z∣z⟩H^{\otimes n}\lvert0^n\rangle=2^{-n/2}\sum_z\lvert z\rangle is the uniform superposition.

Proof (5 steps)
  1. Step 1:
    n=1:H∣x⟩=12(∣0⟩+(−1)x∣1⟩)=12∑z∈{0,1}(−1)xz∣z⟩n=1:\quad H\lvert x\rangle=\tfrac1{\sqrt2}\big(\lvert0\rangle+(-1)^x\lvert1\rangle\big)=\tfrac1{\sqrt2}\sum_{z\in\{0,1\}}(-1)^{xz}\lvert z\rangle
    Base case. H=12(111−1)H=\tfrac1{\sqrt2}\begin{pmatrix}1&1\\1&-1\end{pmatrix}: column x=0x=0 is 12(1,1)T\tfrac1{\sqrt2}(1,1)^T and column x=1x=1 is 12(1,−1)T\tfrac1{\sqrt2}(1,-1)^T. The coefficient of ∣z⟩\lvert z\rangle is 12(−1)xz\tfrac1{\sqrt2}(-1)^{xz}, since xz=1xz=1 only for x=z=1x=z=1.
  2. Step 2:
    x=x′xn∈{0,1}n+1, x′∈{0,1}n:H⊗(n+1)∣x⟩=(H⊗n∣x′⟩)⊗(H∣xn⟩)x=x'x_n\in\{0,1\}^{n+1},\ x'\in\{0,1\}^n:\quad H^{\otimes(n+1)}\lvert x\rangle=\big(H^{\otimes n}\lvert x'\rangle\big)\otimes\big(H\lvert x_n\rangle\big)
    Inductive step: assume the claim for nn and split xx into its first nn bits x′=x0⋯xn−1x'=x_0\cdots x_{n-1} and its last bit xnx_n. Then ∣x⟩=∣x′⟩⊗∣xn⟩\lvert x\rangle=\lvert x'\rangle\otimes\lvert x_n\rangle, H⊗(n+1)=H⊗n⊗HH^{\otimes(n+1)}=H^{\otimes n}\otimes H, and (A⊗B)(∣u⟩⊗∣v⟩)=A∣u⟩⊗B∣v⟩(A\otimes B)(\lvert u\rangle\otimes\lvert v\rangle)=A\lvert u\rangle\otimes B\lvert v\rangle.
  3. Step 3:
    =(12n/2∑z′∈{0,1}n(−1)x′⋅z′∣z′⟩)⊗(12∑zn∈{0,1}(−1)xnzn∣zn⟩)=\Big(\frac1{2^{n/2}}\sum_{z'\in\{0,1\}^n}(-1)^{x'\cdot z'}\lvert z'\rangle\Big)\otimes\Big(\frac1{\sqrt2}\sum_{z_n\in\{0,1\}}(-1)^{x_nz_n}\lvert z_n\rangle\Big)
    Insert the induction hypothesis for the first factor and the base case for the second.
  4. Step 4:
    =12(n+1)/2∑z′∈{0,1}n ∑zn∈{0,1}(−1)x′⋅z′+xnzn ∣z′⟩⊗∣zn⟩=\frac1{2^{(n+1)/2}}\sum_{z'\in\{0,1\}^n}\ \sum_{z_n\in\{0,1\}}(-1)^{x'\cdot z'+x_nz_n}\,\lvert z'\rangle\otimes\lvert z_n\rangle
    The tensor product is bilinear, so the product of two sums is the double sum of the products; the prefactors multiply to 2−n/2⋅2−1/2=2−(n+1)/22^{-n/2}\cdot2^{-1/2}=2^{-(n+1)/2} and the signs multiply to (−1)x′⋅z′+xnzn(-1)^{x'\cdot z'+x_nz_n}.
  5. Step 5:
    =12(n+1)/2∑z∈{0,1}n+1(−1)x⋅z∣z⟩=\frac1{2^{(n+1)/2}}\sum_{z\in\{0,1\}^{n+1}}(-1)^{x\cdot z}\lvert z\rangle
    The pairs (z′,zn)(z',z_n) run exactly once over all z=z′zn∈{0,1}n+1z=z'z_n\in\{0,1\}^{n+1}, ∣z′⟩⊗∣zn⟩=∣z⟩\lvert z'\rangle\otimes\lvert z_n\rangle=\lvert z\rangle, and x⋅z=x′⋅z′⊕xnzn≡x′⋅z′+xnzn(mod2)x\cdot z=x'\cdot z'\oplus x_nz_n\equiv x'\cdot z'+x_nz_n\pmod2, which is all that (−1)(⋅)(-1)^{(\cdot)} sees. This is the claim for n+1n+1; by induction it holds for all n≥1n\ge1.

∎

For n=2n=2 the lemma gives the four columns of H⊗HH\otimes H at once: the coefficient of ∣z⟩\lvert z\rangle in H⊗2∣x⟩H^{\otimes2}\lvert x\rangle is 12(−1)x0z0+x1z1\tfrac12(-1)^{x_0z_0+x_1z_1}, so H⊗H=12(11111−11−111−1−11−1−11),H\otimes H=\frac12\begin{pmatrix}1&1&1&1\\1&-1&1&-1\\1&1&-1&-1\\1&-1&-1&1\end{pmatrix}, with rows and columns in the order 00,01,10,1100,01,10,11. The matrix is real and symmetric and squares to II; its entry (z,x)(z,x) is 12(−1)x⋅z\tfrac12(-1)^{x\cdot z}.

The Deutsch–Jozsa algorithm#

Derivation

The state after each layer of the Deutsch–Jozsa circuit

  1. Step 1:
    ∣ψ0⟩=∣0n⟩∣1⟩\lvert\psi_0\rangle=\lvert0^n\rangle\lvert1\rangle
    Start from ∣0n⟩∣0⟩\lvert0^n\rangle\lvert0\rangle and apply XX to the ancilla.
  2. Step 2:
    ∣ψ1⟩=H⊗(n+1)∣ψ0⟩=(12n/2∑x∈{0,1}n∣x⟩)∣−⟩\lvert\psi_1\rangle=H^{\otimes(n+1)}\lvert\psi_0\rangle=\Big(\frac1{2^{n/2}}\sum_{x\in\{0,1\}^n}\lvert x\rangle\Big)\lvert-\rangle
    The lemma with x=0nx=0^n gives the uniform superposition on the inputs (every sign is (−1)0=1(-1)^{0}=1), and H∣1⟩=∣−⟩H\lvert1\rangle=\lvert-\rangle on the ancilla.
  3. Step 3:
    ∣ψ2⟩=Uf∣ψ1⟩=(12n/2∑x(−1)f(x)∣x⟩)∣−⟩\lvert\psi_2\rangle=U_f\lvert\psi_1\rangle=\Big(\frac1{2^{n/2}}\sum_{x}(-1)^{f(x)}\lvert x\rangle\Big)\lvert-\rangle
    Phase kickback, applied term by term with αx=2−n/2\alpha_x=2^{-n/2}: one query writes the whole truth table of ff into the signs of the 2n2^n amplitudes.
  4. Step 4:
    ∣ψ3⟩=(H⊗n⊗I)∣ψ2⟩=12n/2∑x(−1)f(x) 12n/2∑z(−1)x⋅z∣z⟩∣−⟩\lvert\psi_3\rangle=(H^{\otimes n}\otimes I)\lvert\psi_2\rangle=\frac1{2^{n/2}}\sum_x(-1)^{f(x)}\,\frac1{2^{n/2}}\sum_z(-1)^{x\cdot z}\lvert z\rangle\lvert-\rangle
    Apply the lemma to each ∣x⟩\lvert x\rangle; the ancilla is untouched.
  5. Step 5:
    ∣ψ3⟩=∑z∈{0,1}naz∣z⟩∣−⟩,az=12n∑x∈{0,1}n(−1)f(x)+x⋅z\lvert\psi_3\rangle=\sum_{z\in\{0,1\}^n}a_z\lvert z\rangle\lvert-\rangle,\qquad a_z=\frac1{2^n}\sum_{x\in\{0,1\}^n}(-1)^{f(x)+x\cdot z}
    Exchange the two finite sums and collect the coefficient of each ∣z⟩\lvert z\rangle; (−1)f(x)(−1)x⋅z=(−1)f(x)+x⋅z(-1)^{f(x)}(-1)^{x\cdot z}=(-1)^{f(x)+x\cdot z}.
  6. Step 6:
    P(z)=∣az∣2,a0n=12n∑x(−1)f(x)P(z)=\lvert a_z\rvert^2,\qquad a_{0^n}=\frac1{2^n}\sum_{x}(-1)^{f(x)}
    Born rule on the input register (the ancilla factor ∣−⟩\lvert-\rangle has norm 11). For z=0nz=0^n every x⋅zx\cdot z is 00.

Result

(H⊗n⊗I) Uf H⊗(n+1)∣0n⟩∣1⟩=∑z(12n∑x(−1)f(x)+x⋅z)∣z⟩∣−⟩\boxed{(H^{\otimes n}\otimes I)\,U_f\,H^{\otimes(n+1)}\lvert0^n\rangle\lvert1\rangle=\sum_{z}\Big(\frac1{2^n}\sum_x(-1)^{f(x)+x\cdot z}\Big)\lvert z\rangle\lvert-\rangle}
Theorem

Constant gives 0n0^n with certainty, balanced never gives 0n0^n

Measuring the input register of ∣ψ3⟩\lvert\psi_3\rangle yields 0n0^n with probability 11 if ff is constant and with probability 00 if ff is balanced. Hence one query decides the Deutsch–Jozsa problem with certainty: answer "constant" if and only if the outcome is 0n0^n.

Proof (4 steps)
  1. Step 1:
    f≡c:a0n=12n∑x(−1)c=2n(−1)c2n=(−1)c,P(0n)=1f\equiv c:\quad a_{0^n}=\frac1{2^n}\sum_x(-1)^c=\frac{2^n(-1)^c}{2^n}=(-1)^c,\qquad P(0^n)=1
    All 2n2^n terms equal (−1)c(-1)^c.
  2. Step 2:
    ∑z∣az∣2=1 ⟹ az=0 for all z≠0n,∣ψ3⟩=(−1)c∣0n⟩∣−⟩\sum_z\lvert a_z\rvert^2=1\ \Longrightarrow\ a_z=0\ \text{for all}\ z\neq0^n,\qquad\lvert\psi_3\rangle=(-1)^c\lvert0^n\rangle\lvert-\rangle
    ∣ψ3⟩\lvert\psi_3\rangle is a unit vector (unitaries preserve the norm), so once ∣a0n∣=1\lvert a_{0^n}\rvert=1 all other amplitudes vanish: the full state after the second H⊗nH^{\otimes n} is the basis state ∣0n⟩\lvert0^n\rangle times a global sign.
  3. Step 3:
    f balanced:∑x(−1)f(x)=#{x:f(x)=0}−#{x:f(x)=1}=2n−1−2n−1=0f\ \text{balanced}:\quad\sum_x(-1)^{f(x)}=\#\{x:f(x)=0\}-\#\{x:f(x)=1\}=2^{n-1}-2^{n-1}=0
    Each input with f(x)=0f(x)=0 contributes +1+1 and each input with f(x)=1f(x)=1 contributes −1-1; balanced means both counts are 2n−12^{n-1}.
  4. Step 4:
    a0n=0,P(0n)=0,∣ψ3⟩=∑z≠0naz∣z⟩∣−⟩a_{0^n}=0,\qquad P(0^n)=0,\qquad\lvert\psi_3\rangle=\sum_{z\neq0^n}a_z\lvert z\rangle\lvert-\rangle
    So a balanced function always produces a non-zero string zz; which one depends on ff, but any z≠0nz\neq0^n certifies "balanced".

∎

For a linear balanced function f(x)=s⋅xf(x)=s\cdot x with s≠0ns\neq0^n the full output is a single basis state: az=2−n∑x(−1)x⋅(s⊕z)a_z=2^{-n}\sum_x(-1)^{x\cdot(s\oplus z)}, because s⋅x+x⋅z≡x⋅(s⊕z)(mod2)s\cdot x+x\cdot z\equiv x\cdot(s\oplus z)\pmod2. The character sum factorises over the bits, ∑x∈{0,1}n(−1)x⋅w=∏k=0n−1 ∑xk∈{0,1}(−1)xkwk=∏k=0n−1(1+(−1)wk)={2n,w=0n0,w≠0n,\sum_{x\in\{0,1\}^n}(-1)^{x\cdot w}=\prod_{k=0}^{n-1}\ \sum_{x_k\in\{0,1\}}(-1)^{x_kw_k}=\prod_{k=0}^{n-1}\big(1+(-1)^{w_k}\big)=\begin{cases}2^n,&w=0^n\\0,&w\neq0^n,\end{cases} since a single factor with wk=1w_k=1 is 1−1=01-1=0. Hence az=1a_z=1 for z=sz=s and az=0a_z=0 otherwise. For n=2n=2 and f=x0⊕x1=11⋅xf=x_0\oplus x_1=11\cdot x the inputs therefore read 1111 with certainty. The circuit below adds a final HH on the ancilla, which turns ∣−⟩\lvert-\rangle back into ∣1⟩\lvert1\rangle, so the whole register ends in ∣111⟩\lvert111\rangle.

Circuit · 3 qubits

Quantum circuit on 3 qubitsX on q2; H on q0; H on q1; H on q2; X on q2 controlled by q0; X on q2 controlled by q1; H on q0; H on q1; H on q2q0q1q2XHHHHHH

Deutsch–Jozsa for n=2n=2 and the balanced function f=x0⊕x1f=x_0\oplus x_1, with the oracle realised as CNOT0→2\mathrm{CNOT}_{0\to2} followed by CNOT1→2\mathrm{CNOT}_{1\to2}. The statevector is exactly ∣111⟩\lvert111\rangle and the histogram shows the single outcome 111111: the inputs read x0x1=11=sx_0x_1=11=s, never 0000, so the function is balanced.

∣ψ⟩=1∣111⟩\lvert\psi\rangle = 1\lvert 111\rangle
  • |000⟩0
  • |001⟩0
  • |010⟩0
  • |011⟩0
  • |100⟩0
  • |101⟩0
  • |110⟩0
  • |111⟩1

Quick check

For n=2n=2 the function with truth table (0,0,0,1)(0,0,0,1) is neither constant nor balanced. Running the Deutsch–Jozsa circuit anyway, what is the probability that the inputs read 0000?

tolerance ±0.000001

Quick check

For n=2n=2 and the balanced function f(x0x1)=x0f(x_0x_1)=x_0, which outcome does the Deutsch–Jozsa circuit produce on the inputs?

Select one answer

Worked example: Deutsch's algorithm (n = 1)#

For n=1n=1 there are four functions f:{0,1}→{0,1}f:\{0,1\}\to\{0,1\}: the constants f≡0f\equiv0, f≡1f\equiv1 and the balanced f(x)=xf(x)=x, f(x)=1⊕xf(x)=1\oplus x. The two-qubit register is (x,y)(x,y) on qubits (0,1)(0,1) and vectors are listed in the order ∣00⟩,∣01⟩,∣10⟩,∣11⟩\lvert00\rangle,\lvert01\rangle,\lvert10\rangle,\lvert11\rangle. The oracles are II, I⊗XI\otimes X, CNOT0→1\mathrm{CNOT}_{0\to1} and CNOT0→1(I⊗X)\mathrm{CNOT}_{0\to1}(I\otimes X).

Derivation

Every state vector of Deutsch's algorithm

  1. Step 1:
    ∣ψ0⟩=(I⊗X)∣00⟩=∣01⟩=(0,1,0,0)T\lvert\psi_0\rangle=(I\otimes X)\lvert00\rangle=\lvert01\rangle=(0,1,0,0)^T
    Prepare the ancilla in ∣1⟩\lvert1\rangle.
  2. Step 2:
    ∣ψ1⟩=(H⊗H)∣01⟩=∣+⟩∣−⟩=12(∣00⟩−∣01⟩+∣10⟩−∣11⟩)=12(1,−1,1,−1)T\lvert\psi_1\rangle=(H\otimes H)\lvert01\rangle=\lvert+\rangle\lvert-\rangle=\tfrac12\big(\lvert00\rangle-\lvert01\rangle+\lvert10\rangle-\lvert11\rangle\big)=\tfrac12(1,-1,1,-1)^T
    H∣0⟩=∣+⟩H\lvert0\rangle=\lvert+\rangle, H∣1⟩=∣−⟩H\lvert1\rangle=\lvert-\rangle, and ∣+⟩⊗∣−⟩\lvert+\rangle\otimes\lvert-\rangle multiplied out; this is column 0101 of H⊗HH\otimes H.
  3. Step 3:
    ∣ψ2⟩=Uf∣ψ1⟩=12((−1)f(0)∣0⟩+(−1)f(1)∣1⟩)∣−⟩=12((−1)f(0),−(−1)f(0),(−1)f(1),−(−1)f(1))T\lvert\psi_2\rangle=U_f\lvert\psi_1\rangle=\tfrac1{\sqrt2}\big((-1)^{f(0)}\lvert0\rangle+(-1)^{f(1)}\lvert1\rangle\big)\lvert-\rangle=\tfrac12\big((-1)^{f(0)},-(-1)^{f(0)},(-1)^{f(1)},-(-1)^{f(1)}\big)^T
    Phase kickback on each of the two input branches.
  4. Step 4:
    f≡0: ∣ψ2⟩=12(1,−1,1,−1)T=∣+⟩∣−⟩,f≡1: ∣ψ2⟩=12(−1,1,−1,1)T=−∣+⟩∣−⟩f\equiv0:\ \lvert\psi_2\rangle=\tfrac12(1,-1,1,-1)^T=\lvert+\rangle\lvert-\rangle,\qquad f\equiv1:\ \lvert\psi_2\rangle=\tfrac12(-1,1,-1,1)^T=-\lvert+\rangle\lvert-\rangle
    Constant ff: both branches get the same sign (−1)f(0)=(−1)f(1)(-1)^{f(0)}=(-1)^{f(1)}, which factors out as a global phase.
  5. Step 5:
    f(x)=x: ∣ψ2⟩=12(1,−1,−1,1)T=∣−⟩∣−⟩,f(x)=1⊕x: ∣ψ2⟩=12(−1,1,1,−1)T=−∣−⟩∣−⟩f(x)=x:\ \lvert\psi_2\rangle=\tfrac12(1,-1,-1,1)^T=\lvert-\rangle\lvert-\rangle,\qquad f(x)=1\oplus x:\ \lvert\psi_2\rangle=\tfrac12(-1,1,1,-1)^T=-\lvert-\rangle\lvert-\rangle
    Balanced ff: the two branches get opposite signs, so the input qubit turns from ∣+⟩\lvert+\rangle into ±∣−⟩\pm\lvert-\rangle.
  6. Step 6:
    (H⊗I)∣+⟩∣−⟩=∣0⟩∣−⟩=12(1,−1,0,0)T,(H⊗I)∣−⟩∣−⟩=∣1⟩∣−⟩=12(0,0,1,−1)T(H\otimes I)\lvert+\rangle\lvert-\rangle=\lvert0\rangle\lvert-\rangle=\tfrac1{\sqrt2}(1,-1,0,0)^T,\qquad(H\otimes I)\lvert-\rangle\lvert-\rangle=\lvert1\rangle\lvert-\rangle=\tfrac1{\sqrt2}(0,0,1,-1)^T
    The final Hadamard on the input uses H∣+⟩=∣0⟩H\lvert+\rangle=\lvert0\rangle and H∣−⟩=∣1⟩H\lvert-\rangle=\lvert1\rangle.
  7. Step 7:
    ∣ψ3⟩=(−1)f(0) ∣f(0)⊕f(1)⟩∣−⟩\lvert\psi_3\rangle=(-1)^{f(0)}\,\lvert f(0)\oplus f(1)\rangle\lvert-\rangle
    Collecting the four cases: f≡0↦∣0⟩∣−⟩f\equiv0\mapsto\lvert0\rangle\lvert-\rangle, f≡1↦−∣0⟩∣−⟩f\equiv1\mapsto-\lvert0\rangle\lvert-\rangle, f(x)=x↦∣1⟩∣−⟩f(x)=x\mapsto\lvert1\rangle\lvert-\rangle, f(x)=1⊕x↦−∣1⟩∣−⟩f(x)=1\oplus x\mapsto-\lvert1\rangle\lvert-\rangle. The measured input bit is the parity f(0)⊕f(1)f(0)\oplus f(1), a global property of ff obtained from one query, while neither f(0)f(0) nor f(1)f(1) individually is learned.

Result

Deutsch:  ∣0⟩∣1⟩ → H⊗H  → Uf  → H⊗I  (−1)f(0)∣f(0)⊕f(1)⟩∣−⟩\boxed{\text{Deutsch: }\ \lvert0\rangle\lvert1\rangle\ \xrightarrow{\ H\otimes H\ }\ \xrightarrow{\ U_f\ }\ \xrightarrow{\ H\otimes I\ }\ (-1)^{f(0)}\lvert f(0)\oplus f(1)\rangle\lvert-\rangle}

Classical versus quantum query complexity#

Theorem

Deterministic algorithms need 2n−1+12^{n-1}+1 queries

Every deterministic classical algorithm that solves the Deutsch–Jozsa problem on all constant and balanced f:{0,1}n→{0,1}f:\{0,1\}^n\to\{0,1\} makes at least 2n−1+12^{n-1}+1 queries on some input, and 2n−1+12^{n-1}+1 queries suffice.

Proof (5 steps)
  1. Step 1:
    adversary: answer f(xi)=0 to every query x1,x2,…,xq,q≤2n−1\text{adversary: answer } f(x_i)=0\ \text{to every query } x_1,x_2,\dots,x_q,\qquad q\le2^{n-1}
    Suppose an algorithm AA always halts after at most q≤2n−1q\le2^{n-1} queries. Run it against an adversary who answers 00 to every query. Because AA is deterministic, each query point xi+1x_{i+1} and the final output are functions of the earlier answers only; against the all-zero answers AA queries a fixed set S={x1,…,xq}S=\{x_1,\dots,x_q\} with ∣S∣≤2n−1\lvert S\rvert\le2^{n-1} and outputs a fixed answer.
  2. Step 2:
    f0≡0is constant and consistent with every answerf_0\equiv0\quad\text{is constant and consistent with every answer}
    The constant zero function gives exactly the answers the adversary gave.
  3. Step 3:
    T⊇S, ∣T∣=2n−1;f1(x)={0,x∈T1,x∉Tis balanced and consistentT\supseteq S,\ \lvert T\rvert=2^{n-1};\qquad f_1(x)=\begin{cases}0,&x\in T\\1,&x\notin T\end{cases}\quad\text{is balanced and consistent}
    Since ∣S∣≤2n−1\lvert S\rvert\le2^{n-1} we can enlarge SS to a set TT of exactly 2n−12^{n-1} inputs. Then f1f_1 takes the value 00 on 2n−12^{n-1} inputs and 11 on the other 2n−12^{n-1}, so it is balanced, and it is 00 on every queried point.
  4. Step 4:
    A(f0)=A(f1)whilef0 constant, f1 balancedA(f_0)=A(f_1)\quad\text{while}\quad f_0\ \text{constant},\ f_1\ \text{balanced}
    On f0f_0 and on f1f_1 the algorithm sees the same answers, hence (by determinism, inductively over the queries) asks the same questions and produces the same output. That output is wrong for one of them, contradicting correctness. Therefore some input forces at least 2n−1+12^{n-1}+1 queries.
  5. Step 5:
    2n−1+1 queries suffice: if all answers are equal, f is constant; otherwise balanced2^{n-1}+1\ \text{queries suffice: if all answers are equal, } f \text{ is constant; otherwise balanced}
    Query any 2n−1+12^{n-1}+1 distinct inputs. A balanced function has only 2n−12^{n-1} zeros and 2n−12^{n-1} ones, so it cannot return 2n−1+12^{n-1}+1 equal answers; seeing two different values rules out a constant function. Under the promise the answer is always correct.

∎

Proposition

kk random classical queries err with probability at most 21−k2^{1-k}

The classical randomized algorithm that queries k≥1k\ge1 inputs chosen independently and uniformly at random and answers "constant" exactly when all kk answers agree is always correct on constant ff and errs on balanced ff with probability exactly 21−k2^{1-k}.

Proof (3 steps)
  1. Step 1:
    f≡c ⟹ f(x1)=⋯=f(xk)=c ⟹ output "constant"f\equiv c\ \Longrightarrow\ f(x_1)=\cdots=f(x_k)=c\ \Longrightarrow\ \text{output "constant"}
    A constant function always returns equal answers, so there is no error on constant inputs (one-sided error).
  2. Step 2:
    f balanced:Pr⁡[f(xi)=0]=Pr⁡[f(xi)=1]=2n−12n=12, independently for i=1,…,kf\ \text{balanced}:\quad\Pr[f(x_i)=0]=\Pr[f(x_i)=1]=\tfrac{2^{n-1}}{2^n}=\tfrac12,\ \text{independently for } i=1,\dots,k
    Each xix_i is uniform over {0,1}n\{0,1\}^n and exactly half the inputs map to each value.
  3. Step 3:
    Pr⁡[error]=Pr⁡[all 0]+Pr⁡[all 1]=(12)k+(12)k=21−k\Pr[\text{error}]=\Pr[\text{all }0]+\Pr[\text{all }1]=\big(\tfrac12\big)^k+\big(\tfrac12\big)^k=2^{1-k}
    The algorithm errs on a balanced ff exactly when the kk answers all agree; the two events are disjoint and each has probability 2−k2^{-k} by independence.

∎

TypeScriptrunnable
import { Statevector, matrix } from '@/lib/quantum-simulator';

// Deutsch-Jozsa on n = 2 input qubits (0, 1; qubit 0 = most significant bit) and the ancilla qubit 2,
// run for all 16 functions f: {0,1}^2 -> {0,1}. The truth table is written f(00) f(01) f(10) f(11).
function oracle(f) {
  // U_f |x, y> = |x, y XOR f(x)>: column 2x + y has its single 1 in row 2x + (y XOR f(x))
  const rows = Array.from({ length: 8 }, () => Array(8).fill(0));
  for (let col = 0; col < 8; col++) rows[(col & 6) | ((col & 1) ^ f[col >> 1])][col] = 1;
  return matrix.fromRows(rows);
}
for (let m = 0; m < 16; m++) {
  const f = [3, 2, 1, 0].map((k) => (m >> k) & 1);
  const w = f.reduce((a, b) => a + b, 0);
  const kind = w === 0 || w === 4 ? 'constant' : w === 2 ? 'balanced' : 'neither';
  const sv = Statevector.zero(3)
    .applyGate('X', [2]).applyGate('H', [0]).applyGate('H', [1]).applyGate('H', [2])
    .applyMatrix(oracle(f), [0, 1, 2])
    .applyGate('H', [0]).applyGate('H', [1]);
  const a = sv.amplitudes();
  // P(inputs read 00) = |amp(000)|^2 + |amp(001)|^2, summed over the ancilla
  const p00 = a[0].re ** 2 + a[0].im ** 2 + a[1].re ** 2 + a[1].im ** 2;
  const predicted = ((4 - 2 * w) / 4) ** 2;
  console.log('f = ' + f.join('') + '  ' + kind.padEnd(8) + '  P(00) = ' + p00.toFixed(4) + '  (1 - w/2)^2 = ' + predicted.toFixed(4));
}

Expected output

f = 0000  constant  P(00) = 1.0000  (1 - w/2)^2 = 1.0000
f = 0001  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 0010  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 0011  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 0100  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 0101  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 0110  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 0111  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 1000  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 1001  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 1010  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 1011  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 1100  balanced  P(00) = 0.0000  (1 - w/2)^2 = 0.0000
f = 1101  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 1110  neither   P(00) = 0.2500  (1 - w/2)^2 = 0.2500
f = 1111  constant  P(00) = 1.0000  (1 - w/2)^2 = 1.0000

References

  1. D. Deutsch and R. Jozsa, Rapid solution of problems by quantum computation, Proceedings of the Royal Society of London A 439, 553–558 (1992).doi:10.1098/rspa.1992.0167
  2. R. Cleve, A. Ekert, C. Macchiavello and M. Mosca, Quantum algorithms revisited, Proceedings of the Royal Society of London A 454, 339–354 (1998).doi:10.1098/rspa.1998.0164
  3. M. A. Nielsen and I. L. Chuang, Quantum Computation and Quantum Information, 10th anniversary ed., Cambridge University Press (2010), §1.4.3 (Deutsch’s algorithm) and §1.4.4 (the Deutsch–Jozsa algorithm).doi:10.1017/CBO9780511976667

Hands-on labs

  • Deutsch–Jozsa with one oracle query

    1 step · auto-graded

    Surround a black-box oracle Uf∣x0x1⟩∣y⟩=∣x0x1⟩∣y⊕f(x0x1)⟩U_f\lvert x_0x_1\rangle\lvert y\rangle=\lvert x_0x_1\rangle\lvert y\oplus f(x_0x_1)\rangle with gates from {H,X,Z}\{H,X,Z\} so that a single query decides whether f:{0,1}2→{0,1}f:\{0,1\}^2\to\{0,1\} is constant or balanced. The grader runs your circuit four times, once for each of the oracles f≡0f\equiv0, f≡1f\equiv1, f=x0⊕x1f=x_0\oplus x_1 and f=x0f=x_0, and inspects the measurement distribution of the two input qubits.

    Open lab