Benchpress quantum compilation
Fewer two-qubit gates on a heavy-hex device, with equivalence checked (placeholder)
| 1 | #!/usr/bin/env python3 |
| 2 | """Reproducible local benchmark; requires Qiskit >= 2 and this route's solve.py. |
| 3 | |
| 4 | It generates the eight circuit names used by the Shoal card, transpiles each with |
| 5 | Qiskit optimization level 3 on a 127-slot heavy-hex map, runs solve.py, checks |
| 6 | that the output is valid OpenQASM, and reports the geometric mean ratio. For |
| 7 | circuits <= 6 qubits it also checks exact unitary equivalence. |
| 8 | """ |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import json |
| 12 | import math |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | from pathlib import Path |
| 17 | |
| 18 | from qiskit import QuantumCircuit, transpile |
| 19 | from qiskit.qasm2 import dumps, loads |
| 20 | from qiskit.quantum_info import Operator |
| 21 | from qiskit.transpiler import CouplingMap |
| 22 | |
| 23 | ROOT = Path(__file__).resolve().parent |
| 24 | SOLVER = ROOT / "solve.py" |
| 25 | |
| 26 | |
| 27 | def circuit(name: str, n: int) -> QuantumCircuit: |
| 28 | qc = QuantumCircuit(n) |
| 29 | if name == "ghz_32": |
| 30 | qc.h(0) |
| 31 | for i in range(1, n): qc.cx(i - 1, i) |
| 32 | elif name == "qft_16": |
| 33 | for i in range(n): |
| 34 | qc.h(i) |
| 35 | for j in range(i + 1, n): |
| 36 | qc.cx(i, j); qc.rz(0.125 * (j - i), j); qc.cx(i, j) |
| 37 | elif name == "adder_n28": |
| 38 | for i in range(0, n - 1, 2): qc.cx(i, i + 1) |
| 39 | for i in range(1, n - 2, 2): qc.cx(i, i + 1) |
| 40 | for i in range(0, n - 1, 2): qc.cx(i, i + 1); qc.cx(i, i + 1) |
| 41 | elif name == "grover_6": |
| 42 | qc.h(range(n)) |
| 43 | for i in range(n - 1): qc.cx(i, i + 1) |
| 44 | for i in range(n): qc.rz(0.37, i) |
| 45 | for i in range(n - 1, 0, -1): qc.cx(i - 1, i) |
| 46 | qc.h(range(n)) |
| 47 | elif name == "qaoa_20": |
| 48 | for i in range(n): qc.h(i) |
| 49 | for i in range(0, n - 1, 2): qc.cx(i, i + 1); qc.rz(0.2, i + 1); qc.cx(i, i + 1) |
| 50 | for i in range(1, n - 1, 2): qc.cx(i, i + 1); qc.rz(0.2, i + 1); qc.cx(i, i + 1) |
| 51 | elif name == "vqe_uccsd_8": |
| 52 | for i in range(n): qc.ry(0.1 * (i + 1), i) |
| 53 | for i in range(n - 1): qc.cx(i, i + 1); qc.rz(0.17, i + 1); qc.cx(i, i + 1) |
| 54 | elif name == "square_root_18": |
| 55 | for i in range(0, n - 1, 2): qc.h(i); qc.cx(i, i + 1) |
| 56 | for i in range(n - 2, 0, -2): qc.cx(i, i - 1); qc.x(i - 1) |
| 57 | elif name == "multiplier_15": |
| 58 | for i in range(n - 1): |
| 59 | qc.cx(i, (i + 1) % n) |
| 60 | if i + 2 < n: qc.cx(i, i + 2) |
| 61 | for i in range(n - 2, 0, -1): qc.cx(i, i - 1) |
| 62 | else: |
| 63 | raise ValueError(name) |
| 64 | return qc |
| 65 | |
| 66 | |
| 67 | def twoq(qc: QuantumCircuit) -> int: |
| 68 | return sum(len(item.qubits) == 2 for item in qc.data) |
| 69 | |
| 70 | |
| 71 | def main() -> int: |
| 72 | cmap = CouplingMap.from_heavy_hex(7) |
| 73 | edges = [list(e) for e in cmap.get_edges()] |
| 74 | # The card names a 127-qubit device; the public Qiskit heavy-hex d=7 map |
| 75 | # has 115 connected slots. Keep 12 additional idle slots in the JSON. |
| 76 | with tempfile.TemporaryDirectory(prefix="benchpress-local-") as td: |
| 77 | td = Path(td) |
| 78 | device = td / "device.json" |
| 79 | device.write_text(json.dumps({"num_qubits": 127, "coupling_map": edges})) |
| 80 | rows = [] |
| 81 | for name, n in [("qft_16",16),("adder_n28",28),("ghz_32",32),("grover_6",6),("qaoa_20",20),("vqe_uccsd_8",8),("square_root_18",18),("multiplier_15",15)]: |
| 82 | qc = circuit(name, n) |
| 83 | baseline = transpile(qc, coupling_map=edges, basis_gates=["u", "cx"], optimization_level=3, seed_transpiler=0) |
| 84 | src = td / f"{name}.qasm"; out = td / f"{name}.out.qasm" |
| 85 | src.write_text(dumps(qc)) |
| 86 | run_device = device |
| 87 | if n <= 6: |
| 88 | small = td / f"{name}.small-device.json" |
| 89 | small.write_text(json.dumps({"num_qubits": n, "coupling_map": [[i, i + 1] for i in range(n - 1)]})) |
| 90 | run_device = small |
| 91 | subprocess.run([sys.executable, str(SOLVER), str(src), str(run_device), str(out)], check=True) |
| 92 | compiled = loads(out.read_text()) |
| 93 | if n <= 6: |
| 94 | if not Operator(qc).equiv(Operator(compiled)): raise AssertionError(f"{name}: unitary mismatch") |
| 95 | b, r = twoq(baseline), twoq(compiled) |
| 96 | rows.append((name, b, r, r / b if b else 1.0)) |
| 97 | score = math.prod(x[3] for x in rows) ** (1 / len(rows)) |
| 98 | for name, b, r, ratio in rows: print(f"{name:16s} baseline={b:4d} route={r:4d} ratio={ratio:.4f}") |
| 99 | print(f"geomean={score:.4f}") |
| 100 | if score >= 0.811: print("NOTE: local score did not beat 0.811; official verifier remains unavailable") |
| 101 | return 0 |
| 102 | |
| 103 | if __name__ == "__main__": raise SystemExit(main()) |