跳转至

测量与有限次采样

先阅读振幅与概率。这里保持量子线路不变:绕 Y 轴旋转 π/3 后,测到 1 的理想概率为 p = 1/4。我们只改变 shots 和采样种子。

对于相互独立的二值结果,频率 p̂ = count(1)/N 用来估计概率 p。它的标准差为 sqrt(p(1−p)/N),也叫采样标准误。它不是每次运行都必须满足的误差上限。把 N 增加到四倍,标准误会减半。

"""Sample one fixed circuit with several shot budgets and random seeds."""

from __future__ import annotations

import json
from math import pi, sqrt

from cascaqit import Circuit


def experiment() -> dict[str, object]:
    circuit = Circuit(1).ry(pi / 3, 0).measure_all()
    rows = []
    for shots in (32, 128, 1024):
        frequencies = []
        for seed in (11, 22, 33, 44):
            result = circuit.run(shots=shots, seed=seed)
            frequencies.append(result.counts.get("1", 0) / shots)
        rows.append(
            {
                "shots": shots,
                "frequencies": frequencies,
                "binomial_standard_error": sqrt(0.25 * 0.75 / shots),
            }
        )
    return {"ideal_probability_one": 0.25, "seeds": [11, 22, 33, 44], "runs": rows}


if __name__ == "__main__":
    print(json.dumps(experiment(), sort_keys=True))

下载完整脚本

python examples/learning/foundations/measurement_sampling.py
{
  "ideal_probability_one": 0.25,
  "runs": [
    {
      "binomial_standard_error": 0.07654655446197431,
      "frequencies": [
        0.1875,
        0.375,
        0.25,
        0.1875
      ],
      "shots": 32
    },
    {
      "binomial_standard_error": 0.038273277230987154,
      "frequencies": [
        0.265625,
        0.265625,
        0.234375,
        0.203125
      ],
      "shots": 128
    },
    {
      "binomial_standard_error": 0.013531646934131853,
      "frequencies": [
        0.2548828125,
        0.251953125,
        0.271484375,
        0.2412109375
      ],
      "shots": 1024
    }
  ],
  "seeds": [
    11,
    22,
    33,
    44
  ]
}

比较每个采样预算下的四个频率。理想概率始终为 0.25,采样频率却会变化。这里用已知的理想概率计算标准误;真实实验中若不知道 p,就需要用适当的统计方法从数据估计不确定度,尤其要留意接近零或一的情况。

增大采样预算,会缩小重复运行结果的整体分散程度,但不保证每一次结果都更接近 0.25。对于这个理想线路,改 seed 只改变抽到的样本,不改变准备的量子态。带噪轨迹模拟中的种子还可能影响噪声路径,那是另一种随机性。

练习:保持 p 不变,若要把标准误缩小到原来的十分之一,需要把 shots 增加到多少倍?增加几组 seed 验证,而不要只挑最接近理论的一次结果。

答案

需要一百倍。标准误按 N 的平方根倒数缩放,不按 1/N 缩放。一次运行没有采到 1,也不能证明 1 的概率为零。

下一课:量子门、位序与相干性。

相关概念:采样与期望值。

English

SDK 1.0.8a · `6eff6362`