janus.simulator#
量子电路模拟器模块。
模拟器#
StatevectorSimulator#
- class janus.simulator.StatevectorSimulator(seed=None, precision='double')[源代码]#
基类:
object状态向量模拟器
使用完整状态向量进行精确模拟,适用于小规模量子电路(< 25 qubits)
- Features:
精确模拟(无采样误差)
支持参数化电路
支持初始状态设置
支持部分测量
支持期望值计算
示例
from janus.circuit import Circuit from janus.simulator import StatevectorSimulator
# 创建 Bell 态电路 qc = Circuit(2) qc.h(0) qc.cx(0, 1)
# 模拟 sim = StatevectorSimulator() result = sim.run(qc, shots=1000)
print(result.counts) # {'00': ~500, '11': ~500}
- run(circuit, shots=1024, initial_state=None, parameter_binds=None, measure_qubits=None, return_statevector=True)[源代码]#
运行模拟
- 参数:
- 返回:
模拟结果
- 返回类型:
- run_batch(circuits, shots=1024, **kwargs)[源代码]#
批量运行多个电路
- 参数:
- 返回:
结果列表
- 返回类型:
List[SimulatorResult]
- statevector(circuit, initial_state=None, parameter_binds=None)[源代码]#
获取电路的最终状态向量(不进行测量)
- 参数:
circuit -- 要模拟的电路
initial_state (Statevector | ndarray | str | None) -- 初始状态
parameter_binds (Dict | None) -- 参数绑定
- 返回:
最终状态向量
- 返回类型:
NoisySimulator#
- class janus.simulator.NoisySimulator(noise_model=None, seed=None)[源代码]#
基类:
object噪声模拟器
使用密度矩阵进行带噪声的量子电路模拟
示例
from janus.simulator import NoisySimulator from janus.simulator.noise import NoiseModel, depolarizing_channel
# 创建噪声模型 noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(
depolarizing_channel(0.01), ['cx']
)
# 模拟 sim = NoisySimulator(noise_model) result = sim.run(circuit, shots=1000)
- 参数:
noise_model (Optional[NoiseModel])
seed (Optional[Union[int, np.random.Generator]])
- __init__(noise_model=None, seed=None)[源代码]#
初始化噪声模拟器
- 参数:
noise_model (NoiseModel | None) -- 噪声模型
- property noise_model: NoiseModel | None#
获取噪声模型
- run(circuit, shots=1024, initial_state=None, parameter_binds=None, measure_qubits=None)[源代码]#
运行噪声模拟
- 参数:
circuit -- 要模拟的电路
shots (int) -- 测量次数
initial_state (DensityMatrix | Statevector | ndarray | None) -- 初始状态
parameter_binds (Dict | None) -- 参数绑定
- 返回:
模拟结果
- 返回类型:
- density_matrix(circuit, initial_state=None, parameter_binds=None)[源代码]#
获取电路的最终密度矩阵
- 参数:
circuit -- 要模拟的电路
initial_state (DensityMatrix | Statevector | ndarray | None) -- 初始状态
parameter_binds (Dict | None) -- 参数绑定
- 返回:
最终密度矩阵
- 返回类型:
量子态#
Statevector#
- class janus.simulator.Statevector(data, num_qubits=None)[源代码]#
基类:
object状态向量类
表示纯量子态 |ψ⟩,支持: - 从电路演化 - 测量采样 - 期望值计算 - 子系统操作
示例
# 创建 |00⟩ 态 sv = Statevector.from_label('00')
# 从电路创建 sv = Statevector.from_circuit(circuit)
# 测量 counts = sv.sample_counts(shots=1000)
- 参数:
data (Union[np.ndarray, List, 'Statevector'])
num_qubits (Optional[int])
- __init__(data, num_qubits=None)[源代码]#
初始化状态向量
- 参数:
data (ndarray | List | Statevector) -- 状态向量数据,可以是数组、列表或另一个 Statevector
num_qubits (int | None) -- 量子比特数(可选,自动推断)
- classmethod from_int(i, num_qubits)[源代码]#
从计算基态创建状态向量
- 参数:
- 返回:
|i⟩ 态
- 返回类型:
示例
sv = Statevector.from_int(0, 2) # |00⟩ sv = Statevector.from_int(3, 2) # |11⟩
- classmethod from_label(label)[源代码]#
从标签创建状态向量
支持的标签: - '0', '1': 计算基态 - '+', '-': X 基态 - 'r', 'l': Y 基态
- 参数:
label (str) -- 状态标签字符串
- 返回:
对应的状态向量
- 返回类型:
示例
sv = Statevector.from_label('00') # |00⟩ sv = Statevector.from_label('+0') # |+0⟩ = (|00⟩ + |10⟩)/√2
- classmethod from_circuit(circuit)[源代码]#
从电路创建状态向量
- 参数:
circuit -- Janus Circuit 对象
- 返回:
电路作用后的状态向量
- 返回类型:
- measure(qargs=None)[源代码]#
执行测量并返回结果和坍缩后的状态
- 参数:
- 返回:
(测量结果, 坍缩后的状态)
- 返回类型:
Tuple[str, Statevector]
- tensor(other)[源代码]#
张量积 self ⊗ other
- 参数:
other (Statevector) -- 另一个状态向量
- 返回:
张量积状态
- 返回类型:
- inner(other)[源代码]#
内积 ⟨self|other⟩
- 参数:
other (Statevector) -- 另一个状态向量
- 返回:
内积值
- 返回类型:
- equiv(other, atol=1e-10)[源代码]#
检查两个状态向量是否等价(忽略全局相位)
- 参数:
other (Statevector) -- 另一个状态向量
atol (float) -- 绝对容差
- 返回:
是否等价
- 返回类型:
DensityMatrix#
- class janus.simulator.DensityMatrix(data, num_qubits=None)[源代码]#
基类:
object密度矩阵类
表示混合量子态 ρ,支持: - 噪声信道演化 - 部分迹 - 保真度计算
示例
# 从纯态创建 dm = DensityMatrix.from_statevector(sv)
# 应用去极化噪声 dm = dm.apply_channel(depolarizing_channel(0.01), [0])
- 参数:
data (Union[np.ndarray, 'DensityMatrix'])
num_qubits (Optional[int])
- __init__(data, num_qubits=None)[源代码]#
初始化密度矩阵
- 参数:
data (ndarray | DensityMatrix) -- 密度矩阵数据
num_qubits (int | None) -- 量子比特数
- classmethod from_statevector(sv)[源代码]#
从纯态创建密度矩阵 ρ = |ψ⟩⟨ψ|
- 参数:
sv (Statevector | ndarray)
- 返回类型:
- apply_channel(kraus_ops, qargs=None)[源代码]#
应用量子信道(Kraus 表示)
ρ → Σ_k K_k ρ K_k†
- 参数:
- 返回:
演化后的密度矩阵
- 返回类型:
- fidelity(other)[源代码]#
计算保真度
F(ρ, σ) = (Tr√(√ρ σ √ρ))² F(ρ, |ψ⟩) = ⟨ψ|ρ|ψ⟩
- 参数:
other (DensityMatrix | Statevector)
- 返回类型:
结果#
SimulatorResult#
- class janus.simulator.SimulatorResult(counts=None, statevector=None, shots=0, metadata=None)[源代码]#
模拟器结果类
封装单次模拟运行的所有结果
- counts#
测量计数
- statevector#
最终状态向量(可选)
- probabilities#
概率分布
- shots#
测量次数
- metadata#
额外元数据
Counts#
- class janus.simulator.Counts(data=None)[源代码]#
测量计数类
继承自 Counter,提供额外的量子计算相关方法
示例
counts = Counts({'00': 512, '11': 488}) print(counts.most_frequent()) # '00' print(counts.int_outcomes()) # {0: 512, 3: 488}
- __init__(data=None)[源代码]#
Create a new, empty Counter object. And if given, count elements from an input iterable. Or, initialize the count from another mapping of elements to their counts.
>>> c = Counter() # a new, empty counter >>> c = Counter('gallahad') # a new counter from an iterable >>> c = Counter({'a': 4, 'b': 2}) # a new counter from a mapping >>> c = Counter(a=4, b=2) # a new counter from keyword args
噪声#
NoiseModel#
- class janus.simulator.NoiseModel[源代码]#
噪声模型
定义电路中各种门的噪声
示例
noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(
depolarizing_channel(0.01), ['cx']
) noise_model.add_readout_error(0.02, 0.03)
- add_quantum_error(error, gate_names, qubits=None)[源代码]#
添加量子错误
- 参数:
error (NoiseChannel) -- 噪声信道
- add_all_qubit_quantum_error(error, gate_names)[源代码]#
为所有量子比特添加量子错误
- 参数:
error (NoiseChannel)
- get_gate_error(gate_name, qubits)[源代码]#
获取门的噪声信道
- 参数:
- 返回类型:
NoiseChannel | None
NoiseChannel#
- class janus.simulator.NoiseChannel(kraus_ops, num_qubits=1)[源代码]#
噪声信道基类
使用 Kraus 表示:ρ → Σ_k K_k ρ K_k†
- 参数:
kraus_ops (List[np.ndarray])
num_qubits (int)
- compose(other)[源代码]#
组合两个噪声信道
- 参数:
other (NoiseChannel)
- 返回类型:
- tensor(other)[源代码]#
张量积两个噪声信道
- 参数:
other (NoiseChannel)
- 返回类型:
噪声信道函数#
- janus.simulator.depolarizing_channel(p, num_qubits=1)[源代码]#
去极化信道
ρ → (1-p)ρ + p/3 (XρX + YρY + ZρZ) (单比特) ρ → (1-p)ρ + p/(4^n-1) Σ_P PρP (多比特)
- 参数:
- 返回:
去极化信道
- 返回类型:
- janus.simulator.amplitude_damping_channel(gamma)[源代码]#
振幅阻尼信道(能量弛豫)
- 参数:
gamma (float) -- 衰减概率
- 返回:
振幅阻尼信道
- 返回类型:
- janus.simulator.phase_damping_channel(gamma)[源代码]#
相位阻尼信道(纯退相干)
- 参数:
gamma (float) -- 退相干概率
- 返回:
相位阻尼信道
- 返回类型:
- janus.simulator.bit_flip_channel(p)[源代码]#
比特翻转信道
ρ → (1-p)ρ + p XρX
- 参数:
p (float) -- 翻转概率
- 返回:
比特翻转信道
- 返回类型:
- janus.simulator.phase_flip_channel(p)[源代码]#
相位翻转信道
ρ → (1-p)ρ + p ZρZ
- 参数:
p (float) -- 翻转概率
- 返回:
相位翻转信道
- 返回类型: