# Demo 3:QAOA 投资组合 > 第 14 章 优化与数值量子算法 · [本章演示](index.md) · [代码演示总索引](../index.md) **可视化**:双图 —— 3 资产 Markowitz QUBO **对应教程**:[第 14 章 优化与数值量子算法 中相关小节](https://tutorial.chenzhaoyun.com/quantum-tutorial/ch14-optimization-numerics/) · 依赖安装:`pip install "unified-quantum[visualization,simulation]"` ## 实现要点 核心:Toy Markowitz portfolio over 3 assets A, B, C with expected returns μ = (0.10, 0.15, 0.08), covariance Σ, risk aversion λ = 4.0, and a cardinality penalty that biases toward 2-asset portfolios. The quadratic utility is encoded as a QUBO H_C (negated because uniqc's qaoa_ansatz MAXIMIZES ⟨H_C⟩) and solved by p=1 QAOA. We sweep (γ, β), overlay a classical hill-climb trajectory, then histogram the top bitstrings at the optimum — the dominant sampling peaks correspond to the asset subsets QAOA suggests holding. ## 完整源码 ```{literalinclude} 03_qaoa_portfolio.py :language: python :linenos: :caption: demos/ch14-optimization-numerics/03_qaoa_portfolio.py ``` ## 运行结果 ![Demo 3:QAOA 投资组合](03_qaoa_portfolio.png) ## 运行方式 ```bash # 仓库根目录执行;输出 PNG 到 demos/ch14-optimization-numerics/03_qaoa_portfolio.png,关键统计到 stdout .venv/bin/python3 demos/ch14-optimization-numerics/03_qaoa_portfolio.py ``` --- [← 本章演示](index.md) · [↑ 代码演示总索引](../index.md) · [⌂ 教程门户](https://tutorial.chenzhaoyun.com/)