Skip to content

Speed up commutation-aware rewrite searches - #5298

Open
taalexander wants to merge 1 commit into
NVIDIA:mainfrom
taalexander:perf/general-commutation-interaction-index
Open

Speed up commutation-aware rewrite searches#5298
taalexander wants to merge 1 commit into
NVIDIA:mainfrom
taalexander:perf/general-commutation-interaction-index

Conversation

@taalexander

@taalexander taalexander commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Fixes #5268 and #5269. QuakeSimplify currently scans every earlier operation when looking for a gate to combine. For example,

  S(q0)          possible match
  H(q0)          barrier
  H/X(q1) ...    unrelated gap
  S(q0)          search starts here

Previously, the search checked every q1 gate before reaching the H(q0) that stops it (as wires cannot always be followed due to a bunch of other issues noted below). Repeating this across a growing block can produce quadratic scan work.

This change builds a lazy interaction index for each known virtual qubit to operation. This is done on segment boundaries and when I say "virtual" qubit I mean a qubit that is statically knowable as equivalent to itself in the wire-flow chain:

  q0: S, H, S
  q1: H, X, ...

The q0 search now skips the q1 stream and reaches the same H(q0) immediately. Unsupported operations form segment boundaries, while unresolved qubit identities retain the existing block-order search.

All of the other commutation machinery has been kept the same.

Performance:

I tried to keep it as simple as possible but it was still more LOC than I would have liked. A large portion of the difficulty with all of this work is we really can't "just" follow value form as there are loads of special cases of aliasing, reference form, unknown qubit values, etc., making the wire semantics non-trivial and causing the analysis "segment" within a block. Perhaps the right fix would be to simplify the IR/make the value form more strict, however, I don't think this is possible right now.

To review I recommend looking at it in this order:

  1. find_nearest: unchanged matching rules and the handoff to indexed traversal.
  2. LogicalQubitInteractionIndex: operation classification, segments, and ordered streams.

Build a lazy block-local interaction index keyed by proven logical qubit identity. Segmented streams let find_nearest skip unrelated quantum operations while preserving the existing SSA frontier, endpoint predicates, conservative barriers, and physical fallback for unresolved anchors.

Keep index state private to CommutationAnalysis and maintain or discard it through the existing rewrite listener. Reuse normalized anchor support after construction and avoid indexing work on adjacent or otherwise inexpensive searches.

Verified with the focused CommutationAwareRewrite unit suite, QuakeSimplify commutation lit coverage, the verifier-enabled linear-value pipeline, public issue reproducers, FTQC hotspots, and NISQ regression guards.

Signed-off-by: Thomas Alexander <talexander@nvidia.com>
@taalexander
taalexander force-pushed the perf/general-commutation-interaction-index branch from 95790e1 to d4aace7 Compare August 27, 2026 01:22
@github-actions

Copy link
Copy Markdown

CI Summary (push) — ✅ passed

Run #33029896942 · ✅ 7 · ⏩ 7 · ❌ 0 · ⛔ 0

Top-level jobs (14)
Job Result
binaries ⏩ skipped
build_and_test ✅ success
changes ✅ success
config_devdeps ✅ success
config_source_build ⏩ skipped
config_wheeldeps ✅ success
devdeps ✅ success
docker_image ⏩ skipped
gen_code_coverage ⏩ skipped
metadata ✅ success
python_metapackages ⏩ skipped
python_wheels ⏩ skipped
source_build ⏩ skipped
wheeldeps ✅ success
⏩ Skipped jobs (7) — intentionally skipped on PR builds; run on merge_group / workflow_dispatch
Job
binaries
config_source_build
docker_image
gen_code_coverage
python_metapackages
python_wheels
source_build
All sub-jobs (43) — every matrix leg, with links
Job Status Link
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
CI Summary ❔ in_progress view
Check for stable CUDA-Q changes ✅ success view
Configure build (devdeps) ✅ success view
Configure build (source_build) ⏩ skipped view
Configure build (wheeldeps) ✅ success view
Create CUDA Quantum installer ⏩ skipped view
Create Docker images ⏩ skipped view
Create Python metapackages ⏩ skipped view
Create Python wheels ⏩ skipped view
Gen code coverage ⏩ skipped view
Load dependencies (amd64, gcc12) / Caching ✅ success view
Load dependencies (amd64, gcc12) / Finalize ✅ success view
Load dependencies (amd64, gcc12) / Metadata ✅ success view
Load dependencies (amd64, llvm) / Caching ✅ success view
Load dependencies (amd64, llvm) / Finalize ✅ success view
Load dependencies (amd64, llvm) / Metadata ✅ success view
Load dependencies (arm64, gcc12) / Caching ✅ success view
Load dependencies (arm64, gcc12) / Finalize ✅ success view
Load dependencies (arm64, gcc12) / Metadata ✅ success view
Load dependencies (arm64, llvm) / Caching ✅ success view
Load dependencies (arm64, llvm) / Finalize ✅ success view
Load dependencies (arm64, llvm) / Metadata ✅ success view
Load source build cache ⏩ skipped view
Load wheel dependencies (amd64, 12.6) / Caching ✅ success view
Load wheel dependencies (amd64, 12.6) / Finalize ✅ success view
Load wheel dependencies (amd64, 12.6) / Metadata ✅ success view
Load wheel dependencies (amd64, 13.0) / Caching ✅ success view
Load wheel dependencies (amd64, 13.0) / Finalize ✅ success view
Load wheel dependencies (amd64, 13.0) / Metadata ✅ success view
Load wheel dependencies (arm64, 12.6) / Caching ✅ success view
Load wheel dependencies (arm64, 12.6) / Finalize ✅ success view
Load wheel dependencies (arm64, 12.6) / Metadata ✅ success view
Load wheel dependencies (arm64, 13.0) / Caching ✅ success view
Load wheel dependencies (arm64, 13.0) / Finalize ✅ success view
Load wheel dependencies (arm64, 13.0) / Metadata ✅ success view
Prepare cache clean-up ❔ in_progress view
Retrieve PR info ✅ success view
✅ Required checks (6/6) — declared in .github/required-checks.yml for push
Required check Status Link
Build and test (amd64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Debug) ✅ success view
Build and test (arm64, llvm, openmpi) / Dev environment (Python) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Debug) ✅ success view
Build and test (amd64, gcc12, openmpi) / Dev environment (Python) ✅ success view

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce performance cost of failed searches in CommutationAwareRewriteMatcher

1 participant