关于 alterlab-qutip
QuTiP 量子工具箱技能面向需要用 Python 模拟和分析量子系统的研究者,涵盖封闭与开放系统动力学及多种专门方法。
主要功能
- 01量子态与算符构造
- 02封闭和开放系统求解
- 03稳态与关联函数分析
- 04纠缠和相空间计算
- 05量子态与动力学可视化
- 06Floquet 和 HEOM 方法
适用场景
- 01求解 Lindblad 主方程与薛定谔动力学
- 02模拟退相干和耗散系统
- 03计算纠缠、谱与关联函数
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ alterlab-ieu/ alterlab-academic-skills --skill alterlab-qutip安装后在 Claude Code 中输入/alterlab-qutip确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
evals/evals.jsonreferences/advanced.mdanalysis.mdcore_concepts.mdtime_evolution.mdvisualization.md详细介绍
QuTiP:Python 中的量子工具箱
概述
QuTiP 提供全面的工具,用于模拟和分析量子力学系统。它支持封闭(幺正)和开放(耗散)量子系统,并提供针对不同场景优化的多种求解器。
何时使用此技能
在用户希望进行以下操作时使用此技能:
- 求解 Lindblad 主方程、量子轨迹或 Schrödinger 动力学(
mesolve、mcsolve、sesolve) - 对退相干、耗散、腔 QED 或受驱动/周期性系统进行建模(Floquet、Bloch-Redfield、HEOM)
- 计算稳态、关联函数、谱、纠缠或相空间分布
不适用的场景
| 场景 | 改用 |
|---|---|
| IBM 硬件上的基于门的电路、转译、Runtime 原语 | alterlab-qiskit |
| Cirq 电路、Google 硬件/Quantum Virtual Machine、XEB/RB | alterlab-cirq |
| 使用自动微分训练变分电路(量子机器学习、可微 VQE) | alterlab-pennylane |
| 不涉及数值计算的算符或对易子符号推导 | alterlab-sympy |
安装
uv pip install qutip
此技能面向 QuTiP 5.x(已针对 5.3.1 验证,该版本截至 2026-09 为最新版本;Python ≥ 3.11)。v5 相较 v4 的主要 API 变化:求解器选项为普通 dict(旧的 Options/solver.Options 类已移除);随机求解器使用 heterodyne=True/False,而非 noise 代码;progress_bar 接受字符串('text'/'tqdm'/''),而非 True;除非传入 options={"map": "parallel"},否则 mcsolve 会串行运行轨迹;独立的 floquet_modes() 系列已弃用,建议改用 FloquetBasis。
只有安装 runtime-compilation 附加组件后,字符串格式的时间依赖项('cos(w*t)')才会通过 Cython 编译;否则 QuTiP 会发出警告并回退到速度较慢的 eval:
uv pip install "qutip[runtime-compilation]" # Cython + setuptools + filelock
用于扩展功能的可选软件包:
# Quantum information processing (circuits, gates)
uv pip install qutip-qip
# Quantum optimal control (GRAPE/CRAB pulse optimization)
uv pip install qutip-qtrl
快速开始
from qutip import *
import numpy as np
import matplotlib.pyplot as plt
# Create quantum state
psi = basis(2, 0) # |0⟩ state
# Create operator
H = sigmaz() # Hamiltonian
# Time evolution
tlist = np.linspace(0, 10, 100)
result = sesolve(H, psi, tlist, e_ops=[sigmaz()])
# Plot results
plt.plot(tlist, result.expect[0])
plt.xlabel('Time')
plt.ylabel('⟨σz⟩')
plt.show()
核心功能
1. 量子对象与状态
创建和操作量子状态与算符:
# States
psi = basis(N, n) # Fock state |n⟩
psi = coherent(N, alpha) # Coherent state |α⟩
rho = thermal_dm(N, n_avg) # Thermal density matrix
# Operators
a = destroy(N) # Annihilation operator
H = num(N) # Number operator
sx, sy, sz = sigmax(), sigmay(), sigmaz() # Pauli matrices
# Composite systems
psi_AB = tensor(psi_A, psi_B) # Tensor product
参见 references/core_concepts.md,了解量子对象、状态、算符和张量积的完整介绍。
2. 时间演化与动力学
适用于不同场景的多种求解器:
# Closed systems (unitary evolution)
result = sesolve(H, psi0, tlist, e_ops=[num(N)])
# Open systems (dissipation)
c_ops = [np.sqrt(0.1) * destroy(N)] # Collapse operators
result = mesolve(H, psi0, tlist, c_ops, e_ops=[num(N)])
# Quantum trajectories (Monte Carlo)
result = mcsolve(H, psi0, tlist, c_ops, ntraj=500, e_ops=[num(N)])
求解器选择指南:
sesolve:纯态、幺正演化mesolve:混合态、耗散、一般开放系统mcsolve:量子跳跃、光子计数、单条轨迹brmesolve:弱系统-热浴耦合fmmesolve:时间周期哈密顿量(Floquet)
参见 references/time_evolution.md,了解求解器的详细文档、含时哈密顿量和高级选项。
3. 分析与测量
计算物理量:
# Expectation values
n_avg = expect(num(N), psi)
# Entropy measures
S = entropy_vn(rho) # Von Neumann entropy
C = concurrence(rho) # Entanglement (two qubits)
# Fidelity and distance
F = fidelity(psi1, psi2)
D = tracedist(rho1, rho2)
# Correlation functions
corr = correlation_2op_1t(H, rho0, taulist, c_ops, A, B)
w, S = spectrum_correlation_fft(taulist, corr)
# Steady states
rho_ss = steadystate(H, c_ops)
参见 references/analysis.md,了解熵、保真度、测量、关联函数和稳态计算。
4. 可视化
可视化量子状态与动力学:
# Bloch sphere
b = Bloch()
b.add_states(psi)
b.show()
# Wigner function (phase space)
xvec = np.linspace(-5, 5, 200)
W = wigner(psi, xvec, xvec)
plt.contourf(xvec, xvec, W, 100, cmap='RdBu')
# Fock distribution
plot_fock_distribution(psi)
# Matrix visualization
hinton(rho) # Hinton diagram
matrix_histogram(H.full()) # 3D bars
参见 references/visualization.md,了解 Bloch 球动画、Wigner 函数、Q 函数和矩阵可视化。
5. 高级方法
适用于复杂场景的专用技术:
# Floquet theory (periodic Hamiltonians); floquet_modes() is deprecated in v5
T = 2 * np.pi / w_drive
fbasis = FloquetBasis(H, T, args=args)
f_energies = fbasis.e_quasi # quasi-energies
f_modes = fbasis.mode(0) # Floquet modes at t = 0
result = fmmesolve(H, psi0, tlist, c_ops, T=T, args=args)
# HEOM (non-Markovian, strong coupling)
from qutip.solver.heom import HEOMSolver, BosonicBath
# Bath correlation as a sum of exponentials: ck_real, vk_real, ck_imag, vk_imag
bath = BosonicBath(Q, ck_real, vk_real, ck_imag, vk_imag)
hsolver = HEOMSolver(H_sys, [bath], max_depth=5)
result = hsolver.run(rho0, tlist)
# Permutational invariance (identical particles)
from qutip.piqs.piqs import dicke, jspin # not exported at qutip.piqs top level
psi = dicke(N, j, m) # Dicke states
Jz = jspin(N, 'z') # Collective operators
参见 references/advanced.md,了解 Floquet 理论、HEOM、置换不变性、随机求解器、超算符和性能优化。
常见工作流
模拟阻尼谐振子
# System parameters
N = 20 # Hilbert space dimension
omega = 1.0 # Oscillator frequency
kappa = 0.1 # Decay rate
# Hamiltonian and collapse operators
H = omega * num(N)
c_ops = [np.sqrt(kappa) * destroy(N)]
# Initial state
psi0 = coherent(N, 3.0)
# Time evolution
tlist = np.linspace(0, 50, 200)
result = mesolve(H, psi0, tlist, c_ops, e_ops=[num(N)])
# Visualize
plt.plot(tlist, result.expect[0])
plt.xlabel('Time')
plt.ylabel('⟨n⟩')
plt.title('Photon Number Decay')
plt.show()
双量子比特纠缠动力学
# Create Bell state
psi0 = bell_state('00')
# Local dephasing on each qubit
gamma = 0.1
c_ops = [
np.sqrt(gamma) * tensor(sigmaz(), qeye(2)),
np.sqrt(gamma) * tensor(qeye(2), sigmaz())
]
# Track entanglement
tlist = np.linspace(0, 10, 100)
result = mesolve(qeye([2, 2]), psi0, tlist, c_ops)
# With collapse operators, result.states are density matrices (type 'oper'),
# so pass them straight to concurrence() — do NOT call .proj() on them.
C_t = [concurrence(state) for state in result.states]
plt.plot(tlist, C_t)
plt.xlabel('Time')
plt.ylabel('Concurrence')
plt.title('Entanglement Decay')
plt.show()
Jaynes-Cummings 模型
# System parameters
N = 10 # Cavity Fock space
wc = 1.0 # Cavity frequency
wa = 1.0 # Atom frequency
g = 0.05 # Coupling strength
# Operators
a = tensor(destroy(N), qeye(2)) # Cavity
sm = tensor(qeye(N), sigmam()) # Atom
# Hamiltonian (RWA)
H = wc * a.dag() * a + wa * sm.dag() * sm + g * (a.dag() * sm + a * sm.dag())
# Initial state: cavity in coherent state, atom in ground state
psi0 = tensor(coherent(N, 2), basis(2, 0))
# Dissipation
kappa = 0.1 # Cavity decay
gamma = 0.05 # Atomic decay
c_ops = [np.sqrt(kappa) * a, np.sqrt(gamma) * sm]
# Observables
n_cav = a.dag() * a
n_atom = sm.dag() * sm
# Evolve
tlist = np.linspace(0, 50, 200)
result = mesolve(H, psi0, tlist, c_ops, e_ops=[n_cav, n_atom])
# Plot
fig, axes = plt.subplots(2, 1, figsize=(8, 6), sharex=True)
axes[0].plot(tlist, result.expect[0])
axes[0].set_ylabel('⟨n_cavity⟩')
axes[1].plot(tlist, result.expect[1])
axes[1].set_ylabel('⟨n_atom⟩')
axes[1].set_xlabel('Time')
plt.tight_layout()
plt.show()
高效模拟技巧
- 截断希尔伯特空间:使用能够捕捉动力学的最小维度
- 选择合适的求解器:纯态使用
sesolve比使用mesolve更快 - 含时项:字符串格式(例如,
'cos(w*t)')编译后速度最快(需要qutip[runtime-compilation]) - 仅存储所需数据:使用
e_ops,而不是存储所有状态 - 调整容差:通过
options字典(atol/rtol/nsteps)平衡准确性与计算时间 - 并行轨迹:v5 中
mcsolve默认以串行方式运行——传入options={"map": "parallel", "num_cpus": 4}(或"loky"/"mpi") - 检查收敛性:改变
ntraj、希尔伯特空间大小和容差
故障排除
内存问题:降低希尔伯特空间维度,使用 store_final_state 选项,或考虑使用 Krylov 方法
模拟缓慢:使用基于字符串的含时表达式,稍微放宽容差,或对刚性问题尝试 method='bdf'
数值不稳定:减小时间步长(nsteps 选项),放宽容差,或检查哈密顿量/算符的定义是否正确
导入错误:确保 QuTiP 已正确安装;量子门需要 qutip-qip 包
参考资料
此技能包含详细的参考文档:
references/core_concepts.md:量子对象、状态、算符、张量积、复合系统references/time_evolution.md:所有求解器(sesolve、mesolve、mcsolve、brmesolve 等)、含时哈密顿量、求解器选项references/visualization.md:布洛赫球、Wigner 函数、Q 函数、Fock 分布、矩阵图references/analysis.md:期望值、熵、保真度、纠缠度量、关联函数、稳态references/advanced.md:Floquet 理论、HEOM、置换不变性、随机方法、超算符、性能技巧
外部资源
- 文档:https://qutip.readthedocs.io/
- 教程:https://qutip.org/qutip-tutorials/
- API 参考:https://qutip.readthedocs.io/en/stable/apidoc/apidoc.html
- GitHub:https://github.com/qutip/qutip
AlterLab Academic Skills 套件的一部分。