qalgora-Q Docs Hub量子文档 ✦ Ask AI✦ 问问文档

Mid-Circuit Measurement & Conditional Logic

Measure a qubit partway through a kernel and branch on the result — the basis of teleportation, error correction, and adaptive circuits.

Measuring and branching

Mid-circuit mz and feed-forward (conditioning later gates on a measured bit) run on the reference build's trajectory model.

import qalgora
qalgora.set_target("qpp-cpu")

@qalgora.kernel
def reset_if_one():
    q = qalgora.qvector(2)
    h(q[0])
    b = mz(q[0])          # mid-circuit measurement -> classical bit
    if b:                 # feed-forward: conditional logic on the measured value
        x(q[1])
    mz(q[1])

print(qalgora.sample(reset_if_one, shots_count=1000))

Per-qubit marginal statistics

Read the marginal statistics for a measured qubit by its index. The kernel below runs; the marginal read is a specification interface.

@qalgora.kernel
def tagged():
    q = qalgora.qvector(2)
    h(q[0])
    if mz(q[0]):
        x(q[1])
    mz(q[1])

result = qalgora.sample(tagged)
# Specification API — not in the open reference build yet:
# print(result.get_marginal_counts([0]))   # marginal statistics for q[0]
Specification API — not in the open reference build yet
This example shows a qalgora-Q specification API (or a third-party library) that the open reference build does not bundle today. It documents the intended interface; to run code now, use the reference build’s supported core API.
Run vs. sample
run with an explicit return type is a specification interface for reading structured outcomes; if it isn't available in your build, use sample — see sample vs. run.

线路中测量与条件逻辑

在内核执行过程中对量子比特进行测量,并根据结果进行分支——这是隐形传态、纠错和自适应线路的基础。

测量与分支

线路中 mz 与前馈(用测得比特控制后续门)可在参考实现的轨迹模型上运行。

import qalgora
qalgora.set_target("qpp-cpu")

@qalgora.kernel
def reset_if_one():
    q = qalgora.qvector(2)
    h(q[0])
    b = mz(q[0])          # 线路中测量 -> 经典比特
    if b:                 # 前馈:基于测量值的条件逻辑
        x(q[1])
    mz(q[1])

print(qalgora.sample(reset_if_one, shots_count=1000))

按量子比特读取边缘统计

可通过量子比特索引读取边缘统计。下方内核可运行;边缘统计读取属于规范接口。

@qalgora.kernel
def tagged():
    q = qalgora.qvector(2)
    h(q[0])
    if mz(q[0]):
        x(q[1])
    mz(q[1])

result = qalgora.sample(tagged)
# 规范接口,开放参考实现暂未包含:
# print(result.get_marginal_counts([0]))   # q[0] 的边缘统计
规范接口 · 参考实现暂未包含
此示例展示的是 qalgora-Q 规范中的接口(或第三方库),开放参考实现目前尚未内置,仅用于说明预期用法;如需立即运行,请使用参考实现已支持的核心 API。
run 与 sample
带显式返回类型的 run 是用于读取结构化输出的规范接口;若你的版本尚不支持,请改用 sample——详见sample 与 run