-
Notifications
You must be signed in to change notification settings - Fork 43
MLIR Conversion(s) #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
MLIR Conversion(s) #880
Changes from all commits
0589644
34387f5
3543439
15151e4
63ddd2f
5eefc34
6ef8276
35d781f
54f1f19
12d8f11
9a0fb1c
a46def1
d8aed8f
00b3d40
17291af
0d59f0a
4344df3
27cc84f
d78e5db
839fb5b
e4c0196
a019076
1daaec1
c7deddd
d3a42b3
e593baf
92b4742
ee108b7
c3eea64
3fb5ce3
07e5130
17c1e22
bb3cd4e
54d8064
a6eb526
de4b879
f9d6359
494f495
39f9460
e63f165
9f09e87
4757068
38d3f6a
f2e929d
461845d
bb31dd8
98f3da9
91cff38
2b460c8
684b5cf
562de94
b8a7569
903b5d7
39cf8e5
cef9047
f94019a
4dbd59d
24cec28
f238319
4a3bdf0
6b8d7ad
042d068
3373c76
5e7d6c6
7d12f7c
beec847
4edf6f3
60f3ee8
a8cd104
1ac55bc
0f8e199
893c2f3
ebbda74
2b824d1
7199987
d06c7f0
9246feb
55485c0
89336e2
8b09c68
c1b5efa
166bca6
cffe6bb
af534ff
956260c
4df9fb8
4c721ae
cecd0d6
a6cb11e
a3a651c
6bcf5cc
e81af86
3f0775f
8e45ec1
d3df8c1
47258fe
5d7a702
5d380d6
5abe9b1
889ffd6
8142225
e60af0f
cab888a
c1ec80c
0b084eb
e905f08
7dde58e
7841b33
d2213ea
d1f480f
6166d72
d97e5ef
1ac3384
35236d2
65ec200
fad43e4
c45fadf
a8b32ac
a5c0be4
1f254f7
40845b9
2134e36
dbdf863
08591a5
f3d9ded
4620c18
472616b
5289a0f
02d3c2b
556be79
2163bcc
86e31d2
8a0e9cd
9942df9
0ce0025
b8c6196
d7e298d
934966b
2c3b5cc
1260aee
6fb0e94
8b3b3a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
name: Catalyst CI | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- "mlir/**" | ||
pull_request: | ||
paths: | ||
- "mlir/**" | ||
- ".github/workflows/ci_catalyst.yml" | ||
merge_group: | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
cpp-test-catalyst: | ||
name: 🇨 Test MLIR with LLVM@${{ matrix.llvm-version }} | ||
runs-on: macos-14 | ||
strategy: | ||
matrix: | ||
llvm-version: [19] | ||
env: | ||
CMAKE_BUILD_PARALLEL_LEVEL: 4 | ||
CTEST_PARALLEL_LEVEL: 4 | ||
FORCE_COLOR: 3 | ||
steps: | ||
# check out the repository | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
# set up ccache for faster C++ builds | ||
- name: Setup ccache | ||
uses: Chocobo1/setup-ccache-action@v1 | ||
with: | ||
prepend_symlinks_to_path: false | ||
override_cache_key: c++-tests-mlir-catalyst | ||
|
||
# set up mold as linker for faster C++ builds | ||
- name: Set up mold as linker | ||
uses: rui314/setup-mold@v1 | ||
|
||
# set up uv for faster Python package management | ||
- name: Install the latest version of uv | ||
uses: astral-sh/setup-uv@v6 | ||
with: | ||
python-version: 3.13 | ||
activate-environment: true | ||
|
||
# make sure ninja is installed | ||
- name: Install Ninja | ||
run: uv tool install ninja | ||
|
||
# make sure the lit test runner is installed | ||
- name: Install lit | ||
run: uv pip install lit | ||
|
||
# install LLVM 19 and MLIR via Homebrew and set up environment variables | ||
- name: Install LLVM and MLIR via Homebrew | ||
run: | | ||
brew install llvm@19 | ||
brew link --force --overwrite llvm@19 | ||
echo "CC=$(brew --prefix llvm@19)/bin/clang" >> $GITHUB_ENV | ||
echo "CXX=$(brew --prefix llvm@19)/bin/clang++" >> $GITHUB_ENV | ||
echo "MLIR_DIR=$(brew --prefix llvm@19)/lib/cmake/mlir" >> $GITHUB_ENV | ||
echo "LLVM_DIR=$(brew --prefix llvm@19)/lib/cmake/llvm" >> $GITHUB_ENV | ||
echo "$(brew --prefix llvm@19)/bin" >> $GITHUB_PATH | ||
|
||
# clone PennyLane Catalyst (from patched branch) - excl. submodules | ||
- name: Clone PennyLane Catalyst | ||
run: | | ||
git clone --branch mqt_integration --depth=1 https://github.com/flowerthrower/catalyst.git catalyst | ||
|
||
- name: Set macOS deployment target for linker | ||
run: echo "MACOSX_DEPLOYMENT_TARGET=14.0" >> $GITHUB_ENV | ||
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be necessary as you are running on macOS 14 already.
Comment on lines
+78
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not be necessary as you are running on macOS 14 already. |
||
|
||
# configure only MLIRQuantum dialect (skip building tools) | ||
- name: Configure MLIRQuantum dialect | ||
run: | | ||
cmake -G Ninja -S catalyst/mlir -B catalyst/mlir/build \ | ||
-DCMAKE_BUILD_TYPE=Release \ | ||
-DMLIR_DIR=$MLIR_DIR \ | ||
-DLLVM_DIR=$LLVM_DIR \ | ||
-DCATALYST_BUILD_TOOLS=OFF \ | ||
-DEnzyme_DIR="none" | ||
|
||
# build MLIRQuantum | ||
- name: Build MLIRQuantum dialect | ||
run: | | ||
cmake --build catalyst/mlir/build --target MLIRQuantum | ||
|
||
- name: Check Catalyst CMake config | ||
run: test -f catalyst/mlir/build/lib/cmake/catalyst/CatalystConfig.cmake | ||
|
||
# configure the project with CMake | ||
- name: Configure CMake for MLIR | ||
run: | | ||
cmake -G Ninja -S . -B build \ | ||
-DCMAKE_BUILD_TYPE=Release \ | ||
-DBUILD_MQT_CORE_MLIR=ON \ | ||
-DENABLE_MQT_CORE_MLIR_CATALYST_PLUGIN=ON \ | ||
-DCatalyst_DIR="$PWD/catalyst/mlir/build/lib/cmake/catalyst" | ||
|
||
# build the project | ||
- name: Build MLIR quantum-opt | ||
run: cmake --build build --config Release --target quantum-opt | ||
|
||
# run conversion pipeline | ||
- name: Run conversion -> catalystquantum-to-mqtopt | ||
run: | | ||
./build/mlir/tools/quantum-opt/quantum-opt \ | ||
--pass-pipeline="builtin.module(catalystquantum-to-mqtopt)" \ | ||
mlir/test/Conversion/quantum.mlir | ||
- name: Run conversion -> mqtopt-to-catalystquantum | ||
run: | | ||
./build/mlir/tools/quantum-opt/quantum-opt \ | ||
--pass-pipeline="builtin.module(mqtopt-to-catalystquantum)" \ | ||
mlir/test/Conversion/mqtopt.mlir |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,18 @@ endif() | |
|
||
# Make all declared dependencies available. | ||
FetchContent_MakeAvailable(${FETCH_PACKAGES}) | ||
|
||
if(ENABLE_MQT_CORE_MLIR_CATALYST_PLUGIN) | ||
# Manually detect the installed Catalyst Python and get its cmake directory. | ||
execute_process( | ||
COMMAND "${Python_EXECUTABLE}" -c | ||
"import catalyst, os; print(os.path.dirname(catalyst.__file__))" | ||
OUTPUT_STRIP_TRAILING_WHITESPACE | ||
OUTPUT_VARIABLE Python_Catalyst_DIR | ||
ERROR_QUIET) | ||
message(STATUS "Found Catalyst package: ${Python_Catalyst_DIR}") | ||
|
||
# TODO: once the Catalyst Python package provides the necessary files set(Catalyst_DIR | ||
# "${Python_Catalyst_DIR}/mlir/build/lib/cmake/catalyst") | ||
Comment on lines
+78
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, there would be no need to do something like |
||
find_package(Catalyst REQUIRED) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,13 @@ string(REPLACE "." ";" MLIR_VERSION_COMPONENTS ${MLIR_VERSION}) | |
list(GET MLIR_VERSION_COMPONENTS 0 MLIR_VERSION_MAJOR) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just because I stumbe over it right now: Is this gymnastic with the version string necessary or can the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems reasonable to use |
||
add_compile_definitions(MLIR_VERSION_MAJOR=${MLIR_VERSION_MAJOR}) | ||
|
||
option(BUILD_MQT_CORE_CATALYST_PLUGIN "Enable Catalyst conversion passes" OFF) | ||
if(BUILD_MQT_CORE_CATALYST_PLUGIN) | ||
message(STATUS "Found MLIR version ${MLIR_VERSION_MAJOR}.") | ||
list(APPEND CMAKE_MODULE_PATH "${CATALYST_CMAKE_DIR}") | ||
list(APPEND MLIR_INCLUDE_DIRS "${CATALYST_INCLUDE_DIRS}") | ||
endif() | ||
|
||
# Include the TableGen, LLVM and MLIR CMake modules. | ||
include(TableGen) | ||
include(AddLLVM) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
# Licensed under the MIT License | ||
|
||
add_subdirectory(Dialect) | ||
add_subdirectory(Conversion) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
if(BUILD_MQT_CORE_CATALYST_PLUGIN) | ||
add_subdirectory(Catalyst) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
add_subdirectory(MQTOptToCatalystQuantum) | ||
add_subdirectory(CatalystQuantumToMQTOpt) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
set(LLVM_TARGET_DEFINITIONS CatalystQuantumToMQTOpt.td) | ||
mlir_tablegen(CatalystQuantumToMQTOpt.h.inc -gen-pass-decls -name CatalystQuantumToMQTOpt) | ||
add_public_tablegen_target(CatalystQuantumToMQTOptIncGen) | ||
|
||
add_mlir_doc(CatalystQuantumToMQTOpt CatalystQuantumToMQTOpt ./ -gen-pass-doc) |
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
* Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
* All rights reserved. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Licensed under the MIT License | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <mlir/Pass/Pass.h> // from @llvm-project | ||
|
||
namespace mlir::mqt::ir::conversions { | ||
|
||
#define GEN_PASS_DECL | ||
#include "mlir/Conversion/Catalyst/CatalystQuantumToMQTOpt/CatalystQuantumToMQTOpt.h.inc" | ||
|
||
#define GEN_PASS_REGISTRATION | ||
#include "mlir/Conversion/Catalyst/CatalystQuantumToMQTOpt/CatalystQuantumToMQTOpt.h.inc" | ||
|
||
} // namespace mlir::mqt::ir::conversions |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
// Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
// All rights reserved. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// Licensed under the MIT License | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def CatalystQuantumToMQTOpt : Pass<"catalystquantum-to-mqtopt"> { | ||
let summary = "Convert Catalyst's `Quantum` to MQT's `MQTOpt` dialect."; | ||
|
||
let description = [{ | ||
This pass converts Catalyst's `Quantum` to MQT's `MQTOpt` dialect. | ||
The following operations are currently NOT converted (and instead marked legal): | ||
- DeviceInitOp | ||
- DeviceReleaseOp | ||
- NamedObsOp | ||
- ExpvalOp | ||
- FinalizeOp | ||
- ComputationalBasisOp | ||
- StateOp | ||
- InitializeOp | ||
}]; | ||
|
||
// Define dependent dialects | ||
let dependentDialects = [ | ||
"catalyst::quantum::QuantumDialect", | ||
"::mqt::ir::opt::MQTOptDialect" | ||
]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
set(LLVM_TARGET_DEFINITIONS MQTOptToCatalystQuantum.td) | ||
mlir_tablegen(MQTOptToCatalystQuantum.h.inc -gen-pass-decls -name MQTOptToCatalystQuantum) | ||
add_public_tablegen_target(MQTOptToCatalystQuantumIncGen) | ||
|
||
add_mlir_doc(MQTOptToCatalystQuantum MQTOptToCatalystQuantum ./ -gen-pass-doc) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
* Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
* All rights reserved. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
* | ||
* Licensed under the MIT License | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <mlir/Pass/Pass.h> // from @llvm-project | ||
|
||
namespace mlir::mqt::ir::conversions { | ||
|
||
#define GEN_PASS_DECL | ||
#include "mlir/Conversion/Catalyst/MQTOptToCatalystQuantum/MQTOptToCatalystQuantum.h.inc" | ||
|
||
#define GEN_PASS_REGISTRATION | ||
#include "mlir/Conversion/Catalyst/MQTOptToCatalystQuantum/MQTOptToCatalystQuantum.h.inc" | ||
|
||
} // namespace mlir::mqt::ir::conversions |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
// Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
// All rights reserved. | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
// Licensed under the MIT License | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def MQTOptToCatalystQuantum : Pass<"mqtopt-to-catalystquantum"> { | ||
let summary = "Convert MQT's `MQTOpt` to Catalyst's `Quantum` dialect."; | ||
|
||
let description = [{ | ||
This pass converts MQT's `MQTOpt` to Catalyst's `Quantum` dialect. | ||
The following operations are currently NOT converted (and instead marked legal): | ||
- UOp, U2Op | ||
- SXOp, SXdgOp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This as well as the |
||
- PeresOp, PeresdgOp | ||
- ECROp | ||
}]; | ||
let dependentDialects = [ | ||
"catalyst::quantum::QuantumDialect", | ||
"::mqt::ir::opt::MQTOptDialect" | ||
]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
# Licensed under the MIT License | ||
|
||
add_subdirectory(Dialect) | ||
add_subdirectory(Conversion) |
burgholzer marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
if(BUILD_MQT_CORE_CATALYST_PLUGIN) | ||
add_subdirectory(Catalyst) | ||
endif() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
add_subdirectory(MQTOptToCatalystQuantum) | ||
add_subdirectory(CatalystQuantumToMQTOpt) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright (c) 2023 - 2025 Chair for Design Automation, TUM | ||
# Copyright (c) 2025 Munich Quantum Software Company GmbH | ||
# All rights reserved. | ||
# | ||
# SPDX-License-Identifier: MIT | ||
# | ||
# Licensed under the MIT License | ||
|
||
file(GLOB CONVERSION_SOURCES *.cpp) | ||
|
||
add_mlir_library(CatalystQuantumToMQTOpt ${CONVERSION_SOURCES} LINK_LIBS ${LIBRARIES} DEPENDS | ||
CatalystQuantumToMQTOptIncGen) | ||
|
||
target_link_libraries(CatalystQuantumToMQTOpt PRIVATE MLIRQuantum) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be interesting to see this work on
macos-13
(x86 runners) as well asllvm-version: 20
.The current build pipeline seems to pass, which is great already!
Unrelated to this PR in particular, but what could be a nice byproduct in a separate PR is that this already represents a functional macOS-14 CI pipeline for MLIR, which would be part of #925.