从优化问题生成并运行原生程序¶
ProblemCompiler 从 cascaqit.problems 导入。它把 Graph、MIS、MWIS、QUBO 或 Ising 问题转为原生程序,并保留逻辑顺序和解码规则。先用小问题检查候选解与经典参考,再增加参数预算或层数。
| 模式与算法 | 生成什么 | 选择时先检查 |
|---|---|---|
digital + qaoa |
数字线路 | 层数与参数预算 |
digital + vqe |
数字变分线路 | Ansatz 的表达能力、约束与测量方式 |
hybrid + qaoa |
Digital–Analog 交替程序 | 必须存在非空 Analog 贡献;其余项显式由 Digital 实现 |
analog + qaa |
AHS 程序 | 目标几何与控制能否完整表达 Hamiltonian |
analyze(problem, target=...) 分析映射和各路线可行性,不运行实验。compile(...) 要求明确给出 mode、algorithm 和 target,返回之后还需要绑定参数并执行。
先运行一个三节点问题¶
下面在三节点链上执行一组 Digital QAOA 参数。经典枚举只检查八个候选,最大独立集大小为 2,对应节点 a 与 c。
"""用数字编译路线运行一个小型图问题。"""
from __future__ import annotations
import json
from cascaqit import MockNeutralAtomTarget
from cascaqit.problems import GraphProblemIR, ProblemCompiler, decode_graph_bitstring
def main() -> None:
"""用三节点经典参考检查采样候选解。"""
graph = GraphProblemIR.from_edges(
problem_id="guide.problem.path",
positions={"a": (0.0, 0.0), "b": (6.0, 0.0), "c": (12.0, 0.0)},
edges=(("a", "b"), ("b", "c")),
)
compiled = ProblemCompiler().compile(
graph,
mode="digital",
algorithm="qaoa",
target=MockNeutralAtomTarget.local_ahs_v0_1(),
layers=1,
)
execution = compiled.run(
params={"gamma_0": 0.16, "beta_0": 0.24}, shots=128, seed=508
)
candidates = [decode_graph_bitstring(graph, f"{i:03b}") for i in range(8)]
optimum = max(
item["selection_size"] for item in candidates if item["is_independent"]
)
counts = execution.result.counts
feasible_counts = sum(
count
for bits, count in counts.items()
if decode_graph_bitstring(graph, bits)["is_independent"]
)
if sum(counts.values()) != 128 or optimum != 2:
raise RuntimeError("采样总数或经典参考结果不正确。")
print(
json.dumps(
{
"mode": "digital",
"algorithm": "qaoa",
"logical_order": list(execution.logical_order),
"counts": counts,
"counts_total": sum(counts.values()),
"classical_maximum_size": optimum,
"feasible_frequency": feasible_counts / 128,
"best_observed_bitstring": execution.best_observed_candidate.bitstring,
"best_observed_feasible": execution.best_observed_candidate.feasible,
"objective_value": execution.objective_value,
},
sort_keys=True,
)
)
if __name__ == "__main__":
main()
python examples/learning/guides/problem_compile_zh.py
{
"algorithm": "qaoa",
"best_observed_bitstring": "101",
"best_observed_feasible": true,
"classical_maximum_size": 2,
"counts": {
"000": 19,
"001": 13,
"010": 14,
"011": 19,
"100": 14,
"101": 10,
"110": 15,
"111": 24
},
"counts_total": 128,
"feasible_frequency": 0.546875,
"logical_order": [
"a",
"b",
"c"
],
"mode": "digital",
"objective_value": -0.33529298728005646
}
logical_order 为 a、b、c,所以 101 表示选择 a 和 c。采样总数应为 128;feasible_frequency 是本次采样中满足独立集约束的比例。有限采样次数和种子会影响它。
本例没有搜索参数,也没有证明 QAOA 收敛。objective_value 是这组参数的期望目标值;best_observed_bitstring 只是本次看见的最佳候选,不能把两者混作同一个量。
再比较参数或路线¶
用 compiled.evaluate(params=...) 求一次目标值;用 compiled.optimize(parameter_sets=...) 比较已列出的离散参数点;需要连续搜索时改用 optimizer=OptimizerConfig(...)。离散候选与优化器配置只能选一种。先记录实际求值和采样成本,再比较结果。
三路线 MIS 项目展示同一问题的 Digital、Hybrid 和 Analog 实验及报告。比较前核对 problem_hash 和逻辑顺序,保留各路线的目标、算法、参数、预算和模拟设置。不同单位的资源估算不能直接当作性能排名。
Analog 无法表达某个耦合项时会拒绝编译,不会暗中补一个数字程序块。Hybrid 的 gamma_i 同时决定 Analog 演化时长,必须严格大于零。当前统一编译结果的 Analog 与 Hybrid 路径也会拒绝带噪请求和梯度执行;通用后端支持的能力不能直接套到该入口。
优化器字段见 OptimizerConfig。更多模板、层数实验与结果字段见完整编译指南(仓库文档)。