Skip to content

add mlir python bindings poc for qasm-to-qco pipeline#1763

Open
ibishal wants to merge 6 commits into
munich-quantum-toolkit:mainfrom
ibishal:feat/mlir-python-bindings
Open

add mlir python bindings poc for qasm-to-qco pipeline#1763
ibishal wants to merge 6 commits into
munich-quantum-toolkit:mainfrom
ibishal:feat/mlir-python-bindings

Conversation

@ibishal
Copy link
Copy Markdown

@ibishal ibishal commented Jun 3, 2026

Description

Adds Python bindings for the MQT compiler collection, exposing the full (py:qasm) to (mlir:qc) to (mlir:qco) pipeline from Python.

tested with a 2-qubit Bell circuit as input and the output is the full QCO dialect IR with value semantics.

  from mqt.core.mlir import qasm_to_qco

  result = qasm_to_qco("""
  OPENQASM 2.0;
  include "qelib1.inc";
  qreg q[2];
  h q[0];
  cx q[0], q[1];
  """)
  print(result)
 module {
    func.func @main() -> i64 attributes {passthrough = ["entry_point"]} {
      %c0_i64 = arith.constant 0 : i64
      %c1 = arith.constant 1 : index
      %c0 = arith.constant 0 : index
      %c2 = arith.constant 2 : index
      %0 = qtensor.alloc(%c2) : tensor<2x!qco.qubit>
      %out_tensor, %result = qtensor.extract %0[%c0] : tensor<2x!qco.qubit>
      %out_tensor_0, %result_1 = qtensor.extract %out_tensor[%c1] : tensor<2x!qco.qubit>
      %1 = qco.h %result : !qco.qubit -> !qco.qubit
      %controls_out, %targets_out = qco.ctrl(%1) targets (%arg0 = %result_1) {
        %4 = qco.x %arg0 : !qco.qubit -> !qco.qubit
        qco.yield %4 : !qco.qubit
      } : ({!qco.qubit}, {!qco.qubit}) -> ({!qco.qubit}, {!qco.qubit})
      %2 = qtensor.insert %controls_out into %out_tensor_0[%c0] : tensor<2x!qco.qubit>
      %3 = qtensor.insert %targets_out into %2[%c1] : tensor<2x!qco.qubit>
      qtensor.dealloc %3 : tensor<2x!qco.qubit>
      return %c0_i64 : i64
    }
  }

Fixes #1693

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from f7d3c2e to 765518b Compare June 3, 2026 20:11
@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from 0da01aa to 51b81ef Compare June 3, 2026 20:41
@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from f3dbc9d to b7a05b4 Compare June 3, 2026 20:56
@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from 57add9b to d8f9dfe Compare June 3, 2026 21:20
@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from e21234d to 5acd3f9 Compare June 3, 2026 22:50
@ibishal ibishal force-pushed the feat/mlir-python-bindings branch from ab8cdff to ed720c9 Compare June 3, 2026 23:08
@denialhaag denialhaag self-requested a review June 4, 2026 14:15
Copy link
Copy Markdown
Member

@denialhaag denialhaag left a comment

Choose a reason for hiding this comment

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

Thanks for this first draft for adding Python bindings to mqt-cc, @ibishal! 🙂

This PR will require some more input from you before it is merge-ready. You can find some more details in the comments below.

In addition to the comments below, please ensure the implementation is tested in Python. The tests should live here: https://github.com/munich-quantum-toolkit/core/tree/main/test/python. This will ensure that the tests are also run in our CI. For this to work, BUILD_MQT_CORE_MLIR will have to be enabled here:

core/pyproject.toml

Lines 132 to 136 in 946116d

[tool.scikit-build.cmake.define]
BUILD_MQT_CORE_BINDINGS = "ON"
BUILD_MQT_CORE_TESTS = "OFF"
BUILD_MQT_CORE_SHARED_LIBS = "ON"
BUILD_MQT_CORE_MLIR = "OFF"

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.

It would be nice to structure this more like https://github.com/llvm/llvm-project/tree/main/mlir/include/mlir/CAPI.

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.

It would be nice to structure this more like https://github.com/llvm/llvm-project/tree/main/mlir/lib/CAPI.

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.

Comment thread poc/test_pipeline.cpp
Copy link
Copy Markdown
Member

@denialhaag denialhaag Jun 4, 2026

Choose a reason for hiding this comment

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

This does not need to be committed. We are already testing the C++ implementation in https://github.com/munich-quantum-toolkit/core/tree/main/mlir/unittests.

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.

It would be nice to extend this. All dialects, passes, and conversions should be accessible. It would also be nice to provide a compile_program() function that accepts an OpenQASM program and pushes it through the entire pipeline (see the steps in CompilerPipeline.cpp).

Comment thread CMakeLists.txt
Comment on lines +123 to +128
cmake_dependent_option(
BUILD_MQT_CORE_MLIR_PYTHON
"Build Python bindings for the MQT MLIR compiler collection (requires MLIR_ENABLE_BINDINGS_PYTHON=ON)"
OFF
"BUILD_MQT_CORE_MLIR"
OFF)
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.

BUILD_MQT_CORE_MLIR_PYTHON should be enabled automatically if BUILD_MQT_CORE_MLIR and BUILD_MQT_CORE_BINDINGS are both enabled.

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.

I would even argue there may be no need for a separate option. Whenever MLIR is enabled and the bindings are as well, this should be built.

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.

✨ Enable Python Bindings for the MQT Compiler Collection

3 participants