QAOA for Portfolio Optimization
What You'll Learn:
- How to encode financial portfolio selection as a QUBO problem on a quantum circuit
- How the Markowitz mean-variance model maps to cost and mixer Hamiltonians
- How the budget constraint becomes a quadratic penalty in the QAOA cost operator
- How the risk factor parameter controls the return-vs-risk tradeoff
Level: Intermediate | Time: 30 minutes | Qubits: 4 | Framework: Qiskit
Prerequisites
- QAOA MaxCut — QAOA fundamentals (cost/mixer operators, parameter optimization)
- Bell State — entanglement basics
The Idea
You have four investment assets — a bond fund, a stock index, tech stocks, and crypto. You want to pick exactly two to put in your portfolio, maximizing returns while minimizing risk. Every extra asset adds return but also adds correlated risk (when tech crashes, crypto often follows). This is the Markowitz portfolio selection problem, and it's NP-hard for the binary (in/out) variant.
QAOA tackles this by encoding each asset as a qubit: |1> means "include", |0> means "exclude". The cost Hamiltonian balances three competing objectives — maximize returns, minimize risk, and satisfy the budget constraint — while the mixer explores different asset combinations. After measuring, the most frequently sampled bitstrings correspond to near-optimal portfolios.
The connection to classical finance is direct: this is the same mean-variance optimization that Markowitz introduced in 1952, but encoded as a quantum circuit. The budget constraint (pick exactly B assets) becomes a quadratic penalty term, turning the constrained problem into an unconstrained QUBO.
How It Works
The Problem
Given 4 assets with returns and a covariance matrix, select exactly 2 to maximize risk-adjusted return:
CODEAsset 0 (Bond Fund): return = 5%, variance = 0.01 (low risk) Asset 1 (Stock Index): return = 8%, variance = 0.02 (medium risk) Asset 2 (Tech Stocks): return = 12%, variance = 0.04 (high risk) Asset 3 (Crypto): return = 15%, variance = 0.06 (very high risk) Budget: 2 assets Risk factor (q): 0.5
QUBO Encoding
The objective function to minimize:
CODECost = -Σᵢ rᵢxᵢ + q × Σᵢⱼ σᵢⱼxᵢxⱼ + A × (Σᵢ xᵢ - B)² ───────── ────────────────── ────────────────── return risk (covariance) budget penalty
where xᵢ ∈ {0, 1} indicates asset selection, q is the risk factor, A is the penalty strength, and B is the target budget.
The budget penalty (Σᵢ xᵢ - B)² is zero when exactly B assets are selected, and grows quadratically for any violation. This is the key insight: constraints become penalty terms in the QUBO.
The QAOA Circuit
CODE┌───┐┌──────────┐┌──────────┐┌────────────┐┌─────────┐ q_0: ┤ H ├┤ RZ(-2γr₀)├┤ RZZ(risk)├┤ RZ/RZZ(pen)├┤ RX(2β) ├── measure ├───┤├──────────┤├──────────┤├────────────┤├─────────┤ q_1: ┤ H ├┤ RZ(-2γr₁)├┤ RZZ(risk)├┤ RZ/RZZ(pen)├┤ RX(2β) ├── measure ├───┤├──────────┤├──────────┤├────────────┤├─────────┤ q_2: ┤ H ├┤ RZ(-2γr₂)├┤ RZZ(risk)├┤ RZ/RZZ(pen)├┤ RX(2β) ├── measure ├───┤├──────────┤├──────────┤├────────────┤├─────────┤ q_3: ┤ H ├┤ RZ(-2γr₃)├┤ RZZ(risk)├┤ RZ/RZZ(pen)├┤ RX(2β) ├── measure └───┘└──────────┘└──────────┘└────────────┘└─────────┘ ├─ Return ─┤├── Risk ──┤├── Budget ──┤├─ Mixer ─┤
Step 1: Uniform Superposition
All qubits start in |+> — equal probability over all 2^4 = 16 possible portfolios (from selecting 0 assets to selecting all 4).
Step 2: Cost Operator (Three Terms)
Return term: RZ(-2γ rᵢ) on each qubit i. Larger returns get stronger rotations, biasing the state toward including high-return assets.
Risk term: RZZ(2γ q σᵢⱼ) on each pair (i,j). Penalizes selecting correlated assets together. When both assets are included, the ZZ interaction adds a phase proportional to their covariance.
Budget penalty: RZ and RZZ terms from expanding (Σxᵢ - B)². This is the critical term that was missing in naive implementations — without it, QAOA has no incentive to select exactly B assets.
Step 3: Mixer Operator
Apply RX(2β) on each qubit. This "mixes" between include/exclude for each asset, allowing the state to explore neighboring portfolios.
Step 4: Measure and Optimize
Measure all qubits to get a portfolio, evaluate its risk-adjusted return, repeat. Classically optimize gamma and beta to maximize the expected objective.
PYTHONfrom circuit import run_circuit, optimize_qaoa # Default parameters result = run_circuit() print(f"Best portfolio: assets {result['best_portfolio']['assets']}") print(f"Approximation ratio: {result['approximation_ratio']:.1%}") # Optimize parameters opt = optimize_qaoa(p=1, max_iterations=50, seed=42) print(f"Optimized ratio: {opt['approximation_ratio']:.1%}")
The Math
Markowitz Mean-Variance Model
The classical portfolio optimization problem (Markowitz, 1952):
CODEMaximize: μᵀx - q × xᵀΣx Subject to: 1ᵀx = B, x ∈ {0,1}ⁿ
where μ is the return vector, Σ is the covariance matrix, q is risk aversion, and B is the budget.
QUBO Formulation
Converting the constraint to a penalty:
Minimize: -Σᵢ μᵢxᵢ + q × Σᵢⱼ σᵢⱼxᵢxⱼ + A × (Σᵢ xᵢ - B)²
Z-Encoding
Map binary variables to qubit operators: xᵢ = (1 - Zᵢ)/2
The budget penalty expands under Z-encoding:
(Σ(1-Zᵢ)/2 - B)² = (n/2 - B)² - (n/2 - B)Σ Zᵢ + (1/4)Σᵢ Zᵢ² + (1/2)Σᵢ<ⱼ ZᵢZⱼ
This yields:
- Constant (global phase, drops out)
- Linear terms → RZ gates with coefficient (B - n/2)
- Quadratic terms → RZZ gates with coefficient 1/4
Cost Hamiltonian
CODEC = -Σᵢ rᵢ Zᵢ + q Σᵢ<ⱼ σᵢⱼ ZᵢZⱼ + A[(B - n/2) Σᵢ Zᵢ + (1/4) Σᵢ<ⱼ ZᵢZⱼ] ─────────── ───────────────── ────────────────────────────────────────── returns risk budget penalty
Mixer Hamiltonian
B = Σᵢ Xᵢ
The transverse field mixer drives transitions between portfolios, enabling exploration.
Risk-Return Tradeoff
| Risk Factor (q) | Strategy | Behavior |
|---|---|---|
| 0.0 | Pure return | Selects highest-return assets regardless of correlation |
| 0.5 | Balanced | Trade off between return and diversification |
| 1.0 | Risk-averse | Prefers uncorrelated low-variance assets |
| 2.0+ | Very conservative | Heavily penalizes any variance |
Expected Output
| Metric | Value |
|---|---|
| Number of assets | 4 |
| Budget | 2 |
| Optimal portfolio (q=0.5) | Assets {1, 2} or similar |
| QAOA p=1 (default params) | Finds budget-feasible portfolios |
| QAOA p=1 (optimized) | Near-optimal risk-adjusted return |
| Random baseline | Many budget-violating portfolios |
Running the Circuit
PYTHONfrom circuit import ( run_circuit, optimize_qaoa, verify_qaoa, compute_exact_solution, evaluate_portfolio, ) import numpy as np # Exact solution exact = compute_exact_solution() print(f"Optimal: {exact['optimal_objective']:.4f}") print(f"Solutions: {exact['optimal_bitstrings']}") # QAOA with defaults result = run_circuit(shots=2048) print(f"Expected objective: {result['expected_objective']:.4f}") print(f"Ratio: {result['approximation_ratio']:.1%}") # Optimize opt = optimize_qaoa(p=1, shots=2048, seed=42) print(f"Optimized ratio: {opt['approximation_ratio']:.1%}") # Custom assets (3 assets, budget=1) custom_returns = np.array([0.03, 0.10, 0.20]) custom_cov = np.array([ [0.005, 0.001, 0.002], [0.001, 0.03, 0.01], [0.002, 0.01, 0.08], ]) result = run_circuit( returns=custom_returns, covariance=custom_cov, budget=1, risk_factor=0.3, gamma=[0.5], beta=[0.8], ) # Verification v = verify_qaoa() for check in v["checks"]: print(f"[{'PASS' if check['passed'] else 'FAIL'}] {check['name']}")
Try It Yourself
-
Vary the risk factor: Run with
risk_factor=0.0(pure return) vsrisk_factor=2.0(risk-averse). How does the optimal portfolio change? At what risk factor does the algorithm switch from high-return to low-variance assets? -
Change the budget: Try
budget=1(pick one asset) andbudget=3(pick three). How does the penalty term affect which portfolios are sampled most frequently? -
Increase p: Run with p=2 (
optimize_qaoa(p=2)). Does the approximation ratio improve? How many more iterations does the optimizer need? -
Add correlated assets: Create a covariance matrix where assets 2 and 3 are highly correlated (σ₂₃ = 0.03). Does QAOA learn to avoid selecting both?
-
Compare with MaxCut: The MaxCut QAOA uses only RZZ gates in the cost operator. This circuit adds RZ gates (returns) and budget penalty terms. How does the additional structure affect the optimization landscape?
What's Next
- TSP — QAOA for the Traveling Salesman Problem (more complex constraints)
- VQE H2 Ground State — Another variational algorithm, for quantum chemistry
- QAOA MaxCut — If you haven't already, compare the simpler MaxCut encoding
Applications
| Domain | Use case |
|---|---|
| Asset management | Select stocks/bonds to maximize risk-adjusted return |
| Index tracking | Replicate benchmark performance with fewer assets |
| Credit risk | Optimize loan portfolio diversification |
| Supply chain | Select suppliers balancing cost and reliability |
| Drug discovery | Select compound combinations (analogous structure) |
References
- Markowitz, H. (1952). "Portfolio Selection." The Journal of Finance 7(1), 77-91. DOI: 10.2307/2975974
- Brandhofer, S. et al. (2023). "Benchmarking the performance of portfolio optimization with QAOA." Quantum Science and Technology 8(2), 024002. DOI: 10.1088/2058-9565/acb4b2
- Hodson, M. et al. (2019). "Portfolio rebalancing experiments using the Quantum Alternating Operator Ansatz." arXiv: 1911.05296