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. The goal is for the most frequently sampled bitstrings to be good portfolios. At p=1 this works only in part: the circuit learns the budget (about 74% of samples hold exactly two assets, against 37.5% for random guessing) but barely ranks the six two-asset portfolios, so the optimum {2, 3} comes out about 12% of the time (random guessing: 6.25%).
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(γr₀) ├┤ RZ/RZZ(risk) ├┤ RZZ(budget) ├┤ RX(2β) ├── measure ├───┤├─────────┤├──────────────┤├─────────────┤├────────┤ q_1: ┤ H ├┤ RZ(γr₁) ├┤ RZ/RZZ(risk) ├┤ RZZ(budget) ├┤ RX(2β) ├── measure ├───┤├─────────┤├──────────────┤├─────────────┤├────────┤ q_2: ┤ H ├┤ RZ(γr₂) ├┤ RZ/RZZ(risk) ├┤ RZZ(budget) ├┤ RX(2β) ├── measure ├───┤├─────────┤├──────────────┤├─────────────┤├────────┤ q_3: ┤ H ├┤ RZ(γr₃) ├┤ RZ/RZZ(risk) ├┤ RZZ(budget) ├┤ 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(γ rᵢ) on each qubit i, from -rᵢxᵢ = -rᵢ/2 + (rᵢ/2)Zᵢ. An included asset has Zᵢ = -1, so this lowers the energy of including high-return assets.
Risk term: from q Σᵢⱼ σᵢⱼxᵢxⱼ: RZZ(γ q σᵢⱼ) on each pair (i,j) plus RZ(-γ q Σⱼ σᵢⱼ) on each qubit (the sum includes the variance σᵢᵢ). A ZZ term alone would give "both excluded" the same phase as "both included"; with the single-qubit part, the two together reproduce the risk term exactly: each included asset adds q times its variance, and each pair of included assets adds 2q times their covariance.
Budget penalty: RZZ(γA) on each pair and RZ(2γA(B - n/2)) on each qubit, from expanding A(Σxᵢ - B)² (see The Math). This is the critical term that was missing in naive implementations: without it, QAOA has no incentive to select exactly B assets. For the default B = 2 = n/2 the RZ coefficient is zero, so the default circuit has only the budget RZZ gates. With these weights the lowest-energy portfolio holds exactly B assets for B = 1, 2 and 3 alike (it is the exact optimum in each case).
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/2
Cost Hamiltonian
Substituting xᵢ = (1 - Zᵢ)/2 into the whole QUBO gives (dropping the constant):
C = Σᵢ [ rᵢ/2 - (q/2) Σⱼ σᵢⱼ + A(B - n/2) ] Zᵢ + Σᵢ<ⱼ ( q σᵢⱼ/2 + A/2 ) ZᵢZⱼ
The sum over j in the first term includes j = i, so the variances σᵢᵢ appear there. This is the operator the circuit applies: a term h Zᵢ becomes RZ(2γh) and a term J ZᵢZⱼ becomes RZZ(2γJ). verify_qaoa() checks that the circuit's energies equal the QUBO for all 16 portfolios (up to a constant) and that its lowest-energy portfolio is the optimum {2, 3}.
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 | Moderately risk-averse | For the default assets the optimum is still {2, 3}; it shifts to {1, 2} at q = 1.5 and {0, 2} at q = 2.0 |
| 2.0+ | Very conservative | Heavily penalizes any variance |
Expected Output
| Metric | Value |
|---|---|
| Number of assets | 4 |
| Budget | 2 |
| Optimal portfolio (q=0.5) | Assets {2, 3} (tech and crypto), objective 0.205 |
| QAOA p=1 (default γ = 0.49, β = 2.86) | Exactly 2 assets in ~74% of samples; the optimum {2, 3} in ~12.4%; expected objective -0.14 (approximation ratio about -0.7) |
QAOA p=1 (optimize_qaoa, seed 42) | Expected objective -0.14 to -0.29 (approximation ratio -0.7 to -1.4) in 24 runs: no better than the default angles |
| Random baseline | Exactly 2 assets in 37.5% of samples; the optimum in 6.25%; expected objective -0.84 |
These are noiseless simulator results. The approximation ratio (expected objective divided by the optimum 0.205) is negative because the budget penalty A = 1 is about five times the best portfolio's objective: the roughly 26% of samples that break the budget pull the average below zero. Within the budget, p=1 barely ranks the portfolios. The six two-asset portfolios each come out between 12.1% ({0, 1}) and 12.4% ({2, 3}) of the time, in the same order as their objectives, so in a single Run of 1,024 shots the tallest bar is often another pair: the optimum was the most frequent bitstring in 4 of our 16 runs. The default angles maximize the expected objective on a p=1 grid with γ in [0, π] (-0.1403 at γ = 0.490, β = 2.859); β = 2.86 gives the same probabilities as β = 2.86 - π = -0.28.
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 Information Processing 22, 25. DOI: 10.1007/s11128-022-03766-5
- Hodson, M. et al. (2019). "Portfolio rebalancing experiments using the Quantum Alternating Operator Ansatz." arXiv: 1911.05296