|q⟩ Bad Qubits

intermediate · Programming · QAOA & Combinatorial Optimization

Checkpoint: Solve Max-Cut

This checkpoint asks you to assemble a complete QAOA circuit for a new graph — from blank starter code — and verify that the output distribution concentrates on the optimal cuts. No scaffolding is provided: you apply every gate yourself.

The problem graph

Three nodes connected in a line (a path graph P3P_3):

0120 - 1 - 2

Edge set E={(0,1),(1,2)}E = \{(0,1),\,(1,2)\}. Each qubit represents a vertex; 0|0\rangle places a vertex in partition SS and 1|1\rangle places it in Sˉ\bar{S}. An edge contributes 1 to the cut value when its endpoints are in opposite partitions.

Optimal cuts

The maximum cut value for P3P_3 is 2 (both edges are cut). The two bitstrings that achieve this are 010010 (vertex 1 alone in Sˉ\bar{S}) and 101101 (vertices 0 and 2 alone in Sˉ\bar{S}). Every other assignment cuts at most one edge.

QAOA p=1p = 1 recipe

The p=1p = 1 QAOA ansatz is

γ,β=UB(β)UC(γ)H3000,|\gamma, \beta\rangle = U_B(\beta)\,U_C(\gamma)\,H^{\otimes 3}\,|000\rangle,

where

UC(γ)=eiγHC,HC=(i,j)E12(IZiZj),U_C(\gamma) = e^{-i\gamma H_C}, \qquad H_C = \sum_{(i,j)\in E} \tfrac{1}{2}(I - Z_i Z_j),

and the transverse-field mixer is

UB(β)=eiβHB,HB=X0+X1+X2.U_B(\beta) = e^{-i\beta H_B}, \qquad H_B = X_0 + X_1 + X_2.

Compiling the cost unitary

The ZZZZ rotation for one edge (i,j)(i,j) decomposes as

ei(γ/2)ZiZj=CXij    Rz(γ)j    CXij.e^{-i(\gamma/2)Z_i Z_j} = \mathrm{CX}_{ij}\;\cdot\;R_z(\gamma)_j\;\cdot\;\mathrm{CX}_{ij}.

Because HCH_C contains a factor of 12\tfrac{1}{2} and Rz(θ)=ei(θ/2)ZR_z(\theta) = e^{-i(\theta/2)Z}, the angle you pass to RzR_z is 2γ2\gamma.

Apply the CNOT-RZ-CNOT block for edge (0,1)(0,1) and then for edge (1,2)(1,2).

Compiling the mixer

Each term eiβXqe^{-i\beta X_q} is a single rotation:

eiβXq=Rx(2β)q.e^{-i\beta X_q} = R_x(2\beta)_q.

The factor of 22 arises from the convention Rx(θ)=ei(θ/2)XR_x(\theta) = e^{-i(\theta/2)X}.

Decoding the result

After measurement, the most frequent bitstrings tell you the best cut found. Bitstring b0b1b2b_0 b_1 b_2 encodes the partition: vertex qq goes to Sˉ\bar{S} if bq=1b_q = 1. For 010010 the partition is {0,2}{1}\{0,2\} \mid \{1\}; for 101101 it is {1}{0,2}\{1\} \mid \{0,2\} — the same cut by symmetry.

The approximation ratio α\alpha for a single run is Cbest/CmaxC_\text{best} / C_\text{max}. Here Cmax=2C_\text{max} = 2, and if the QAOA output is one of the optimal strings then α=1\alpha = 1.

Try it

Build the complete QAOA circuit for P3P_3 with the fixed angles γ=3π/4\gamma = 3\pi/4, β=π/8\beta = \pi/8. The grader checks the output distribution, so an empty or incomplete circuit will fail — the angles must actually shift probability onto 010|010\rangle and 101|101\rangle.

Run your code to see the quantum state.

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