From 6d145ec8728f04cca445e4639fe462b3146d46aa Mon Sep 17 00:00:00 2001 From: devin davis Date: Sat, 21 Nov 2026 23:30:34 -0500 Subject: [PATCH] tket_wrapper: export single-bit conditions via hqslib1; honour no_reduce on the pytket path With pytket installed (2.18.5), 12 tests in test_quantinuum_client.py failed: - circuit_to_qasm_str(qelib1) raises QASMUnsupportedError on single-bit conditions such as if(c[0]==1); retry with Quantinuum's hqslib1 header. - no_reduce / level 0 were ignored on the pytket path (the static path skips all reduction); now both paths behave the same. - Guard: pytket output is rejected (static fallback used) if it changes the number of measurements, resets or conditionals. Text-matching tests now check the lowering output; the compiled output is checked structurally (pytket legitimately rewrites x as u3). Two new tests. Full suite with pytket: 3085 passed, 26 skipped, 0 failed. Quantinuum tests with pytket blocked (as in CI): 26 passed, 1 skipped. Co-Authored-By: Claude Opus 5.5 --- osiris_cli/providers/tket_wrapper.py | 26 ++++++++++++---- tests/test_quantinuum_client.py | 44 +++++++++++++++++++++++----- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/osiris_cli/providers/tket_wrapper.py b/osiris_cli/providers/tket_wrapper.py index fbffec38..e82931c2 100644 --- a/osiris_cli/providers/tket_wrapper.py +++ b/osiris_cli/providers/tket_wrapper.py @@ -877,19 +877,35 @@ def _try_pytket_compile( except Exception: return None - if not options.no_opt: - if not options.no_reduce and options.tket_opt_level >= 1: + # Same switches as the static optimizer: no_opt, no_reduce or level 0 means "leave the circuit alone". + if options.no_opt or options.no_reduce or options.tket_opt_level == 0: + diags.append("pytket optimization disabled by compiler options") + else: + if options.tket_opt_level >= 1: RemoveRedundancies().apply(circ) diags.append("Applied pytket.passes.RemoveRedundancies") if options.tket_opt_level >= 2: FullPeepholeOptimise().apply(circ) diags.append("Applied pytket.passes.FullPeepholeOptimise") - else: - diags.append("pytket optimization disabled via no_opt=True") - compiled = circuit_to_qasm_str(circ) + # qelib1 only allows conditions on a whole register; Quantinuum's hqslib1 dialect also allows + # single-bit conditions such as if(c[0]==1), which mid-circuit feed-forward needs. + try: + compiled = circuit_to_qasm_str(circ, header="qelib1") + except Exception: + try: + compiled = circuit_to_qasm_str(circ, header="hqslib1") + diags.append("Exported with the hqslib1 header (single-bit conditions)") + except Exception: + return None if not compiled.endswith("\n"): compiled += "\n" + + # Refuse output that changed the classical structure (measure, reset, conditional counts). + before, after = _analyze_qasm2(qasm2), _analyze_qasm2(compiled) + for field in ("measurements", "resets", "feedforward_conditionals"): + if getattr(before, field) != getattr(after, field): + return None return compiled, diags diff --git a/tests/test_quantinuum_client.py b/tests/test_quantinuum_client.py index 702f31d4..6022504b 100644 --- a/tests/test_quantinuum_client.py +++ b/tests/test_quantinuum_client.py @@ -5,6 +5,7 @@ import hashlib import io import json +import re import stat from pathlib import Path from typing import Any, Dict, List, Optional, Tuple @@ -178,9 +179,14 @@ def test_lower_example_bitflip_qec_qasm() -> None: assert res.metrics.has_mid_circuit_measurement is True assert res.metrics.has_reset is True assert res.metrics.has_classical_control is True - assert "if(syn==1) x data[0];" in res.qasm2 - assert "if(syn==3) x data[1];" in res.qasm2 - assert "if(syn==2) x data[2];" in res.qasm2 + lowered = lower_qasm3_to_quantinuum_qasm2(source) + assert "if(syn==1) x data[0];" in lowered + assert "if(syn==3) x data[1];" in lowered + assert "if(syn==2) x data[2];" in lowered + assert "reset anc[0];" in lowered + # pytket may rewrite x as u3; the compiled circuit must keep every correction and its condition. + assert res.metrics.feedforward_conditionals == 3 + assert sorted(re.findall(r"if\(syn==(\d)\) \S+ data\[(\d)\]", res.qasm2)) == [("1", "0"), ("2", "2"), ("3", "1")] assert "reset anc[0];" in res.qasm2 @@ -197,14 +203,19 @@ def test_lower_dnalang_minimal_circuit() -> None: MEASURE(1, 1) } """ + lowered = lower_qasm3_to_quantinuum_qasm2(dna) + assert "qreg q[2];" in lowered + assert "creg c[2];" in lowered + assert "h q[0];" in lowered + assert "cx q[0], q[1];" in lowered + assert "measure q[0] -> c[0];" in lowered + assert "reset q[0];" in lowered + assert "if(c[0]==1) x q[1];" in lowered res = compile_for_quantinuum(dna, target="H2-1SC") - assert "qreg q[2];" in res.qasm2 - assert "creg c[2];" in res.qasm2 - assert "h q[0];" in res.qasm2 - assert "cx q[0], q[1];" in res.qasm2 assert "measure q[0] -> c[0];" in res.qasm2 assert "reset q[0];" in res.qasm2 - assert "if(c[0]==1) x q[1];" in res.qasm2 + assert re.search(r"if\(c\[0\]==1\) \S+ q\[1\];", res.qasm2) + assert res.metrics.feedforward_conditionals == 1 assert res.metrics.has_mid_circuit_measurement is True assert res.metrics.has_reset is True assert res.metrics.has_classical_control is True @@ -895,3 +906,20 @@ def test_osiris_repl_main_dispatches_new_subcommands_preserving_legacy( monkeypatch.setattr(osiris_repl, "execute_sync", lambda subargs=None: seen.append(subargs)) osiris_repl.main(["sync", "status"]) assert seen == [["status"]] + + +def test_pytket_path_handles_single_bit_conditions_and_keeps_feedforward() -> None: + """With pytket installed, single-bit conditions used to raise QASMUnsupportedError at export.""" + pytest.importorskip("pytket") + res = compile_for_quantinuum(SAMPLE_QASM3_MID_CIRCUIT, target="H2-1SC") + assert res.ok and res.compiler_backend == "pytket" + assert res.metrics.feedforward_conditionals == 2 + assert res.metrics.mid_circuit_measurements == compile_for_quantinuum( + SAMPLE_QASM3_MID_CIRCUIT, target="H2-1SC", options=QuantinuumCompilerOptions(no_opt=True) + ).metrics.mid_circuit_measurements + + +def test_static_and_pytket_paths_honour_no_reduce() -> None: + src = 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[1];\ncreg c[1];\nx q[0];\nx q[0];\nmeasure q[0] -> c[0];\n' + res = compile_for_quantinuum(src, target="H2-1SC", options=QuantinuumCompilerOptions(no_reduce=True)) + assert res.metrics.total_gates == 2