In [1]:
Copied!
# 环境导入
import numpy as np
import torch
import matplotlib.pyplot as plt
from scipy.special import comb
import warnings
warnings.filterwarnings('ignore')
# 导入deepquantum
import deepquantum as dq
import deepquantum.photonic as dqp
# 设置
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
np.random.seed(42)
torch.manual_seed(42)
print("环境导入成功!")
print(f"PyTorch: {torch.__version__}")
print(f"NumPy: {np.__version__}")
# 环境导入
import numpy as np
import torch
import matplotlib.pyplot as plt
from scipy.special import comb
import warnings
warnings.filterwarnings('ignore')
# 导入deepquantum
import deepquantum as dq
import deepquantum.photonic as dqp
# 设置
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
np.random.seed(42)
torch.manual_seed(42)
print("环境导入成功!")
print(f"PyTorch: {torch.__version__}")
print(f"NumPy: {np.__version__}")
环境导入成功! PyTorch: 2.9.1+cpu NumPy: 2.4.0
第1章:光量子计算基础理论¶
1.1 为什么使用光子?¶
光子的优势:
- 室温工作:不需要极低温
- 长相干时间:与环境耦合弱
- 高带宽:光速传播
- 低串扰:光子间相互作用弱
1.2 Fock态定义¶
Fock态 $|n\rangle$ 表示包含 $n$ 个光子 的量子态。
单模Fock态¶
$$ |n\rangle = \frac{(a^\dagger)^n}{\sqrt{n!}}|0\rangle $$
多模Fock态¶
$$ |n_1, n_2, \ldots, n_m\rangle = |n_1\rangle \otimes |n_2\rangle \otimes \cdots \otimes |n_m\rangle $$
例子:
- $|0\rangle$:真空态(0个光子)
- $|1\rangle$:单光子态
- $|1,0\rangle$:模式1有1个光子,模式2有0个
- $|1,1\rangle$:两个模式各有1个光子
1.3 产生与湮灭算符¶
湮灭算符: $$ \hat{a}|n\rangle = \sqrt{n}|n-1\rangle $$
产生算符: $$ \hat{a}^\dagger|n\rangle = \sqrt{n+1}|n+1\rangle $$
对易关系: $$ [\hat{a}, \hat{a}^\dagger] = 1 $$
物理意义:
- $\sqrt{n}$ 反映了玻色子的"聚集"特性
- 导致Hong-Ou-Mandel干涉等现象
In [2]:
Copied!
# 创建Fock态
print("=== 创建Fock态示例 ===")
# 单模态
state_0 = dqp.FockState(state=[0], nmode=1, cutoff=3, basis=True)
state_1 = dqp.FockState(state=[1], nmode=1, cutoff=3, basis=True)
state_2 = dqp.FockState(state=[2], nmode=1, cutoff=3, basis=True)
print(f"真空态 |0>: {state_0.state}")
print(f"单光子态 |1>: {state_1.state}")
print(f"双光子态 |2>: {state_2.state}")
# 多模态
state_10 = dqp.FockState(state=[1, 0], nmode=2, cutoff=3, basis=True)
state_11 = dqp.FockState(state=[1, 1], nmode=2, cutoff=3, basis=True)
print(f"\n态 |1,0>: {state_10.state}")
print(f"态 |1,1>: {state_11.state}")
# 创建Fock态
print("=== 创建Fock态示例 ===")
# 单模态
state_0 = dqp.FockState(state=[0], nmode=1, cutoff=3, basis=True)
state_1 = dqp.FockState(state=[1], nmode=1, cutoff=3, basis=True)
state_2 = dqp.FockState(state=[2], nmode=1, cutoff=3, basis=True)
print(f"真空态 |0>: {state_0.state}")
print(f"单光子态 |1>: {state_1.state}")
print(f"双光子态 |2>: {state_2.state}")
# 多模态
state_10 = dqp.FockState(state=[1, 0], nmode=2, cutoff=3, basis=True)
state_11 = dqp.FockState(state=[1, 1], nmode=2, cutoff=3, basis=True)
print(f"\n态 |1,0>: {state_10.state}")
print(f"态 |1,1>: {state_11.state}")
=== 创建Fock态示例 === 真空态 |0>: tensor([0]) 单光子态 |1>: tensor([1]) 双光子态 |2>: tensor([2]) 态 |1,0>: tensor([1, 0]) 态 |1,1>: tensor([1, 1])
第2章:Fock后端实现原理 ⭐¶
2.1 Fock空间截断¶
理论上Fock空间是无限维的,但计算机需要截断:
$$ |0\rangle, |1\rangle, \ldots, |\text{cutoff}-1\rangle $$
参数 cutoff:控制Fock空间的最大光子数
2.2 希尔伯特空间维度¶
对于 $m$ 个模式、$n$ 个光子的系统:
$$ \dim(\mathcal{H}) = \binom{n+m-1}{n} = \frac{(n+m-1)!}{n!(m-1)!} $$
例子:
- 3模式2光子:$\binom{3}{2} = 3$ 维
- 4模式2光子:$\binom{4}{2} = 6$ 维
- 14模式5光子:$\binom{14}{5} = 2002$ 维
指数增长:这是经典模拟的根本困难!
2.3 状态表示方式¶
basis=True(基态表示)¶
- 直接存储光子数列表:
[1, 0, 1] - 内存效率高
basis=False(叠加态表示)¶
- 存储完整复数张量
- 可以表示任意叠加态
In [6]:
Copied!
# 计算希尔伯特空间维度
def hilbert_dimension(nmode, nphoton):
return comb(nphoton + nmode - 1, nphoton, exact=True)
print("=== 希尔伯特空间维度 ===")
print("2模式2光子:", hilbert_dimension(2, 2))
print("3模式2光子:", hilbert_dimension(3, 2))
print("5模式3光子:", hilbert_dimension(5, 3))
print("10模式5光子:", f"{hilbert_dimension(10, 5):,}")
# 可视化
modes = [2, 3, 4, 5,6, 7, 8, 9, 10]
photons = [1, 2, 3,4,5,6]
plt.figure(figsize=(10, 6))
for n in photons:
dims = [hilbert_dimension(m, n) for m in modes]
plt.plot(modes, dims, 'o-', label=f'{n} photon(s)', linewidth=2)
plt.xlabel('模式数')
plt.ylabel('希尔伯特空间维度')
plt.title('希尔伯特空间维度增长')
plt.yscale('log')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# 计算希尔伯特空间维度
def hilbert_dimension(nmode, nphoton):
return comb(nphoton + nmode - 1, nphoton, exact=True)
print("=== 希尔伯特空间维度 ===")
print("2模式2光子:", hilbert_dimension(2, 2))
print("3模式2光子:", hilbert_dimension(3, 2))
print("5模式3光子:", hilbert_dimension(5, 3))
print("10模式5光子:", f"{hilbert_dimension(10, 5):,}")
# 可视化
modes = [2, 3, 4, 5,6, 7, 8, 9, 10]
photons = [1, 2, 3,4,5,6]
plt.figure(figsize=(10, 6))
for n in photons:
dims = [hilbert_dimension(m, n) for m in modes]
plt.plot(modes, dims, 'o-', label=f'{n} photon(s)', linewidth=2)
plt.xlabel('模式数')
plt.ylabel('希尔伯特空间维度')
plt.title('希尔伯特空间维度增长')
plt.yscale('log')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
In [8]:
Copied!
# 创建光量子电路
cir = dq.QumodeCircuit(
nmode=2,
init_state=[1, 1], # |1,1>
cutoff=3,
basis=True
)
# 添加门操作
cir.ps([0], encode=True) # 相移
cir.bs_theta([0, 1], encode=True) # 分束器
# 获取结果
probs = cir(is_prob=True)
print("输出概率分布:")
for state, prob in probs.items():
print(f" {state}: {prob}")
# 创建光量子电路
cir = dq.QumodeCircuit(
nmode=2,
init_state=[1, 1], # |1,1>
cutoff=3,
basis=True
)
# 添加门操作
cir.ps([0], encode=True) # 相移
cir.bs_theta([0, 1], encode=True) # 分束器
# 获取结果
probs = cir(is_prob=True)
print("输出概率分布:")
for state, prob in probs.items():
print(f" {state}: {prob}")
输出概率分布: |11>: tensor([0.7605]) |02>: tensor([0.1197]) |20>: tensor([0.1197])
In [13]:
Copied!
# HOM干涉模拟
cir_hom = dq.QumodeCircuit(nmode=2, init_state=[1, 1], cutoff=3, basis=True)
# 使用 bs_theta 方法,inputs 参数指定 theta 值
# 50:50 分束器对应 theta = pi/4
cir_hom.bs_theta([0, 1], inputs=np.pi/4)
probs_hom = cir_hom(is_prob=True)
print("HOM干涉结果:")
for state, prob in probs_hom.items():
print(f"{state} 概率: {prob}")
# 可视化
states = list(probs_hom.keys())
values = [float(p) for p in probs_hom.values()]
# FockState对象可以直接转为字符串
states_str = [str(s).replace('|', '').replace('>', '') for s in states]
plt.figure(figsize=(8, 5))
plt.bar(states_str, values)
plt.xlabel('输出态')
plt.ylabel('概率')
plt.title('Hong-Ou-Mandel干涉')
plt.grid(True, alpha=0.3)
plt.show()
print("\n注意:|1,1>概率接近0,这是HOM干涉的特征!")
# HOM干涉模拟
cir_hom = dq.QumodeCircuit(nmode=2, init_state=[1, 1], cutoff=3, basis=True)
# 使用 bs_theta 方法,inputs 参数指定 theta 值
# 50:50 分束器对应 theta = pi/4
cir_hom.bs_theta([0, 1], inputs=np.pi/4)
probs_hom = cir_hom(is_prob=True)
print("HOM干涉结果:")
for state, prob in probs_hom.items():
print(f"{state} 概率: {prob}")
# 可视化
states = list(probs_hom.keys())
values = [float(p) for p in probs_hom.values()]
# FockState对象可以直接转为字符串
states_str = [str(s).replace('|', '').replace('>', '') for s in states]
plt.figure(figsize=(8, 5))
plt.bar(states_str, values)
plt.xlabel('输出态')
plt.ylabel('概率')
plt.title('Hong-Ou-Mandel干涉')
plt.grid(True, alpha=0.3)
plt.show()
print("\n注意:|1,1>概率接近0,这是HOM干涉的特征!")