Skip to content

Commit

Permalink
refactor(hugr-py): freeze custom ops (#1279)
Browse files Browse the repository at this point in the history
Makes singletons like `Measure = MeasureDef()` safer, as the instance
isn't going to get mutated between uses
  • Loading branch information
ss2165 authored Jul 9, 2024
1 parent d6775ea commit 94702d2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hugr-py/src/hugr/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def _set_in_types(self, types: tys.TypeRow) -> None:
self._types = types


@dataclass()
@dataclass(frozen=True)
class Custom(DataflowOp):
"""A non-core dataflow operation defined in an extension."""

Expand Down
18 changes: 9 additions & 9 deletions hugr-py/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def to_value(self) -> val.Extension:
return val.Extension("float", FLOAT_T, self.v)


@dataclass
@dataclass(frozen=True)
class LogicOps(Custom):
extension: tys.ExtensionId = "logic"

Expand All @@ -61,7 +61,7 @@ class LogicOps(Custom):


# TODO get from YAML
@dataclass
@dataclass(frozen=True)
class NotDef(LogicOps):
num_out: int = 1
op_name: str = "Not"
Expand All @@ -74,15 +74,15 @@ def __call__(self, a: ComWire) -> Command:
Not = NotDef()


@dataclass
@dataclass(frozen=True)
class QuantumOps(Custom):
extension: tys.ExtensionId = "tket2.quantum"


_OneQbSig = tys.FunctionType.endo([tys.Qubit])


@dataclass
@dataclass(frozen=True)
class OneQbGate(QuantumOps):
op_name: str
num_out: int = 1
Expand All @@ -98,7 +98,7 @@ def __call__(self, q: ComWire) -> Command:
_TwoQbSig = tys.FunctionType.endo([tys.Qubit] * 2)


@dataclass
@dataclass(frozen=True)
class TwoQbGate(QuantumOps):
op_name: str
num_out: int = 2
Expand All @@ -113,7 +113,7 @@ def __call__(self, q0: ComWire, q1: ComWire) -> Command:
_MeasSig = tys.FunctionType([tys.Qubit], [tys.Qubit, tys.Bool])


@dataclass
@dataclass(frozen=True)
class MeasureDef(QuantumOps):
op_name: str = "Measure"
num_out: int = 2
Expand All @@ -128,7 +128,7 @@ def __call__(self, q: ComWire) -> Command:
_RzSig = tys.FunctionType([tys.Qubit, FLOAT_T], [tys.Qubit])


@dataclass
@dataclass(frozen=True)
class RzDef(QuantumOps):
op_name: str = "Rz"
num_out: int = 1
Expand All @@ -141,15 +141,15 @@ def __call__(self, q: ComWire, fl_wire: ComWire) -> Command:
Rz = RzDef()


@dataclass
@dataclass(frozen=True)
class IntOps(Custom):
extension: tys.ExtensionId = "arithmetic.int"


ARG_5 = tys.BoundedNatArg(n=5)


@dataclass
@dataclass(frozen=True)
class DivModDef(IntOps):
num_out: int = 2
extension: tys.ExtensionId = "arithmetic.int"
Expand Down

0 comments on commit 94702d2

Please sign in to comment.