关于 alterlab-cirq
alterlab-cirq 是使用 Google Quantum AI 的 Python 框架 Cirq 的技能,可用于构建、模拟、变换和运行量子电路,并指导研究人员开展噪声研究、实验及硬件执行。
主要功能
- 01构建和变换量子电路
- 02模拟状态向量与密度矩阵
- 03建模噪声并表征电路
- 04在支持的硬件上运行电路
- 05设计和分析量子实验
适用场景
- 01原型设计电路并进行参数扫描
- 02研究噪声对电路的影响
- 03为量子硬件准备电路
- 04开展基准测试和量子实验
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ alterlab-ieu/ alterlab-academic-skills --skill alterlab-cirq安装后在 Claude Code 中输入/alterlab-cirq确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
evals/evals.jsonreferences/building.mdexperiments.mdhardware.mdnoise.mdsimulation.mdtransformation.md详细介绍
Cirq - 使用 Python 进行量子计算
Cirq 是 Google Quantum AI 的开源框架,用于设计、模拟量子电路,并在量子计算机和模拟器上运行这些电路。
何时使用此技能
当用户希望执行以下操作时,使用此技能:
- 在 Cirq 中构建和模拟电路(状态向量、密度矩阵、参数扫描)
- 对 Google 硬件噪声建模,或在 Quantum Virtual Machine(
willow_pink、weber、rainbow)上运行 - 将 Cirq 电路提交到 Google Quantum Engine(经批准的合作伙伴)、IonQ、AQT、Pasqal 或 Azure Quantum
- 运行表征实验(随机基准测试、XEB)或 ReCirq 风格的研究
不适用的场景
| 场景 | 改用 |
|---|---|
| IBM Quantum 硬件、Qiskit Runtime 原语或 Qiskit 转译 | alterlab-qiskit |
| 使用自动微分训练梯度的量子机器学习/混合模型 | alterlab-pennylane |
| Lindblad 主方程、退相干或腔 QED 动力学(不涉及电路) | alterlab-qutip |
安装
uv pip install cirq # Python ≥ 3.11; current 1.7.x as of 2026-09
cirq 包是一个元包,会自动安装 cirq-core、cirq-google、
cirq-ionq、cirq-aqt、cirq-pasqal 和 cirq-web;如果只需轻量级模拟环境,请单独安装
cirq-core。可选扩展:
# OpenQASM import (cirq.contrib.qasm_import needs `ply`), quimb tensor-network tools
uv pip install "cirq-core[contrib]"
# Azure Quantum — its `cirq` extra pins cirq-core 1.6.x, so use a separate environment
uv pip install "azure-quantum[cirq]"
快速入门
基本电路
import cirq
import numpy as np
# Create qubits
q0, q1 = cirq.LineQubit.range(2)
# Build circuit
circuit = cirq.Circuit(
cirq.H(q0), # Hadamard on q0
cirq.CNOT(q0, q1), # CNOT with q0 control, q1 target
cirq.measure(q0, q1, key='result')
)
print(circuit)
# Simulate
simulator = cirq.Simulator()
result = simulator.run(circuit, repetitions=1000)
# Display results
print(result.histogram(key='result'))
参数化电路
import sympy
# Define symbolic parameter
theta = sympy.Symbol('theta')
# Create parameterized circuit
circuit = cirq.Circuit(
cirq.ry(theta)(q0),
cirq.measure(q0, key='m')
)
# Sweep over parameter values
sweep = cirq.Linspace('theta', start=0, stop=2*np.pi, length=20)
results = simulator.run_sweep(circuit, params=sweep, repetitions=1000)
# Process results
for params, result in zip(sweep, results):
theta_val = params['theta']
counts = result.histogram(key='m')
print(f"θ={theta_val:.2f}: {counts}")
核心功能
电路构建
有关构建量子电路的全面信息,包括量子比特、门、操作、自定义门和电路模式,请参阅:
- references/building.md - 电路构建完整指南
常见主题:
- 量子比特类型(GridQubit、LineQubit、NamedQubit)
- 单量子比特门和双量子比特门
- 参数化门和操作
- 自定义门分解
- 使用时刻组织电路
- 标准电路模式(Bell 态、GHZ、QFT)
- 导入/导出(OpenQASM、JSON)
- 使用量子比特和可观测量
模拟
有关量子电路模拟的详细信息,包括精确模拟、噪声模拟、参数扫描和 Quantum Virtual Machine,请参阅:
- references/simulation.md - 量子模拟完整指南
常见主题:
- 精确模拟(状态向量、密度矩阵)
- 采样和测量
- 参数扫描(单个和多个参数)
- 噪声模拟
- 状态直方图和可视化
- Quantum Virtual Machine(QVM)
- 期望值和可观测量
- 性能优化
电路变换
有关优化、编译和操作量子电路的信息,请参阅:
- references/transformation.md - 电路变换完整指南
常见主题:
- Transformer 框架
- 门分解
- 电路优化(合并门、移除 Z 门、丢弃可忽略的操作)
- 面向硬件的电路编译
- 量子比特路由和 SWAP 插入
- 自定义 Transformer
- 变换流水线
硬件集成
有关在不同提供商的真实量子硬件上运行电路的信息,请参阅:
- references/hardware.md - 硬件集成完整指南
支持的提供商:
- Google Quantum AI(cirq-google)- 面向经批准的合作伙伴的 Quantum Engine;所有人均可使用 Quantum Virtual Machine 对
willow_pink、weber、rainbow建模 - IonQ(cirq-ionq)- 离子阱量子计算机
- Azure Quantum(azure-quantum)- IonQ 和 Quantinuum 目标
- AQT(cirq-aqt)- Alpine Quantum Technologies(ARNICA API;工作区和资源 ID)
- Pasqal(cirq-pasqal)- 中性原子量子计算机
主题包括设备表示、量子比特选择、身份验证、作业管理和面向硬件的电路优化。
噪声建模
有关噪声建模、含噪模拟、表征和误差缓解的信息,请参阅:
- references/noise.md - 噪声建模完整指南
常见主题:
- 噪声通道(退极化、振幅阻尼、相位阻尼)
- 噪声模型(恒定、特定门、特定量子比特、热噪声)
- 向电路添加噪声
- 读出噪声
- 噪声表征(随机基准测试、XEB)
- 噪声可视化(热力图)
- 误差缓解技术
量子实验
有关实验设计、参数扫描、数据收集和 ReCirq 框架使用的信息,请参阅:
- references/experiments.md - 量子实验完整指南
常见主题:
- 实验设计模式
- 参数扫描和数据收集
- ReCirq 框架结构
- 常见算法(VQE、QAOA、QPE)
- 数据分析和可视化
- 统计分析和保真度估计
- 并行数据收集
常见模式
变分算法模板
import scipy.optimize
def variational_algorithm(ansatz, cost_function, initial_params):
"""Template for variational quantum algorithms."""
def objective(params):
circuit = ansatz(params)
simulator = cirq.Simulator()
result = simulator.simulate(circuit)
return cost_function(result)
# Optimize
result = scipy.optimize.minimize(
objective,
initial_params,
method='COBYLA'
)
return result
# Define ansatz
def my_ansatz(params):
q = cirq.LineQubit(0)
return cirq.Circuit(
cirq.ry(params[0])(q),
cirq.rz(params[1])(q)
)
# Define cost function
def my_cost(result):
state = result.final_state_vector
# Calculate cost based on state
return np.real(state[0])
# Run optimization
result = variational_algorithm(my_ansatz, my_cost, [0.0, 0.0])
硬件执行模板
def run_on_hardware(circuit, provider='google', device_name='<processor_id>',
device_config='<config_name>', target='ionq.simulator',
repetitions=1000):
"""Template for running on quantum hardware."""
if provider == 'google':
# Approved Quantum Engine users only; otherwise use the Quantum Virtual Machine
# (cirq_google.engine.create_default_noisy_quantum_virtual_machine).
import cirq_google
engine = cirq_google.get_engine() # uses GOOGLE_CLOUD_PROJECT
processor = engine.get_processor(device_name)
# cirq-google 1.7: device_config_name is required (see processor.list_configs())
# and run() returns a cirq.Result directly.
return processor.run(circuit, device_config_name=device_config,
repetitions=repetitions)
elif provider == 'ionq':
import cirq_ionq
service = cirq_ionq.Service() # reads IONQ_API_KEY
return service.run(circuit, repetitions=repetitions, target='qpu')
elif provider == 'azure':
from azure.quantum.cirq import AzureQuantumService
# Setup workspace...
service = AzureQuantumService(workspace)
# Target names are system-specific (e.g. 'ionq.qpu.forte-1'); see service.targets()
return service.run(circuit, repetitions=repetitions, target=target)
else:
raise ValueError(f"Unknown provider: {provider}")
噪声研究模板
def noise_comparison_study(circuit, noise_levels):
"""Compare circuit performance at different noise levels."""
results = {}
for noise_level in noise_levels:
# Create noisy circuit
noisy_circuit = circuit.with_noise(cirq.depolarize(p=noise_level))
# Simulate
simulator = cirq.DensityMatrixSimulator()
result = simulator.run(noisy_circuit, repetitions=1000)
# Analyze
results[noise_level] = {
'histogram': result.histogram(key='result'),
'dominant_state': max(
result.histogram(key='result').items(),
key=lambda x: x[1]
)
}
return results
# Run study
noise_levels = [0.0, 0.001, 0.01, 0.05, 0.1]
results = noise_comparison_study(circuit, noise_levels)
最佳实践
-
电路设计
- 根据拓扑使用适当的量子比特类型
- 保持电路模块化且可复用
- 使用描述性键标记测量
- 执行前根据设备约束验证电路
-
模拟
- 对纯态使用态矢量模拟(效率更高)
- 仅在需要时使用密度矩阵模拟(混合态、噪声)
- 利用参数扫描,而非单独运行
- 监控大型系统的内存使用量(2^n 会快速增长)
-
硬件执行
- 始终先在模拟器上测试
- 使用校准数据选择最佳量子比特
- 针对目标硬件门集优化电路
- 在生产运行中实施误差缓解
- 立即保存昂贵的硬件运行结果
-
电路优化
- 从高级内置变换器开始
- 按顺序串联多项优化
- 跟踪深度和门数的减少情况
- 变换后验证正确性
-
噪声建模
- 使用基于校准数据的逼真噪声模型
- 纳入所有误差来源(门、退相干、读出)
- 缓解前先进行表征
- 保持电路较浅,以尽量减少噪声累积
-
实验
- 明确划分实验结构(数据生成、收集、分析)
- 使用 ReCirq 模式以确保可复现性
- 经常保存中间结果
- 并行执行相互独立的任务
- 通过元数据进行详尽记录
更多资源
- 官方文档:https://quantumai.google/cirq
- API 参考:https://quantumai.google/reference/python/cirq
- 教程:https://quantumai.google/cirq/tutorials
- 示例:https://github.com/quantumlib/Cirq/tree/main/examples
- ReCirq:https://github.com/quantumlib/ReCirq
常见问题
电路对于硬件来说太深:
- 使用电路优化变换器来降低深度
- 优化技术请参阅
transformation.md
模拟时出现内存问题:
- 从密度矩阵模拟器切换为态矢量模拟器
- 减少量子比特数量,或对 Clifford 电路使用稳定子模拟器
设备验证错误:
- 使用 device.metadata.nx_graph 检查量子比特连通性
- 将门分解为设备原生门集
- 设备专属编译请参阅
hardware.md
属于 AlterLab 学术技能套件的一部分。
含噪模拟速度太慢:
- 密度矩阵模拟的复杂度为 O(2^2n)——可考虑减少量子比特数量
- 仅在关键操作上选择性使用噪声模型
- 性能优化请参阅
simulation.md
评论
支持 Markdown