|q⟩ Bad Qubits

intermediate · Programming · QAOA & Combinatorial Optimization

Cost Hamiltonians

In the Quantum Approximate Optimization Algorithm (QAOA), finding a good solution to a combinatorial problem requires encoding that problem's objective function as a Hamiltonian. The circuit then applies this Hamiltonian as a phase gate, so that basis states representing cheaper solutions accumulate more phase — which interference amplifies into higher probability. This lesson shows exactly how that encoding works.

From cost function to Hamiltonian

Consider the MaxCut problem: given a graph, partition its vertices into two sets so that the number of edges crossing the partition is as large as possible. Each vertex vv is assigned a binary variable zv∈{0,1}z_v \in \{0, 1\}.

An edge (u,v)(u, v) contributes to the cut when zu≠zvz_u \neq z_v. The contribution of a single edge can be written as

Cuv(z)=1−(−1)zu+zv2=1−(−1)zu(−1)zv2.C_{uv}(z) = \frac{1 - (-1)^{z_u + z_v}}{2} = \frac{1 - (-1)^{z_u}(-1)^{z_v}}{2}.

The Pauli ZZ operator has eigenvalues +1+1 on ∣0⟩|0\rangle and −1-1 on ∣1⟩|1\rangle, so (−1)zv(-1)^{z_v} is exactly the eigenvalue of ZvZ_v. Replacing classical bits by Pauli operators gives the quantum operator for one edge:

Huv=12(I−ZuZv).H_{uv} = \frac{1}{2}(I - Z_u Z_v).

The full cost Hamiltonian is the sum over all edges:

HC=∑(u,v)∈EHuv=∑(u,v)∈E12(I−ZuZv).H_C = \sum_{(u,v) \in E} H_{uv} = \sum_{(u,v) \in E} \frac{1}{2}(I - Z_u Z_v).

Because HCH_C is diagonal in the computational basis, its eigenvalues are exactly the integer cut values of each bitstring. A bitstring that cuts kk edges is an eigenstate of HCH_C with eigenvalue kk.

The phase-encoding unitary

QAOA does not apply HCH_C as a measurement; it applies it as a time evolution with variational angle γ\gamma:

UC(γ)=e−iγHC.U_C(\gamma) = e^{-i\gamma H_C}.

Because each HuvH_{uv} commutes with every other (they share the same diagonal basis), the product factorizes edge by edge:

UC(γ)=∏(u,v)∈Ee−iγHuv=∏(u,v)∈Ee−iγ(I−ZuZv)/2.U_C(\gamma) = \prod_{(u,v) \in E} e^{-i\gamma H_{uv}} = \prod_{(u,v) \in E} e^{-i\gamma(I - Z_u Z_v)/2}.

The global phase from the identity part is unobservable, so only the ZZZZ term matters. For one edge the essential unitary is

eiθZuZv,θ=γ2.e^{i\theta Z_u Z_v}, \quad \theta = \frac{\gamma}{2}.

Circuit for eiθZuZve^{i\theta Z_u Z_v}

To implement eiθZuZve^{i\theta Z_u Z_v} in terms of standard gates, use the identity

ZuZv=CNOTu→v⋅(Iu⊗Zv)⋅CNOTu→v,Z_u Z_v = \text{CNOT}_{u \to v} \cdot (I_u \otimes Z_v) \cdot \text{CNOT}_{u \to v},

which follows from CNOT\text{CNOT} mapping ∣x,y⟩→∣x,x⊕y⟩|x, y\rangle \to |x, x \oplus y\rangle and from how Pauli operators transform under conjugation. Inserting this decomposition yields

eiθZuZv=CNOTu→v⋅Rz(−2θ)v⋅CNOTu→v,e^{i\theta Z_u Z_v} = \text{CNOT}_{u \to v} \cdot R_z(-2\theta)_v \cdot \text{CNOT}_{u \to v},

where Rz(ϕ)=e−iϕZ/2R_z(\phi) = e^{-i\phi Z/2} is the standard rotation. For a single edge with γ=π/4\gamma = \pi/4 the angle is θ=π/8\theta = \pi/8 and the RzR_z argument is −π/4-\pi/4.

The three-gate sequence has depth 3 (two CNOTs and one RzR_z) and applies one edge's cost Hamiltonian exactly, without approximation.

Try it

Complete the circuit that starts in the uniform superposition ∣++⟩|{++}\rangle and applies ei(π/8) Z0Z1e^{i(\pi/8)\,Z_0 Z_1} — the cost unitary for a single-edge MaxCut instance with γ=π/4\gamma = \pi/4. The grader checks the full statevector, so the correct three-gate insertion is required to pass.

Run your code to see the quantum state.

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