| Name | nexus-quantum-core JSON |
| Version |
1.5.0
JSON |
| download |
| home_page | None |
| Summary | 一个高性能、双模式、拓扑感知的量子计算模拟核心库。 |
| upload_time | 2025-11-02 01:13:59 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.8 |
| license | MIT License
Copyright (c) 2025 uraniceye
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|
| keywords |
quantum
quantum computing
simulator
quantum circuit
density matrix
statevector
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
NEXUS QUANTUM DEFENSE - 量子计算核心库
版本: 1.5.0 (拓扑感知API版)
作者: 跳舞的火公子
一份为高性能量子模拟而生的核心库,致力于在纯态模拟的效率与混合态模拟的功能之间取得极致平衡。



📖 简介
quantum_core 是一个功能强大、设计精良的Python量子计算模拟库。它封装了从基础的量子态、量子线路数据结构到高性能并行计算引擎的全套工具,旨在为量子算法研究、量子机器学习和带噪量子系统模拟提供一个稳定、高效且易于扩展的底层框架。
本库的核心设计哲学是 “惰性求值” 与 “双模式模拟引擎” 的结合。在理想情况下(无噪声),它通过惰性求值最大化性能,仅在需要时才执行昂贵的态矢量计算。当引入非相干噪声时,它能无缝、不可逆地切换到功能更全面的密度矩阵模拟模式,从而准确地描述混合态的演化。
✨ 核心特性
🧠 惰性求值与双模式引擎:
态矢量模式 (Statevector): 默认模式,用于纯态模拟。门操作仅被记录在电路中,实际计算被推迟到查询结果时,极大提升了构建复杂线路的效率。
密度矩阵模式 (Density Matrix): 当应用非相干噪声(如量子通道)时,系统会自动、不可逆地切换至此模式,能够精确模拟混合态和退相干过程。
⚡️ 高性能计算后端:
纯Python后端: 无任何第三方依赖,保证了极致的稳定性和跨平台兼容性,是逻辑验证和调试的基石。
CuPy GPU加速: 可选的 CuPy 后端,能够利用NVIDIA GPU进行大规模并行计算,显著加速大型量子系统的模拟。
内置CPU并行计算: 对于大规模纯Python模拟,内置了基于 multiprocessing 的并行计算基础设施,可充分利用多核CPU资源加速特定门操作和酉演化。
🏛️ 稳健的架构设计:
中央算子库 (QuantumOperatorLibrary): 所有量子门的定义(矩阵、分解规则、优化内核)都集中管理,是全系统的“单一事实来源”,确保了行为的一致性和可验证性。
可扩展的噪声模型: 通过抽象基类 NoiseModel 定义了清晰的噪声插件接口,用户可以轻松实现自定义的相干或非相干噪声模型,如硬件噪声、关联噪声等。
拓扑感知API (v1.5.0新增): 核心API run_circuit_on_state 接收 topology 参数,能够与外部编译器(如 nexus_optimizer)无缝集成,实现对真实硬件拓扑的感知优化。
🔋 丰富的功能集与API:
全面的门操作集: 支持所有标准单比特、多比特门,以及高级多控制门(Toffoli, MCX, MCZ等)。
算法构建器 (AlgorithmBuilders): 内置了用于快速构建常用量子算法(如QFT, QPE, Grover)和VQA拟设(Hardware-Efficient Ansatz)的便捷工具。
强大的分析能力: 提供了计算哈密顿量期望值、冯·诺依曼纠缠熵、布洛赫矢量、边际概率等高级分析工具。
⚙️ 安装
本核心库被设计为一个独立的Python文件,不依赖任何难以安装的第三方库(CuPy 除外)。
基础安装 (仅CPU)
只需将 quantum_core.py 文件放置在您的项目目录中,即可直接导入使用。
code
Python
download
content_copy
expand_less
import quantum_core as nq
本库需要 Python 3.8 或更高版本。
可选:启用GPU加速
为了获得极致的模拟性能,您可以安装 CuPy 库来启用GPU加速。请根据您的CUDA版本选择合适的 CuPy 版本进行安装。
例如,如果您的系统安装了CUDA 11.8,可以使用以下命令:
code
Bash
download
content_copy
expand_less
pip install cupy-cuda11x
如果您的CUDA版本不同,请参考 CuPy官方安装指南 进行安装。
安装完成后,quantum_core 将在启动时自动检测到 CuPy 并优先使用GPU后端。
🚀 快速上手
下面是一个创建贝尔纠缠态 |Φ+⟩ = (|00⟩ + |11⟩)/√2 并验证其概率分布的简单示例。
code
Python
download
content_copy
expand_less
import quantum_core as nq
import math
# 1. 创建一个包含2个量子比特的初始量子态 |00⟩
# 这是一个惰性对象,此时没有进行任何大规模内存分配。
print("--- 创建初始量子态 ---")
initial_state = nq.create_quantum_state(num_qubits=2)
print(f"初始态: {initial_state.num_qubits} qubits, 模式: {initial_state._simulation_mode}")
# 2. 构建一个用于制备贝尔态的量子线路
print("\n--- 构建量子线路 ---")
bell_circuit = nq.QuantumCircuit(num_qubits=2, description="Bell State Preparation")
bell_circuit.h(0) # 在 q0 上应用 Hadamard 门
bell_circuit.cnot(0, 1) # 在 (q0, q1) 上应用 CNOT 门
print(bell_circuit)
# 3. 在初始态上执行线路,得到一个新的演化后状态
# 此操作遵循函数式编程的不可变性原则,原始的 initial_state 不会被修改。
# 此时,计算仍然是惰性的。
print("\n--- 执行线路 (惰性) ---")
final_state = nq.run_circuit_on_state(initial_state, bell_circuit)
print(f"最终态: {final_state.num_qubits} qubits, 模式: {final_state._simulation_mode}")
print(f"最终态内部线路指令数: {len(final_state.circuit.instructions)}")
# 4. 获取测量概率分布
# 这是第一个需要实际计算结果的操作,它将触发内部的“展开计算”。
print("\n--- 获取结果 (触发计算) ---")
probabilities = nq.get_measurement_probabilities(final_state)
# 5. 验证结果
print(f"\n测量概率: {probabilities}")
expected_probs = [0.5, 0.0, 0.0, 0.5]
assert all(math.isclose(p_actual, p_expected, abs_tol=1e-9)
for p_actual, p_expected in zip(probabilities, expected_probs))
print("✅ 验证成功!最终态为贝尔态 |Φ+⟩。")
核心概念与架构
🧠 惰性求值与双模式引擎
这是本库的核心设计。
惰性求值: 在 statevector 模式下,对 QuantumState 对象应用门操作(如 state.h(0)) 不会立即计算新的态矢量。相反,它仅将指令记录到内部的 QuantumCircuit 中。这使得构建包含数千个门的复杂线路几乎是零成本的。只有当您请求一个需要完整状态的结果时(例如 get_measurement_probabilities()),系统才会一次性地、高效地执行所有累积的指令来计算最终的态矢量。
双模式引擎:
statevector 模式: 默认模式,使用一个 (2^N,) 的复数向量来表示纯态。计算效率高,内存占用相对较小。
density_matrix 模式: 当您应用一个非相干噪声通道(例如 apply_quantum_channel)时,系统会自动将态矢量 |ψ⟩ 转换为密度矩阵 ρ = |ψ⟩⟨ψ|,并将模式不可逆地切换为 density_matrix。此后,所有操作都将在一个 (2^N, 2^N) 的密度矩阵上进行,这虽然计算成本更高,但能够精确地模拟混合态和退相干过程。
code
Python
download
content_copy
expand_less
# 演示模式切换
state = nq.create_quantum_state(1)
print(f"初始模式: {state._simulation_mode}") # -> statevector
# 应用一个非相干噪声通道
state.apply_quantum_channel(
channel_type='depolarizing',
target_qubits=[0],
params={'probability': 0.1}
)
print(f"应用噪声后的模式: {state._simulation_mode}") # -> density_matrix
🏛️ 量子算子库 (QuantumOperatorLibrary)
QuantumOperatorLibrary 是本库的基石。它是一个中央注册中心,存储了所有内置量子门的完整定义 (OperatorDefinition)。每个定义都包含:
数学表示: 固定的酉矩阵(如 X 门)或用于生成酉矩阵的函数(如 RX(θ))。
分解规则: 对于复合门(如 SWAP),定义了如何将其分解为更基础的门。
优化内核: 指向 _StateVectorEntity 或 _DensityMatrixEntity 内部专门优化的、无需构建全局矩阵即可执行操作的高性能函数。
这种设计确保了全系统对门操作的解释和执行是统一、可验证且高效的。
⚡️ 并行计算
本库内置了基于 multiprocessing 的CPU并行计算框架,可用于加速大规模(通常 > 12量子比特)的纯Python模拟。
启用并行计算:
您必须在脚本的主执行块 (if __name__ == '__main__':) 中显式启用并行模式。
code
Python
download
content_copy
expand_less
import quantum_core as nq
import sys
if __name__ == '__main__':
# 启用并行计算,自动检测CPU核心数
nq.enable_parallelism()
try:
# 在这里运行您的大规模模拟
state = nq.create_quantum_state(14)
state.h(0) # 这个操作在展开时可能会被并行化
# ... 更多操作 ...
# 触发计算
probs = nq.get_measurement_probabilities(state)
print("大规模模拟完成。")
finally:
# 在程序结束前优雅地关闭并行池
nq.disable_parallelism()
📚 公共 API 参考
以下是 quantum_core 提供的核心公共API函数。
create_quantum_state(num_qubits)
创建一个处于 |0...0⟩ 态的初始惰性量子态。
参数 类型 描述
num_qubits int 要创建的量子态的比特数。
返回: 一个新的 QuantumState 实例。
run_circuit_on_state(state, circuit, noise_model, topology)
在一个量子态上执行一个量子线路,返回一个表示演化后状态的新量子态实例。
参数 类型 描述 默认值
state QuantumState 初始量子态。
circuit QuantumCircuit 要执行的量子线路。
noise_model Optional[NoiseModel] 可选的噪声模型。 None
topology Optional[Dict] 可选的硬件拓扑图。 None
返回: 演化后的新 QuantumState 实例。
get_state_data(state, format)
安全地从一个 QuantumState 对象中提取底层的状态数据(态矢量或密度矩阵)。此函数会根据需要触发展开计算。
参数 类型 描述 默认值
state QuantumState 要提取数据的量子态。
format str 返回数据的格式。目前仅支持 'python_list'。 'python_list'
返回: List[complex] (态矢量) 或 List[List[complex]] (密度矩阵)。
get_measurement_probabilities(state)
获取一个量子态所有计算基的测量概率。
参数 类型 描述
state QuantumState 要分析的量子态。
返回: List[float],一个包含所有测量概率的列表。
calculate_hamiltonian_expectation_value(state, hamiltonian)
计算给定哈密顿量在当前量子态下的期望值。
参数 类型 描述
state QuantumState 要分析的量子态。
hamiltonian List[PauliString] 表示哈密顿量的 PauliString 列表。
返回: float,哈密顿量的期望值。
get_effective_unitary(circuit, backend_choice)
计算给定 QuantumCircuit 对应的全局有效酉矩阵。
参数 类型 描述 默认值
circuit QuantumCircuit 要计算酉矩阵的线路。
backend_choice str 用于计算的后端 ('auto', 'pure_python', 'cupy')。 'auto'
返回: 酉矩阵 (Python list 或 cupy.ndarray)。
🔬 高级用法
使用噪声模型
本库支持通过 NoiseModel 子类来模拟带噪量子计算。
code
Python
download
content_copy
expand_less
# 1. 定义一个模拟真实硬件噪声的校准数据
fake_calib = {
'qubits': {
0: {'T1': 50e-6, 'T2': 70e-6, 'readout_error': 0.01},
},
'gates': {
'h': {'duration': 50e-9, 'error_rate': 0.001}, # 50ns H-gate, 0.1% 去极化错误
}
}
# 2. 实例化预置的硬件噪声模型
noise_model = nq.PrebuiltNoiseModels.HardwareBackend(fake_calib)
# 3. 创建初始态和线路
state = nq.create_quantum_state(1)
circuit = nq.QuantumCircuit(1)
circuit.h(0)
# 4. 在运行线路时传入噪声模型
noisy_state = nq.run_circuit_on_state(state, circuit, noise_model=noise_model)
# 5. 验证状态已变为混合态
assert noisy_state._simulation_mode == 'density_matrix'
purity = nq.calculate_state_purity(noisy_state) # 假设有这样一个API
print(f"带噪状态的纯度: {purity:.4f}") # 纯度应小于1
算法构建器
使用 AlgorithmBuilders 可以快速生成复杂算法的线路。
code
Python
download
content_copy
expand_less
# 构建一个3比特的量子傅里叶变换(QFT)线路
qft_circuit = nq.AlgorithmBuilders.build_qft_circuit(num_qubits=3)
print("--- 3-qubit QFT Circuit ---")
print(qft_circuit)
# 构建一个用于VQA的硬件高效拟设
params = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]
ansatz = nq.AlgorithmBuilders.build_hardware_efficient_ansatz(
num_qubits=2,
depth=2,
parameters=params,
entanglement_type='circular'
)
print("\n--- Hardware-Efficient Ansatz ---")
print(ansatz)
配置核心库
您可以在运行时通过 configure_quantum_core 函数调整库的行为。
code
Python
download
content_copy
expand_less
# 强制使用纯Python后端,并设置最大量子比特数上限
nq.configure_quantum_core({
"BACKEND_CHOICE": "pure_python",
"MAX_QUBITS": 10,
"MAX_SYSTEM_RAM_GB_LIMIT": 16 # 手动设置内存上限为16GB
})
🧪 测试
本文件包含一个全面的自测试套件。要运行测试,只需直接执行此脚本:
code
Bash
download
content_copy
expand_less
python quantum_core.py
测试套件将自动在可用的后端(Pure Python 和/或 CuPy)上运行,并验证所有核心功能、算法构建器和噪声模型的正确性。测试采用“快速失败”策略,任何一个测试失败都会立即中止整个测试流程。
📜 版本历史
v1.5.0 - 拓扑感知API版
API 升级: 公共 API run_circuit_on_state 和相关内部方法现在接收 topology 参数,以实现对量子子程序的拓扑感知优化。
架构重构: 彻底移除了 get_effective_unitary 中的猴子补丁,采用内置的模式感知执行,提高了代码的健壮性和可维护性。
噪声模型扩展: 引入了 CorrelatedNoise 模型,用于模拟串扰等非局部效应。
逻辑修复: 修正了相干噪声的应用逻辑,确保其只作用于旋转门的旋转角度。
内核调度优化: _StateVectorEntity 内部改用调度字典来管理优化内核,提高了代码的可读性和可扩展性。
👤 作者
跳舞的火公子
📄 许可证
本项目采用 MIT 许可证。详情请见 LICENSE 文件。
Raw data
{
"_id": null,
"home_page": null,
"name": "nexus-quantum-core",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "quantum, quantum computing, simulator, quantum circuit, density matrix, statevector",
"author": null,
"author_email": "\u8df3\u821e\u7684\u706b\u516c\u5b50 <your-email@example.com>",
"download_url": "https://files.pythonhosted.org/packages/71/51/73cc84c3516c2a64e410b9d84a1f4376850202b064433f9e149004a7eadc/nexus_quantum_core-1.5.0.tar.gz",
"platform": null,
"description": "NEXUS QUANTUM DEFENSE - \u91cf\u5b50\u8ba1\u7b97\u6838\u5fc3\u5e93\r\n\r\n\u7248\u672c: 1.5.0 (\u62d3\u6251\u611f\u77e5API\u7248)\r\n\u4f5c\u8005: \u8df3\u821e\u7684\u706b\u516c\u5b50\r\n\r\n\u4e00\u4efd\u4e3a\u9ad8\u6027\u80fd\u91cf\u5b50\u6a21\u62df\u800c\u751f\u7684\u6838\u5fc3\u5e93\uff0c\u81f4\u529b\u4e8e\u5728\u7eaf\u6001\u6a21\u62df\u7684\u6548\u7387\u4e0e\u6df7\u5408\u6001\u6a21\u62df\u7684\u529f\u80fd\u4e4b\u95f4\u53d6\u5f97\u6781\u81f4\u5e73\u8861\u3002\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\ud83d\udcd6 \u7b80\u4ecb\r\n\r\nquantum_core \u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u3001\u8bbe\u8ba1\u7cbe\u826f\u7684Python\u91cf\u5b50\u8ba1\u7b97\u6a21\u62df\u5e93\u3002\u5b83\u5c01\u88c5\u4e86\u4ece\u57fa\u7840\u7684\u91cf\u5b50\u6001\u3001\u91cf\u5b50\u7ebf\u8def\u6570\u636e\u7ed3\u6784\u5230\u9ad8\u6027\u80fd\u5e76\u884c\u8ba1\u7b97\u5f15\u64ce\u7684\u5168\u5957\u5de5\u5177\uff0c\u65e8\u5728\u4e3a\u91cf\u5b50\u7b97\u6cd5\u7814\u7a76\u3001\u91cf\u5b50\u673a\u5668\u5b66\u4e60\u548c\u5e26\u566a\u91cf\u5b50\u7cfb\u7edf\u6a21\u62df\u63d0\u4f9b\u4e00\u4e2a\u7a33\u5b9a\u3001\u9ad8\u6548\u4e14\u6613\u4e8e\u6269\u5c55\u7684\u5e95\u5c42\u6846\u67b6\u3002\r\n\r\n\u672c\u5e93\u7684\u6838\u5fc3\u8bbe\u8ba1\u54f2\u5b66\u662f \u201c\u60f0\u6027\u6c42\u503c\u201d \u4e0e \u201c\u53cc\u6a21\u5f0f\u6a21\u62df\u5f15\u64ce\u201d \u7684\u7ed3\u5408\u3002\u5728\u7406\u60f3\u60c5\u51b5\u4e0b\uff08\u65e0\u566a\u58f0\uff09\uff0c\u5b83\u901a\u8fc7\u60f0\u6027\u6c42\u503c\u6700\u5927\u5316\u6027\u80fd\uff0c\u4ec5\u5728\u9700\u8981\u65f6\u624d\u6267\u884c\u6602\u8d35\u7684\u6001\u77e2\u91cf\u8ba1\u7b97\u3002\u5f53\u5f15\u5165\u975e\u76f8\u5e72\u566a\u58f0\u65f6\uff0c\u5b83\u80fd\u65e0\u7f1d\u3001\u4e0d\u53ef\u9006\u5730\u5207\u6362\u5230\u529f\u80fd\u66f4\u5168\u9762\u7684\u5bc6\u5ea6\u77e9\u9635\u6a21\u62df\u6a21\u5f0f\uff0c\u4ece\u800c\u51c6\u786e\u5730\u63cf\u8ff0\u6df7\u5408\u6001\u7684\u6f14\u5316\u3002\r\n\r\n\u2728 \u6838\u5fc3\u7279\u6027\r\n\r\n\ud83e\udde0 \u60f0\u6027\u6c42\u503c\u4e0e\u53cc\u6a21\u5f0f\u5f15\u64ce:\r\n\r\n\u6001\u77e2\u91cf\u6a21\u5f0f (Statevector): \u9ed8\u8ba4\u6a21\u5f0f\uff0c\u7528\u4e8e\u7eaf\u6001\u6a21\u62df\u3002\u95e8\u64cd\u4f5c\u4ec5\u88ab\u8bb0\u5f55\u5728\u7535\u8def\u4e2d\uff0c\u5b9e\u9645\u8ba1\u7b97\u88ab\u63a8\u8fdf\u5230\u67e5\u8be2\u7ed3\u679c\u65f6\uff0c\u6781\u5927\u63d0\u5347\u4e86\u6784\u5efa\u590d\u6742\u7ebf\u8def\u7684\u6548\u7387\u3002\r\n\r\n\u5bc6\u5ea6\u77e9\u9635\u6a21\u5f0f (Density Matrix): \u5f53\u5e94\u7528\u975e\u76f8\u5e72\u566a\u58f0\uff08\u5982\u91cf\u5b50\u901a\u9053\uff09\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u3001\u4e0d\u53ef\u9006\u5730\u5207\u6362\u81f3\u6b64\u6a21\u5f0f\uff0c\u80fd\u591f\u7cbe\u786e\u6a21\u62df\u6df7\u5408\u6001\u548c\u9000\u76f8\u5e72\u8fc7\u7a0b\u3002\r\n\r\n\u26a1\ufe0f \u9ad8\u6027\u80fd\u8ba1\u7b97\u540e\u7aef:\r\n\r\n\u7eafPython\u540e\u7aef: \u65e0\u4efb\u4f55\u7b2c\u4e09\u65b9\u4f9d\u8d56\uff0c\u4fdd\u8bc1\u4e86\u6781\u81f4\u7684\u7a33\u5b9a\u6027\u548c\u8de8\u5e73\u53f0\u517c\u5bb9\u6027\uff0c\u662f\u903b\u8f91\u9a8c\u8bc1\u548c\u8c03\u8bd5\u7684\u57fa\u77f3\u3002\r\n\r\nCuPy GPU\u52a0\u901f: \u53ef\u9009\u7684 CuPy \u540e\u7aef\uff0c\u80fd\u591f\u5229\u7528NVIDIA GPU\u8fdb\u884c\u5927\u89c4\u6a21\u5e76\u884c\u8ba1\u7b97\uff0c\u663e\u8457\u52a0\u901f\u5927\u578b\u91cf\u5b50\u7cfb\u7edf\u7684\u6a21\u62df\u3002\r\n\r\n\u5185\u7f6eCPU\u5e76\u884c\u8ba1\u7b97: \u5bf9\u4e8e\u5927\u89c4\u6a21\u7eafPython\u6a21\u62df\uff0c\u5185\u7f6e\u4e86\u57fa\u4e8e multiprocessing \u7684\u5e76\u884c\u8ba1\u7b97\u57fa\u7840\u8bbe\u65bd\uff0c\u53ef\u5145\u5206\u5229\u7528\u591a\u6838CPU\u8d44\u6e90\u52a0\u901f\u7279\u5b9a\u95e8\u64cd\u4f5c\u548c\u9149\u6f14\u5316\u3002\r\n\r\n\ud83c\udfdb\ufe0f \u7a33\u5065\u7684\u67b6\u6784\u8bbe\u8ba1:\r\n\r\n\u4e2d\u592e\u7b97\u5b50\u5e93 (QuantumOperatorLibrary): \u6240\u6709\u91cf\u5b50\u95e8\u7684\u5b9a\u4e49\uff08\u77e9\u9635\u3001\u5206\u89e3\u89c4\u5219\u3001\u4f18\u5316\u5185\u6838\uff09\u90fd\u96c6\u4e2d\u7ba1\u7406\uff0c\u662f\u5168\u7cfb\u7edf\u7684\u201c\u5355\u4e00\u4e8b\u5b9e\u6765\u6e90\u201d\uff0c\u786e\u4fdd\u4e86\u884c\u4e3a\u7684\u4e00\u81f4\u6027\u548c\u53ef\u9a8c\u8bc1\u6027\u3002\r\n\r\n\u53ef\u6269\u5c55\u7684\u566a\u58f0\u6a21\u578b: \u901a\u8fc7\u62bd\u8c61\u57fa\u7c7b NoiseModel \u5b9a\u4e49\u4e86\u6e05\u6670\u7684\u566a\u58f0\u63d2\u4ef6\u63a5\u53e3\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u76f8\u5e72\u6216\u975e\u76f8\u5e72\u566a\u58f0\u6a21\u578b\uff0c\u5982\u786c\u4ef6\u566a\u58f0\u3001\u5173\u8054\u566a\u58f0\u7b49\u3002\r\n\r\n\u62d3\u6251\u611f\u77e5API (v1.5.0\u65b0\u589e): \u6838\u5fc3API run_circuit_on_state \u63a5\u6536 topology \u53c2\u6570\uff0c\u80fd\u591f\u4e0e\u5916\u90e8\u7f16\u8bd1\u5668\uff08\u5982 nexus_optimizer\uff09\u65e0\u7f1d\u96c6\u6210\uff0c\u5b9e\u73b0\u5bf9\u771f\u5b9e\u786c\u4ef6\u62d3\u6251\u7684\u611f\u77e5\u4f18\u5316\u3002\r\n\r\n\ud83d\udd0b \u4e30\u5bcc\u7684\u529f\u80fd\u96c6\u4e0eAPI:\r\n\r\n\u5168\u9762\u7684\u95e8\u64cd\u4f5c\u96c6: \u652f\u6301\u6240\u6709\u6807\u51c6\u5355\u6bd4\u7279\u3001\u591a\u6bd4\u7279\u95e8\uff0c\u4ee5\u53ca\u9ad8\u7ea7\u591a\u63a7\u5236\u95e8\uff08Toffoli, MCX, MCZ\u7b49\uff09\u3002\r\n\r\n\u7b97\u6cd5\u6784\u5efa\u5668 (AlgorithmBuilders): \u5185\u7f6e\u4e86\u7528\u4e8e\u5feb\u901f\u6784\u5efa\u5e38\u7528\u91cf\u5b50\u7b97\u6cd5\uff08\u5982QFT, QPE, Grover\uff09\u548cVQA\u62df\u8bbe\uff08Hardware-Efficient Ansatz\uff09\u7684\u4fbf\u6377\u5de5\u5177\u3002\r\n\r\n\u5f3a\u5927\u7684\u5206\u6790\u80fd\u529b: \u63d0\u4f9b\u4e86\u8ba1\u7b97\u54c8\u5bc6\u987f\u91cf\u671f\u671b\u503c\u3001\u51af\u00b7\u8bfa\u4f9d\u66fc\u7ea0\u7f20\u71b5\u3001\u5e03\u6d1b\u8d6b\u77e2\u91cf\u3001\u8fb9\u9645\u6982\u7387\u7b49\u9ad8\u7ea7\u5206\u6790\u5de5\u5177\u3002\r\n\r\n\u2699\ufe0f \u5b89\u88c5\r\n\r\n\u672c\u6838\u5fc3\u5e93\u88ab\u8bbe\u8ba1\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684Python\u6587\u4ef6\uff0c\u4e0d\u4f9d\u8d56\u4efb\u4f55\u96be\u4ee5\u5b89\u88c5\u7684\u7b2c\u4e09\u65b9\u5e93\uff08CuPy \u9664\u5916\uff09\u3002\r\n\r\n\u57fa\u7840\u5b89\u88c5 (\u4ec5CPU)\r\n\r\n\u53ea\u9700\u5c06 quantum_core.py \u6587\u4ef6\u653e\u7f6e\u5728\u60a8\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\uff0c\u5373\u53ef\u76f4\u63a5\u5bfc\u5165\u4f7f\u7528\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\nimport quantum_core as nq\r\n\r\n\u672c\u5e93\u9700\u8981 Python 3.8 \u6216\u66f4\u9ad8\u7248\u672c\u3002\r\n\r\n\u53ef\u9009\uff1a\u542f\u7528GPU\u52a0\u901f\r\n\r\n\u4e3a\u4e86\u83b7\u5f97\u6781\u81f4\u7684\u6a21\u62df\u6027\u80fd\uff0c\u60a8\u53ef\u4ee5\u5b89\u88c5 CuPy \u5e93\u6765\u542f\u7528GPU\u52a0\u901f\u3002\u8bf7\u6839\u636e\u60a8\u7684CUDA\u7248\u672c\u9009\u62e9\u5408\u9002\u7684 CuPy \u7248\u672c\u8fdb\u884c\u5b89\u88c5\u3002\r\n\r\n\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u7684\u7cfb\u7edf\u5b89\u88c5\u4e86CUDA 11.8\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a\r\n\r\ncode\r\nBash\r\ndownload\r\ncontent_copy\r\nexpand_less\r\npip install cupy-cuda11x\r\n\r\n\u5982\u679c\u60a8\u7684CUDA\u7248\u672c\u4e0d\u540c\uff0c\u8bf7\u53c2\u8003 CuPy\u5b98\u65b9\u5b89\u88c5\u6307\u5357 \u8fdb\u884c\u5b89\u88c5\u3002\r\n\r\n\u5b89\u88c5\u5b8c\u6210\u540e\uff0cquantum_core \u5c06\u5728\u542f\u52a8\u65f6\u81ea\u52a8\u68c0\u6d4b\u5230 CuPy \u5e76\u4f18\u5148\u4f7f\u7528GPU\u540e\u7aef\u3002\r\n\r\n\ud83d\ude80 \u5feb\u901f\u4e0a\u624b\r\n\r\n\u4e0b\u9762\u662f\u4e00\u4e2a\u521b\u5efa\u8d1d\u5c14\u7ea0\u7f20\u6001 |\u03a6+\u27e9 = (|00\u27e9 + |11\u27e9)/\u221a2 \u5e76\u9a8c\u8bc1\u5176\u6982\u7387\u5206\u5e03\u7684\u7b80\u5355\u793a\u4f8b\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\nimport quantum_core as nq\r\nimport math\r\n\r\n# 1. \u521b\u5efa\u4e00\u4e2a\u5305\u542b2\u4e2a\u91cf\u5b50\u6bd4\u7279\u7684\u521d\u59cb\u91cf\u5b50\u6001 |00\u27e9\r\n# \u8fd9\u662f\u4e00\u4e2a\u60f0\u6027\u5bf9\u8c61\uff0c\u6b64\u65f6\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u5927\u89c4\u6a21\u5185\u5b58\u5206\u914d\u3002\r\nprint(\"--- \u521b\u5efa\u521d\u59cb\u91cf\u5b50\u6001 ---\")\r\ninitial_state = nq.create_quantum_state(num_qubits=2)\r\nprint(f\"\u521d\u59cb\u6001: {initial_state.num_qubits} qubits, \u6a21\u5f0f: {initial_state._simulation_mode}\")\r\n\r\n# 2. \u6784\u5efa\u4e00\u4e2a\u7528\u4e8e\u5236\u5907\u8d1d\u5c14\u6001\u7684\u91cf\u5b50\u7ebf\u8def\r\nprint(\"\\n--- \u6784\u5efa\u91cf\u5b50\u7ebf\u8def ---\")\r\nbell_circuit = nq.QuantumCircuit(num_qubits=2, description=\"Bell State Preparation\")\r\nbell_circuit.h(0) # \u5728 q0 \u4e0a\u5e94\u7528 Hadamard \u95e8\r\nbell_circuit.cnot(0, 1) # \u5728 (q0, q1) \u4e0a\u5e94\u7528 CNOT \u95e8\r\nprint(bell_circuit)\r\n\r\n# 3. \u5728\u521d\u59cb\u6001\u4e0a\u6267\u884c\u7ebf\u8def\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6f14\u5316\u540e\u72b6\u6001\r\n# \u6b64\u64cd\u4f5c\u9075\u5faa\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e0d\u53ef\u53d8\u6027\u539f\u5219\uff0c\u539f\u59cb\u7684 initial_state \u4e0d\u4f1a\u88ab\u4fee\u6539\u3002\r\n# \u6b64\u65f6\uff0c\u8ba1\u7b97\u4ecd\u7136\u662f\u60f0\u6027\u7684\u3002\r\nprint(\"\\n--- \u6267\u884c\u7ebf\u8def (\u60f0\u6027) ---\")\r\nfinal_state = nq.run_circuit_on_state(initial_state, bell_circuit)\r\nprint(f\"\u6700\u7ec8\u6001: {final_state.num_qubits} qubits, \u6a21\u5f0f: {final_state._simulation_mode}\")\r\nprint(f\"\u6700\u7ec8\u6001\u5185\u90e8\u7ebf\u8def\u6307\u4ee4\u6570: {len(final_state.circuit.instructions)}\")\r\n\r\n# 4. \u83b7\u53d6\u6d4b\u91cf\u6982\u7387\u5206\u5e03\r\n# \u8fd9\u662f\u7b2c\u4e00\u4e2a\u9700\u8981\u5b9e\u9645\u8ba1\u7b97\u7ed3\u679c\u7684\u64cd\u4f5c\uff0c\u5b83\u5c06\u89e6\u53d1\u5185\u90e8\u7684\u201c\u5c55\u5f00\u8ba1\u7b97\u201d\u3002\r\nprint(\"\\n--- \u83b7\u53d6\u7ed3\u679c (\u89e6\u53d1\u8ba1\u7b97) ---\")\r\nprobabilities = nq.get_measurement_probabilities(final_state)\r\n\r\n# 5. \u9a8c\u8bc1\u7ed3\u679c\r\nprint(f\"\\n\u6d4b\u91cf\u6982\u7387: {probabilities}\")\r\nexpected_probs = [0.5, 0.0, 0.0, 0.5]\r\nassert all(math.isclose(p_actual, p_expected, abs_tol=1e-9) \r\n for p_actual, p_expected in zip(probabilities, expected_probs))\r\nprint(\"\u2705 \u9a8c\u8bc1\u6210\u529f\uff01\u6700\u7ec8\u6001\u4e3a\u8d1d\u5c14\u6001 |\u03a6+\u27e9\u3002\")\r\n\u6838\u5fc3\u6982\u5ff5\u4e0e\u67b6\u6784\r\n\ud83e\udde0 \u60f0\u6027\u6c42\u503c\u4e0e\u53cc\u6a21\u5f0f\u5f15\u64ce\r\n\r\n\u8fd9\u662f\u672c\u5e93\u7684\u6838\u5fc3\u8bbe\u8ba1\u3002\r\n\r\n\u60f0\u6027\u6c42\u503c: \u5728 statevector \u6a21\u5f0f\u4e0b\uff0c\u5bf9 QuantumState \u5bf9\u8c61\u5e94\u7528\u95e8\u64cd\u4f5c\uff08\u5982 state.h(0)) \u4e0d\u4f1a\u7acb\u5373\u8ba1\u7b97\u65b0\u7684\u6001\u77e2\u91cf\u3002\u76f8\u53cd\uff0c\u5b83\u4ec5\u5c06\u6307\u4ee4\u8bb0\u5f55\u5230\u5185\u90e8\u7684 QuantumCircuit \u4e2d\u3002\u8fd9\u4f7f\u5f97\u6784\u5efa\u5305\u542b\u6570\u5343\u4e2a\u95e8\u7684\u590d\u6742\u7ebf\u8def\u51e0\u4e4e\u662f\u96f6\u6210\u672c\u7684\u3002\u53ea\u6709\u5f53\u60a8\u8bf7\u6c42\u4e00\u4e2a\u9700\u8981\u5b8c\u6574\u72b6\u6001\u7684\u7ed3\u679c\u65f6\uff08\u4f8b\u5982 get_measurement_probabilities()\uff09\uff0c\u7cfb\u7edf\u624d\u4f1a\u4e00\u6b21\u6027\u5730\u3001\u9ad8\u6548\u5730\u6267\u884c\u6240\u6709\u7d2f\u79ef\u7684\u6307\u4ee4\u6765\u8ba1\u7b97\u6700\u7ec8\u7684\u6001\u77e2\u91cf\u3002\r\n\r\n\u53cc\u6a21\u5f0f\u5f15\u64ce:\r\n\r\nstatevector \u6a21\u5f0f: \u9ed8\u8ba4\u6a21\u5f0f\uff0c\u4f7f\u7528\u4e00\u4e2a (2^N,) \u7684\u590d\u6570\u5411\u91cf\u6765\u8868\u793a\u7eaf\u6001\u3002\u8ba1\u7b97\u6548\u7387\u9ad8\uff0c\u5185\u5b58\u5360\u7528\u76f8\u5bf9\u8f83\u5c0f\u3002\r\n\r\ndensity_matrix \u6a21\u5f0f: \u5f53\u60a8\u5e94\u7528\u4e00\u4e2a\u975e\u76f8\u5e72\u566a\u58f0\u901a\u9053\uff08\u4f8b\u5982 apply_quantum_channel\uff09\u65f6\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u5c06\u6001\u77e2\u91cf |\u03c8\u27e9 \u8f6c\u6362\u4e3a\u5bc6\u5ea6\u77e9\u9635 \u03c1 = |\u03c8\u27e9\u27e8\u03c8|\uff0c\u5e76\u5c06\u6a21\u5f0f\u4e0d\u53ef\u9006\u5730\u5207\u6362\u4e3a density_matrix\u3002\u6b64\u540e\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u5c06\u5728\u4e00\u4e2a (2^N, 2^N) \u7684\u5bc6\u5ea6\u77e9\u9635\u4e0a\u8fdb\u884c\uff0c\u8fd9\u867d\u7136\u8ba1\u7b97\u6210\u672c\u66f4\u9ad8\uff0c\u4f46\u80fd\u591f\u7cbe\u786e\u5730\u6a21\u62df\u6df7\u5408\u6001\u548c\u9000\u76f8\u5e72\u8fc7\u7a0b\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\n# \u6f14\u793a\u6a21\u5f0f\u5207\u6362\r\nstate = nq.create_quantum_state(1)\r\nprint(f\"\u521d\u59cb\u6a21\u5f0f: {state._simulation_mode}\") # -> statevector\r\n\r\n# \u5e94\u7528\u4e00\u4e2a\u975e\u76f8\u5e72\u566a\u58f0\u901a\u9053\r\nstate.apply_quantum_channel(\r\n channel_type='depolarizing', \r\n target_qubits=[0], \r\n params={'probability': 0.1}\r\n)\r\n\r\nprint(f\"\u5e94\u7528\u566a\u58f0\u540e\u7684\u6a21\u5f0f: {state._simulation_mode}\") # -> density_matrix\r\n\ud83c\udfdb\ufe0f \u91cf\u5b50\u7b97\u5b50\u5e93 (QuantumOperatorLibrary)\r\n\r\nQuantumOperatorLibrary \u662f\u672c\u5e93\u7684\u57fa\u77f3\u3002\u5b83\u662f\u4e00\u4e2a\u4e2d\u592e\u6ce8\u518c\u4e2d\u5fc3\uff0c\u5b58\u50a8\u4e86\u6240\u6709\u5185\u7f6e\u91cf\u5b50\u95e8\u7684\u5b8c\u6574\u5b9a\u4e49 (OperatorDefinition)\u3002\u6bcf\u4e2a\u5b9a\u4e49\u90fd\u5305\u542b\uff1a\r\n\r\n\u6570\u5b66\u8868\u793a: \u56fa\u5b9a\u7684\u9149\u77e9\u9635\uff08\u5982 X \u95e8\uff09\u6216\u7528\u4e8e\u751f\u6210\u9149\u77e9\u9635\u7684\u51fd\u6570\uff08\u5982 RX(\u03b8)\uff09\u3002\r\n\r\n\u5206\u89e3\u89c4\u5219: \u5bf9\u4e8e\u590d\u5408\u95e8\uff08\u5982 SWAP\uff09\uff0c\u5b9a\u4e49\u4e86\u5982\u4f55\u5c06\u5176\u5206\u89e3\u4e3a\u66f4\u57fa\u7840\u7684\u95e8\u3002\r\n\r\n\u4f18\u5316\u5185\u6838: \u6307\u5411 _StateVectorEntity \u6216 _DensityMatrixEntity \u5185\u90e8\u4e13\u95e8\u4f18\u5316\u7684\u3001\u65e0\u9700\u6784\u5efa\u5168\u5c40\u77e9\u9635\u5373\u53ef\u6267\u884c\u64cd\u4f5c\u7684\u9ad8\u6027\u80fd\u51fd\u6570\u3002\r\n\r\n\u8fd9\u79cd\u8bbe\u8ba1\u786e\u4fdd\u4e86\u5168\u7cfb\u7edf\u5bf9\u95e8\u64cd\u4f5c\u7684\u89e3\u91ca\u548c\u6267\u884c\u662f\u7edf\u4e00\u3001\u53ef\u9a8c\u8bc1\u4e14\u9ad8\u6548\u7684\u3002\r\n\r\n\u26a1\ufe0f \u5e76\u884c\u8ba1\u7b97\r\n\r\n\u672c\u5e93\u5185\u7f6e\u4e86\u57fa\u4e8e multiprocessing \u7684CPU\u5e76\u884c\u8ba1\u7b97\u6846\u67b6\uff0c\u53ef\u7528\u4e8e\u52a0\u901f\u5927\u89c4\u6a21\uff08\u901a\u5e38 > 12\u91cf\u5b50\u6bd4\u7279\uff09\u7684\u7eafPython\u6a21\u62df\u3002\r\n\r\n\u542f\u7528\u5e76\u884c\u8ba1\u7b97:\r\n\u60a8\u5fc5\u987b\u5728\u811a\u672c\u7684\u4e3b\u6267\u884c\u5757 (if __name__ == '__main__':) \u4e2d\u663e\u5f0f\u542f\u7528\u5e76\u884c\u6a21\u5f0f\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\nimport quantum_core as nq\r\nimport sys\r\n\r\nif __name__ == '__main__':\r\n # \u542f\u7528\u5e76\u884c\u8ba1\u7b97\uff0c\u81ea\u52a8\u68c0\u6d4bCPU\u6838\u5fc3\u6570\r\n nq.enable_parallelism()\r\n\r\n try:\r\n # \u5728\u8fd9\u91cc\u8fd0\u884c\u60a8\u7684\u5927\u89c4\u6a21\u6a21\u62df\r\n state = nq.create_quantum_state(14)\r\n state.h(0) # \u8fd9\u4e2a\u64cd\u4f5c\u5728\u5c55\u5f00\u65f6\u53ef\u80fd\u4f1a\u88ab\u5e76\u884c\u5316\r\n \r\n # ... \u66f4\u591a\u64cd\u4f5c ...\r\n \r\n # \u89e6\u53d1\u8ba1\u7b97\r\n probs = nq.get_measurement_probabilities(state)\r\n print(\"\u5927\u89c4\u6a21\u6a21\u62df\u5b8c\u6210\u3002\")\r\n\r\n finally:\r\n # \u5728\u7a0b\u5e8f\u7ed3\u675f\u524d\u4f18\u96c5\u5730\u5173\u95ed\u5e76\u884c\u6c60\r\n nq.disable_parallelism()\r\n\ud83d\udcda \u516c\u5171 API \u53c2\u8003\r\n\r\n\u4ee5\u4e0b\u662f quantum_core \u63d0\u4f9b\u7684\u6838\u5fc3\u516c\u5171API\u51fd\u6570\u3002\r\n\r\ncreate_quantum_state(num_qubits)\r\n\r\n\u521b\u5efa\u4e00\u4e2a\u5904\u4e8e |0...0\u27e9 \u6001\u7684\u521d\u59cb\u60f0\u6027\u91cf\u5b50\u6001\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\r\nnum_qubits\tint\t\u8981\u521b\u5efa\u7684\u91cf\u5b50\u6001\u7684\u6bd4\u7279\u6570\u3002\r\n\r\n\u8fd4\u56de: \u4e00\u4e2a\u65b0\u7684 QuantumState \u5b9e\u4f8b\u3002\r\n\r\nrun_circuit_on_state(state, circuit, noise_model, topology)\r\n\r\n\u5728\u4e00\u4e2a\u91cf\u5b50\u6001\u4e0a\u6267\u884c\u4e00\u4e2a\u91cf\u5b50\u7ebf\u8def\uff0c\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u6f14\u5316\u540e\u72b6\u6001\u7684\u65b0\u91cf\u5b50\u6001\u5b9e\u4f8b\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\t\u9ed8\u8ba4\u503c\r\nstate\tQuantumState\t\u521d\u59cb\u91cf\u5b50\u6001\u3002\t\r\ncircuit\tQuantumCircuit\t\u8981\u6267\u884c\u7684\u91cf\u5b50\u7ebf\u8def\u3002\t\r\nnoise_model\tOptional[NoiseModel]\t\u53ef\u9009\u7684\u566a\u58f0\u6a21\u578b\u3002\tNone\r\ntopology\tOptional[Dict]\t\u53ef\u9009\u7684\u786c\u4ef6\u62d3\u6251\u56fe\u3002\tNone\r\n\r\n\u8fd4\u56de: \u6f14\u5316\u540e\u7684\u65b0 QuantumState \u5b9e\u4f8b\u3002\r\n\r\nget_state_data(state, format)\r\n\r\n\u5b89\u5168\u5730\u4ece\u4e00\u4e2a QuantumState \u5bf9\u8c61\u4e2d\u63d0\u53d6\u5e95\u5c42\u7684\u72b6\u6001\u6570\u636e\uff08\u6001\u77e2\u91cf\u6216\u5bc6\u5ea6\u77e9\u9635\uff09\u3002\u6b64\u51fd\u6570\u4f1a\u6839\u636e\u9700\u8981\u89e6\u53d1\u5c55\u5f00\u8ba1\u7b97\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\t\u9ed8\u8ba4\u503c\r\nstate\tQuantumState\t\u8981\u63d0\u53d6\u6570\u636e\u7684\u91cf\u5b50\u6001\u3002\t\r\nformat\tstr\t\u8fd4\u56de\u6570\u636e\u7684\u683c\u5f0f\u3002\u76ee\u524d\u4ec5\u652f\u6301 'python_list'\u3002\t'python_list'\r\n\r\n\u8fd4\u56de: List[complex] (\u6001\u77e2\u91cf) \u6216 List[List[complex]] (\u5bc6\u5ea6\u77e9\u9635)\u3002\r\n\r\nget_measurement_probabilities(state)\r\n\r\n\u83b7\u53d6\u4e00\u4e2a\u91cf\u5b50\u6001\u6240\u6709\u8ba1\u7b97\u57fa\u7684\u6d4b\u91cf\u6982\u7387\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\r\nstate\tQuantumState\t\u8981\u5206\u6790\u7684\u91cf\u5b50\u6001\u3002\r\n\r\n\u8fd4\u56de: List[float]\uff0c\u4e00\u4e2a\u5305\u542b\u6240\u6709\u6d4b\u91cf\u6982\u7387\u7684\u5217\u8868\u3002\r\n\r\ncalculate_hamiltonian_expectation_value(state, hamiltonian)\r\n\r\n\u8ba1\u7b97\u7ed9\u5b9a\u54c8\u5bc6\u987f\u91cf\u5728\u5f53\u524d\u91cf\u5b50\u6001\u4e0b\u7684\u671f\u671b\u503c\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\r\nstate\tQuantumState\t\u8981\u5206\u6790\u7684\u91cf\u5b50\u6001\u3002\r\nhamiltonian\tList[PauliString]\t\u8868\u793a\u54c8\u5bc6\u987f\u91cf\u7684 PauliString \u5217\u8868\u3002\r\n\r\n\u8fd4\u56de: float\uff0c\u54c8\u5bc6\u987f\u91cf\u7684\u671f\u671b\u503c\u3002\r\n\r\nget_effective_unitary(circuit, backend_choice)\r\n\r\n\u8ba1\u7b97\u7ed9\u5b9a QuantumCircuit \u5bf9\u5e94\u7684\u5168\u5c40\u6709\u6548\u9149\u77e9\u9635\u3002\r\n\r\n\u53c2\u6570\t\u7c7b\u578b\t\u63cf\u8ff0\t\u9ed8\u8ba4\u503c\r\ncircuit\tQuantumCircuit\t\u8981\u8ba1\u7b97\u9149\u77e9\u9635\u7684\u7ebf\u8def\u3002\t\r\nbackend_choice\tstr\t\u7528\u4e8e\u8ba1\u7b97\u7684\u540e\u7aef ('auto', 'pure_python', 'cupy')\u3002\t'auto'\r\n\r\n\u8fd4\u56de: \u9149\u77e9\u9635 (Python list \u6216 cupy.ndarray)\u3002\r\n\r\n\ud83d\udd2c \u9ad8\u7ea7\u7528\u6cd5\r\n\u4f7f\u7528\u566a\u58f0\u6a21\u578b\r\n\r\n\u672c\u5e93\u652f\u6301\u901a\u8fc7 NoiseModel \u5b50\u7c7b\u6765\u6a21\u62df\u5e26\u566a\u91cf\u5b50\u8ba1\u7b97\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\n# 1. \u5b9a\u4e49\u4e00\u4e2a\u6a21\u62df\u771f\u5b9e\u786c\u4ef6\u566a\u58f0\u7684\u6821\u51c6\u6570\u636e\r\nfake_calib = {\r\n 'qubits': {\r\n 0: {'T1': 50e-6, 'T2': 70e-6, 'readout_error': 0.01},\r\n },\r\n 'gates': {\r\n 'h': {'duration': 50e-9, 'error_rate': 0.001}, # 50ns H-gate, 0.1% \u53bb\u6781\u5316\u9519\u8bef\r\n }\r\n}\r\n\r\n# 2. \u5b9e\u4f8b\u5316\u9884\u7f6e\u7684\u786c\u4ef6\u566a\u58f0\u6a21\u578b\r\nnoise_model = nq.PrebuiltNoiseModels.HardwareBackend(fake_calib)\r\n\r\n# 3. \u521b\u5efa\u521d\u59cb\u6001\u548c\u7ebf\u8def\r\nstate = nq.create_quantum_state(1)\r\ncircuit = nq.QuantumCircuit(1)\r\ncircuit.h(0)\r\n\r\n# 4. \u5728\u8fd0\u884c\u7ebf\u8def\u65f6\u4f20\u5165\u566a\u58f0\u6a21\u578b\r\nnoisy_state = nq.run_circuit_on_state(state, circuit, noise_model=noise_model)\r\n\r\n# 5. \u9a8c\u8bc1\u72b6\u6001\u5df2\u53d8\u4e3a\u6df7\u5408\u6001\r\nassert noisy_state._simulation_mode == 'density_matrix'\r\npurity = nq.calculate_state_purity(noisy_state) # \u5047\u8bbe\u6709\u8fd9\u6837\u4e00\u4e2aAPI\r\nprint(f\"\u5e26\u566a\u72b6\u6001\u7684\u7eaf\u5ea6: {purity:.4f}\") # \u7eaf\u5ea6\u5e94\u5c0f\u4e8e1\r\n\u7b97\u6cd5\u6784\u5efa\u5668\r\n\r\n\u4f7f\u7528 AlgorithmBuilders \u53ef\u4ee5\u5feb\u901f\u751f\u6210\u590d\u6742\u7b97\u6cd5\u7684\u7ebf\u8def\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\n# \u6784\u5efa\u4e00\u4e2a3\u6bd4\u7279\u7684\u91cf\u5b50\u5085\u91cc\u53f6\u53d8\u6362(QFT)\u7ebf\u8def\r\nqft_circuit = nq.AlgorithmBuilders.build_qft_circuit(num_qubits=3)\r\n\r\nprint(\"--- 3-qubit QFT Circuit ---\")\r\nprint(qft_circuit)\r\n\r\n# \u6784\u5efa\u4e00\u4e2a\u7528\u4e8eVQA\u7684\u786c\u4ef6\u9ad8\u6548\u62df\u8bbe\r\nparams = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]\r\nansatz = nq.AlgorithmBuilders.build_hardware_efficient_ansatz(\r\n num_qubits=2,\r\n depth=2,\r\n parameters=params,\r\n entanglement_type='circular'\r\n)\r\nprint(\"\\n--- Hardware-Efficient Ansatz ---\")\r\nprint(ansatz)\r\n\u914d\u7f6e\u6838\u5fc3\u5e93\r\n\r\n\u60a8\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u901a\u8fc7 configure_quantum_core \u51fd\u6570\u8c03\u6574\u5e93\u7684\u884c\u4e3a\u3002\r\n\r\ncode\r\nPython\r\ndownload\r\ncontent_copy\r\nexpand_less\r\n# \u5f3a\u5236\u4f7f\u7528\u7eafPython\u540e\u7aef\uff0c\u5e76\u8bbe\u7f6e\u6700\u5927\u91cf\u5b50\u6bd4\u7279\u6570\u4e0a\u9650\r\nnq.configure_quantum_core({\r\n \"BACKEND_CHOICE\": \"pure_python\",\r\n \"MAX_QUBITS\": 10,\r\n \"MAX_SYSTEM_RAM_GB_LIMIT\": 16 # \u624b\u52a8\u8bbe\u7f6e\u5185\u5b58\u4e0a\u9650\u4e3a16GB\r\n})\r\n\ud83e\uddea \u6d4b\u8bd5\r\n\r\n\u672c\u6587\u4ef6\u5305\u542b\u4e00\u4e2a\u5168\u9762\u7684\u81ea\u6d4b\u8bd5\u5957\u4ef6\u3002\u8981\u8fd0\u884c\u6d4b\u8bd5\uff0c\u53ea\u9700\u76f4\u63a5\u6267\u884c\u6b64\u811a\u672c\uff1a\r\n\r\ncode\r\nBash\r\ndownload\r\ncontent_copy\r\nexpand_less\r\npython quantum_core.py\r\n\r\n\u6d4b\u8bd5\u5957\u4ef6\u5c06\u81ea\u52a8\u5728\u53ef\u7528\u7684\u540e\u7aef\uff08Pure Python \u548c/\u6216 CuPy\uff09\u4e0a\u8fd0\u884c\uff0c\u5e76\u9a8c\u8bc1\u6240\u6709\u6838\u5fc3\u529f\u80fd\u3001\u7b97\u6cd5\u6784\u5efa\u5668\u548c\u566a\u58f0\u6a21\u578b\u7684\u6b63\u786e\u6027\u3002\u6d4b\u8bd5\u91c7\u7528\u201c\u5feb\u901f\u5931\u8d25\u201d\u7b56\u7565\uff0c\u4efb\u4f55\u4e00\u4e2a\u6d4b\u8bd5\u5931\u8d25\u90fd\u4f1a\u7acb\u5373\u4e2d\u6b62\u6574\u4e2a\u6d4b\u8bd5\u6d41\u7a0b\u3002\r\n\r\n\ud83d\udcdc \u7248\u672c\u5386\u53f2\r\nv1.5.0 - \u62d3\u6251\u611f\u77e5API\u7248\r\n\r\nAPI \u5347\u7ea7: \u516c\u5171 API run_circuit_on_state \u548c\u76f8\u5173\u5185\u90e8\u65b9\u6cd5\u73b0\u5728\u63a5\u6536 topology \u53c2\u6570\uff0c\u4ee5\u5b9e\u73b0\u5bf9\u91cf\u5b50\u5b50\u7a0b\u5e8f\u7684\u62d3\u6251\u611f\u77e5\u4f18\u5316\u3002\r\n\r\n\u67b6\u6784\u91cd\u6784: \u5f7b\u5e95\u79fb\u9664\u4e86 get_effective_unitary \u4e2d\u7684\u7334\u5b50\u8865\u4e01\uff0c\u91c7\u7528\u5185\u7f6e\u7684\u6a21\u5f0f\u611f\u77e5\u6267\u884c\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\r\n\r\n\u566a\u58f0\u6a21\u578b\u6269\u5c55: \u5f15\u5165\u4e86 CorrelatedNoise \u6a21\u578b\uff0c\u7528\u4e8e\u6a21\u62df\u4e32\u6270\u7b49\u975e\u5c40\u90e8\u6548\u5e94\u3002\r\n\r\n\u903b\u8f91\u4fee\u590d: \u4fee\u6b63\u4e86\u76f8\u5e72\u566a\u58f0\u7684\u5e94\u7528\u903b\u8f91\uff0c\u786e\u4fdd\u5176\u53ea\u4f5c\u7528\u4e8e\u65cb\u8f6c\u95e8\u7684\u65cb\u8f6c\u89d2\u5ea6\u3002\r\n\r\n\u5185\u6838\u8c03\u5ea6\u4f18\u5316: _StateVectorEntity \u5185\u90e8\u6539\u7528\u8c03\u5ea6\u5b57\u5178\u6765\u7ba1\u7406\u4f18\u5316\u5185\u6838\uff0c\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002\r\n\r\n\ud83d\udc64 \u4f5c\u8005\r\n\r\n\u8df3\u821e\u7684\u706b\u516c\u5b50\r\n\r\n\ud83d\udcc4 \u8bb8\u53ef\u8bc1\r\n\r\n\u672c\u9879\u76ee\u91c7\u7528 MIT \u8bb8\u53ef\u8bc1\u3002\u8be6\u60c5\u8bf7\u89c1 LICENSE \u6587\u4ef6\u3002\r\n",
"bugtrack_url": null,
"license": "MIT License\r\n \r\n Copyright (c) 2025 uraniceye\r\n \r\n Permission is hereby granted, free of charge, to any person obtaining a copy\r\n of this software and associated documentation files (the \"Software\"), to deal\r\n in the Software without restriction, including without limitation the rights\r\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n copies of the Software, and to permit persons to whom the Software is\r\n furnished to do so, subject to the following conditions:\r\n \r\n The above copyright notice and this permission notice shall be included in all\r\n copies or substantial portions of the Software.\r\n \r\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\n SOFTWARE.\r\n ",
"summary": "\u4e00\u4e2a\u9ad8\u6027\u80fd\u3001\u53cc\u6a21\u5f0f\u3001\u62d3\u6251\u611f\u77e5\u7684\u91cf\u5b50\u8ba1\u7b97\u6a21\u62df\u6838\u5fc3\u5e93\u3002",
"version": "1.5.0",
"project_urls": {
"Bug Tracker": "https://github.com/uraniceye/nexus-quantum-core/issues",
"Homepage": "https://github.com/uraniceye/nexus-quantum-core"
},
"split_keywords": [
"quantum",
" quantum computing",
" simulator",
" quantum circuit",
" density matrix",
" statevector"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b3536d43c2b0b38c7f8e2a8fa0a4a1de8da380596dddecc9ef85af620b74b8ae",
"md5": "2380e2d2309345a1787e5c89ec19d017",
"sha256": "14b715423434aedbe05588a972c9dbf9264890c5067e63145e6ffa37b5356aa7"
},
"downloads": -1,
"filename": "nexus_quantum_core-1.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2380e2d2309345a1787e5c89ec19d017",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 217597,
"upload_time": "2025-11-02T01:13:57",
"upload_time_iso_8601": "2025-11-02T01:13:57.403218Z",
"url": "https://files.pythonhosted.org/packages/b3/53/6d43c2b0b38c7f8e2a8fa0a4a1de8da380596dddecc9ef85af620b74b8ae/nexus_quantum_core-1.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "715173cc84c3516c2a64e410b9d84a1f4376850202b064433f9e149004a7eadc",
"md5": "484a425a4a48c3ea2a7481f4b862f1bc",
"sha256": "c9930070466512a070fcc58ca867d0e84ac1c827590b701ceedf50c5b216cccd"
},
"downloads": -1,
"filename": "nexus_quantum_core-1.5.0.tar.gz",
"has_sig": false,
"md5_digest": "484a425a4a48c3ea2a7481f4b862f1bc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 221039,
"upload_time": "2025-11-02T01:13:59",
"upload_time_iso_8601": "2025-11-02T01:13:59.153002Z",
"url": "https://files.pythonhosted.org/packages/71/51/73cc84c3516c2a64e410b9d84a1f4376850202b064433f9e149004a7eadc/nexus_quantum_core-1.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-11-02 01:13:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "uraniceye",
"github_project": "nexus-quantum-core",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "nexus-quantum-core"
}