Skip to content

Map measured qubits through the qubit permutation (#26) - #72

Open
ondrik wants to merge 1 commit into
fmlab-iis:mainfrom
ondrik:fix-measure-permutation
Open

ondrik wants to merge 1 commit into
fmlab-iis:mainfrom
ondrik:fix-measure-permutation

Conversation

@ondrik

@ondrik ondrik commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Fixes #26.

Problem

When the .hsl files make AutoQ reorder the qubits, every gate maps its qubit index through qubit_permutation, but r = measure q[k]; stored the raw index k. The if (r) / while (r) guard then split the automaton on a different qubit than q[k]. autoq ver gave wrong verdicts in both directions, and the verdict depended on whether a spec was written as {|s0s> : |s|=1} or as the equivalent {|000>, |101>}.

Fix

src/execute.cc: store the automaton level of the measured qubit, 1 + qubit_permutation[k], as first_qubit_index and parse_qubit_indices do for gates. This is the only place where a measured qubit is recorded; the if and while handlers already pass the stored level to Automata::measure.

Test

measurement_guard_uses_qubit_permutation in unit_tests/explicit_tree_aut_test.cc runs the three circuits from the issue (unit_tests/testcase/MeasurePermutation/) with pre {|sts>} and post {|s0s>}. It first requires the permutation to be [0, 2, 1], so the test keeps exercising the reordering, then checks the verdicts:

circuit expected main this PR
if_reset.qasm (resets q[1]) [OK] [failed] [OK]
if_wrong_qubit.qasm (measures q[2]) [failed] [OK] [failed]
while_reset.qasm [OK] [failed] [OK]

The test fails on main with all three cases and passes with the fix. With the explicit post {|000>, |101>}, both builds already give the expected verdicts.

Validation

  • Existing inputs are unaffected. Of the 119 folders under benchmarks/ and unit_tests/ whose circuit contains measure, only the new test folder reorders qubits; the other 118 use the identity permutation, where the changed line gives the same value as before. Running ver on all of them with main and with this PR gives the same verdicts. The only differences are the new test folder and two GroverWhile cases (76, 77) that timed out on main while other jobs were running in parallel. Re-run on their own, they give the same verdict on both builds.

  • Differential fuzz test. I generated 1,000 random cases with 3 or 4 qubits. Each case is a random classical circuit (x, cx, ccx) with up to four r = measure q[k]; if (r) { ... } blocks. Each pre/post is written twice: once with variables that repeat at non-adjacent positions (|s0s>, |stst>, ...), which reorders the qubits, and once as the explicit list of basis states, which keeps the identity order. The two spellings describe the same sets, so they must get the same verdict:

    reordered cases verdicts differ
    main 880 51 (23 where explicit gives [OK], 28 where it gives [failed])
    this PR 880 0

    The other 120 cases ended up with the identity permutation and agree on both builds.

  • ctest: both test binaries pass.

The fix merges cleanly with the open PRs #67–#71.

🤖 Generated with Claude Code

https://claude.ai/code/session_013gtFQRPe1eSSpmA7EJ2KNY

Gates map each circuit qubit index through qubit_permutation, but
`r = measure q[k];` stored the raw index. When the .hsl files make AutoQ
reorder the qubits, the `if (r)` / `while (r)` guard then projected onto a
different qubit than q[k], and `ver` gave wrong verdicts in both
directions. Store the automaton level of the qubit, as the gates do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013gtFQRPe1eSSpmA7EJ2KNY
@ondrik
ondrik marked this pull request as ready for review October 27, 2026 23:15
@ondrik
ondrik requested a review from alan23273850 October 27, 2026 23:15

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Measurement in if/while guard ignores the qubit permutation from .hsl files

1 participant