Repository navigation
Move only when it saves a two-qubit gate, and make the check cheap - #2
Open
masterFoad wants to merge 6 commits into
Open
masterFoad wants to merge 6 commits into
masterFoad wants to merge 6 commits into
Conversation
check_commutation_entries now takes each qubit label as a Qubit or as an integer converted with operator.index, so NumPy integers name the same qubit as equal Python ints. A bool or any other label raises QiskitError instead of silently giving None. The shared get_bits_from_py helper, commute() and commute_nodes() are unchanged. _AbsorbIntoSwaps keeps the labels it derives from the session commutation table in a module-level cache keyed by (name, width), so a second pass call does not query the table again. Tests that replace the lookup clear the cache for their duration. Document why the crz, crx, rzz, rxx, ryy and rzx labels found by the parameter-free lookup hold at every angle.
A move is now made only when the summed two-qubit cost of the same-pair blocks around the swap and the moved unit drops by at least 1. The cost is the count the optimization stage's two-qubit synthesis produces (Weyl coordinates with the synthesis' default specialization; a coordinate between 1e-12 and 1e-9 makes it unknown). An unknown cost, a block longer than 256 units on a wire, or a symbolic parameter in a block means no move. A dropped move keeps its unit taken. Zero-gain moves were the cause of the device ESP losses of the lean pass (more single-qubit gates after resynthesis at an unchanged two-qubit count); on Benchpress run 1 this rule (arm S1b) took esp_gates-worse pairs from 21/22 to 0/0 at O2/O3 for +2,714/+2,654 CX on 99M. The preset pass managers pass their target, used only to cost a continuous rzz as one gate.
_matrix now reads a standard gate's matrix straight from the DAG node, after the same classical-argument and parameter checks, without first touching node.op, which builds a Python gate object. Other gates keep the existing Gate/to_matrix path. The returned matrices are unchanged.
The gain analysis lifts a 2x2 single-qubit matrix to 4x4 with the same np.kron call many times. One _GainReuse per _find_moves call now keeps those lifts, keyed by the wire side and the matrix shape, dtype and bytes, so only byte-identical inputs share a result and a miss still runs the original kron. Each unit matrix in a block is also read once. The predicted gains, and so the moves, are unchanged.
The same 4x4 block is often costed again for neighbouring moves. The per-analysis _GainReuse now also keeps two_qubit_cost results, keyed by the fractional flag and the matrix shape, dtype and bytes. An unknown (None) cost is cached as well, so an unchanged undecidable block still rejects a move. The predicted gains, and so the moves, are unchanged.
Readability only; the predicted gains, and so the moves made, are unchanged. _move_gain now reads as: widen the slice on each wire, cut the slices into blocks before and after the move, and return the summed cost difference. The widening, block, unit-matrix, soft-unit and cost-sum steps are module helpers with explicit arguments. The per-move unit-matrix memo stays local to _move_gain, and the one _GainReuse is still shared by the moves of one gainful _find_moves call. Iteration order, matrix products, cache keys, early returns and the None results for missing matrices, unknown costs and over-cap slices are kept as they were; cost-provider errors still propagate. The eight-tuple context becomes the _GainContext named tuple over the same lists, and the -2/-1 unit keys are named _SINGLE_QUBIT_UNIT and _UNSUPPORTED_UNIT. The _GainReuse docstring now covers both lifts and costs, and _MAX_SLICE's bound on the work per move is explained once. Add regression tests for lift and unknown-cost reuse, a failing custom matrix provider, cost-provider exceptions, the exact slice cap boundary and the order and scope of reuse across candidate moves.
masterFoad
changed the base branch from
commutation-checker-followup
to
absorb-into-swaps-lean-commutation-checker
October 25, 2026 10:22
masterFoad
pushed a commit
that referenced
this pull request
Nov 23, 2026
…DiagonalGate (Qiskit#16908) * Initial/partial porting of _dec_ucg to Rust * Formatting * Rust porting improvements, formatting * Formatting * Fix lint and miri errors * Fixed dependency of accelerate on synthesis create * Formatting * Removed unused dec_ucg_help PyO3 wrapper and renamed the dec_ucg_help_inner * Updated DiagonalGate define to call ported rust synthesis * Formating * add releasenotes * Fix of rust functions visibility * Removed unused python interface from symplify function * Extended Rust tests and added error handling in PYO3 wrappers * Docstring added to uc_gate.rs and diagonal.rs * shortened release notes * Fixes addressing reiviewers comments * Formatting * Fix Lint Errors * Address reviewers comments #2 * Add missing file * Address reviewers comments * Addressing reviewers' comments * Final minor/cosmetics updates to the fixes * Formatting * Add GATE_EQ_TOL constan * Add test verifying cx count * Add verification of input arguments for dec_ucg
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.
Into your branch, one PR for everything (replaces #1).
bool and others instead of returning None) and cache the derived labels.
one two-qubit gate fewer. Zero-gain moves were adding 1q gates and lowering
estimated device fidelity on some cases.
building the Python op, exact one-qubit lifts, exact two-qubit costs).
Benchpress on CCC (3057 cases x 2 levels x 3 seeds), vs main:
2q count -0.39% at O2, 1018 better / 0 worse; device ESP 181 better / 0 worse.
Output identical with and without the caches on all 6246 cases; the pass is
~8.5% faster with them (transpile 1.079x of main at O2, 1.039x at O3).
161 tests pass. Commits are kept separate so you can review them one by one.