为什么使用 HLQuantum?
🔁
一次编写,随处运行
同一份电路代码可在 6 种受支持后端中的任意一种上运行——无需转换,无需重写。
⚡
内置 GPU 加速
将电路透明地路由到 NVIDIA GPU 模拟器,为大型电路带来大幅加速。
🛡️
误差缓解
内置 ZNE、读出缓解等技术——只需一个参数即可应用。
🧠
内置算法
QFT、Grover、VQE、QAOA、Bernstein-Vazirani——开箱即用,与后端无关。
🔀
异步多后端
在多个后端上同时运行实验,用于基准测试与结果验证。
🤖
AI / MCP 集成
对 Model Context Protocol 的支持使 AI 智能体能够编排量子实验。
受支持的后端
| 后端 | 框架 | 安装 | 真实 QPU |
|---|---|---|---|
| qiskit | IBM Qiskit | pip install hlquantum[qiskit] | 是 |
| cirq | Google Cirq | pip install hlquantum[cirq] | 仅模拟器 |
| pennylane | Xanadu PennyLane | pip install hlquantum[pennylane] | 仅模拟器 |
| braket | Amazon Braket | pip install hlquantum[braket] | 是 |
| cudaq | NVIDIA CUDA-Q | pip install hlquantum[cudaq] | 仅模拟器 |
| ionq | IonQ (via Qiskit) | pip install hlquantum[ionq] | 是 |
快速入门
1. 安装
terminal
# Install with your preferred backend pip install hlquantum[qiskit] # IBM Qiskit backend pip install hlquantum[cirq] # Google Cirq backend pip install hlquantum[pennylane] # PennyLane backend pip install hlquantum[braket] # Amazon Braket backend pip install hlquantum[cudaq] # NVIDIA CUDA-Q backend pip install hlquantum[ionq] # IonQ backend # Or install multiple at once pip install "hlquantum[qiskit,cirq,cudaq]"2. 创建你的第一个电路
hello_quantum.py
import hlquantum as hlq # Create a 2-qubit circuit qc = hlq.Circuit(2) # Apply gates using the fluent API qc.h(0).cx(0, 1).measure_all() # Run on the default backend (auto-detects installed SDK) result = hlq.run(qc, shots=1000) print(result) # {'00': 507, '11': 493}3. 用一个参数切换后端
switch_backends.py
import hlquantum as hlq qc = hlq.Circuit(2) qc.h(0).cx(0, 1).measure_all() # The SAME circuit, on DIFFERENT backends — zero code changes r1 = hlq.run(qc, shots=1000, backend="qiskit") # Qiskit Aer r2 = hlq.run(qc, shots=1000, backend="cirq") # Google Cirq r3 = hlq.run(qc, shots=1000, backend="pennylane") # PennyLane r4 = hlq.run(qc, shots=1000, backend="cudaq") # NVIDIA GPU r5 = hlq.run(qc, shots=1000, backend="braket") # Amazon Braket r6 = hlq.run(qc, shots=1000, backend="ionq") # IonQ for name, result in zip(["Qiskit","Cirq","PennyLane","CUDA-Q","Braket","IonQ"], [r1, r2, r3, r4, r5, r6]): print(f"{name}: {result}")@kernel 装饰器
使用 @hlq.kernel 装饰器,你可以将量子逻辑写成标准的 Python 函数。HLQuantum 会自动在所选后端上编译并执行它们。
kernel_example.py
import hlquantum as hlq @hlq.kernel def ghz_state(n: int): """Create an n-qubit GHZ state.""" qubits = hlq.qvector(n) hlq.h(qubits[0]) for i in range(n - 1): hlq.cx(qubits[i], qubits[i + 1]) hlq.measure_all(qubits) # Run the kernel result = hlq.run(ghz_state, args=(5,), shots=1000) print(result) # {'00000': ~500, '11111': ~500} # Works on any backend result_gpu = hlq.run( ghz_state, args=(20,), # 20-qubit GHZ! shots=1000, backend="cudaq" # GPU acceleration )内置量子算法
HLQuantum 包含常见量子算法的开箱即用实现,可在任意后端上运行。
量子傅里叶变换 (QFT)相位估计Shor 算法
离散傅里叶变换的量子模拟。作为子例程用于许多算法,包括 Shor 的因数分解算法。
python
import hlquantum as hlq
from hlquantum.algorithms import QFT
# Create a QFT circuit for 4 qubits
qft_circuit = QFT(n_qubits=4)
result = hlq.run(qft_circuit, shots=1000)
print(result)Grover 搜索算法搜索二次加速
为无结构搜索提供二次加速。以 √N 步而非 N 步找到被标记的项。
python
import hlquantum as hlq
from hlquantum.algorithms import Grover
# Search for item "101" in a 3-qubit space
grover = Grover(oracle_string="101")
result = hlq.run(grover.circuit, shots=2000)
# The marked state should have high probability
print(result) # {'101': ~1800, others: ~200}VQE — 变分量子本征求解器化学优化
求解哈密顿量的基态能量。在 NISQ 设备上进行量子化学的关键算法。
python
import hlquantum as hlq
from hlquantum.algorithms import VQE
from hlquantum.operators import PauliSum
# Define Hamiltonian
H = PauliSum.from_list([
("ZZ", -1.052), ("IZ", 0.398),
("ZI", -0.398), ("XX", 0.181),
])
vqe = VQE(hamiltonian=H, n_qubits=2, ansatz="TwoLocal", reps=2)
energy, params = vqe.run(backend="qiskit", max_iter=200)
print(f"Ground state energy: {energy:.6f}")QAOA — 量子近似优化组合优化
用于组合优化问题的近似算法,如 MaxCut、图划分和调度。
python
import hlquantum as hlq
from hlquantum.algorithms import QAOA
import networkx as nx
# Define a MaxCut problem
graph = nx.Graph([(0,1),(1,2),(2,3),(3,0),(0,2)])
qaoa = QAOA(problem="maxcut", graph=graph, p=2)
result = qaoa.run(backend="pennylane", shots=2000)
print(f"Best cut: {result.best_solution}")
print(f"Cut value: {result.best_value}")量子 ML 层与流水线
HLQuantum 包含受 ML 启发的量子电路组合方式——将变分拟设构建为分层模型,类似于 PyTorch 的 nn.Sequential。
hl_pipeline.py
import hlquantum as hlq from hlquantum.layers import RYLayer, EntanglingLayer, Sequential # Build a variational quantum model model = Sequential([ RYLayer(n_qubits=4), # Layer of RY rotations EntanglingLayer(n_qubits=4), # CNOT entangling layer RYLayer(n_qubits=4), # Another RY layer EntanglingLayer(n_qubits=4), ]) # Run the model (initializes random params) result = model.run(shots=1000, backend="pennylane") # Train the model (gradient-based) loss_history = model.fit( X_train, y_train, optimizer="adam", learning_rate=0.01, epochs=50 )异步多后端执行
在多个后端上同时运行同一份电路并比较结果。非常适合对噪声水平进行基准测试或跨平台验证结果。
async_run.py
import hlquantum as hlq import asyncio qc = hlq.Circuit(3) qc.h(0).cx(0, 1).cx(1, 2).measure_all() async def benchmark_backends(): tasks = { name: hlq.run_async(qc, shots=1000, backend=name) for name in ["qiskit", "cirq", "pennylane", "cudaq"] } results = {name: await task for name, task in tasks.items()} for name, result in results.items(): print(f"{name}: {result}") asyncio.run(benchmark_backends())误差缓解
HLQuantum 包含内置的误差缓解技术,用于真实硬件执行。
error_mitigation.py
import hlquantum as hlq from hlquantum.mitigation import ZNE, ReadoutMitigation qc = hlq.Circuit(2) qc.h(0).cx(0, 1).measure_all() # Zero-Noise Extrapolation (ZNE) mitigated_result = hlq.run( qc, shots=2000, backend="qiskit", device="ibm_sherbrooke", # Real hardware mitigation=ZNE(noise_factors=[1, 2, 3]) ) # Readout error mitigation result_mit = hlq.run( qc, shots=2000, backend="qiskit", mitigation=ReadoutMitigation() ) print(f"Raw result: {hlq.run(qc, shots=2000)}") print(f"Mitigated result: {mitigated_result}")GPU 加速
gpu_accel.py
import hlquantum as hlq # Large circuit — 28 qubits qc = hlq.Circuit(28) for i in range(28): qc.h(i) for i in range(27): qc.cx(i, i + 1) qc.measure_all() # CPU simulation (may be slow for 28 qubits) result_cpu = hlq.run(qc, shots=100, backend="qiskit") # GPU simulation — orders of magnitude faster! result_gpu = hlq.run(qc, shots=100, backend="cudaq") # NVIDIA result_gpu2 = hlq.run(qc, shots=100, backend="pennylane", device="lightning.gpu") # lightning.gpu print(f"CPU: {result_cpu}") print(f"GPU (CUDA-Q): {result_gpu}")🤖
AI 驱动的量子计算 (MCP)
HLQuantum 包含对 Model Context Protocol (MCP) 的支持,使 AI 智能体能够自主构建、优化和执行量子电路。这开启了 AI 驱动量子算法发现的全新范式。
了解 HLQuantum 的 MCP 集成