|q⟩ Bad Qubits

intermediate · Programming · QAOA & Combinatorial Optimization

Parameterizing Layers

A single QAOA layer is a rough approximation. Stacking pp layers — each with its own pair of angles (γk,βk)(\gamma_k, \beta_k) — gives the optimizer more freedom and generally produces better solutions. This lesson builds the p=2p = 2 circuit from scratch.

QAOA recap: one layer

For a problem graph with edges EE, the cost Hamiltonian is

HC=12(i,j)E(IZiZj).H_C = \frac{1}{2}\sum_{(i,j)\in E}(I - Z_i Z_j).

A single QAOA layer applies two unitaries in sequence:

UC(γ)=eiγHC,UB(β)=eiβHB,U_C(\gamma) = e^{i\gamma H_C}, \qquad U_B(\beta) = e^{-i\beta H_B},

where HB=iXiH_B = \sum_i X_i is the mixing Hamiltonian. Starting from the uniform superposition s=Hn0n|s\rangle = H^{\otimes n}|0\rangle^{\otimes n}, one full layer reads

ψ1=UB(β1)UC(γ1)s.|\psi_1\rangle = U_B(\beta_1)\,U_C(\gamma_1)\,|s\rangle.

Going deeper: pp layers

With pp layers the ansatz becomes

ψp(γ,β)=UB(βp)UC(γp)    UB(β1)UC(γ1)s.|\psi_p(\vec\gamma,\vec\beta)\rangle = U_B(\beta_p)\,U_C(\gamma_p)\;\cdots\;U_B(\beta_1)\,U_C(\gamma_1)\,|s\rangle.

The 2p2p real parameters (γ1,,γp,β1,,βp)(\gamma_1,\ldots,\gamma_p,\beta_1,\ldots,\beta_p) are tuned by a classical optimizer to maximize HC\langle H_C\rangle. As pp\to\infty, the QAOA state can exactly represent the true optimum; finite pp trades circuit depth for solution quality.

Building the circuit gates

For a graph with a single edge (0,1)(0, 1), the ZZ interaction eiγZ0Z1/2e^{-i\gamma Z_0 Z_1/2} is compiled as three gates:

ei(γ/2)Z0Z1=CX01    RZ(γ)1    CX01.e^{-i(\gamma/2) Z_0 Z_1} = \mathrm{CX}_{01}\;\cdot\;R_Z(\gamma)_1\;\cdot\;\mathrm{CX}_{01}.

The factor of 1/21/2 in the Hamiltonian means the angle passed to RZR_Z is γ\gamma (one factor of 2 from the eigenvalue of Z0Z1Z_0 Z_1, one from the exponent convention of RZR_Z).

The mixer eiβXqe^{-i\beta X_q} on each qubit qq is a single rotation gate:

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

The extra factor of 2 arises because RX(θ)=ei(θ/2)XR_X(\theta) = e^{-i(\theta/2)X}.

Structure of the p=2p = 2 circuit

q0: ─H─[cx]─────────[cx]─Rx(2β₁)─[cx]─────────[cx]─Rx(2β₂)─
q1: ─H─[cx]─Rz(γ₁)─[cx]─Rx(2β₁)─[cx]─Rz(γ₂)─[cx]─Rx(2β₂)─

The superposition is prepared once; then each layer alternates the cost and mixer unitaries with independent angles.

Try it

Complete the p=2p = 2 circuit using the fixed angles γ1=0.4\gamma_1 = 0.4, γ2=0.8\gamma_2 = 0.8, β1=0.3\beta_1 = 0.3, β2=0.6\beta_2 = 0.6. The grader compares the full statevector, so every gate placement matters.

Run your code to see the quantum state.

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