Skip to content

✨ Add replaceWithAdditionalQubits utility function to qco.if - #1872

Merged
MatthiasReumann merged 19 commits into
mainfrom
enh/qco-if-extend-qubits-func
Jul 9, 2026
Merged

✨ Add replaceWithAdditionalQubits utility function to qco.if#1872
MatthiasReumann merged 19 commits into
mainfrom
enh/qco-if-extend-qubits-func

Conversation

@MatthiasReumann

@MatthiasReumann MatthiasReumann commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

Description

Adds the analog of MLIR's replaceWithreplaceWithAdditionalIterOperands to the qco::IfOp.

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 added this to the MLIR Support milestone Jul 8, 2026
@MatthiasReumann MatthiasReumann self-assigned this Jul 8, 2026
@MatthiasReumann MatthiasReumann added c++ Anything related to C++ code MLIR Anything related to MLIR labels Jul 8, 2026
@MatthiasReumann
MatthiasReumann marked this pull request as ready for review July 8, 2026 12:23
@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a new IfOp::replaceWithAdditionalQubits helper in QCOOps.td and IfOp.cpp that creates a replacement qco.if with extra qubit operands and updated branch yields. Updates CHANGELOG.md with the PR citation and link.

Changes

IfOp qubit expansion

Layer / File(s) Summary
IfOp interface declaration
mlir/include/mlir/Dialect/QCO/IR/QCOOps.td
Declares IfOp::replaceWithAdditionalQubits(RewriterBase&, ValueRange) as a C++ extra class method with documentation.
IfOp replacement implementation
mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp
Implements the method: builds a new IfOp with combined qubit operands, merges then/else blocks with expanded arguments, and rewrites YieldOp terminators to yield additional qubit values.
Changelog entry
CHANGELOG.md
Adds #1872 to the Unreleased → Added citation line and the PR links section.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • munich-quantum-toolkit/core#1506: Both PRs modify the QCO IfOp implementation in QCOOps.td/SCF/IfOp.cpp, with #1506 introducing the base semantics extended here.

Suggested labels: feature

Suggested reviewers: burgholzer

Poem

A rabbit hops through branches two,
Then and else, both bend anew,
Extra qubits join the yield,
New IfOp born upon the field,
Hop, hop, hooray — the code compiles true! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
Title check ✅ Passed The title is concise and accurately names the new replaceWithAdditionalQubits utility for qco.if.
Description check ✅ Passed The description includes a clear summary and the required checklist, and the omitted issue/dependency details appear acceptable here.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch enh/qco-if-extend-qubits-func

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp`:
- Around line 360-361: The insertion point set on the YieldOp is redundant
because replaceOpWithNewOp<YieldOp>(yield, newResults) already inserts the new
op at the erased op’s location. Remove the unnecessary
rewriter.setInsertionPoint(yield) call in the IfOp lowering path and leave the
replacement call as the only insertion-site logic.
- Around line 340-343: The new region block in IfOp::create/rewriting currently
hardcodes every block argument to QubitType, which breaks qtensor operands and
causes a type mismatch with qco.if’s Variadic<LinearType> signature. Update the
block creation to preserve the original operand types by using inits.getTypes()
when building the argument type list, so the region arguments match the
operand/result types for both qubits and qtensors.
🪄 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: 47b3d03e-7dd0-4086-926c-8e136383c065

📥 Commits

Reviewing files that changed from the base of the PR and between 0cb7894 and a31907f.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QCO/IR/QCOOps.td
  • mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp

Comment thread mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp Outdated
Comment thread mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp Outdated
@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 27 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp 0.0% 27 Missing ⚠️

📢 Thoughts on this report? Let us know!

@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@burgholzer

Should we add an additional test-suite for this function or rely on the usage of the function in #1870 for coverage?

@burgholzer

Copy link
Copy Markdown
Member

@burgholzer

Should we add an additional test-suite for this function or rely on the usage of the function in #1870 for coverage?

Let's rely on #1870. 👍🏼

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a couple of smaller comments. Generally LGTM

Comment thread mlir/include/mlir/Dialect/QCO/IR/QCOOps.td Outdated
Comment thread mlir/include/mlir/Dialect/QCO/IR/QCOOps.td
Comment thread mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp Outdated
Comment thread mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp Outdated
@mergify mergify Bot added the conflict label Jul 8, 2026
@mergify mergify Bot removed the conflict label Jul 9, 2026
@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp`:
- Around line 345-373: Replace the original IfOp before returning the new one in
the IfOp::replace/clone flow: processRegion correctly migrates the then/else
bodies into newIfOp, but the old op is never rewritten, so existing uses still
reference its results. Update the logic around processRegion and the final
return to call rewriter.replaceOp on the current IfOp with the corresponding
results from newIfOp, or otherwise ensure the caller performs that replacement
consistently.
🪄 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: b1a67669-b1e7-4162-8028-772f9bf73d1a

📥 Commits

Reviewing files that changed from the base of the PR and between ca33c41 and ebb4254.

📒 Files selected for processing (3)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QCO/IR/QCOOps.td
  • mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp

Comment thread mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp Outdated

@burgholzer burgholzer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Pushed one commit that simplifies part of the implementation a bit.
Feel free to merge if you agree with the implementation and the CI is green.

@MatthiasReumann
MatthiasReumann enabled auto-merge July 9, 2026 10:35
@MatthiasReumann
MatthiasReumann merged commit c92ea1e into main Jul 9, 2026
28 of 29 checks passed
@MatthiasReumann
MatthiasReumann deleted the enh/qco-if-extend-qubits-func branch July 9, 2026 10:36
@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@burgholzer Thanks for the cleanup! Looks good 🚀

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