-
Notifications
You must be signed in to change notification settings - Fork 730
[Python Compiler] Integration of Python Compiler #7367
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
3c35dc8
print-parse-print-parse
erick-xanadu 6c2e364
starting point
erick-xanadu f5546cb
add initial impl
erick-xanadu 9dec689
add register pass
erick-xanadu 0cc27bc
Use context instead of MLContext
erick-xanadu d487995
Apply suggestions from code review
erick-xanadu 074b564
style
erick-xanadu 5a688db
style
erick-xanadu ac551bd
no frozen
erick-xanadu e6fbbc0
add stablehlo
erick-xanadu ddd8363
changelog
erick-xanadu 0960cec
Merge branch 'master' into eochoa/2025-05-01/python-compiler
erick-xanadu 852f379
nocover
erick-xanadu b834451
style
erick-xanadu 59ce1c6
f
erick-xanadu adb0b52
too-few-public-methods
erick-xanadu 0264101
exclude file
erick-xanadu fc5fe01
changelog
erick-xanadu 3b275f5
Add pragma: exclude file
erick-xanadu 05b4366
jax utils
erick-xanadu b48aaef
tests
erick-xanadu ae3a76a
Merge branch 'master' into eochoa/2025-05-01/python-compiler
erick-xanadu 5ba37dd
Revert "Add pragma: exclude file"
erick-xanadu 8edbd93
codecov ignore for now
erick-xanadu 37f63c7
skip import
erick-xanadu 84dee73
import skip jax
erick-xanadu 3ef0ec3
isinstance fix
erick-xanadu 98a81d4
importor
erick-xanadu 3f57c75
rename file
erick-xanadu 650b6a3
more coverage
erick-xanadu d319fb9
more coverage
erick-xanadu 21c81ea
no cover
erick-xanadu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """This file contains the implementation of the PennyLane-xDSL integration API.""" | ||
|
|
||
|
|
||
| import io | ||
|
|
||
| from jax._src.interpreters import mlir | ||
| from jaxlib.mlir.dialects import stablehlo | ||
| from jaxlib.mlir.ir import Context as jaxContext # pylint: disable=no-name-in-module | ||
| from jaxlib.mlir.ir import Module as jaxModule # pylint: disable=no-name-in-module | ||
| from xdsl.context import Context as xdslContext | ||
| from xdsl.dialects import arith, builtin, func, scf | ||
| from xdsl.dialects import stablehlo as xdslstablehlo | ||
| from xdsl.dialects import tensor, transform | ||
| from xdsl.parser import Parser | ||
| from xdsl.passes import PipelinePass | ||
| from xdsl.printer import Printer | ||
|
|
||
| from .quantum_dialect import QuantumDialect as Quantum | ||
| from .transforms import ApplyTransformSequence | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| class Compiler: | ||
| """Compiler namespace""" | ||
|
|
||
| @staticmethod | ||
| def run(jmod: jaxModule) -> jaxModule: | ||
| """Runs the apply-transform-sequence pass. | ||
|
|
||
| The apply-transform-sequence pass is a "meta-pass". In other words, | ||
| it is a pass that runs other passes. | ||
| """ | ||
|
|
||
| gentxtmod: str = jmod.operation.get_asm( | ||
| binary=False, print_generic_op_form=True, assume_verified=True | ||
| ) | ||
|
|
||
| ctx = xdslContext(allow_unregistered=True) | ||
| ctx.load_dialect(arith.Arith) | ||
| ctx.load_dialect(builtin.Builtin) | ||
| ctx.load_dialect(func.Func) | ||
| ctx.load_dialect(scf.Scf) | ||
| ctx.load_dialect(xdslstablehlo.StableHLO) | ||
| ctx.load_dialect(tensor.Tensor) | ||
| ctx.load_dialect(transform.Transform) | ||
erick-xanadu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx.load_dialect(Quantum) | ||
|
|
||
| xmod: builtin.ModuleOp = Parser(ctx, gentxtmod).parse_module() | ||
| pipeline = PipelinePass((ApplyTransformSequence(),)) | ||
| # xmod is modified in place | ||
| pipeline.apply(ctx, xmod) | ||
PietropaoloFrisoni marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| buffer = io.StringIO() | ||
| Printer(stream=buffer, print_generic_format=True).print(xmod) | ||
| with jaxContext() as ctx: | ||
| ctx.allow_unregistered_dialects = True | ||
| ctx.append_dialect_registry(mlir.upstream_dialects) | ||
| stablehlo.register_dialect(ctx) # pylint: disable=no-member | ||
| newmod: jaxModule = jaxModule.parse(buffer.getvalue()) | ||
|
|
||
| return newmod | ||
139 changes: 139 additions & 0 deletions
139
pennylane/compiler/python_compiler/jax_utils/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Utilities for translating JAX to xDSL""" | ||
|
|
||
| from functools import wraps | ||
| from typing import Any, Callable, TypeAlias | ||
|
|
||
| import jax | ||
| import jaxlib | ||
|
|
||
|
|
||
| from jax.extend import mlir as jmlir # pylint: disable=no-name-in-module | ||
| from jaxlib.mlir.ir import Context as jContext # pylint: disable=no-name-in-module | ||
| from jaxlib.mlir.ir import Module as jModule # pylint: disable=no-name-in-module | ||
| from jaxlib.mlir.dialects import stablehlo as jstablehlo # pylint: disable=no-name-in-module | ||
|
|
||
| from xdsl.dialects import arith as xarith | ||
| from xdsl.dialects import builtin as xbuiltin | ||
| from xdsl.dialects import func as xfunc | ||
| from xdsl.dialects import scf as xscf | ||
| from xdsl.dialects import stablehlo as xstablehlo | ||
| from xdsl.dialects import tensor as xtensor | ||
| from xdsl.dialects import transform as xtransform | ||
|
|
||
| from xdsl.parser import Parser as xParser | ||
| from xdsl.context import Context as xContext | ||
|
|
||
| JaxJittedFunction: TypeAlias = jaxlib.xla_extension.PjitFunction | ||
|
|
||
|
|
||
| def _module_inline(func: JaxJittedFunction, *args, **kwargs) -> jModule: | ||
| """Get the module from the jax.jitted function""" | ||
| return func.lower(*args, **kwargs).compiler_ir() | ||
|
|
||
|
|
||
| def module(func: JaxJittedFunction) -> Callable[Any, jModule]: | ||
| """ | ||
| Decorator for _module_inline | ||
| """ | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs) -> jModule: | ||
| return _module_inline(func, *args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def _generic_inline(func: JaxJittedFunction, *args, **kwargs) -> str: # pragma: no cover | ||
| """ | ||
| Create the generic textual representation for the jax.jit'ed function | ||
| """ | ||
| lowered = func.lower(*args, **kwargs) | ||
| mod = lowered.compiler_ir() | ||
| return mod.operation.get_asm(binary=False, print_generic_op_form=True, assume_verified=True) | ||
|
|
||
|
|
||
| def generic(func: JaxJittedFunction) -> Callable[Any, str]: # pragma: no cover | ||
| """ | ||
| Decorator for _generic_inline. | ||
| """ | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs) -> str: | ||
| return _generic_inline(func, *args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def parse_generic_to_xdsl_module(program: str) -> xbuiltin.ModuleOp: # pragma: no cover | ||
| """Parses generic MLIR program to xDSL module""" | ||
| ctx = xContext(allow_unregistered=True) | ||
| ctx.load_dialect(xarith.Arith) | ||
| ctx.load_dialect(xbuiltin.Builtin) | ||
| ctx.load_dialect(xfunc.Func) | ||
| ctx.load_dialect(xscf.Scf) | ||
| ctx.load_dialect(xstablehlo.StableHLO) | ||
| ctx.load_dialect(xtensor.Tensor) | ||
| ctx.load_dialect(xtransform.Transform) | ||
| moduleOp: xbuiltin.ModuleOp = xParser(ctx, program).parse_module() | ||
| return moduleOp | ||
|
|
||
|
|
||
| def parse_generic_to_jax_module(program: str) -> jModule: # pragma: no cover | ||
| """Parses an MLIR program in string representation to a jax Module""" | ||
| with jContext() as ctx: | ||
| ctx.allow_unregistered_dialects = True | ||
| jstablehlo.register_dialect(ctx) # pylint: disable=no-member | ||
| return jModule.parse(program) | ||
|
|
||
|
|
||
| def jax_from_docstring(func: Callable) -> jModule: # pragma: no cover | ||
| """Parses an MLIR program in string representation located in the docstring.""" | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*_, **__): | ||
| return parse_generic_to_jax_module(func.__doc__) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def _xdsl_module_inline( | ||
| func: JaxJittedFunction, *args, **kwargs | ||
| ) -> xbuiltin.ModuleOp: # pragma: no cover | ||
| generic_repr = _generic_inline(func, *args, **kwargs) | ||
| return parse_generic_to_xdsl_module(generic_repr) | ||
|
|
||
|
|
||
| def xdsl_from_docstring(func: Callable) -> xbuiltin.ModuleOp: # pragma: no cover | ||
| """Parses a docstring into an xdsl module""" | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*_, **__): | ||
| return parse_generic_to_xdsl_module(func.__doc__) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| def xdsl_module(func: JaxJittedFunction) -> Callable[Any, xbuiltin.ModuleOp]: # pragma: no cover | ||
| """ | ||
| Decorator for _xdsl_module_inline | ||
| """ | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs) -> xbuiltin.ModuleOp: | ||
| return _xdsl_module_inline(func, *args, **kwargs) | ||
|
|
||
| return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """PennyLane-xDSL transformations API.""" | ||
|
|
||
| from xdsl.transforms.transform_interpreter import TransformInterpreterPass | ||
| from .apply_transform_sequence import ApplyTransformSequence, register_pass | ||
erick-xanadu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| __all__ = [ | ||
| "ApplyTransformSequence", | ||
| "TransformInterpreterPass", | ||
| "register_pass", | ||
| ] | ||
71 changes: 71 additions & 0 deletions
71
pennylane/compiler/python_compiler/transforms/apply_transform_sequence.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """This file contains the pass that applies all passes present in the program representation.""" | ||
|
|
||
|
|
||
| from dataclasses import dataclass | ||
erick-xanadu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from xdsl.context import Context | ||
| from xdsl.dialects import builtin | ||
| from xdsl.passes import ModulePass, PipelinePass | ||
|
|
||
| from .transform_interpreter import TransformInterpreterPass # pylint: disable=no-name-in-module | ||
|
|
||
| available_passes = {} | ||
|
|
||
|
|
||
| def register_pass(name, _callable): | ||
| """Registers the passes available in the dictionary""" | ||
| available_passes[name] = _callable # pragma: no cover | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| @dataclass(frozen=True) | ||
| class ApplyTransformSequence(ModulePass): | ||
| """ | ||
| Looks for nested modules. Nested modules in this context are guaranteed to correspond | ||
| to qnodes. These modules are already annotated with which passes are to be executed. | ||
| The pass ApplyTransformSequence will run passes annotated in the qnode modules. | ||
|
|
||
| At the end, we delete the list of passes as they have already been applied. | ||
| """ | ||
|
|
||
| name = "apply-transform-sequence" | ||
|
|
||
| def apply( # pylint: disable=arguments-renamed,no-self-use | ||
| self, ctx: Context, module: builtin.ModuleOp | ||
| ) -> None: | ||
| """Applies the transformation""" | ||
| nested_modules = [] | ||
| for region in module.regions: | ||
| for block in region.blocks: | ||
| for op in block.ops: | ||
| if isinstance(op, builtin.ModuleOp): | ||
| nested_modules.append(op) | ||
|
|
||
| pipeline = PipelinePass( | ||
| # pylint: disable-next=unexpected-keyword-arg | ||
| (TransformInterpreterPass(passes=available_passes),) | ||
| ) | ||
| for op in nested_modules: | ||
| pipeline.apply(ctx, op) | ||
|
|
||
| for mod in nested_modules: | ||
| for region in mod.regions: | ||
| for block in region.blocks: | ||
| for op in block.ops: | ||
| if isinstance(op, builtin.ModuleOp) and op.get_attr_or_prop( | ||
| "transform.with_named_sequence" | ||
| ): | ||
| block.erase_op(op) # pragma: no cover | ||
18 changes: 18 additions & 0 deletions
18
pennylane/compiler/python_compiler/transforms/transform_interpreter/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Public API""" | ||
|
|
||
| from .transform_interpreter_catalyst import TransformInterpreterPass | ||
erick-xanadu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| __all__ = ["TransformInterpreterPass"] | ||
19 changes: 19 additions & 0 deletions
19
pennylane/compiler/python_compiler/transforms/transform_interpreter/interpreter/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright 2025 Xanadu Quantum Technologies Inc. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
|
|
||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Public API""" | ||
|
|
||
| from .impl import TransformFunctionsExt | ||
erick-xanadu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| __all__ = ["TransformFunctionsExt"] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.