-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: builder ops separate from serialised ops
no more parent=-1
- Loading branch information
Showing
6 changed files
with
167 additions
and
211 deletions.
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,98 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass, field | ||
from typing import Generic, 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 | ||
|
||
|
||
class Op(ABC): | ||
@abstractmethod | ||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> BaseOp: ... | ||
|
||
|
||
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) -> BaseOp: | ||
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) | ||
|
||
|
||
@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): | ||
extension: tys.ExtensionId | ||
op_name: str | ||
signature: tys.FunctionType = field(default_factory=tys.FunctionType.empty) | ||
description: str = "" | ||
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] | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.MakeTuple: | ||
return sops.MakeTuple( | ||
parent=parent.idx, | ||
tys=self.types, | ||
) | ||
|
||
|
||
@dataclass() | ||
class UnpackTuple(Op): | ||
types: list[tys.Type] | ||
|
||
def to_serial(self, node: Node, parent: Node, hugr: Hugr) -> sops.UnpackTuple: | ||
return sops.UnpackTuple( | ||
parent=parent.idx, | ||
tys=self.types, | ||
) | ||
|
||
|
||
@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, | ||
) |
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.