Quantum Computing Master Course — 158. QAOA
Lesson 158: QAOA
Smartyfy Quantum Computing Master Course
Objectives and prerequisites
Translate a small MaxCut objective into a diagonal operator, construct one QAOA layer, and distinguish expected reward from individual sampled solutions. Prerequisites are Pauli rotations, tensor products, expectation values, and classical optimization. QAOA stands for quantum approximate optimization algorithm. This lesson uses an ideal two-qubit example that is easy to solve classically and does not demonstrate a computational advantage.
Specify the cost and sign convention
A cut assigns each vertex of a graph a bit. An edge contributes one when its endpoint bits differ and zero when they agree. For one unweighted edge between qubits 0 and 1, define C = (I − Z0Z1)/2. In basis |q1q0⟩ ordered 00, 01, 10, 11, its diagonal entries are 0, 1, 1, 0. We maximize ⟨C⟩. A minimization optimizer should instead receive −⟨C⟩.
The computational basis contains candidate assignments, not numerical estimates of their quality. The cost operator assigns a real reward to each assignment. In a graph with several edges, sum the corresponding edge operators, with explicit weights if the problem is weighted. Since these operators are diagonal, all their terms commute. Negative weights and constrained variants need separately stated objective and feasibility conventions.
| Assignment q1q0 | Z0Z1 eigenvalue | Cut reward |
|---|---|---|
| 00 | +1 | 0 |
| 01 | −1 | 1 |
| 10 | −1 | 1 |
| 11 | +1 | 0 |
Build the alternating circuit
Start with |+⟩ on each qubit, producing equal amplitude 1/2 for all four assignments. Define the mixer B = X0 + X1. One layer prepares |ψ(γ,β)⟩ = exp(−iβB) exp(−iγC)|++⟩. Operators act from right to left: apply the cost phase before the mixer. The real angles γ and β are variational parameters, not probabilities.
The cost unitary leaves amplitudes of 00 and 11 unchanged and multiplies those of 01 and 10 by exp(−iγ). This changes relative phases but leaves computational-basis probabilities unchanged at that stage. The mixer creates interference that can turn those phase differences into different output probabilities.
Because X0 and X1 commute, exp(−iβB) = Rx0(2β) Rx1(2β). Our rotation convention is Rx(θ) = exp(−iθX/2). Likewise, exp(−iγC) equals exp(−iγ/2) exp(+iγZ0Z1/2), so it is Rzz(−γ) up to a global phase, with Rzz(θ) = exp(−iθZ0Z1/2). The minus sign is important. Another source may define the cost phase with the opposite sign and consequently quote different optimal angles.
Worked exact one-edge expectation
After the cost phase the state is [1, exp(−iγ), exp(−iγ), 1]/2. Write c = cosβ and s = sinβ. Applying the two mixer rotations gives the amplitude of 01 as [exp(−iγ) cos(2β) − i sin(2β)]/2. The 10 amplitude is identical by symmetry. Squaring the magnitude and adding both cut outcomes gives ⟨C⟩ = [1 + sin(4β) sinγ]/2.
At β = 0 the value is 1/2 regardless of γ: cost phases alone do not change the sampled reward. At γ = 0 the value is also 1/2 because the initial plus state is a mixer eigenstate. At γ = π/2 and β = π/8, the value reaches one. The final state has equal probabilities 1/2 on 01 and 10, and zero on 00 and 11.
An expectation of one here means every ideal sample is a maximum cut, but it does not select a unique bit string. The two optimal assignments differ by flipping both bits and describe the same bipartition with swapped labels. On a larger graph, an improved average reward need not imply that most samples are globally optimal. Report both the objective statistics and the actual quality of the returned assignments.
Depth, optimization, and realistic comparisons
For p layers, alternate p cost unitaries and p mixers with their own angle pairs. In the ideal variational family, the best attainable objective cannot decrease when a new layer can be set to identity. This expressibility statement assumes exact optimization over the larger family. A practical optimizer with a finite budget may find worse parameters, and extra operations on a noisy device may degrade performance.
For a maximization problem with a positive optimum Cmax, an expected approximation ratio can be defined as ⟨C⟩/Cmax. This ratio depends on knowing or bounding the optimum. Do not automatically transfer a theorem for one graph family, depth, or angle selection to a different experiment. Include strong classical heuristics and the cost of tuning parameters in a meaningful comparison.
Feasibility is another design issue. The simple X mixer can leave a constrained feasible subspace. Problems with fixed Hamming weight may use different initial states and mixers that preserve that constraint. Penalty terms are another possibility, but their scale affects optimization and does not magically enforce feasibility at every intermediate step.
Executed local state-vector check
Save Lesson 141’s first code block as local_statevector141.py beside this script. The following standard-library calculation uses that local simulator. It checks sixteen angle pairs against the analytic expectation and verifies the optimum distribution. It runs on a classical computer, uses no SDK or external service, and models ideal unitary dynamics without physical-device noise.
import math, cmath
from local_statevector141 import apply_one, probabilities
def edge_state(gamma,beta):
v=[.5+0j]*4
v=[a*cmath.exp(-1j*gamma*(i.bit_count()%2)) for i,a in enumerate(v)]
rx=[[math.cos(beta),-1j*math.sin(beta)],[-1j*math.sin(beta),math.cos(beta)]]
return apply_one(apply_one(v,rx,0),rx,1)
for gamma in (0,.2,math.pi/2,2.4):
for beta in (0,.1,math.pi/8,.7):
p=probabilities(edge_state(gamma,beta))
value=p[1]+p[2]
expected=(1+math.sin(4*beta)*math.sin(gamma))/2
assert abs(value-expected)<1e-12
p=probabilities(edge_state(math.pi/2,math.pi/8))
assert abs(p[1]-.5)<1e-12 and abs(p[2]-.5)<1e-12
print('16 exact-state comparisons passed; optimum probabilities:',p)
Four worked exercises
-
Evaluate the expected reward at γ = π/2 and β = π/16.
Solution. Since sin(4β) = sin(π/4) = 1/√2, the expectation is (1 + 1/√2)/2 ≈ 0.853553. It is a probability-weighted average of rewards zero and one.
-
What probabilities appear immediately after the cost phase for any γ?
Solution. Every assignment still has probability 1/4. Only relative phases have changed; the following mixer is needed to convert them into different computational-basis probabilities.
-
Translate the mixer into single-qubit rotation angles for β = π/8.
Solution. Apply Rx(π/4) to each qubit. Using Rx(π/8) would miss the factor of two from the definition exp(−iθX/2).
-
If 800 of 1000 sampled assignments cut this single edge, estimate the reward and its binomial standard error.
Solution. The sample reward is 0.8. The plug-in standard error is √[0.8 × 0.2/1000] ≈ 0.012649. This sampling uncertainty is separate from parameter optimization error and any physical bias.
Five explained multiple-choice questions
-
Which cost operator rewards different endpoint bits? A. (I + Z0Z1)/2. B. Z0 + Z1. C. X0X1. D. (I − Z0Z1)/2.
Answer: D. Opposite Z eigenvalues have product −1, giving reward one in the stated operator.
-
What changes directly under the diagonal cost unitary? A. Relative phases between cost sectors. B. The magnitudes of every amplitude. C. The sampled reward distribution before applying the mixer. D. The normalization of the state.
Answer: A. The factors are unit-modulus phases; probability changes arise after interference with the mixer.
-
Which rotation realizes exp(−iβX)? A. Rx(β/2). B. Rx(2β). C. Rz(2β). D. Rx(−2β) for every β.
Answer: B. The defined rotation contains a factor one half in its exponent.
-
Why is best attainable ideal reward nondecreasing with layer count? A. Every optimizer iteration finds the global optimum. B. Noise decreases when gates are added. C. A new layer can be set to identity. D. Each new layer strictly improves the objective even when its angles are zero.
Answer: C. The larger variational family contains the shallower family, but finite optimization and device performance need not improve.
-
What distribution occurs at the declared one-edge optimum? A. All probability on 00. B. Equal probability on all four assignments. C. All probability on 01 alone. D. Equal probabilities on 01 and 10.
Answer: D. The two complementary optimal assignments each have probability one half in this symmetric ideal circuit.
Pitfalls and summary
Keep maximization, cost-phase signs, rotation factors, and bit ordering explicit. QAOA alternates phase encoding with mixing; it does not read every assignment at once. Separate representational capability, optimizer performance, sampling quality, and total resource cost.
Primary sources and further study
These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.