Skip to content

✨ Add QCO If operation#1506

Merged
burgholzer merged 72 commits intomunich-quantum-toolkit:mainfrom
li-mingbao:qco-if
Feb 21, 2026
Merged

✨ Add QCO If operation#1506
burgholzer merged 72 commits intomunich-quantum-toolkit:mainfrom
li-mingbao:qco-if

Conversation

@li-mingbao
Copy link
Contributor

@li-mingbao li-mingbao commented Feb 9, 2026

Description

This PR adds the qco.if operation to the QCO dialect. This operation is similar to the scf.if operation but it takes an additional list of qubits as input that are passed down to the branches as block arguments. This is used in order to support linar typing for qubit values.

Example:

 %q2 = qco.if %condition qubits(%arg0 = %q0) {
      %q1 = qco.h %arg0 : !qco.qubit -> !qco.qubit
      qco.yield %q1
    } else qubits(%arg0 = %q0) {
      qco.yield %arg0
    } : {i1, !qco.qubit} -> {!qco.qubit}

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.

@codecov
Copy link

codecov bot commented Feb 9, 2026

Codecov Report

❌ Patch coverage is 71.18644% with 51 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
mlir/lib/Dialect/QCO/IR/QCOOps.cpp 18.7% 26 Missing ⚠️
mlir/lib/Dialect/QCO/IR/SCF/IfOp.cpp 78.5% 24 Missing ⚠️
mlir/lib/Dialect/QCO/Builder/QCOProgramBuilder.cpp 96.9% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@mergify mergify bot added the conflict label Feb 11, 2026
@mergify mergify bot removed the conflict label Feb 12, 2026
@li-mingbao
Copy link
Contributor Author

@coderabbitai resume

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 20, 2026

✅ Actions performed

Reviews resumed.

Copy link
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/IR/QCOOps.cpp`:
- Around line 118-152: The parser currently deduplicates operands after parsing
then/else aliasing, which can mask ordering or set mismatches; update
parseIfOpAliasing to validate that the aliasing lists produced by
parseTargetAliasing for thenRegion and elseRegion are identical (same operands
in the same order) before performing the llvm::erase_if dedupe step, and return
failure() if they differ so IfOp::verify() and operand-to-block-argument
alignment remain correct; locate this check inside parseIfOpAliasing immediately
after both parseTargetAliasing calls and before the duplicate-removal logic.

In `@mlir/unittests/programs/qco_programs.cpp`:
- Around line 2017-2050: In nestedTrueIf and nestedFalseIf fix incorrect
brace-initializer conversions from ValueRange/ValueRange-returning qcoIf to
SmallVector: where the lambdas return
llvm::SmallVector<mlir::Value>{innerQubits} or
llvm::SmallVector<mlir::Value>{innerResult}, replace those with
llvm::to_vector(innerQubits) and llvm::to_vector(innerResult) respectively so
the ValueRange is properly converted to a SmallVector; update the inner lambdas
passed to qcoIf (referenced symbols: nestedTrueIf, nestedFalseIf, innerQubits,
innerResult, qcoIf) accordingly.

Copy link
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/unittests/programs/qco_programs.cpp`:
- Around line 2026-2027: Remove the stray unreachable semicolon after the return
expression in the function that returns llvm::to_vector(innerResult) (the line
showing "return llvm::to_vector(innerResult); ;"); locate the return statement
(e.g., where llvm::to_vector(innerResult) is returned) and delete the extra
trailing ";" so the function has a single valid return statement.

@li-mingbao
Copy link
Contributor Author

@burgholzer
I applied your feedback and added the tests to the new testsuite.
The coverage is still a bit low but that is mainly the direct builder function that is never used as the QCOProgramBuilder does not call it and the parser. I tested them locally so they should work.

@mergify mergify bot added the conflict label Feb 21, 2026
Signed-off-by: Lukas Burgholzer <burgholzer@me.com>
@mergify mergify bot removed the conflict label Feb 21, 2026
Copy link
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.

Thanks @li-mingbao 🙏🏼
I only have one final round of comments for the tests, which I have not looked at in detail before. These should be very easy to address and then this can go in. Looking forward to it!

Copy link
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/unittests/Dialect/QCO/IR/test_qco_ir.cpp`:
- Around line 120-138: The test suite name has a typo: change the instantiation
identifier QCOIflOpTest to QCOIfOpTest in the INSTANTIATE_TEST_SUITE_P call so
the suite name matches the IfOp naming; update the symbol QCOIflOpTest to
QCOIfOpTest where used in that instantiation (INSTANTIATE_TEST_SUITE_P) to
correct the spelling.

Copy link
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.

Nice! Let's get this in. I'll quickly apply the one suggestion to the test.

Signed-off-by: Lukas Burgholzer <burgholzer@me.com>
@burgholzer
Copy link
Member

Ah damn. I obviously messed up one of the tests with the last commit 🙃

@burgholzer burgholzer enabled auto-merge (squash) February 21, 2026 16:45
@burgholzer burgholzer merged commit d9209b7 into munich-quantum-toolkit:main Feb 21, 2026
33 of 34 checks passed
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 feature New feature or request MLIR Anything related to MLIR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants