-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support hugr packages, fix the notebooks (#622)
This is a small bundle of changes aimed at fixing the example notebooks. - Use packages when building hugrs. This includes parsing guppy packages from rust, as well as returning packages from the circuit builder. - This required adding exports for the tket2 extensions from python. - We temporarily add a py dependency on `hugr-cli`, since packages are currently defined there. See CQCL/hugr#1530 The `guppy -> pytket` pipeline is still broken, as guppy now generates many more function calls and nested control flow primitives, which we do not currently support. drive-by: Make `crate::serialize::guppy::find_function` public. Closes #621.
- Loading branch information
Showing
12 changed files
with
3,120 additions
and
367 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,26 +1,38 @@ | ||
"""Some utility functions for the example notebooks.""" | ||
|
||
from typing import TYPE_CHECKING, Any | ||
from hugr import Hugr | ||
from tket2.passes import lower_to_pytket | ||
from tket2.circuit import Tk2Circuit | ||
from guppylang.definition.function import RawFunctionDef # type: ignore[import-untyped, import-not-found, unused-ignore] # noqa: F401 | ||
|
||
if TYPE_CHECKING: | ||
try: | ||
from guppylang.definition.function import RawFunctionDef # type: ignore[import-untyped, import-not-found, unused-ignore] # noqa: F401 | ||
except ImportError: | ||
RawFunctionDef = Any | ||
|
||
def setup_jupyter_rendering(): | ||
"""Set up hugr rendering for Jupyter notebooks.""" | ||
|
||
# We need to define this helper function for now. It will be included in guppy in the future. | ||
def _repr_hugr( | ||
h: Hugr, include=None, exclude=None, **kwargs | ||
) -> dict[str, bytes | str]: | ||
return h.render_dot()._repr_mimebundle_(include, exclude, **kwargs) | ||
|
||
def _repr_tk2circ( | ||
circ: Tk2Circuit, include=None, exclude=None, **kwargs | ||
) -> dict[str, bytes | str]: | ||
h = Hugr.load_json(circ.to_hugr_json()) | ||
return _repr_hugr(h, include, exclude, **kwargs) | ||
|
||
setattr(Hugr, "_repr_mimebundle_", _repr_hugr) | ||
setattr(Tk2Circuit, "_repr_mimebundle_", _repr_tk2circ) | ||
|
||
|
||
# TODO: Should this be part of the guppy API? Or tket2? | ||
def guppy_to_circuit(func_def: RawFunctionDef) -> Tk2Circuit: | ||
"""Convert a Guppy function definition to a `Tk2Circuit`.""" | ||
module = func_def.id.module | ||
assert module is not None, "Function definition must belong to a module" | ||
|
||
hugr = module.compile() | ||
assert hugr is not None, "Module must be compilable" | ||
pkg = module.compile() | ||
|
||
json = hugr.to_raw().to_json() | ||
json = pkg.to_json() | ||
circ = Tk2Circuit.from_guppy_json(json, func_def.name) | ||
|
||
return lower_to_pytket(circ) |
This file contains 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 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 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,42 @@ | ||
"""Hugr extension definitions for tket2 circuits.""" | ||
# This will be moved to a separate python library soon. | ||
|
||
import pkgutil | ||
import functools | ||
|
||
from hugr._serialization.extension import Extension as PdExtension | ||
from hugr.ext import Extension | ||
|
||
|
||
@functools.cache | ||
def rotation() -> Extension: | ||
return load_extension("tket2.rotation") | ||
|
||
|
||
@functools.cache | ||
def futures() -> Extension: | ||
return load_extension("tket2.futures") | ||
|
||
|
||
@functools.cache | ||
def hseries() -> Extension: | ||
return load_extension("tket2.hseries") | ||
|
||
|
||
@functools.cache | ||
def quantum() -> Extension: | ||
return load_extension("tket2.quantum") | ||
|
||
|
||
@functools.cache | ||
def result() -> Extension: | ||
return load_extension("tket2.result") | ||
|
||
|
||
def load_extension(name: str) -> Extension: | ||
replacement = name.replace(".", "/") | ||
json_str = pkgutil.get_data(__name__, f"_json_defs/{replacement}.json") | ||
assert json_str is not None, f"Could not load json for extension {name}" | ||
# TODO: Replace with `Extension.from_json` once that is implemented | ||
# https://github.com/CQCL/hugr/issues/1523 | ||
return PdExtension.model_validate_json(json_str).deserialize() |
This file contains 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 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 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 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