Repository navigation
Conversation
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
marked this pull request as ready for review
October 27, 2026 23:15
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #26.
Problem
When the
.hslfiles make AutoQ reorder the qubits, every gate maps its qubit index throughqubit_permutation, butr = measure q[k];stored the raw indexk. Theif (r)/while (r)guard then split the automaton on a different qubit thanq[k].autoq vergave 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], asfirst_qubit_indexandparse_qubit_indicesdo for gates. This is the only place where a measured qubit is recorded; theifandwhilehandlers already pass the stored level toAutomata::measure.Test
measurement_guard_uses_qubit_permutationinunit_tests/explicit_tree_aut_test.ccruns 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:if_reset.qasm(resetsq[1])[OK][failed][OK]if_wrong_qubit.qasm(measuresq[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/andunit_tests/whose circuit containsmeasure, only the new test folder reorders qubits; the other 118 use the identity permutation, where the changed line gives the same value as before. Runningveron 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 fourr = 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:[OK], 28 where it gives[failed])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