Skip to content
Quantum ComputingAN INTERACTIVE OPEN TEXTBOOK
charithj.com.au

The Hadamard test

Turn a complex overlap into a measurable bias on one auxiliary qubit.

Undergraduate18 min lessonDr. Charith Jayasekara↓ Chapter PDF
By the end, you’ll be able to
  • Derive the real and imaginary expectation-value tests.
  • Translate an ancilla probability into a signed expectation.
  • State the controlled-operation and preparation requirements.

Before you begin: Inner products, complex numbers, controlled gates and measurement.

Many algorithms need a number describing how a state changes under an operation. If a circuit implements a unitary UU and we can prepare ∣ψ⟩|\psi\rangle, that number is

z=⟨ψ∣U∣ψ⟩.z=\langle\psi|U|\psi\rangle.

It can be complex. A measurement gives a classical outcome, so we need a circuit that converts a component of zz into an outcome probability. The Hadamard test does this with one auxiliary qubit, called the ancilla, and a controlled implementation of UU.

The idea resembles comparing two routes through an interferometer. One route leaves the target unchanged; the other applies UU. Recombining them exposes their overlap. The ancilla labels the routes, while the target may contain many qubits.

Prepare the ancilla in ∣0⟩|0\rangle and the target in ∣ψ⟩|\psi\rangle. Apply HH to the ancilla, controlled-UU from the ancilla to the target, and a second HH to the ancilla. Just before the second HH, the joint state is

∣0⟩∣ψ⟩+∣1⟩U∣ψ⟩2.\frac{|0\rangle|\psi\rangle+|1\rangle U|\psi\rangle}{\sqrt2}.

After it, collect the two possible ancilla outcomes:

12[∣0⟩(I+U)∣ψ⟩+∣1⟩(I−U)∣ψ⟩].\frac12\left[|0\rangle(I+U)|\psi\rangle+|1\rangle(I-U)|\psi\rangle\right].

Taking squared norms gives

P(0)=1+Re⁡z2,P(1)=1−Re⁡z2.P(0)=\frac{1+\operatorname{Re}z}{2},\qquad P(1)=\frac{1-\operatorname{Re}z}{2}.

Assign the number +1+1 to an ancilla outcome 0 and −1-1 to outcome 1. Its average estimates Re⁡z\operatorname{Re}z. This connects the algebra to recorded experimental data: the circuit produces samples, and a classical calculation turns their frequency into an estimate.

Checkpoint. If zero appears in 750 of 1,000 shots, what is the estimated real part? 2(750/1000)−1=0.52(750/1000)-1=0.5. The frequency 0.75 and the estimated overlap component 0.5 are different quantities.

Insert S†=diag⁡(1,−i)S^\dagger=\operatorname{diag}(1,-i) on the ancilla immediately before its final HH. With this specific convention,

P(0)=1+Im⁡z2.P(0)=\frac{1+\operatorname{Im}z}{2}.

Using SS instead reverses the sign. This is a useful implementation test: choose a state and unitary with a known positive imaginary overlap and check which ancilla outcome is favoured.

Hadamard test: measure an overlap componentAncilla H, controlled U on target, optional S dagger on ancilla, H then measurement. Below, real-part and imaginary-part zero probabilities vary as one half of one plus cosine phi and one half of one plus sine phi. Hadamard test: measure an overlap component ancilla |0⟩ target |ψ⟩ H U S† imaginary part only H Example: target |1⟩, U = diag(1, eⁱᵠ) 0 0.5 1 0 π/2 π 3π/2 2π Real-part test: P(0) = (1 + cos φ)/2 With S†: P(0) = (1 + sin φ)/2
Hadamard-test circuit with real and imaginary overlap components plotted across a phase sweep.

In the plotted example, the target is ∣1⟩|1\rangle and U=diag⁡(1,eiϕ)U=\operatorname{diag}(1,e^{i\phi}). The overlap is eiϕe^{i\phi}, so the blue and teal curves trace its real and imaginary components after conversion to probabilities.

Choose ∣ψ⟩=∣+⟩|\psi\rangle=|+\rangle and U=SU=S. Applying SS changes the target to (∣0⟩+i∣1⟩)/2(|0\rangle+i|1\rangle)/\sqrt2, so

z=⟨+∣S∣+⟩=1+i2.z=\langle+|S|+\rangle=\frac{1+i}{2}.

The real-part circuit has P(0)=3/4P(0)=3/4. The imaginary-part circuit with S†S^\dagger also has P(0)=3/4P(0)=3/4. Two ensembles of shots estimate the two components separately. Simply measuring S∣+⟩S|+\rangle in the Z basis gives 50/50 and does not reveal either component in this way.

For 1,000 real-part shots with exactly 750 zeros, the estimate is 0.5. A simple estimated standard error is

2p^(1−p^)N≈0.0274.2\sqrt{\frac{\hat p(1-\hat p)}{N}}\approx0.0274.

Actual records fluctuate. Increasing the shot count by four roughly halves this sampling uncertainty. A wrong controlled gate can instead cause a systematic bias that more shots will measure more precisely.

Sweep ϕ\phi freely through 0,π/2,π,3π/20,\pi/2,\pi,3\pi/2 using the figure’s phase-gate example. The real-part zero probabilities are 1,1/2,0,1/21,1/2,0,1/2; the imaginary-part values are 1/2,1,1/2,01/2,1,1/2,0. Keep the same prediction choice available while changing ϕ\phi. Then add simulated readout flips and compare the measured curve with the ideal one.

The controlled-UU implementation is an important assumption. A black-box operation does not automatically supply its controlled version cheaply. Even an overall phase matters when controlling an operation: replacing UU by eiχUe^{i\chi}U changes the relative phase between the ancilla branches. State preparation, controlled-gate depth and repeated shots all belong in the cost.

  1. Let U=ZU=Z and ∣ψ⟩=3∣0⟩/2+∣1⟩/2|\psi\rangle=\sqrt3|0\rangle/2+|1\rangle/2. Find zz and P(0)P(0).

  2. If U∣ψ⟩=−∣ψ⟩U|\psi\rangle=-|\psi\rangle, predict the real-part test.

Worked answers
  1. z=3/4−1/4=1/2z=3/4-1/4=1/2, so P(0)=3/4P(0)=3/4.

  2. z=−1z=-1, so the ancilla gives 1 with certainty in the ideal model.

PAUSE & THINK

The real-part test returns P(0)=0.6. What does that imply?

Choose your answer

The Hadamard test is a bridge from complex amplitudes to measurable statistics. Its derivation identifies exactly which component is measured and which implementation resources are assumed.

Aharonov, Jones and Landau, section 2.2 and applications. The two-branch derivation, phase sweep, uncertainty calculation and exercises above are original instructional calculations.

ONE IDEA FURTHER

Ready for the next step?

Your progress stays in this browser.

View progress & resume a chapter →