Loading...
Loading...
Estimating real and imaginary parts of expectation values
The Hadamard Test provides a method to estimate the expectation value \langle \psi | \hat{U} | \psi \rangle of a unitary operator \hat{U} with respect to a quantum state |\psi\rangle by introducing a single ancilla qubit and performing Hadamard-controlled operations. By measuring the ancilla in different bases, one can extract both the real and imaginary parts of complex expectation values. This test is foundational for quantum phase estimation, quantum chemistry simulations, and variational algorithms requiring expectation value estimation.
The Hadamard test circuit requires only one additional ancilla qubit beyond the register that prepares the state |\psi\rangle. The protocol begins by initializing the ancilla in |0\rangle and applying a Hadamard gate to create the superposition (|0\rangle + |1\rangle)/\sqrt{2}. A controlled-unitary operation is then applied with the ancilla as control and the unitary \hat{U} acting on the state register. This entangles the ancilla with the register such that the relative phase of \hat{U} is encoded in the ancilla's coherence.
After the controlled-U operation, a second Hadamard gate is applied to the ancilla, effectively converting the phase information into a population difference measurable in the computational basis. The probability of measuring the ancilla in |0\rangle is related to the real part of the expectation value, while the probability of |1\rangle provides complementary information. The simplicity of this circuit, requiring only one control operation per expectation value, makes it highly efficient for near-term quantum hardware.
The controlled-unitary operation is the most resource-intensive component of the circuit. For arbitrary unitaries, it may decompose into many two-qubit gates. However, in many practical settings, such as variational quantum eigensolvers, \hat{U} is a product of Pauli rotations, and the controlled version can be implemented with minimal overhead using known decomposition rules. The test can also be generalized to estimate expectation values of non-unitary operators through block-encoding techniques.
Ancilla Superposition
Post-Controlled-U State
Real Part Probability
To obtain the complete complex expectation value \langle \psi | \hat{U} | \psi \rangle, the Hadamard test must be performed in two configurations. The standard configuration, with a final Hadamard gate on the ancilla, yields the real part through the measurement statistics. To access the imaginary part, one inserts an S^\dagger gate (phase gate with -\pi/2 rotation) before the final Hadamard. This modifies the interference pattern such that the measurement probabilities encode the imaginary component instead.
The Hadamard test is central to the quantum power method and many quantum chemistry algorithms where expectation values of Pauli strings must be estimated. In variational quantum eigensolvers, the Hamiltonian is decomposed into a weighted sum of Pauli operators, and the energy is computed by estimating each expectation value via the Hadamard test or its variants. While direct measurement in the Pauli basis is often preferred for VQEs due to its avoidance of controlled operations, the Hadamard test remains essential for estimating overlaps and non-Pauli observables.
Statistically, the Hadamard test is an unbiased estimator of the target expectation value, but its variance can be large when the expectation value is close to zero. This occurs frequently in molecular Hamiltonians where positive and negative contributions nearly cancel. Variance reduction techniques, such as classical shadow tomography and importance sampling, have been developed to mitigate this issue and reduce the overall measurement cost of expectation value estimation.
Imaginary Part
Expectation Value
Variance
Runnable implementations you can copy and experiment with.
Implement the Hadamard test to estimate the real part of a controlled-Z expectation value using Qiskit.
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
n = 2
anc = QuantumRegister(1, "anc")
state = QuantumRegister(n, "q")
creg = ClassicalRegister(1, "c")
circuit = QuantumCircuit(anc, state, creg)
# Prepare state |psi>
circuit.h(state)
# Ancilla in |+>
circuit.h(anc[0])
# Controlled-U (CZ as example unitary)
circuit.cz(anc[0], state[0])
# Final Hadamard
circuit.h(anc[0])
circuit.measure(anc[0], creg[0])
print(circuit.draw())Use PennyLane to estimate the real part of an expectation value via the Hadamard test protocol.
import pennylane as qml
import numpy as np
n = 2
dev = qml.device("default.qubit", wires=n + 1)
@qml.qnode(dev)
def hadamard_test():
# Prepare |psi>
qml.Hadamard(1)
qml.Hadamard(2)
# Ancilla in |+>
qml.Hadamard(0)
# Controlled-Z
qml.CZ(wires=[0, 1])
# Final Hadamard
qml.Hadamard(0)
return qml.probs(wires=0)
probs = hadamard_test()
real_part = 2 * probs[0] - 1
print(f"Re<psi|U|psi> = {real_part}")