-
Notifications
You must be signed in to change notification settings - Fork 58
Implement new mlir_specs Python function to get resource counts from MLIR passes
#2238
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
Open
jzaia18
wants to merge
93
commits into
main
Choose a base branch
from
feature/xdsl-specs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
93 commits
Select commit
Hold shift + click to select a range
df8b97b
Migrate unified compiler to Catalyst
mudit2812 9536707
Fixing migration artifacts; linting
mudit2812 1453aae
Fix CI errors; Ignore unified compiler in coverage reports
mudit2812 efc0f88
Test out how graphviz is installed
mudit2812 10cd749
Lint some more
mudit2812 27de22d
Merge branch 'main' into migrate-unified-compiler
mudit2812 f7aa2b6
[skip ci] Skip CI
mudit2812 012f58b
[skip ci] Skip CI
mudit2812 f0a0c90
Merge branch 'migrate-unified-compiler' of https://github.com/PennyLa…
mudit2812 c98755e
Try installing graphviz with apt
mudit2812 bb5bd00
Fix codefactor complaints
mudit2812 1448472
Merge branch 'main' into migrate-unified-compiler
mudit2812 5e745f5
Try appeasing codefactor again
mudit2812 0409933
Try appeasing codefactor once again
mudit2812 e786a12
Pylint suppression
mudit2812 4ae10ff
Reduce complexity of stablehlo.reduce and stablehlo.dynamic_broadcast…
mudit2812 dc41a7b
Fix some failures
mudit2812 58b13e4
Try change to graphviz installation
mudit2812 4e5f904
Merge branch 'main' into migrate-unified-compiler
mudit2812 637d2cf
Try installing graphviz with pip
mudit2812 091c5e1
Merge branch 'main' into migrate-unified-compiler
mudit2812 64e0fd4
Try installing graphviz with both apt and pip
mudit2812 dc15d3a
Add utils file to remove conftest imports
mudit2812 e7e33d6
Merge branch 'main' into migrate-unified-compiler
mudit2812 f919e8d
Remove unused imports
mudit2812 005ba9f
Add graphviz dependencies to lightning.kokkos testing workflow
mudit2812 6117189
Update cookbook
mudit2812 6961324
Migrate all changelog entries from PennyLane
mudit2812 0fdbb46
Merge branch 'main' into migrate-unified-compiler
mudit2812 a868bc3
Add EOF new line to .codecov.yml
mudit2812 8f00b2f
change changelog entry slightly
mudit2812 22a6e6e
Merge branch 'main' into migrate-unified-compiler
mudit2812 b4a321d
Merge branch 'main' into migrate-unified-compiler
mudit2812 e3ef736
Copy over xdsl_conversion changes
jzaia18 b7995ff
Rename visualization -> inspection
jzaia18 67705a9
finalize migration of branch
jzaia18 e735ca2
Update CHANGELOG
jzaia18 a979216
Copy over changes from PennyLane
jzaia18 4cbe02a
Remove reference to 'remove-chained-self-inverses'
mudit2812 e8e84e6
Woops, forgot to add the main files
jzaia18 c5f08c3
Remove leftover references to pennylane.compiler.python_compiler
mudit2812 fe76357
Streamline circuit inspection utilities (#2237)
jzaia18 be3ff3a
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 3395581
Merge branch 'main' into migrate-unified-compiler
mudit2812 c21850f
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 ad7c306
Better display for 'all' in compbasis
jzaia18 a69c9ba
Remove more references to python_compiler
mudit2812 9d208f9
Address more code review comments
mudit2812 7946287
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 e223b3f
Undo accidental change
jzaia18 8ce6f96
Fix jit
mudit2812 664d24a
Fix jit again
mudit2812 e07a332
Merge branch 'main' into migrate-unified-compiler
mudit2812 f9035c0
Fix line-too-long
mudit2812 ca1aa3a
Merge branch 'migrate-unified-compiler' of https://github.com/PennyLa…
mudit2812 d146453
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 87be66c
Update specs unit tests
jzaia18 f43cb3d
Remove vestigial workaround
jzaia18 0e4cd8d
Apply suggestions from code review
jzaia18 ef7c6c9
Update frontend/test/pytest/python_interface/inspection/test_mlir_spe…
jzaia18 eb11eb0
Update function names and docs
jzaia18 869dcc0
Merge branch 'feature/xdsl-specs' of github.com:PennyLaneAI/catalyst …
jzaia18 611a0ea
Oops
jzaia18 6711a0f
Minor tweaks to simplify
jzaia18 0288e6c
Update unit tests
jzaia18 02bbdfc
Simplify unit tests
jzaia18 28ab6e6
Separate out collect_region into 2 functions, 1 for regions and 1 for…
jzaia18 32261bf
Update changelog
jzaia18 7f4eb06
Remove the local transform dialect (#2261)
mehrdad2m 3947e05
Remove unneeded XDSL plugin load
jzaia18 a4e86cd
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 24eb758
Merge branch 'main' into migrate-unified-compiler
dime10 2413520
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 5bf82d8
Apply PR review comments
jzaia18 b6fb585
[skip ci] add importorskip for graphviz to mlir_graph tests
mudit2812 0b1f237
Merge branch 'migrate-unified-compiler' of https://github.com/PennyLa…
mudit2812 8ef2b58
Merge branch 'main' into migrate-unified-compiler
mudit2812 848795d
[skip ci] Skip CI
mudit2812 58e06f0
Merge branch 'migrate-unified-compiler' of https://github.com/PennyLa…
mudit2812 14eb727
Update .codecov.yml
mudit2812 7477272
Add better testing for Hamiltonians
jzaia18 dce6a8d
Merge branch 'migrate-unified-compiler' into feature/xdsl-specs
jzaia18 14b8380
Small formatting fix
jzaia18 8980de6
Merge branch 'feature/xdsl-specs' of github.com:PennyLaneAI/catalyst …
jzaia18 4badc05
Add graph decomp test
jzaia18 d9647ca
Merge branch 'main' into feature/xdsl-specs
jzaia18 b5802db
Fix botched merge
jzaia18 187d283
Fix changelog
jzaia18 0a4eba3
Placate codefactor
jzaia18 792e807
More codefactor nitpicks
jzaia18 2e96f6d
YET MORE NITPICKS!
jzaia18 cf4b2ca
Change for xdsl is imported
jzaia18 b26c967
Reorder imports
jzaia18 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
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
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
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,103 @@ | ||
| # 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 `specs` function for the Unified Compiler.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from typing import TYPE_CHECKING, Literal | ||
|
|
||
| from ..compiler import Compiler | ||
| from .specs_collector import ResourcesResult, specs_collect | ||
| from .xdsl_conversion import get_mlir_module | ||
|
|
||
| if TYPE_CHECKING: | ||
| from catalyst.jit import QJIT | ||
|
|
||
|
|
||
| class StopCompilation(Exception): | ||
| """Custom exception to stop compilation early when the desired specs level is reached.""" | ||
|
|
||
|
|
||
| def mlir_specs( | ||
| qnode: QJIT, level: int | tuple[int] | list[int] | Literal["all"], *args, **kwargs | ||
| ) -> ResourcesResult | dict[str, ResourcesResult]: | ||
| """Compute the specs used for a circuit at the level of an MLIR pass. | ||
mudit2812 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Args: | ||
| qnode (QNode): The (QJIT'd) qnode to get the specs for | ||
| level (int | tuple[int] | list[int] | "all"): The MLIR pass level to get the specs for | ||
| *args: Positional arguments to pass to the QNode | ||
| **kwargs: Keyword arguments to pass to the QNode | ||
|
|
||
| Returns: | ||
| ResourcesResult | dict[str, ResourcesResult]: The resources for the circuit at the | ||
| specified level | ||
| """ | ||
| cache: dict[int, tuple[ResourcesResult, str]] = {} | ||
|
|
||
| if args or kwargs: | ||
| warnings.warn( | ||
| "The `specs` function does not yet support dynamic arguments, " | ||
| "so the results may not reflect information provided by the arguments.", | ||
| UserWarning, | ||
| ) | ||
|
|
||
| max_level = level | ||
| if max_level == "all": | ||
| max_level = None | ||
| elif isinstance(level, (tuple, list)): | ||
| max_level = max(level) | ||
| elif not isinstance(level, int): | ||
| raise ValueError("The `level` argument must be an int, a tuple/list of ints, or 'all'.") | ||
|
|
||
| def _specs_callback(previous_pass, module, next_pass, pass_level=0): | ||
| """Callback function for gathering circuit specs.""" | ||
|
|
||
| pass_instance = previous_pass if previous_pass else next_pass | ||
| result = specs_collect(module) | ||
|
|
||
| pass_name = pass_instance.name if hasattr(pass_instance, "name") else pass_instance | ||
| cache[pass_level] = ( | ||
| result, | ||
| pass_name if pass_level else "Before MLIR Passes", | ||
| ) | ||
|
|
||
| if max_level is not None and pass_level >= max_level: | ||
| raise StopCompilation("Stopping compilation after reaching max specs level.") | ||
|
|
||
| mlir_module = get_mlir_module(qnode, args, kwargs) | ||
| try: | ||
| Compiler.run(mlir_module, callback=_specs_callback) | ||
| except StopCompilation: | ||
mudit2812 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # We use StopCompilation to interrupt the compilation once we reach | ||
| # the desired level | ||
| pass | ||
|
|
||
| if level == "all": | ||
| return {f"{cache[lvl][1]} (MLIR-{lvl})": cache[lvl][0] for lvl in sorted(cache.keys())} | ||
|
|
||
| if isinstance(level, (tuple, list)): | ||
| if any(lvl not in cache for lvl in level): | ||
| missing = [str(lvl) for lvl in level if lvl not in cache] | ||
| raise ValueError( | ||
| f"Requested specs levels {', '.join(missing)} not found in MLIR pass list." | ||
| ) | ||
| return {f"{cache[lvl][1]} (MLIR-{lvl})": cache[lvl][0] for lvl in level if lvl in cache} | ||
|
|
||
| # Just one level was specified | ||
| if level not in cache: | ||
| raise ValueError(f"Requested specs level {level} not found in MLIR pass list.") | ||
| return cache[level][0] | ||
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.