A reproducible proof of concept that solves the cardinality-constrained mean-variance portfolio problem with the Quantum Approximate Optimisation Algorithm (QAOA), and compares two ways of encoding the budget constraint:
- Standard QAOA - the budget is enforced with a quadratic penalty and the
circuit uses the usual transverse-field (
X) mixer over the full Hilbert space. - Constraint-preserving QAOA - the circuit starts from a Dicke state and
uses a ring
XYmixer, so the search never leaves the budget-feasible subspace and no penalty is needed.
The headline finding, on a six-asset instance, is that how the constraint is
encoded matters more than circuit depth: the penalty version learns to be
feasible but not optimal, while the XY version stays feasible by
construction and genuinely optimises within the feasible set, concentrating up
to 35% of the measurement probability on the exact optimum (~ 7x the uniform
baseline).
The study goes further than a single instance. The effect is confirmed across
20 random instances (with error bars), it persists up to 14 assets, and
its cost is measured honestly: with generic state synthesis the XY
circuit's two-qubit gate count explodes to over 16,000 at 14 assets; the
project implements the structured Dicke preparation of Bartschi and
Eidenbenz (verified to fidelity one), which restores a polynomial budget of
860 gates at the same size, still several times the penalty circuit.
Under a depolarising noise model the XY feasibility guarantee and
accuracy edge both vanish around a 1% two-qubit error rate, and a
simulated-annealing baseline solves every instance in a median of 11 to
156 objective evaluations. In short: the encoding wins, it is not free, and
classically these problems are easy.
No quantum advantage is claimed. The instance is small and classically trivial; the value of the project is a careful, honest, fully reproducible account of the method, of why the constraint encoding is decisive, and of what it costs.
This project was developed within the Mediterranean Quantitative Finance Society (MQFS).
quantum-portfolio-mqfs/
├── src/
│ ├── data.py # single-factor synthetic market; forecast mu, estimated Sigma
│ ├── realdata.py # Kenneth French 10 Industry Portfolios (daily), cached fetch
│ ├── rmt.py # Marchenko-Pastur cleaning of Sigma (Laloux et al. 1999)
│ ├── qubo.py # mean-variance QUBO, exact QUBO<->Ising mapping, brute force
│ ├── qaoa.py # both ansaetze (X / XY), fast statevector evaluator,
│ │ # SparsePauliOp/Estimator cross-check, COBYLA, Aer sampling
│ └── plotting.py # all publication figures
├── experiments/
│ ├── run_experiment.py # headline instance -> results/ (numbers + figures)
│ ├── robustness.py # 20-instance aggregate, circuit-cost & penalty-sensitivity
│ ├── scaling.py # n = 6..14: quality, dimensions, generic vs structured cost
│ ├── noise.py # depolarising + readout sweep; feasibility/accuracy decay
│ ├── realdata.py # REAL data: French industries, raw vs RMT-cleaned Sigma
│ └── baseline.py # classical simulated-annealing reference
├── tests/
│ ├── test_core.py # mapping, evaluator equivalence, feasibility, ...
│ └── test_realdata_rmt.py # French parser + Marchenko-Pastur known answers
├── results/ # generated: 5 *.json + eight figures (.pdf)
├── paper/ # MQFS working paper: LaTeX source + compiled paper.pdf
├── requirements.txt
├── pyproject.toml
├── .github/workflows/tests.yml # CI: pytest on every push
├── LICENSE # MIT
└── README.md
pip install -r requirements.txt
python experiments/run_experiment.pyThis regenerates results/results.json and all five core figures in well under a
minute on a laptop CPU. Everything is seeded, so the numbers are deterministic.
For the referee-grade controls (20-instance aggregate with error bars, circuit-cost comparison, penalty sensitivity, scaling, and noise), run:
python experiments/robustness.py # ~4 min: 20-instance aggregate + cost + sensitivity
python experiments/scaling.py # ~2 min: n = 6..14, quality + dimensions + gate counts
python experiments/noise.py # ~1 min: depolarising + readout error sweep
python experiments/baseline.py # seconds: classical simulated-annealing referenceRun the test suite with:
pip install pytest
pytest -qTo rebuild the paper (needs a LaTeX distribution with pdflatex and bibtex):
cd paper
pdflatex paper.tex && bibtex paper && pdflatex paper.tex && pdflatex paper.texFor a binary holding vector x, expected returns mu and covariance Sigma,
we minimise q xTSigmax - muTx subject to holding exactly B assets. The problem is
written as a QUBO and mapped exactly to an Ising Hamiltonian (the mapping is
verified to machine precision over all 2^n states). QAOA alternates the cost
unitary exp(-igammaH_C) with a mixer; we compare the transverse-field mixer (with a
budget penalty) against a Hamming-weight-preserving XY mixer (with a Dicke
initial state). Angles are trained with COBYLA and multiple restarts. Expected
energies are computed by an exact, vectorised statevector simulator that matches
Qiskit's full circuit simulation to ~1e-15; the Qiskit circuits are used for the
figures and for shot-based sampling on the Aer backend.
A deliberate modelling choice: expected returns mu are treated as an
exogenous forecast while the covariance Sigma is estimated from the
simulated history - standard practice, since sample means of returns are far too
noisy to use directly.
On a representative instance (chosen for a well-separated optimum):
| metric (best depth) | standard (penalty) | constraint-preserving (XY) |
|---|---|---|
| feasible-conditional ratio | 0.48 | 0.68 |
| P(optimum) | 0.06 | 0.35 |
| P(feasible) | <= 0.996 | 1.000 (exact) |
Across 20 random instances the effect is consistent: the penalty method's
conditional ratio stays at the random level (~ 0.50) at every depth, while the
XY method rises from 0.58 to 0.65 and its P(optimum) reaches ~ 2-3x the uniform
baseline. It persists to 14 assets (XY ratio 0.58 -> 0.70 vs penalty ~ 0.51 ->
0.55). The cost picture: at 14 assets the XY circuit needs ~16,600 two-qubit
gates with generic state synthesis but only 860 with the structured Dicke
preparation implemented here (verified to fidelity one), against ~180 for the
penalty circuit; under noise the XY feasibility guarantee and accuracy edge
both vanish around a 1% two-qubit error rate (paper, Sec. 5.5-5.6). A classical
simulated annealer reaches the exact optimum on every run, in a median of 11
(n=6) to 156 (n=14) objective evaluations. Brute-force optimum: {A02, A03, A06}; shot-based sampling (8192 shots) recovers it as the most frequent
feasible outcome for both methods.
The natural referee question — does any of this survive an empirical
covariance? — is answered in experiments/realdata.py on the standard
academic dataset for portfolio experiments, Kenneth French's 10 Industry
Portfolios (daily value-weighted returns, trailing 10 years, budget 4):
- the encoding effect is stronger on real data: the penalty method stays
at the random conditional ratio (~0.50) at every depth, while the
XYansatz climbs 0.68 → 0.76, with P(optimum) up to 13x the uniform-over-feasible baseline; - Marchenko–Pastur cleaning of the covariance (Laloux et al. 1999) finds
exactly one signal eigenvalue (the market mode), removes 34.5% of
the spectral weight as estimation noise, improves the conditioning of
Sigmafrom 79 to 17 — and, independently of anything quantum, changes the optimal basket: the unfiltered covariance selects a different 4-asset portfolio than the cleaned one. Without RMT, part of what the optimiser "solves" is estimation noise (Markowitz error-maximisation, made concrete); - the QAOA picture is unchanged on the cleaned matrix — the encoding effect
is a property of the constraint geometry, not of the noise in
Sigma.
python experiments/realdata.py # ~2 min: downloads + caches the French data,
# runs raw vs RMT-cleaned, writes results/realdata.json
python experiments/network_viz.py # seconds: interactive 3D correlation network
# (raw vs RMT morph) -> results/correlation_network.htmlBoth optimised p=3 circuits ran on ibm_marrakesh (156-qubit IBM Heron r2,
8192 shots, job d93djcooamcc73dc3kr0; median calibrated 2-qubit error 0.31%):
| p=3, 8192 shots | noiseless | depol. model | measured |
|---|---|---|---|
| penalty — P(feasible) | 0.993 | 0.871 | 0.577 |
| XY — P(feasible) | 1.000 | 0.707 | 0.306 |
| penalty — P(optimum) | 0.052 | 0.049 | 0.028 |
| XY — P(optimum) | 0.245 | 0.143 | 0.024 |
Two lessons, both quantitative. (1) Transpilation is part of the cost: routing
to the heavy-hex topology inflates the 2-qubit count to 167 (penalty) and 346
(XY), and a first-order decay law with the transpiled counts,
P(feasible) ≈ exp(-m·eps), lands within a few points of the measurement
(0.60 vs 0.577; 0.35 vs 0.306) — the depolarising sweep was optimistic only
because it was fed logical counts. (2) The noise sweep's crossover forecast is
confirmed on silicon: at an accumulated error budget m·eps ≈ 1.07 the XY
accuracy edge is erased (0.024 vs 0.028, both near the uniform floor). On 2026
hardware at n=6 the shallow penalty circuit wins — the constraint-preserving
advantage is a low-error-regime claim, and the run quantifies the regime:
m·eps ≲ 0.5. Reproduce with python experiments/hardware.py
(credentials via QiskitRuntimeService.save_account, never in this repo).
This optimiser is intended as the quantum component of a larger
econophysics-flavoured pipeline: larger asset universes (where the feasible
subspace becomes a real saving for the XY ansatz), heavy-tailed return
models, error mitigation on the hardware runs (dynamical decoupling, twirling
— the measured m·eps ≈ 1.07 budget is exactly what mitigation attacks), and
better angle-initialisation strategies.
Nicolo Angileri - Mediterranean Quantitative Finance Society (MQFS).
If you use this code, please cite the accompanying paper (paper/paper.pdf).
- Every result and figure in the paper is produced by running the code in
this repository; the numbers are not hand-written. Run the scripts in
experiments/to regenerateresults/from scratch (the seeds are fixed).
This project was built with AI-assisted engineering. The algorithms (QAOA, Dicke/XY mixers, RMT), the hardware analysis, and the validation were designed, reviewed, and are fully understood and owned by the author — the AI served as an implementation partner, not a substitute for judgement.