Skip to content

🐛 Fix linear typing violation in SCF conversion#1700

Open
li-mingbao wants to merge 2 commits into
munich-quantum-toolkit:mainfrom
li-mingbao:fix-scf-conversion
Open

🐛 Fix linear typing violation in SCF conversion#1700
li-mingbao wants to merge 2 commits into
munich-quantum-toolkit:mainfrom
li-mingbao:fix-scf-conversion

Conversation

@li-mingbao
Copy link
Copy Markdown
Contributor

Description

This PR modifies the insertion/extraction of qubits when a register is passed to an scf operation.
Qubits that are passed along their origin register are not inserted into the register anymore to avoid linear typing violation.

Fixes #1699

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.

@li-mingbao li-mingbao marked this pull request as ready for review May 12, 2026 16:05
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@burgholzer
Copy link
Copy Markdown
Member

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

✅ Actions performed

Full review triggered.

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.

This LGTM. Let's see if the rabbit still finds something.
(We turned off automatic code reviews from CodeRabbit because we were frequently hitting rate limits)

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Improved qubit handling in nested loop control operations by optimizing insertion and extraction logic to avoid redundant operations.
    • Enhanced QC-to-QCO dialect conversion with more efficient qubit management strategies.
  • Tests

    • Expanded test coverage for control operations in nested loops with distinct qubit sourcing scenarios.

Walkthrough

This PR fixes a linear typing violation in QC-to-QCO conversion when using SCF operations. The fix refactors qubit insertion/extraction to skip qubits already required by the operation target, preventing duplicate qubit uses. Test coverage is expanded with separate variants for different qubit sourcing patterns.

Changes

QC-to-QCO Linear Typing Fix

Layer / File(s) Summary
QCOProgramBuilder API contract
mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h
The helper method for preprocessing initialization arguments is renamed from insertExtractedQubits to prepareInitArgs with updated documentation clarifying its behavior.
Selective qubit insertion/extraction in QCToQCO conversion
mlir/lib/Conversion/QCToQCO/QCToQCO.cpp
New helpers insertQubitsBeforeOp and extractQubitsAfterOp replace the all-encompassing insert/extract methods, conditionally skipping qubits already present in state.regionQubitMap[target]. Applied across scf.for, scf.while, scf.if, scf.yield, and scf.condition.
QCOProgramBuilder prepareInitArgs implementation
mlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cpp
The new prepareInitArgs helper validates init argument types, collects provided qubits, and selectively reinserts extracted qubits from tensors. Call sites in scfFor, scfWhile, and qcoIf are wired to use the new helper.
Test program helpers for qubit sourcing patterns
mlir/unittests/programs/qc_programs.h, mlir/unittests/programs/qc_programs.cpp, mlir/unittests/programs/qco_programs.h, mlir/unittests/programs/qco_programs.cpp
Test helper nestedForLoopCtrlOp is split into two variants: nestedForLoopCtrlOpWithSeparateQubit (separate control allocation) and nestedForLoopCtrlOpWithExtractedQubit (extracted control from register), covering both qubit sourcing modes for QC and QCO builders.
Test instantiation updates
mlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cpp, mlir/unittests/Conversion/QCToQCO/test_qc_to_qco.cpp
Parameterized test suites updated to instantiate both new test program variants instead of the single prior case, expanding coverage of the selective insertion/extraction logic.
Changelog documentation
CHANGELOG.md
PR #1700 reference added to the Unreleased "Added" section for QC and QCO MLIR dialect infrastructure.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

bug, fix, c++, MLIR

Suggested reviewers

  • burgholzer
  • denialhaag

Poem

🐰 A qubit's journey through the loop so tight,
No longer duplicates—they're reinserted right!
Selective handlers skip what's already there,
SCF ops now convert with tensor care!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.04% 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 clearly summarizes the main change: fixing a linear typing violation in SCF conversion, which directly aligns with the PR's core objective.
Description check ✅ Passed The description provides the necessary context: it identifies the specific problem (linear typing violation), explains the solution (avoiding redundant insertion of qubits), and references the fixed issue #1699.
Linked Issues check ✅ Passed The code changes directly address issue #1699 by modifying qubit insertion/extraction logic to prevent reusing SSA values that originate from the same tensor register when already present as init arguments.
Out of Scope Changes check ✅ Passed All changes are narrowly focused on fixing the linear typing violation in SCF operations: helper function renames, updated insertion/extraction logic, and corresponding test case updates remain in scope.

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

✨ Finishing Touches
✨ Simplify code
  • Create PR with simplified code

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

Inline comments:
In `@mlir/unittests/programs/qco_programs.h`:
- Around line 1043-1045: The API comment for
nestedForLoopCtrlOpWithExtractedQubit contains a duplicated word "the" ("where
the the qubit..."); edit the comment above the function declaration to remove
the extra "the" so it reads "where the qubit is extracted from the register."
and keep the rest of the sentence unchanged.
🪄 Autofix (Beta)

❌ Autofix failed (check again to retry)

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: 1bd1b969-bcaa-4447-8978-9e6b79421974

📥 Commits

Reviewing files that changed from the base of the PR and between 4fe097c and fb8cb31.

📒 Files selected for processing (10)
  • CHANGELOG.md
  • mlir/include/mlir/Dialect/QCO/Builder/QCOProgramBuilder.h
  • mlir/lib/Conversion/QCToQCO/QCToQCO.cpp
  • mlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cpp
  • mlir/unittests/Conversion/QCOToQC/test_qco_to_qc.cpp
  • mlir/unittests/Conversion/QCToQCO/test_qc_to_qco.cpp
  • mlir/unittests/programs/qc_programs.cpp
  • mlir/unittests/programs/qc_programs.h
  • mlir/unittests/programs/qco_programs.cpp
  • mlir/unittests/programs/qco_programs.h

Comment on lines +1043 to +1045
/// Creates a circuit with a for operation with a register and a qubit and a
/// nested ctrl operation where the the qubit is extracted from the register.
void nestedForLoopCtrlOpWithExtractedQubit(QCOProgramBuilder& b);
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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Fix duplicated word in the API comment.

Line 1044 says “where the the qubit…”. Please remove the duplicated “the”.

🤖 Prompt for 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.

In `@mlir/unittests/programs/qco_programs.h` around lines 1043 - 1045, The API
comment for nestedForLoopCtrlOpWithExtractedQubit contains a duplicated word
"the" ("where the the qubit..."); edit the comment above the function
declaration to remove the extra "the" so it reads "where the qubit is extracted
from the register." and keep the rest of the sentence unchanged.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 12, 2026

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

An unexpected error occurred while generating fixes: Not Found - https://docs.github.com/rest/git/refs#get-a-reference

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Linear Typing Violation After QC to QCO Conversion Using SCF Ops

2 participants