Hands-On: IBM Quantum
Prepare an IBM workflow framed as a Qiskit Runtime adapter — not native qalgora-Q submission. The open qalgora-Q reference build runs locally and does not submit directly; real execution requires a configured Qiskit Runtime account/service and an IBM Quantum account (the Open plan gives free monthly time).
1 · Get a token
- Create a free account at the IBM Quantum platform.
- Copy your API token from the dashboard.
- Export it:
export QALGORA_IBM_TOKEN="..."(note: real execution still needs a configured Qiskit Runtime service).
2 · Validate locally
import qalgora
@qalgora.kernel
def bell():
q = qalgora.qvector(2)
h(q[0]); x.ctrl(q[0], q[1]); mz(q)
qalgora.set_target("qpp-cpu") # reference implementation, local validation
print(qalgora.sample(bell, shots_count=1000))
3 · Submit via the Qiskit Runtime adapter (planned)
# qalgora.set_target("ibm", machine="<ibm_backend_name>") # planned adapter — needs Qiskit Runtime SDK + credentials
# job = qalgora.sample_async(bell, shots_count=4000)
# print(job.status())
# print(job.get()) # adapter future; blocks until the remote job finishesjob.get()4 · Inspect the transpiled circuit (specification interface)
IBM chips are heavy-hex, so two-qubit gates may need SWAPs. The transpile check below is a specification interface; check the compiled depth before you spend a long queue:
compiled = qalgora.transpile(bell, optimization_level=3) # 规范接口 / specification interface
print("depth:", compiled.depth(), " 2q-gates:", compiled.num_two_qubit_gates())5 · Variational loop in a session (adapter workflow · planned)
For VQE/QAOA, the adapter would open a session so iterations share a low-latency window. This is a planned adapter workflow (Qiskit Runtime session); shown commented out:
# with qalgora.Session(target="ibm", machine="<ibm_backend_name>"): # planned adapter — Qiskit Runtime session
# energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=n)resilience_level= on observe for error mitigation, and prefer the Open
plan's smaller systems while debugging.
实操:IBM Quantum
把 IBM 工作流视作一个 Qiskit Runtime 适配器,而非 qalgora-Q 原生提交。开放的 qalgora-Q 参考实现在本地运行、不会直接提交;真机执行需要配置好的 Qiskit Runtime 账号/服务与 IBM Quantum 账号(Open 计划提供每月免费额度)。
1 · 获取令牌
- 在 IBM Quantum 平台注册免费账号。
- 从控制台复制你的 API 令牌。
- 导出令牌:
export QALGORA_IBM_TOKEN="..."(注意:真机执行仍需配置好的 Qiskit Runtime 服务)。
2 · 本地验证
import qalgora
@qalgora.kernel
def bell():
q = qalgora.qvector(2)
h(q[0]); x.ctrl(q[0], q[1]); mz(q)
qalgora.set_target("qpp-cpu") # 参考实现本地验证
print(qalgora.sample(bell, shots_count=1000))
3 · 经 Qiskit Runtime 适配器提交(规划中)
# qalgora.set_target("ibm", machine="<ibm_backend_name>") # 规划中 / 适配示意:需 Qiskit Runtime SDK 与凭据
# job = qalgora.sample_async(bell, shots_count=4000)
# print(job.status())
# print(job.get()) # 适配器 future;阻塞直到远程任务完成job.get()4 · 查看转译后的电路(规范接口)
IBM 芯片采用重六边形拓扑,双比特门可能需要 SWAP 布线。下方的转译检查属于规范接口;在进入漫长排队前,先检查编译后的深度:
compiled = qalgora.transpile(bell, optimization_level=3) # 规范接口
print("depth:", compiled.depth(), " 2q-gates:", compiled.num_two_qubit_gates())5 · 在会话中运行变分循环(适配器工作流·规划中)
对于 VQE/QAOA,适配器会开启会话让每次迭代共享低延迟窗口。这是规划中的适配器工作流(Qiskit Runtime 会话),以下代码已注释:
# with qalgora.Session(target="ibm", machine="<ibm_backend_name>"): # 规划中适配器 — Qiskit Runtime 会话
# energy, params = qalgora.vqe(ansatz, hamiltonian, optimizer, parameter_count=n)observe 上设置
resilience_level= 可开启纠错缓解;调试阶段优先使用 Open
plan 的小规模系统。