Fix CommutativeCancellation dropping negative X-rotation sums#16605
Fix CommutativeCancellation dropping negative X-rotation sums#16605ashishpatel26 wants to merge 1 commit into
Conversation
When a set of commuting X-axis rotations summed to a negative angle that was an odd multiple of pi/2 (or -pi), the sx resynthesis loop computed the gate count with Rust's remainder operator, which returns a negative value for negative inputs. The insertion loop then ran zero times and the rotation was removed entirely, producing a non-equivalent circuit. Use Euclidean remainder instead; SX**4 == I exactly, so inserting num_sx.rem_euclid(4) gates is algebraically identical. Fixes Qiskit#16594
|
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the following people are relevant to this code:
|
|
Thanks for the interest. Unfortunately, this is a duplicate of #16599. Also: please take care that you are following the contributing guide. I believe this to be an unsupervised LLM PR in violation of the guide. These are not useful to us. |
Summary
Fixes #16594
CommutativeCancellationremoved a commuting pair ofsxdggates as if they were the identity, even thoughsxdg·sxdg = X(up to global phase), producing wrong results atoptimization_level≥ 2.Details and comments
Root cause: in
crates/transpiler/src/passes/commutation_cancellation.rs, thesxresynthesis branch computes the number ofSXgates to insert as(num_sx as i64) % 4. Rust's%returns a negative remainder for negative inputs (e.g. twosxdggive a combined angle of −π →num_sx = −2→-2 % 4 == -2), so the insertion loop0..-2ran zero times and the rotation vanished.Fix: use
rem_euclid(4). SinceSX⁴ == Iexactly (RX(2π)·e^{iπ} = (−I)·(−1) = I), insertingnum_sx.rem_euclid(4)gates is algebraically identical to insertingnum_sx, and the existing global-phase bookkeeping stays exact. The neighbouringXbranch is safe for negative angles: a singleXwith phase−π/2·num_xis exact for every oddnum_x, positive or negative.Added a regression test with the issue reproducer plus a parametrized test over other negative-sum sequences (
sxdg×3,sxdg×4,sxdg·x·sxdg), and a release note.Test evidence: issue reproducer now folds to
sx×3withOperator.equiv == True; full-transpile check equivalent at O1 and O3;test_commutative_cancellation.py66 passed + 28 subtests; adjacent suites (test_commutation_analysis,test_commutative_inverse_cancellation,test_optimize_1q_decomposition) 191 passed; 300-circuit random fuzz overx/sx/sxdg/rxsequences with exact-π angles: 0 non-equivalent outputs.