Skip to content

✨ Skip Two-Qubit Blocks In Mapping Pass#1588

Merged
MatthiasReumann merged 4 commits intomainfrom
enh/skip-2q-blocks
Mar 29, 2026
Merged

✨ Skip Two-Qubit Blocks In Mapping Pass#1588
MatthiasReumann merged 4 commits intomainfrom
enh/skip-2q-blocks

Conversation

@MatthiasReumann
Copy link
Copy Markdown
Collaborator

@MatthiasReumann MatthiasReumann commented Mar 29, 2026

Description

This pull request re-implements the skip two-qubit block logic of the old implementation.

Checklist

  • The pull request only contains commits that are focused and relevant to this change.
  • I have added appropriate tests that cover the new/changed functionality.
  • I have updated the documentation to reflect these changes.
  • I have added entries to the changelog for any noteworthy additions, changes, fixes, or removals.
  • I have added migration instructions to the upgrade guide (if needed).
  • The changes follow the project's style guidelines and introduce no new warnings.
  • The changes are fully tested and pass the CI checks.
  • I have reviewed my own code changes.

If PR contains AI-assisted content:

  • I have disclosed the use of AI tools in the PR description as per our AI Usage Guidelines.
  • AI-assisted commits include an Assisted-by: [Model Name] via [Tool Name] footer.
  • I confirm that I have personally reviewed and understood all AI-generated content, and accept full responsibility for it.

@MatthiasReumann MatthiasReumann self-assigned this Mar 29, 2026
@MatthiasReumann MatthiasReumann added c++ Anything related to C++ code MLIR Anything related to MLIR labels Mar 29, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 29, 2026

Warning

Rate limit exceeded

@MatthiasReumann has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 5 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 3 minutes and 5 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6a542123-cf23-4a25-aee9-94897c438806

📥 Commits

Reviewing files that changed from the base of the PR and between 148aae6 and d905c5c.

📒 Files selected for processing (2)
  • mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
  • mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp
📝 Walkthrough

Walkthrough

Refactors wire traversal and two-qubit-block advancement in the QCO mapping pass, changes trial layout generation to exclude the identity layout, updates a unit test to add additional gate sequence, and adds a PR link in CHANGELOG.md.

Changes

Cohort / File(s) Summary
Documentation
CHANGELOG.md
Added PR reference [#1588] in the place-and-route pass entry and added link definition for [#1588].
Mapping Pass Logic
mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp
Refactored traversal termination into proceedOnWire<Direction>; added skipTwoQubitBlock<Direction> to advance paired wire iterators over matching two-qubit blocks; removed identity layout from trial generation; changed LayoutInfo forward declaration from struct to class and updated Layout friendship.
Unit Tests
mlir/unittests/Dialect/QCO/Transforms/Mapping/test_mapping.cpp
Extended TEST_P(MappingPassTest, Sabre) by inserting h, y, cx sequence between existing two-qubit gates to exercise updated block-skipping behavior.
Pass Option Metadata
mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
Clarified MappingPass ntrials option help text to state “Must be > 0” (documentation-only change).

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • burgholzer
  • denialhaag

Poem

🐰 I hop through wires, two qubits in tune,
I skip matched blocks beneath the moon,
A tiny refactor, a clever rearrange,
Trials trimmed down, the mapping's changed—
Hoppity hop, the quantum tune! 🐇

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change - implementing skip two-qubit blocks logic in the mapping pass, which aligns with the primary modifications in the Mapping.cpp file.
Description check ✅ Passed The description provides a summary of the change and includes all required checklist items marked as complete, indicating proper documentation, testing, and changelog updates.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch enh/skip-2q-blocks

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp`:
- Around line 391-396: ntrials is allowed to be 0 which leaves trials/results
empty and makes findBestTrial() return nullptr; change the logic in the trial
creation path (around the loop that fills trials using
Layout::random(arch.nqubits(), rng())) to explicitly handle this->ntrials == 0:
either reject the option up front (return an error) or ensure at least one
fallback trial is pushed (e.g., push an identity/initial layout before or
instead of random trials) so trials and results are never empty and
findBestTrial() cannot return nullptr; update any related uses that assume
trials.size() > 0 accordingly.
- Around line 643-649: After calling advanceUntilTwoQubitOp(first) and
advanceUntilTwoQubitOp(second) ensure you do not dereference exhausted
iterators: before using first.operation() or second.operation() check that
neither wire iterator is exhausted (e.g., test the iterator/handle sentinel or a
provided isDone()/isEnd()/hasValue() predicate) and break out if either is
exhausted; only call first.operation() and second.operation() when both are
valid. This prevents dereferencing a wire that hit the forward sentinel or
backward start inside advanceUntilTwoQubitOp.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f667debf-55c8-496f-be81-efe1a1317ac9

📥 Commits

Reviewing files that changed from the base of the PR and between b1c09da and 34918d9.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp
  • mlir/unittests/Dialect/QCO/Transforms/Mapping/test_mapping.cpp

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

♻️ Duplicate comments (1)
mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp (1)

393-397: ⚠️ Potential issue | 🟠 Major

Reject ntrials == 0 in code, not just in the help text.

At Line 394, 0 is still accepted. That leaves trials and results empty, so findBestTrial() returns nullptr and the pass fails even on otherwise routable input.

🛠️ Suggested guard
       SmallVector<Layout> trials;
+      if (this->ntrials == 0) {
+        func.emitError() << "`ntrials` must be greater than 0";
+        signalPassFailure();
+        return;
+      }
       trials.reserve(this->ntrials);
       for (std::size_t i = 0; i < this->ntrials; ++i) {
         trials.emplace_back(Layout::random(arch.nqubits(), rng()));
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp` around lines 393 - 397,
Reject zero trials early: guard against this->ntrials == 0 in Mapping.cpp before
reserving/emplacing into trials (the loop that calls Layout::random) and
fail-fast (e.g., return error/emit diagnostic/throw) so trials and results are
never left empty and findBestTrial() won't be called on an empty set; update the
code path that constructs trials (referencing this->ntrials, trials,
Layout::random, and findBestTrial()) to validate ntrials >= 1 and produce a
clear diagnostic if it's 0.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@mlir/include/mlir/Dialect/QCO/Transforms/Passes.td`:
- Around line 67-69: Update the long pass description in Passes.td to match the
new behavior of ntrials: edit the textual paragraph that describes the pass (the
same block that contains the Option<"ntrials", "ntrials", "std::size_t", "4",
...>) to remove the claim that the pass "always explores the identity layout"
and instead state that the pass performs up to ntrials random forward/backward
mapping trials (possibly in parallel) to search for improved layouts, and
clarify that ntrials must be > 0; ensure the wording aligns with the
implementation in Mapping.cpp which no longer forces an identity-layout
exploration.

In `@mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp`:
- Around line 610-619: Update the Doxygen for template <Direction d> static bool
proceedOnWire(const WireIterator& it) to correctly describe its contract: state
that it returns true when traversal can continue along the wire (i.e., for
Direction::Forward it returns true while the iterator has not reached the end
sentinel, and for Direction::Backward it returns true while the iterator's
operation is not an AllocOp), and false when traversal should stop; reference
the symbols proceedOnWire, Direction, WireIterator, and AllocOp in the comment
so callers understand the precise forward/backward semantics.

---

Duplicate comments:
In `@mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp`:
- Around line 393-397: Reject zero trials early: guard against this->ntrials ==
0 in Mapping.cpp before reserving/emplacing into trials (the loop that calls
Layout::random) and fail-fast (e.g., return error/emit diagnostic/throw) so
trials and results are never left empty and findBestTrial() won't be called on
an empty set; update the code path that constructs trials (referencing
this->ntrials, trials, Layout::random, and findBestTrial()) to validate ntrials
>= 1 and produce a clear diagnostic if it's 0.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: acee8b66-b0f5-4e2a-a57d-708a1dc6e920

📥 Commits

Reviewing files that changed from the base of the PR and between 34918d9 and 148aae6.

📒 Files selected for processing (2)
  • mlir/include/mlir/Dialect/QCO/Transforms/Passes.td
  • mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Copy Markdown
Member

@burgholzer burgholzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already approved before. Still looks good. I am going to ignore the one duplicate comment from code rabbit about actually enforcing ntrials to be non-zero. One could place an assertion in the code for that. Feel free to merge if you are happy.

@MatthiasReumann MatthiasReumann merged commit 3e6b458 into main Mar 29, 2026
29 checks passed
@MatthiasReumann MatthiasReumann deleted the enh/skip-2q-blocks branch March 29, 2026 15:57
@denialhaag denialhaag changed the title ✨Skip Two-Qubit Blocks In Mapping Pass ✨ Skip Two-Qubit Blocks In Mapping Pass Mar 31, 2026
@coderabbitai coderabbitai bot mentioned this pull request Apr 1, 2026
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Anything related to C++ code MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants