# Demo 2:凸优化 > 第 14 章 优化与数值量子算法 · [本章演示](index.md) · [代码演示总索引](../index.md) **可视化**:2 子图 —— FD 经典 vs Jordan 量子梯度 **对应教程**:[第 14 章 优化与数值量子算法 中相关小节](https://tutorial.chenzhaoyun.com/quantum-tutorial/ch14-optimization-numerics/) · 依赖安装:`pip install "unified-quantum[visualization,simulation]"` ## 实现要点 核心:2D convex quadratic f(x, y) = 0.5 (x-2)^2 + 0.5 (y+1)^2, gradient g(x, y) = (x-2, y+1). The convex-optimization chapter contrasts two oracle models: classical finite-difference gradient = 2d function-value queries per step Jordan's quantum gradient (QPE + iQFT) = 1 coherent phase query per step Here d=2 → 4× query gap. Both loops run gradient descent with the same starting point and step size; the difference is the per-step query counter. The quantum loop builds the Jordan-style phase oracle explicitly so its OriginIR is visible — the analytical gradient is used for the actual descent step (the *full* Jordan oracle recovers it exactly given a sufficiently wide grid). ## 完整源码 ```{literalinclude} 02_convex_optimization.py :language: python :linenos: :caption: demos/ch14-optimization-numerics/02_convex_optimization.py ``` ## 运行结果 ![Demo 2:凸优化](02_convex_optimization.png) ## 运行方式 ```bash # 仓库根目录执行;输出 PNG 到 demos/ch14-optimization-numerics/02_convex_optimization.png,关键统计到 stdout .venv/bin/python3 demos/ch14-optimization-numerics/02_convex_optimization.py ``` --- [← 本章演示](index.md) · [↑ 代码演示总索引](../index.md) · [⌂ 教程门户](https://tutorial.chenzhaoyun.com/)