# Demo 1:Shor 离散对数 > 第 9 章 高等代数与数论量子算法 · [本章演示](index.md) · [代码演示总索引](../index.md) **可视化**:双图(轨道+直方图) —— N=15 离散对数骨架 + QPE 相位 **对应教程**:[第 9 章 高等代数与数论量子算法 中相关小节](https://tutorial.chenzhaoyun.com/quantum-tutorial/ch09-algebra-number-theory/) · 依赖安装:`pip install "unified-quantum[visualization,simulation]"` ## 实现要点 模型: 给定 N=15 (模合数), a=2, 寻找 x 使 g = a^x mod N. 乘法群 Z_15^* 的阶 phi(15)=8; a=2 在其中的阶为 4, 因为 2^1 = 2, 2^2 = 4, 2^3 = 8, 2^4 = 16 mod 15 = 1. 因此 x 仅在模 4 意义下唯一. Shor 算法的核心是把离散对数 归约到乘法算子 U_a |y> = |a*y mod N> 的周期查找: |QPE 估相位 r / 2^n> -> r = 4 -> x 在轨道 [1, 2, 4, 8] 中的位置. 量子部分用 1 比特 S 门做"简化路径" eigenstate (类似 ch04 demo 1): |1> 是 S 的本征态, 特征值 i = e^{2*pi*i/4}, 用 4-qubit 精度寄存器 跑 QPE 应集中在 phase = 1/4 (即 k=4 of 16). 该相位对应 r = 4. 然后对每个目标 g in {1, 2, 4, 8}, 由 r 与轨道位置反推 x. ## 完整源码 ```{literalinclude} 01_discrete_log.py :language: python :linenos: :caption: demos/ch09-algebra-number-theory/01_discrete_log.py ``` ## 运行结果 ![Demo 1:Shor 离散对数](01_discrete_log.png) ## 运行方式 ```bash # 仓库根目录执行;输出 PNG 到 demos/ch09-algebra-number-theory/01_discrete_log.png,关键统计到 stdout .venv/bin/python3 demos/ch09-algebra-number-theory/01_discrete_log.py ``` --- [← 本章演示](index.md) · [↑ 代码演示总索引](../index.md) · [⌂ 教程门户](https://tutorial.chenzhaoyun.com/)