-
Notifications
You must be signed in to change notification settings - Fork 7
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
fix: Support hugr packages, fix the notebooks #622
Merged
Merged
Changes from all commits
Commits
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is nastiness with mismatched extensions here. What happens when the in-package registry and our REGISTRY have different versions of the same extension? Nothing to do for now I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, currently we merge the extensions and keep the latest one but I see how that may cause problems.
https://github.com/CQCL/hugr/blob/836b6656adc4f014f1182a39e403184f7f8d45e7/hugr-core/src/extension.rs#L90-L95