Skip to content

🐛 Propagate correct mappings to parent of scf::IfOp branches#1904

Merged
burgholzer merged 10 commits into
mainfrom
bug/wires-after-if-op-map
Jul 15, 2026
Merged

🐛 Propagate correct mappings to parent of scf::IfOp branches#1904
burgholzer merged 10 commits into
mainfrom
bug/wires-after-if-op-map

Conversation

@MatthiasReumann

@MatthiasReumann MatthiasReumann commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Description

Currently, the mapping pass has the following critical issues:

  • After mapping an qco::IfOp, the dispatch function does not properly propagate the updated mappings (layout, wire-infos) to its parent operation.
  • The qco::Yield operation in each branch does not guarantee that the same hardware qubits, in the same order, are returned. However, this property must be enforced for proper linear typing. Semantically, any SCF-op must act as a black-box consuming and producing N hardware qubits in the same order. However, contrary to the scf::ForOp where we restore the layout, the qco::IfOp acts like a permutation network, where the return hardware qubits might "house" different program qubits.

This pull request fixes these issues. I've also tried to improve the documentation (the comments) and code quality along the way for both the mapping pass and its tests.

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 15, 2026
@MatthiasReumann MatthiasReumann self-assigned this Jul 15, 2026
@MatthiasReumann MatthiasReumann added bug Something isn't working c++ Anything related to C++ code MLIR Anything related to MLIR labels Jul 15, 2026
@MatthiasReumann

Copy link
Copy Markdown
Collaborator Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

QCO mapping updates add layout equality, correct additional-qubit IfOp rewriting, return converged routing layouts, restore branch and parent wire ordering, and expand mapping tests for control-flow hardware ordering and GHZ circuits.

Changes

QCO mapping behavior

Layer / File(s) Summary
Conditional operation result rewriting
mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp
Additional-qubit rewriting creates located blocks and replaces the original IfOp with the matching new results.
Layout convergence and branch routing
mlir/include/mlir/Dialect/QCO/Utils/Layout.h, mlir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp
Routing convergence returns the shared layout and branch SWAP sequences; dispatch updates parent layouts, wire information, and YieldOp ordering.
Mapping executability tests
mlir/unittests/Dialect/QCO/Transforms/Mapping/test_mapping.cpp
Tests validate coupling and control-flow hardware ordering, add GHZ helpers, and rename mapping scenarios.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant dispatch
  participant converge
  participant IfOp
  participant Layout
  dispatch->>IfOp: Route then and else regions
  dispatch->>converge: Converge branch layouts
  converge->>Layout: Return converged layout
  dispatch->>IfOp: Insert branch SWAPs
  dispatch->>IfOp: Reorder yielded qubits in Hot mode
  dispatch->>Layout: Update parent layout and wire information
Loading

Possibly related PRs

Suggested labels: fix

Suggested reviewers: pre-commit-ci[bot], burgholzer

Poem

I’m a rabbit routing qubits tonight,
Through branching paths kept neat and tight.
Layouts now agree, swaps fall in line,
GHZ loops sparkle, outcomes shine.
Hop, hop—the mapping is right!

🚥 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 accurately summarizes the main change: propagating correct mappings for qco::IfOp branches.
Description check ✅ Passed The description includes the change summary, motivation, context, and a completed checklist, which matches the template well.
✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch bug/wires-after-if-op-map

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.

@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.64706% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...lir/lib/Dialect/QCO/Transforms/Mapping/Mapping.cpp 97.4% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@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.

LGTM 👍🏼

@burgholzer
burgholzer merged commit cf2f78c into main Jul 15, 2026
29 checks passed
@burgholzer
burgholzer deleted the bug/wires-after-if-op-map branch July 15, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working 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