-
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
feat(hugr-py): builder ops separate from serialised ops #1140
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ffd745
feat: builder ops separate from serialised ops
ss2165 3e699fc
refactor: commands -> call on ops
ss2165 d180047
fixup! feat: builder ops separate from serialised ops
ss2165 ebfd51b
impl `num_out` for more ops
ss2165 ac56552
refactor: use _ops in circular import
ss2165 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 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,123 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
from typing import Generic, Protocol, TypeVar, TYPE_CHECKING | ||
from hugr.serialization.ops import BaseOp | ||
import hugr.serialization.ops as sops | ||
import hugr.serialization.tys as tys | ||
|
||
if TYPE_CHECKING: | ||
from hugr._hugr import Hugr, Node, Wire | ||
|
||
|
||
class Op(Protocol): | ||
@property | ||
def num_out(self) -> int | None: | ||
return None | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> BaseOp: ... | ||
|
||
def __call__(self, *args) -> Command: | ||
return Command(self, list(args)) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Command: | ||
op: Op | ||
incoming: list[Wire] | ||
|
||
|
||
T = TypeVar("T", bound=BaseOp) | ||
|
||
|
||
@dataclass() | ||
class SerWrap(Op, Generic[T]): | ||
# catch all for serial ops that don't have a corresponding Op class | ||
_serial_op: T | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> T: | ||
root = self._serial_op.model_copy() | ||
root.parent = parent.idx | ||
return root | ||
|
||
|
||
@dataclass() | ||
class Input(Op): | ||
types: list[tys.Type] | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.Input: | ||
return sops.Input(parent=parent.idx, types=self.types) | ||
|
||
def __call__(self) -> Command: | ||
return super().__call__() | ||
|
||
|
||
@dataclass() | ||
class Output(Op): | ||
types: list[tys.Type] | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.Output: | ||
return sops.Output(parent=parent.idx, types=self.types) | ||
|
||
|
||
@dataclass() | ||
class Custom(Op): | ||
op_name: str | ||
signature: tys.FunctionType = field(default_factory=tys.FunctionType.empty) | ||
description: str = "" | ||
extension: tys.ExtensionId = "" | ||
args: list[tys.TypeArg] = field(default_factory=list) | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.CustomOp: | ||
return sops.CustomOp( | ||
parent=parent.idx, | ||
extension=self.extension, | ||
op_name=self.op_name, | ||
signature=self.signature, | ||
description=self.description, | ||
args=self.args, | ||
) | ||
|
||
|
||
@dataclass() | ||
class MakeTuple(Op): | ||
types: list[tys.Type] | ||
num_out: int | None = 1 | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.MakeTuple: | ||
return sops.MakeTuple( | ||
parent=parent.idx, | ||
tys=self.types, | ||
) | ||
|
||
def __call__(self, *elements: Wire) -> Command: | ||
return super().__call__(*elements) | ||
|
||
|
||
@dataclass() | ||
class UnpackTuple(Op): | ||
types: list[tys.Type] | ||
|
||
@property | ||
def num_out(self) -> int | None: | ||
return len(self.types) | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.UnpackTuple: | ||
return sops.UnpackTuple( | ||
parent=parent.idx, | ||
tys=self.types, | ||
) | ||
|
||
def __call__(self, tuple_: Wire) -> Command: | ||
return super().__call__(tuple_) | ||
|
||
|
||
@dataclass() | ||
class DFG(Op): | ||
signature: tys.FunctionType = field(default_factory=tys.FunctionType.empty) | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.DFG: | ||
return sops.DFG( | ||
parent=parent.idx, | ||
signature=self.signature, | ||
) |
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,3 +1,4 @@ | ||
from __future__ import annotations | ||
import inspect | ||
import sys | ||
from abc import ABC | ||
|
@@ -41,6 +42,10 @@ | |
"""Name of the op for visualisation""" | ||
return self.__class__.__name__ | ||
|
||
def deserialize(self) -> ops.Op: | ||
"""Deserializes the model into the corresponding Op.""" | ||
return ops.SerWrap(self) | ||
|
||
|
||
# ---------------------------------------------------------- | ||
# --------------- Module level operations ------------------ | ||
|
@@ -209,6 +214,9 @@ | |
assert len(in_types) == 0 | ||
self.types = list(out_types) | ||
|
||
def deserialize(self) -> ops.Input: | ||
return ops.Input(types=self.types) | ||
|
||
|
||
class Output(DataflowOp): | ||
"""An output node. The inputs are the outputs of the function.""" | ||
|
@@ -220,6 +228,9 @@ | |
assert len(out_types) == 0 | ||
self.types = list(in_types) | ||
|
||
def deserialize(self) -> ops.Output: | ||
return ops.Output(types=self.types) | ||
|
||
|
||
class Call(DataflowOp): | ||
""" | ||
|
@@ -292,6 +303,9 @@ | |
input=list(inputs), output=list(outputs), extension_reqs=ExtensionSet([]) | ||
) | ||
|
||
def deserialize(self) -> ops.DFG: | ||
return ops.DFG(self.signature) | ||
|
||
|
||
# ------------------------------------------------ | ||
# --------------- ControlFlowOp ------------------ | ||
|
@@ -388,6 +402,14 @@ | |
def display_name(self) -> str: | ||
return self.op_name | ||
|
||
def deserialize(self) -> ops.Custom: | ||
return ops.Custom( | ||
extension=self.extension, | ||
op_name=self.op_name, | ||
signature=self.signature, | ||
args=self.args, | ||
) | ||
|
||
model_config = ConfigDict( | ||
# Needed to avoid random '\n's in the pydantic description | ||
json_schema_extra={ | ||
|
@@ -424,6 +446,9 @@ | |
in_types = [] | ||
self.tys = list(in_types) | ||
|
||
def deserialize(self) -> ops.MakeTuple: | ||
return ops.MakeTuple(self.tys) | ||
|
||
|
||
class UnpackTuple(DataflowOp): | ||
"""An operation that packs all its inputs into a tuple.""" | ||
|
@@ -434,6 +459,9 @@ | |
def insert_port_types(self, in_types: TypeRow, out_types: TypeRow) -> None: | ||
self.tys = list(out_types) | ||
|
||
def deserialize(self) -> ops.UnpackTuple: | ||
return ops.UnpackTuple(self.tys) | ||
|
||
|
||
class Tag(DataflowOp): | ||
"""An operation that creates a tagged sum value from one of its variants.""" | ||
|
@@ -529,3 +557,6 @@ | |
) | ||
|
||
tys_model_rebuild(dict(classes)) | ||
|
||
# | ||
import hugr._ops as ops # noqa: E402 # needed to avoid circular imports | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh -.- This re-exports from hugr import _ops for now. |
Oops, something went wrong.
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.
This is unset for many op definitions (
Input
,SerWrap
,Custom
,DFG
)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.
it can't be known in general - but can be for many of those! (which I've added). Am unsure about Custom but I've added it for now. It should be left as the default
None
for SerWrap.