Same problem as the 3D version: five assets → five qubits → a 5-bit portfolio z ∈ {0,1}⁵, one of 32 basis states. The score to maximize is a mean-variance objective with a soft budget penalty:
O(z) = Σ zᵢrᵢ − λ Σᵢⱼ zᵢzⱼ Covᵢⱼ − P·(Σzᵢ − 3)²
H_C = diag(−O(z)) (cost Hamiltonian)
H_M = Σᵢ Xᵢ (mixer Hamiltonian)
|ψ(θ)⟩ = Π_{l=1..p} e^(−iβ_l H_M) e^(−iγ_l H_C) |+⟩⊗5
This 2D version adds the piece the 3D one leaves to you: a real classical optimization loop. Every "Optimize Step" is one full Hooke–Jeeves coordinate/pattern search sweep over the 2p angles θ=(γ₁,β₁,…,γ_p,β_p) — for each angle it tries a small step in both directions on the exact 32-dim statevector, keeps whichever change raises the expectation value ⟨O⟩ = Σ P(z)·O(z), and shrinks the step size once a full sweep finds no improvement. When a sweep stalls it jitters θ to a fresh random point (a restart) so the search can escape a local optimum, while always keeping the best θ ever found. This is exactly the outer loop real QAOA hardware runs: a classical optimizer (Nelder–Mead, SPSA, COBYLA, gradient descent via the parameter-shift rule…) proposing angles, a quantum circuit evaluating ⟨O⟩ for them, repeated until convergence.
- Bar chart — the exact Born-rule probability |⟨z|ψ(θ)⟩|² of every one of the 32 portfolios under the best θ found so far.
- Convergence plot — ⟨O⟩ of the best θ found after each sweep, climbing toward the dashed brute-force optimum O* (computed by direct enumeration of all 32 classical portfolios, shown only as a reference — the optimizer never sees it).
- Because this is a heuristic classical optimizer over a non-convex landscape, it usually reaches O* exactly but can also settle for a near-optimal θ, especially at low p — the same behavior real QAOA hardware shows.