Hadamard Test
Prerequisites: Bell State (entanglement, controlled gates, measurement)
The Idea
Imagine you are given a black box that performs some quantum operation U. You cannot open the box to see its wiring diagram, and you cannot clone a quantum state to test it multiple times. Yet you need to know how much U changes a state you care about.
The Hadamard test solves this by converting the question "what does U do?" into an interference experiment on a single ancilla qubit. The ancilla acts like a probe: it picks up a phase that depends on U, and when you interfere the two paths back together, the measurement statistics encode the answer.
The result is the complex number <psi|U|psi> --- the overlap between your original state and the state after U acts. This single primitive underlies variational quantum algorithms (VQE), quantum chemistry, the SWAP test for state comparison, and quantum kernel methods in machine learning.
How It Works
CODE┌───┐ ┌──────┐ ┌───┐ Ancilla: ┤ H ├──────────┤ ├──────────┤ H ├──M── └───┘ │ CU │ └───┘ │ │ Target: ───── |psi> ───┤ ├───────────────────── └──────┘
Step 1 --- Superpose the ancilla.
Apply a Hadamard gate to qubit 0, creating (|0> + |1>) / sqrt(2). The ancilla now labels two "paths": one where U will fire, and one where it will not.
Step 2 --- Apply controlled-U.
If the ancilla is |1>, apply U to the target register (initially |psi> = |0>). The joint state becomes:
(|0>|psi> + |1> U|psi>) / sqrt(2)
Step 3 --- Interfere. Apply Hadamard to the ancilla again. This recombines the two paths:
|0> (|psi> + U|psi>) / 2 + |1> (|psi> - U|psi>) / 2
Step 4 --- Measure the ancilla.
The probability of outcome 0 encodes the overlap:
P(0) = (1 + Re(<psi|U|psi>)) / 2
Imaginary part. To extract Im(<psi|U|psi>), insert an S-dagger gate on the ancilla before the final Hadamard. This rotates the measurement basis from X to Y:
P(0) = (1 + Im(<psi|U|psi>)) / 2
The Math
Starting from |0>|psi>:
CODE1. H on ancilla: (|0> + |1>)/sqrt(2) x |psi> 2. Controlled-U: (|0>|psi> + |1> U|psi>) / sqrt(2) 3. H on ancilla: |0> (|psi> + U|psi>)/2 + |1> (|psi> - U|psi>)/2 4. P(ancilla = 0): || (|psi> + U|psi>)/2 ||^2 = (1 + Re(<psi|U|psi>)) / 2 P(ancilla = 1): (1 - Re(<psi|U|psi>)) / 2
Therefore:
Re(<psi|U|psi>) = P(0) - P(1)
For the imaginary part, step 3 becomes S-dagger then H, which maps the Y component into the measurement outcome:
Im(<psi|U|psi>) = P(0) - P(1) [with S-dagger inserted]
Extracting Real and Imaginary Parts
To fully reconstruct the complex expectation value <psi|U|psi>, you need two separate experiments:
| Experiment | Circuit modification | Extracts |
|---|---|---|
| Real part (default) | None | Re(<psi|U|psi>) = P(0) - P(1) |
| Imaginary part | Insert S-dagger before final H | Im(<psi|U|psi>) = P(0) - P(1) |
The two values are then combined: <psi|U|psi> = Re + i * Im.
Common Results
For |psi> = |0>, each unitary has a known analytical value:
| Unitary | <0|U|0> | Re | Im | P(0) real circuit | P(0) imag circuit |
|---|---|---|---|---|---|
| Z | 1 | 1 | 0 | 1.000 | 0.500 |
| X | 0 | 0 | 0 | 0.500 | 0.500 |
| H | 1/sqrt(2) | 0.707 | 0 | 0.854 | 0.500 |
| T | 1 | 1 | 0 | 1.000 | 0.500 |
Why these values?
- Z|0> = |0>, so
<0|Z|0> = 1. The ancilla always measures 0. - X|0> = |1>, so
<0|X|0> = 0. The ancilla is perfectly random. - H|0> = |+>, so
<0|H|0> = 1/sqrt(2). Partial overlap gives a bias toward 0. - T|0> = |0> (T only adds phase to |1>), so
<0|T|0> = 1.
Running the Circuit
PYTHONfrom circuit import run_circuit, compute_expectation, verify_hadamard_test # Quick test with U=H counts = run_circuit(unitary="H", shots=4096) expectation = compute_expectation(counts) print(f"Re(<0|H|0>) = {expectation:.4f}") # ~0.707 # Imaginary part of T gate counts = run_circuit(unitary="T", measure_imaginary=True, shots=4096) im_val = compute_expectation(counts) print(f"Im(<0|T|0>) = {im_val:.4f}") # ~0.000 # Full verification suite result = verify_hadamard_test(shots=8192) for check in result["checks"]: status = "PASS" if check["passed"] else "FAIL" print(f"[{status}] {check['name']}: {check['detail']}")
Try It Yourself
-
Different initial states. Modify the circuit to prepare
|psi> = |1>instead of|0>(add an X gate on the target before the controlled-U). What happens to<1|Z|1>? What about<1|X|1>? -
Full complex reconstruction. Run both the real-part and imaginary-part circuits for
U = S(the phase gate,S = diag(1, i)). Verify that<0|S|0> = 1 + 0i. Then try with|psi> = |+>--- what complex number do you get? -
SWAP test connection. Replace the single-qubit controlled-U with a controlled-SWAP acting on two target registers prepared in states
|psi>and|phi>. Show thatP(0) = (1 + |<psi|phi>|^2) / 2. This is the famous SWAP test. -
Shot noise. Run the
U=Htest with 100, 1000, and 10000 shots. Plot the measuredRe(<0|H|0>)for each and observe how the error bars shrink as1/sqrt(shots). At what shot count does the error consistently stay below 0.01?
What's Next
- Quantum Phase Estimation --- The Hadamard test generalises to multi-bit phase estimation by using multiple ancilla qubits.
- GHZ State --- Explore multi-qubit entanglement beyond 2 qubits.
- Quantum Teleportation --- Another protocol that uses entanglement and classical communication.
References
- Aharonov, D., Jones, V., & Landau, Z. (2009). "A polynomial quantum algorithm for approximating the Jones polynomial." Algorithmica 55(3), 395-421.
- Cleve, R., Ekert, A., Macchiavello, C., & Mosca, M. (1998). "Quantum algorithms revisited." Proceedings of the Royal Society A 454, 339-354.
- Nielsen, M.A. & Chuang, I.L. (2010). Quantum Computation and Quantum Information. Cambridge University Press. Section 5.2.1, Exercise 4.34.