|q⟩ Bad Qubits

intermediate · Programming · Variational Quantum Eigensolver (VQE)

Measuring Energy Expectation

The Variational Quantum Eigensolver (VQE) finds the ground-state energy of a molecule by minimizing ψ(θ)Hψ(θ)\langle \psi(\theta) | H | \psi(\theta) \rangle over circuit parameters θ\theta. On real quantum hardware you cannot read off the full state vector; instead you must measure each Pauli term separately and add up the contributions.

Decomposing the Hamiltonian

Any Hermitian operator on nn qubits can be written as a weighted sum of Pauli tensor-products:

H=kckPk,Pk{I,X,Y,Z}n.H = \sum_k c_k \, P_k, \qquad P_k \in \{I, X, Y, Z\}^{\otimes n}.

By linearity,

H=kckPk.\langle H \rangle = \sum_k c_k \langle P_k \rangle.

This means you only need to measure Pk\langle P_k \rangle for each Pauli string and then form the weighted sum classically.

Measuring a single Pauli term

Z terms are the easiest: measure in the computational basis and compute

Z=P(0)P(1).\langle Z \rangle = P(0) - P(1).

For a qubit in state ψ=α0+β1|\psi\rangle = \alpha|0\rangle + \beta|1\rangle this gives α2β2|\alpha|^2 - |\beta|^2, which is exactly the diagonal matrix element ψZψ\langle \psi | Z | \psi \rangle.

X and Y terms require a basis rotation before measurement. To measure X\langle X \rangle apply a Hadamard HH to rotate +0|+\rangle \to |0\rangle and 1|-\rangle \to |1\rangle, then measure in the ZZ basis. To measure Y\langle Y \rangle apply SHS^\dagger H first.

Multi-qubit Pauli strings

For a product operator like Z0Z1Z_0 \otimes Z_1 (often written ZZZZ) the expectation value factors only when the state is a product state. In general,

Z0Z1=P(00)P(01)P(10)+P(11),\langle Z_0 Z_1 \rangle = P(00) - P(01) - P(10) + P(11),

where P(xy)P(xy) is the probability of measuring the bit string xyxy. The sign rule follows from the fact that Z0=+0Z|0\rangle = +|0\rangle and Z1=1Z|1\rangle = -|1\rangle: the contribution of a bit string is the product of the signs of each individual bit under its Pauli.

Putting it together in VQE

A single VQE energy estimate proceeds as follows:

  1. Prepare ψ(θ)|\psi(\theta)\rangle with the parametrized ansatz circuit.
  2. For each Pauli term PkP_k: rotate the state to the measurement basis for PkP_k, measure, collect shot statistics, compute Pk\langle P_k \rangle.
  3. Return E(θ)=kckPkE(\theta) = \sum_k c_k \langle P_k \rangle to the classical optimizer.
  4. The optimizer proposes a new θ\theta and the loop repeats until convergence.

On a simulator the expectation values can be computed exactly from the statevector; on hardware you average over many shots, so statistical noise limits precision.

Try it

For the product state +0|{+}\rangle \otimes |0\rangle (qubit 0 in +|{+}\rangle, qubit 1 in 0|0\rangle), what is the expectation value Z1\langle Z_1 \rangle of the Pauli-Z operator acting on qubit 1? Compute the exact value and return it.

Run your code to see the quantum state.

Sign in on the full site to ask questions and join the discussion.