Category: simulation | Difficulty: intermediate | Qubits: 2 | Gates: 4 | Depth: 4
The Hadamard test estimates the real part of the expectation value ⟨ψ|U|ψ⟩ using one ancilla qubit. The ancilla is placed in superposition, a controlled-U is applied, and a final Hadamard on the ancilla transforms the relative phase into a probability difference. For U=Z and |ψ⟩=|+⟩, Re(⟨+|Z|+⟩) = 0, so both outcomes are equally likely. The imaginary part uses an S† gate.
P(0) = P(1) = 0.5 (Re(⟨+|Z|+⟩) = 0)
The OpenQASM 2.0 circuit is in circuit.qasm.
OPENQASM 2.0;
include "qelib1.inc";
// Hadamard test: estimate Re(<psi|U|psi>) for U=Z, |psi>=|+>
// q[0]: ancilla, q[1]: system qubit |psi>
qreg q[2];
creg c[1];
// Prepare |psi> = |+>
h q[1];
// Hadamard test circuit
h q[0];
cz q[0],q[1];
h q[0];
// Measure ancilla: P(0) = (1 + Re(<psi|U|psi>)) / 2 = 0.5
measure q[0] -> c[0];
hadamard-test expectation-value quantum-simulation ancilla
MIT — part of the OpenQC Algorithm Catalog.