Skip to content

Commit

Permalink
feat(hugr-py): add alias, make deserialize abstract
Browse files Browse the repository at this point in the history
all ops covered!
  • Loading branch information
ss2165 committed Jun 21, 2024
1 parent 5153df6 commit b9940da
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
8 changes: 7 additions & 1 deletion hugr-py/src/hugr/_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ._dfg import _DfBase
from hugr._node_port import Node
from ._hugr import Hugr
from ._tys import TypeRow, TypeParam, PolyFuncType
from ._tys import TypeRow, TypeParam, PolyFuncType, Type, TypeBound


@dataclass
Expand Down Expand Up @@ -47,3 +47,9 @@ def declare_function(self, name: str, signature: PolyFuncType) -> Node:

def add_const(self, value: val.Value) -> Node:
return self.hugr.add_node(ops.Const(value), self.hugr.root)

def add_alias_defn(self, name: str, ty: Type) -> Node:
return self.hugr.add_node(ops.AliasDefn(name, ty), self.hugr.root)

def add_alias_decl(self, name: str, bound: TypeBound) -> Node:
return self.hugr.add_node(ops.AliasDecl(name, bound), self.hugr.root)
34 changes: 34 additions & 0 deletions hugr-py/src/hugr/_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,37 @@ def outer_signature(self) -> tys.FunctionType:

def set_in_types(self, types: tys.TypeRow) -> None:
self._type_row = types


@dataclass
class AliasDecl(Op):
name: str
bound: tys.TypeBound
num_out: int | None = 0

def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.AliasDecl:
return sops.AliasDecl(
parent=parent.idx,
name=self.name,
bound=self.bound,
)

def port_kind(self, port: InPort | OutPort) -> tys.Kind:
raise InvalidPort(port)

Check warning on line 790 in hugr-py/src/hugr/_ops.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/_ops.py#L790

Added line #L790 was not covered by tests


@dataclass
class AliasDefn(Op):
name: str
definition: tys.Type
num_out: int | None = 0

def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.AliasDefn:
return sops.AliasDefn(
parent=parent.idx,
name=self.name,
definition=self.definition.to_serial_root(),
)

def port_kind(self, port: InPort | OutPort) -> tys.Kind:
raise InvalidPort(port)

Check warning on line 807 in hugr-py/src/hugr/_ops.py

View check run for this annotation

Codecov / codecov/patch

hugr-py/src/hugr/_ops.py#L807

Added line #L807 was not covered by tests
8 changes: 7 additions & 1 deletion hugr-py/src/hugr/serialization/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def display_name(self) -> str:
"""Name of the op for visualisation"""
return self.__class__.__name__

@abstractmethod
def deserialize(self) -> _ops.Op:
"""Deserializes the model into the corresponding Op."""
raise NotImplementedError


# ----------------------------------------------------------
Expand Down Expand Up @@ -602,12 +602,18 @@ class AliasDecl(BaseOp):
name: str
bound: TypeBound

def deserialize(self) -> _ops.AliasDecl:
return _ops.AliasDecl(self.name, self.bound)


class AliasDefn(BaseOp):
op: Literal["AliasDefn"] = "AliasDefn"
name: str
definition: Type

def deserialize(self) -> _ops.AliasDefn:
return _ops.AliasDefn(self.name, self.definition.deserialize())


class OpType(RootModel):
"""A constant operation."""
Expand Down
8 changes: 8 additions & 0 deletions hugr-py/tests/test_hugr_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,11 @@ def test_lift() -> None:
lift = d.add(ops.Lift("X")(q))
d.set_outputs(lift)
_validate(d.hugr)


def test_alias() -> None:
mod = Module()
_dfn = mod.add_alias_defn("my_int", INT_T)
_dcl = mod.add_alias_decl("my_bool", tys.TypeBound.Eq)

_validate(mod.hugr)

0 comments on commit b9940da

Please sign in to comment.