Skip to content

Commit dc452a8

Browse files
authored
Create xDSL universe for the unified compiler (#2208)
Duplicate of PennyLaneAI/pennylane#8372 [sc-100654]
1 parent 9f017f9 commit dc452a8

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

doc/releases/changelog-dev.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
`catalyst.python_interface` namespace.
1111
[(#2199)](https://github.com/PennyLaneAI/catalyst/pull/2199)
1212

13+
* An xDSL `Universe` containing all custom xDSL dialects and passes has been registered as an entry point,
14+
allowing usage of PennyLane's dialects and passes with xDSL's command-line tools.
15+
[(#2208)](https://github.com/PennyLaneAI/catalyst/pull/2208)
16+
1317
* A new `catalyst.python_interface.inspection.mlir_specs` function has been added to facilitate
1418
PennyLane's new pass-by-pass specs feature. This function returns information gathered by parsing
1519
the xDSL generated by a given QJIT object, such as gate counts, measurements, or qubit allocations.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2025 Xanadu Quantum Technologies Inc.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""xDSL universe for containing all dialects and passes."""
15+
16+
xdsl_available = True
17+
18+
try:
19+
from xdsl.passes import ModulePass
20+
from xdsl.universe import Universe
21+
except (ImportError, ModuleNotFoundError):
22+
xdsl_available = False # pragma: no cover
23+
24+
# We must check that xDSL is installed because we're adding an entry point to
25+
# PennyLane that references this file, and we must ensure that PennyLane can
26+
# be installed in environments where xDSL is not installed.
27+
XDSL_UNIVERSE = None
28+
29+
if xdsl_available:
30+
# pylint: disable=import-outside-toplevel
31+
from . import dialects, transforms
32+
33+
shared_dialects = ("stablehlo", "transform")
34+
35+
# Create a map from dialect names to dialect classes. Dialects that are already
36+
# provided by xDSL cannot be loaded into the multiverse, so we don't add them to
37+
# our universe.
38+
names_to_dialects = {
39+
d.name: d
40+
for name in dialects.__all__
41+
if (d := getattr(dialects, name)).name not in shared_dialects
42+
}
43+
44+
# Create a map from pass names to their respective ModulePass. The transforms module
45+
# contains PassDispatcher instances as well as ModulePasses. We only want to collect
46+
# the ModulePasses. We cannot use issubclass with instances, which is why we first
47+
# check if isinstance(transform, type).
48+
names_to_passes = {
49+
t.name: t
50+
for name in transforms.__all__
51+
if isinstance((t := getattr(transforms, name)), type) and issubclass(t, ModulePass)
52+
}
53+
54+
# The Universe is used to expose custom dialects and transforms to xDSL. It is
55+
# specified as an entry point in PennyLane's pyproject.toml file, which makes
56+
# it available to look up by xDSL for tools such as xdsl-opt, xdsl-gui, etc.
57+
XDSL_UNIVERSE = Universe(all_dialects=names_to_dialects, all_passes=names_to_passes)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright 2025 Xanadu Quantum Technologies Inc.
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Unit tests for the xDSL universe."""
15+
16+
import pytest
17+
18+
pytestmark = pytest.mark.xdsl
19+
xdsl = pytest.importorskip("xdsl")
20+
21+
# pylint: disable=wrong-import-position
22+
from xdsl.passes import ModulePass
23+
from xdsl.universe import Universe as xUniverse
24+
25+
from catalyst.python_interface import dialects, transforms
26+
from catalyst.python_interface.xdsl_universe import XDSL_UNIVERSE, shared_dialects
27+
28+
all_dialects = tuple(getattr(dialects, name) for name in dialects.__all__)
29+
all_transforms = tuple(
30+
transform
31+
for name in transforms.__all__
32+
if isinstance((transform := getattr(transforms, name)), type)
33+
and issubclass(transform, ModulePass)
34+
)
35+
36+
37+
def test_correct_universe():
38+
"""Test that all the available dialects and transforms are available in the universe."""
39+
for d in all_dialects:
40+
if d.name not in shared_dialects:
41+
assert d.name in XDSL_UNIVERSE.all_dialects
42+
assert XDSL_UNIVERSE.all_dialects[d.name] == d
43+
44+
for t in all_transforms:
45+
assert t.name in XDSL_UNIVERSE.all_passes
46+
assert XDSL_UNIVERSE.all_passes[t.name] == t
47+
48+
49+
def test_correct_multiverse():
50+
"""Test that all the available dialects and transforms are available in the multiverse."""
51+
multiverse = xUniverse.get_multiverse()
52+
53+
for d in all_dialects:
54+
assert d.name in multiverse.all_dialects
55+
if d.name not in shared_dialects:
56+
assert multiverse.all_dialects[d.name] == d
57+
58+
for t in all_transforms:
59+
assert t.name in multiverse.all_passes
60+
assert multiverse.all_passes[t.name] == t

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ def parse_dep_versions():
147147
"cuda_quantum.ops = catalyst.api_extensions",
148148
"cuda_quantum.qjit = catalyst.third_party.cuda:cudaqjit",
149149
],
150+
"xdsl.universe": [
151+
"catalyst-xdsl-universe = catalyst.python_interface.xdsl_universe:XDSL_UNIVERSE"
152+
],
150153
}
151154

152155
classifiers = [

0 commit comments

Comments
 (0)