跳转至

结果不同的三种原因

先完成单位与采样两课。一个观测量可能因为物理模型含噪声、采样次数有限,或数值演化存在近似而变化。三种原因需要分别检查。

原因 可以怎样做对照 什么做法不能解决它
物理噪声 保持设置一致,比较理想与带噪模型 增加 shots 不会消除物理退相干
采样不确定度 增加 shots 或独立重复次数 收紧求解容差不会增加样本数
数值误差 细化积分,与独立参考比较 重复使用同一 seed 不能证明数值收敛

下面的脚本分别演示三者。它为初态 plus 定义简单退相位规律 ρ01(t) = ρ01(0) exp(−γt),因此 X 期望值为 exp(−γt)。这里明确给出了 γ 的定义,不能把它直接当作某个 SDK 噪声通道的速率归一化约定。

"""Separate a toy dephasing model, sampling uncertainty and integration error."""

from __future__ import annotations

import json
from math import exp, sqrt

import numpy as np
from numpy.typing import NDArray
from scipy.integrate import solve_ivp
from scipy.linalg import expm


def experiment() -> dict[str, object]:
    x = np.array([[0, 1], [1, 0]], dtype=complex)
    z = np.diag([1, -1])
    initial = np.array([1, 0], dtype=complex)
    duration = 0.8

    def hamiltonian(time: float) -> NDArray[np.complex128]:
        return (1.7 * x + 3.0 * time * z) / 2

    solution = solve_ivp(
        lambda time, state: -1j * hamiltonian(time) @ state,
        (0, duration),
        initial,
        method="DOP853",
        rtol=1e-11,
        atol=1e-13,
    )
    if not solution.success:
        raise RuntimeError(solution.message)
    reference = solution.y[:, -1]
    integration = []
    for steps in (4, 64):
        state = initial.copy()
        dt = duration / steps
        for step in range(steps):
            state = expm(-1j * hamiltonian((step + 0.5) * dt) * dt) @ state
        integration.append(
            {
                "steps": steps,
                "state_error_norm": float(np.linalg.norm(state - reference)),
            }
        )
    return {
        "toy_dephasing_gamma_per_us": 0.4,
        "duration_us": duration,
        "x_after_toy_dephasing": exp(-0.4 * duration),
        "sampling_se_at_p_half": {str(n): sqrt(0.25 / n) for n in (100, 10000)},
        "midpoint_integration": integration,
    }


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

下载完整脚本

python examples/learning/foundations/noise_and_precision.py
{
  "duration_us": 0.8,
  "midpoint_integration": [
    {
      "state_error_norm": 0.006047460792580759,
      "steps": 4
    },
    {
      "state_error_norm": 2.3549057319816534e-05,
      "steps": 64
    }
  ],
  "sampling_se_at_p_half": {
    "100": 0.05,
    "10000": 0.005
  },
  "toy_dephasing_gamma_per_us": 0.4,
  "x_after_toy_dephasing": 0.7261490370736908
}

p = 1/2 时,100 和 10,000 次采样的标准误分别为 0.05 和 0.005。积分例子使用含有不对易项的时变 Hamiltonian,把简单的中点矩阵指数方法与较严格容差的 DOP853 参考比较。这是教学计算,不代表 CASCAQit 某种模拟方法的实现或性能。参考解本身也来自数值计算,因此与它收敛属于一致性检查,不是精确证明。

练习:增加 shots 后,某项差异基本不变,接下来应该调查什么?要提高数值参考的可靠性,可以改变哪个设置?

分析思路

检查物理模型、单位、测量约定和积分误差。收紧参考容差,细化两种计算,再看观测量是否在所需精度下稳定。不能仅因采样误差较小,就认定剩余差异来自物理噪声。

SDK 的具体设置见本地模拟算法和噪声模型。下一课:设计可复现的实验。

相关概念:误差来源。

English

SDK 1.0.8a · `8b227bff`