Skip to content

Add CancelConsecutiveHermitianGates Pass#1684

Closed
MatthiasReumann wants to merge 2 commits intomainfrom
feat/hermitian-op-cancellation
Closed

Add CancelConsecutiveHermitianGates Pass#1684
MatthiasReumann wants to merge 2 commits intomainfrom
feat/hermitian-op-cancellation

Conversation

@MatthiasReumann
Copy link
Copy Markdown
Collaborator

@MatthiasReumann MatthiasReumann commented May 4, 2026

Description

The "Writing Your First Optimization Pass" section in #1555 requires a rather simple example of an optimization pass. I thought a CancelConsecutiveHermitianGates pass could be a good choice. Hence, this pull request.

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 and others added 2 commits May 4, 2026 15:45
Co-authored-by: Copilot <copilot@github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-tidy (v22.1.3) reports: 14 concern(s)
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:17:1: warning: [misc-include-cleaner]

    included header Debug.h is not used directly

       17 | #include <llvm/Support/Debug.h>
          | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       18 | #include <mlir/IR/Operation.h>
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:29:7: warning: [misc-include-cleaner]

    no header providing "mlir::OpInterfaceRewritePattern" is directly included

       20 | #include <mlir/Transforms/GreedyPatternRewriteDriver.h>
       21 | 
       22 | namespace mlir::qco {
       23 | 
       24 | #define GEN_PASS_DEF_CANCELCONSECUTIVEHERMITIANGATES
       25 | #include "mlir/Dialect/QCO/Transforms/Passes.h.inc"
       26 | 
       27 | namespace {
       28 | struct CancelConsecutiveHermitianGatesPattern final
       29 |     : OpInterfaceRewritePattern<UnitaryOpInterface> {
          |       ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:30:51: warning: [misc-include-cleaner]

    no header providing "mlir::MLIRContext" is directly included

       18 | #include <mlir/IR/Operation.h>
       19 | #include <mlir/IR/OperationSupport.h>
       20 | #include <mlir/Transforms/GreedyPatternRewriteDriver.h>
       21 | 
       22 | namespace mlir::qco {
       23 | 
       24 | #define GEN_PASS_DEF_CANCELCONSECUTIVEHERMITIANGATES
       25 | #include "mlir/Dialect/QCO/Transforms/Passes.h.inc"
       26 | 
       27 | namespace {
       28 | struct CancelConsecutiveHermitianGatesPattern final
       29 |     : OpInterfaceRewritePattern<UnitaryOpInterface> {
       30 |   explicit CancelConsecutiveHermitianGatesPattern(MLIRContext* context)
          |                                                   ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:31:9: warning: [misc-include-cleaner]

    no header providing "mlir::OpInterfaceRewritePattern" is directly included

       31 |       : OpInterfaceRewritePattern(context) {}
          |         ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:46:3: warning: [misc-include-cleaner]

    no header providing "mlir::LogicalResult" is directly included

       20 |   LogicalResult matchAndRewrite(UnitaryOpInterface op,
          |   ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:47:33: warning: [misc-include-cleaner]

    no header providing "mlir::PatternRewriter" is directly included

       47 |                                 PatternRewriter& rewriter) const override {
          |                                 ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:49:14: warning: [misc-include-cleaner]

    no header providing "mlir::failure" is directly included

       49 |       return failure();
          |              ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:80:12: warning: [misc-include-cleaner]

    no header providing "mlir::success" is directly included

       80 |     return success();
          |            ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:88:8: warning: [misc-use-internal-linkage]

    struct 'CancelConsecutiveHermitianGates' can be moved into an anonymous namespace to enforce internal linkage

       88 | struct CancelConsecutiveHermitianGates final
          |        ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:99:9: warning: [misc-include-cleaner]

    no header providing "mlir::failed" is directly included

       99 |     if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) {
          |         ^
  • mlir/lib/Dialect/QCO/Transforms/Optimizations/CancelConsecutiveHermitianGates.cpp:99:59: warning: [misc-include-cleaner]

    no header providing "std::move" is directly included

       21 |     if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) {
          |                                                           ^
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_cancel_consecutive_hermitian_gates.cpp:31:7: warning: [misc-use-internal-linkage]

    class 'CancelConsecutiveHermitianGatesTest' can be moved into an anonymous namespace to enforce internal linkage

       31 | class CancelConsecutiveHermitianGatesTest : public testing::Test {
          |       ^
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_cancel_consecutive_hermitian_gates.cpp:37:20: warning: [misc-include-cleaner]

    no header providing "std::make_unique" is directly included

       27 | 
       28 | using namespace mlir;
       29 | using namespace mlir::qco;
       30 | 
       31 | class CancelConsecutiveHermitianGatesTest : public testing::Test {
       32 | protected:
       33 |   void SetUp() override {
       34 |     // Register all necessary dialects
       35 |     DialectRegistry registry;
       36 |     registry.insert<qco::QCODialect, arith::ArithDialect, func::FuncDialect>();
       37 |     context = std::make_unique<MLIRContext>();
          |                    ^
  • mlir/unittests/Dialect/QCO/Transforms/Optimizations/test_cancel_consecutive_hermitian_gates.cpp:48:8: warning: [misc-include-cleaner]

    no header providing "std::unique_ptr" is directly included

       48 |   std::unique_ptr<MLIRContext> context;
          |        ^

Have any feedback or feature suggestions? Share it here.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@MatthiasReumann
Copy link
Copy Markdown
Collaborator Author

I just saw that there are the removeInversePair (etc.) functions in the QCOUtils.h file, which are used for canonicalization. Consequently, this PR probably doesn't require to be merged. 😅 Bit unfortunate, but... 🫠

@burgholzer
Copy link
Copy Markdown
Member

I just saw that there are the removeInversePair (etc.) functions in the QCOUtils.h file, which are used for canonicalization. Consequently, this PR probably doesn't require to be merged. 😅 Bit unfortunate, but... 🫠

I was just about to chime in here and say that the respective functionality is already covered by canonicalizations. 😅

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.

2 participants