Skip to content

♻️ Use WireIterator in MergeSingleQubitRotationGates #1674

Merged
burgholzer merged 11 commits intomainfrom
enh/use-wireiterator-in-merge-1q-rotations
May 4, 2026
Merged

♻️ Use WireIterator in MergeSingleQubitRotationGates #1674
burgholzer merged 11 commits intomainfrom
enh/use-wireiterator-in-merge-1q-rotations

Conversation

@MatthiasReumann
Copy link
Copy Markdown
Collaborator

@MatthiasReumann MatthiasReumann commented Apr 29, 2026

Description

While looking at the pass I noticed an opportunity to utilize the wire iterator. Hence, this pull request. Cherry-picks the wire iterator changes introduced in #1600 to support tensors.

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 changed the title ✨ Use WireIterator in MergeSingleQubitRotationGates ♻️ Use WireIterator in MergeSingleQubitRotationGates Apr 29, 2026
@MatthiasReumann MatthiasReumann self-assigned this Apr 29, 2026
@MatthiasReumann MatthiasReumann added c++ Anything related to C++ code MLIR Anything related to MLIR labels Apr 29, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@MatthiasReumann MatthiasReumann marked this pull request as ready for review April 29, 2026 12:58
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 29, 2026

📝 Walkthrough

Walkthrough

Refactors MergeSingleQubitRotationGates to use pointer-based operation checks and replaces SSA-user traversal with qubit-wire traversal via WireIterator; WireIterator traversal was updated to treat qtensor::InsertOp/qtensor::ExtractOp as traversal sentinels. CHANGELOG entry updated to add PR [#1674].

Changes

Merge pass -> WireIterator integration

Layer / File(s) Summary
Iterator semantics
mlir/lib/Dialect/QCO/Utils/WireIterator.cpp
WireIterator now treats qtensor::InsertOp/qtensor::ExtractOp as traversal sentinels: qubit() returns nullptr for SinkOp and qtensor::InsertOp; forward()/backward() stop/rewind at qtensor::ExtractOp and treat YieldOp/qtensor::InsertOp as end/start markers.
Core pass signature change
mlir/lib/Dialect/QCO/Transforms/Optimizations/MergeSingleQubitRotationGates.cpp
areQuaternionMergeable changed to accept Operation* parameters and calls isMergeable on pointers.
Chain collection / traversal
mlir/lib/Dialect/QCO/Transforms/Optimizations/MergeSingleQubitRotationGates.cpp
collectChain rewritten to iterate forward along the same qubit wire using WireIterator from the chain head’s output qubit, appending UnitaryOpInterface ops until areQuaternionMergeable fails. isChainStart updated to fetch defining op pointers and use pointer-based mergeability checks.
Changelog
CHANGELOG.md
Adds [#1674] reference to the merge-single-qubit-rotation-gates entry and PR links section.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • burgholzer

Poem

🐰 I hop along the qubit line,
Pointers lead where wires entwine,
Insert and extract mark the way,
Gates gather neat, then glide away,
A rabbit's cheer for cleaner play.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: refactoring the MergeSingleQubitRotationGates pass to use WireIterator, which is the primary objective of this PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The PR description is well-structured and comprehensive, following the template with a clear summary, checked checklist items, and AI usage disclosure.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch enh/use-wireiterator-in-merge-1q-rotations

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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: 1

🤖 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/Utils/WireIterator.cpp`:
- Around line 87-95: In backward() guard against op_ being nullptr before the
TypeSwitch<Operation*>(op_) so we don't dereference op in the .Default() path;
check if op_ == nullptr and handle it (e.g., return/stop iteration) prior to the
TypeSwitch, and apply the same nullptr guard for the other similar occurrence
around the code that uses op->getName() (the block using report_fatal_error and
the cases for UnitaryOpInterface, MeasureOp, ResetOp) so report_fatal_error is
never called on a null op_.
🪄 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: b0f0a221-5b5b-4450-b4d5-449e67738d0e

📥 Commits

Reviewing files that changed from the base of the PR and between d7ced80 and 0b5f825.

📒 Files selected for processing (2)
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/MergeSingleQubitRotationGates.cpp
  • mlir/lib/Dialect/QCO/Utils/WireIterator.cpp

Comment thread mlir/lib/Dialect/QCO/Utils/WireIterator.cpp
@burgholzer
Copy link
Copy Markdown
Member

@MatthiasReumann would you mind resolving the conflicts stemming from #1673 before I take a look here?

Signed-off-by: Daniel Haag <121057143+denialhaag@users.noreply.github.com>
@mergify mergify Bot removed the conflict label Apr 29, 2026
@denialhaag
Copy link
Copy Markdown
Member

@MatthiasReumann would you mind resolving the conflicts stemming from #1673 before I take a look here?

Should be done. 👍

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.

LGTM 👍🏼
It does not save much here, but still feels rather clean.

@burgholzer burgholzer added this to the MLIR Support milestone May 4, 2026
@burgholzer burgholzer added the code quality Code quality improvements label May 4, 2026
@burgholzer burgholzer enabled auto-merge (squash) May 4, 2026 15:23
@burgholzer burgholzer merged commit 7dfcbea into main May 4, 2026
29 checks passed
@burgholzer burgholzer deleted the enh/use-wireiterator-in-merge-1q-rotations branch May 4, 2026 15:40
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 code quality Code quality improvements MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants