Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from abc import ABC
from dataclasses import dataclass, field, fields
from typing import Any, ClassVar, Generic, TypeVar
from typing import Any, ClassVar, Generic, Literal, TypeVar

from hugr.hugr.node_port import ToNode

Expand All @@ -26,12 +26,17 @@ class MetadataMaxQubits(GuppyMetadataValue[int]):
key = "tket.hint.max_qubits"


class MetadataInline(GuppyMetadataValue[Literal["always"]]):
key = "tket.inline"


@dataclass(frozen=True, init=True, kw_only=True)
class GuppyMetadata:
"""DTO for metadata within the scope of the guppy compiler for attachment to HUGR
nodes. See `add_metadata`."""

max_qubits: MetadataMaxQubits = field(default_factory=MetadataMaxQubits, init=False)
inline: MetadataInline = field(default_factory=MetadataInline, init=False)

@classmethod
def reserved_keys(cls) -> set[str]:
Expand Down
4 changes: 3 additions & 1 deletion guppylang/src/guppylang/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
from collections.abc import Callable, Sequence
from types import FrameType
from typing import Any, NamedTuple, ParamSpec, TypedDict, TypeVar, cast, overload
from typing import Any, Literal, NamedTuple, ParamSpec, TypedDict, TypeVar, cast, overload

from guppylang_internals.ast_util import annotate_location
from guppylang_internals.compiler.core import (
Expand Down Expand Up @@ -94,6 +94,7 @@ class GuppyKwargs(TypedDict, total=False):
dagger: bool
power: bool
max_qubits: int
inline: Literal["always"]
link_name: str


Expand Down Expand Up @@ -704,6 +705,7 @@ def _parse_kwargs(kwargs: GuppyKwargs) -> ParsedGuppyKwargs:

metadata = GuppyMetadata()
metadata.max_qubits.value = kwargs.pop("max_qubits", None)
metadata.inline.value = kwargs.pop("inline", None)

link_name = kwargs.pop("link_name", None)

Expand Down
11 changes: 11 additions & 0 deletions tests/definition/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,14 @@ def test_add_metadata_property_max_qubits():
add_metadata(mock_hugr_node, guppy_metadata)

assert mock_hugr_node.metadata == {"tket.hint.max_qubits": 5}


def test_add_metadata_property_inline():
mock_hugr_node = Mock()
mock_hugr_node.metadata = {}

guppy_metadata = GuppyMetadata()
guppy_metadata.inline.value = "always"
add_metadata(mock_hugr_node, guppy_metadata)

assert mock_hugr_node.metadata == {"tket.inline": "always"}
22 changes: 22 additions & 0 deletions tests/integration/test_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from hugr.ops import FuncDefn

from guppylang_internals.definition.metadata import MetadataInline
from guppylang.decorator import guppy
from guppylang.std.builtins import result
from guppylang.std.quantum import (
qubit,
measure,
)


def test_hinted_inline(validate) -> None:
@guppy(inline="always")
def main() -> None:
result("c", measure(qubit()))

compiled = main.compile_function()
validate(compiled)

hugr = compiled.modules[0]
[fd] = [data for _, data in hugr.nodes() if isinstance(data.op, FuncDefn)]
assert fd.metadata[MetadataInline.key] == "always"
Loading