Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
erick-xanadu committed Jan 9, 2025
1 parent f26e3c2 commit 71500ba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
32 changes: 31 additions & 1 deletion frontend/catalyst/passes/pass_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,20 @@ class Pass:
:class:`Pass` will be used when generating `ApplyRegisteredPassOp`s.
The attribute `pass_name` corresponds to the field `name`.
The attribute `options` is generated by the `get_options` method.
People working on MLIR plugins may use this or :class:`PassPlugin` to
schedule their compilation pass. E.g.,
.. code-block:: python
def an_optimization(qnode):
@functools.wraps(qnode)
def wrapper(*args, **kwargs):
pass_pipeline = kwargs.pop("pass_pipeline", [])
pass_pipeline.append(Pass("my_library.my_optimization", *args, **kwargs))
kwargs["pass_pipeline"] = pass_pipeline
return qnode(*args, **kwargs)
return wrapper
"""

def __init__(self, name: str, *options: list[str], **valued_options: dict[str, str]):
Expand Down Expand Up @@ -277,7 +291,9 @@ def __repr__(self):
return (
self.name
+ " ".join(f"--{str(option)}" for option in self.options)
+ " ".join([f"--{str(option)}={str(value)}" for option, value in self.valued_options.items()])
+ " ".join(
[f"--{str(option)}={str(value)}" for option, value in self.valued_options.items()]
)
)


Expand All @@ -288,6 +304,20 @@ class PassPlugin(Pass):
E.g.,
--pass-plugin=path/to/plugin --dialect-plugin=path/to/plugin
People working on MLIR plugins may use this or :class:`Pass` to
schedule their compilation pass. E.g.,
.. code-block:: python
def an_optimization(qnode):
@functools.wraps(qnode)
def wrapper(*args, **kwargs):
pass_pipeline = kwargs.pop("pass_pipeline", [])
pass_pipeline.append(PassPlugin(path_to_plugin, "my_optimization", *args, **kwargs))
kwargs["pass_pipeline"] = pass_pipeline
return qnode(*args, **kwargs)
return wrapper
"""

def __init__(
Expand Down
2 changes: 2 additions & 0 deletions frontend/test/lit/test_mlir_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def module():
from pathlib import Path

import pennylane as qml

import catalyst
from catalyst.compiler import CompileOptions, Compiler
from catalyst.utils.filesystem import WorkspaceManager
Expand Down Expand Up @@ -112,4 +113,5 @@ def example():

print(example.mlir)


test_pass_options()
6 changes: 5 additions & 1 deletion frontend/test/pytest/test_mlir_plugin_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def example():

assert example.mlir


def test_get_options():
"""
ApplyRegisteredPassOp expects options to be a single StringAttr
Expand All @@ -82,5 +83,8 @@ def test_get_options():
However, experimentally we found that single-options also work without values.
"""
assert catalyst.passes.Pass("example-pass", "single-option").get_options() == "single-option"
assert catalyst.passes.Pass("example-pass", "an-option", "bn-option").get_options() == "an-option bn-option"
assert (
catalyst.passes.Pass("example-pass", "an-option", "bn-option").get_options()
== "an-option bn-option"
)
assert catalyst.passes.Pass("example-pass", option=True).get_options() == "option=True"

0 comments on commit 71500ba

Please sign in to comment.