# Demo 2:配分函数 > 第 13 章 拓扑、统计物理与采样 · [本章演示](index.md) · [代码演示总索引](../index.md) **可视化**:单子图(4 柱) —— Z = 1/P(ground) 估 Z(β) **对应教程**:[第 13 章 拓扑、统计物理与采样 中相关小节](https://tutorial.chenzhaoyun.com/quantum-tutorial/ch13-topology-statistical-physics/) · 依赖安装:`pip install "unified-quantum[visualization,simulation]"` ## 实现要点 H = diag(0, 1, 2, 3) on 2 qubits. 经典 Z(β) = Σ e^{-β E_j}. 量子 "Gibbs sampling" trick: 用 uniqc.rotation_prepare 装 |ψ_β> ∝ Σ e^{-β E_j/2}|j> 到 2 个 qubit. 由于 |00> 是 H 的基态 (E_0=0), P(|00>) = e^{-β·0} / Z = 1/Z, 因此 Z = 1 / P(|00>) 可从 shots 直接读出. 对比经典 Z(β), 并验证 Boltzmann 分布 p_j ∝ e^{-β E_j}. 扫描 β ∈ [0, 3]. ## 完整源码 ```{literalinclude} 02_partition_function.py :language: python :linenos: :caption: demos/ch13-topology-statistical-physics/02_partition_function.py ``` ## 运行结果 ![Demo 2:配分函数](02_partition_function.png) ## 运行方式 ```bash # 仓库根目录执行;输出 PNG 到 demos/ch13-topology-statistical-physics/02_partition_function.png,关键统计到 stdout .venv/bin/python3 demos/ch13-topology-statistical-physics/02_partition_function.py ``` --- [← 本章演示](index.md) · [↑ 代码演示总索引](../index.md) · [⌂ 教程门户](https://tutorial.chenzhaoyun.com/)