跳转至

SampledSelectionConfig

from cascaqit import SampledSelectionConfig

SampledSelectionConfig

SampledSelectionConfig(
    candidate_count: int = 3,
    repeats_per_candidate: int = 3,
    max_repeats_per_candidate: int | None = None,
    shots_per_group: int | None = None,
    confidence_level: float = 0.95,
    max_backend_executions: int | None = None,
    schema_version: str = SAMPLED_SELECTION_SCHEMA_VERSION,
)

用独立的确认采样,在采样 VQE 的优化历史中选择参数点。candidate_count 至少为 2,是最多考虑的不同参数绑定数;历史中可用绑定不足时,实际候选数可以更少。重复绑定只保留一个来源。独立确认减少仅按一次低估能量选点的偏差,但不保证选中真实最优参数。

repeats_per_candidate 至少为 2。未设置 max_repeats_per_candidate,或上限等于起始次数时,按固定重复次数确认;上限大于起始次数时进入顺序确认,在不确定候选之间追加采样,直到置信比较分开、达到重复上限或后端预算用完。max_backend_executions 是确认阶段的正整数预算,至少需覆盖所有实际候选的第一轮完整确认;不足会报错,而不是只比较已测的一部分候选。

shots_per_group 可覆盖优化阶段的测量预算,至少为 2;省略则继承。分组、分配方式和读出缓解仍保持一致。confidence_level 在 (0, 1) 内;顺序比较另使用 decision_confidence_level 做多次比较修正。候选区间与决策区间可能不同,不能把置信分离写成无条件的最优性证明。最终计算基采样是之后的独立步骤,不属于这里的确认成本。

from cascaqit import PauliMeasurementConfig, SampledSelectionConfig

selection = SampledSelectionConfig(candidate_count=2, repeats_per_candidate=2,
    max_repeats_per_candidate=4, shots_per_group=64)
measurement = selection.resolved_measurement(PauliMeasurementConfig(shots_per_group=32))
assert selection.mode == "sequential"
assert selection.planned_look_count == 5
assert selection.decision_confidence_level > selection.confidence_level
assert measurement.shots_per_group == 64
assert SampledSelectionConfig.from_json(selection.to_json()) == selection

mode

mode: Literal['fixed', 'sequential']

有效最大重复数等于 repeats_per_candidate 时返回 fixed,否则返回 sequential。显式提供相等的上限不会启用顺序追加。

resolved_max_repeats_per_candidate

resolved_max_repeats_per_candidate: int

返回显式最大重复数;未提供时返回 repeats_per_candidate。它限制单个候选的确认重复数。

planned_look_count

planned_look_count: int

固定模式返回 1;顺序模式返回 1 + candidate_count * (最大重复数 - 起始重复数)。这是多次比较修正使用的保守上限,不是实际已执行轮数。

decision_confidence_level

decision_confidence_level: float

固定模式使用 confidence_level。顺序模式将 alpha=1-confidence_level 除以 planned_look_count 与候选对数的乘积,再返回 1-alpha。这是 Bonferroni 修正后的单次决策置信水平,使用配置的候选数。

resolved_measurement

resolved_measurement(
    optimization_measurement: PauliMeasurementConfig,
) -> PauliMeasurementConfig

从优化的 PauliMeasurementConfig 返回确认测量配置,只按需覆盖 shots_per_group,并保留分组、分配、试采样配置与缓解。覆盖预算过小而不满足原试采样要求时会报错;输入不是 PauliMeasurementConfig 时抛出 TypeError。

from_dict

from_dict(
    data: Mapping[str, Any],
) -> SampledSelectionConfig

从字典还原 SampledSelectionConfig。省略字段使用默认值,并重新校验候选数、重复数、预算与置信水平。 缺少必需字段或字段不合法时可能抛出 KeyError、TypeError 或 ValueError。

from_json

from_json(text: str) -> SampledSelectionConfig

解析 JSON 对象并调用 from_dict(),返回 SampledSelectionConfig。非法 JSON 会抛出解析错误;顶层不是对象时抛出 TypeError。

to_dict

to_dict() -> dict[str, Any]

返回可写入 JSON 的字典,嵌套对象一并序列化。元组转成数组;这个字典是保存的声明,不是执行结果。

to_json

to_json(*, indent: int | None = None) -> str

返回 JSON 字符串,不写文件。indent=None 使用紧凑格式;提供缩进宽度可便于阅读。

stable_hash

stable_hash() -> str

返回规范 JSON 的 SHA-256 十六进制摘要。字段、标识或元数据变化都可能改变摘要;它用于比较保存内容,不判断两个声明在物理上是否等价。