Skip to content

Commit

Permalink
feat(hugr-py): add type_bound method to Type (#1410)
Browse files Browse the repository at this point in the history
Closes #1365
  • Loading branch information
ss2165 authored Aug 9, 2024
1 parent 373fb22 commit bd5ba47
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions hugr-py/src/hugr/tys.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def to_serial_root(self) -> stys.TypeArg:
class Type(Protocol):
"""A HUGR type."""

def type_bound(self) -> stys.TypeBound:
"""The bound of this type.
Example:
>>> Tuple(Bool, Bool).type_bound()
<TypeBound.Copyable: 'C'>
>>> Tuple(Qubit, Bool).type_bound()
<TypeBound.Any: 'A'>
"""
... # pragma: no cover

def to_serial(self) -> stys.BaseType:
"""Convert to serializable model."""
... # pragma: no cover
Expand Down Expand Up @@ -201,6 +212,9 @@ class Array(Type):
def to_serial(self) -> stys.Array:
return stys.Array(ty=self.ty.to_serial_root(), len=self.size)

def type_bound(self) -> TypeBound:
return self.ty.type_bound()


@dataclass()
class Sum(Type):
Expand All @@ -223,6 +237,9 @@ def as_tuple(self) -> Tuple:
def __repr__(self) -> str:
return f"Sum({self.variant_rows})"

def type_bound(self) -> TypeBound:
return TypeBound.join(*(t.type_bound() for r in self.variant_rows for t in r))


@dataclass()
class UnitSum(Sum):
Expand Down Expand Up @@ -268,6 +285,9 @@ class Variable(Type):
def to_serial(self) -> stys.Variable:
return stys.Variable(i=self.idx, b=self.bound)

def type_bound(self) -> TypeBound:
return self.bound


@dataclass(frozen=True)
class RowVariable(Type):
Expand All @@ -279,6 +299,9 @@ class RowVariable(Type):
def to_serial(self) -> stys.RowVar:
return stys.RowVar(i=self.idx, b=self.bound)

def type_bound(self) -> TypeBound:
return self.bound


@dataclass(frozen=True)
class USize(Type):
Expand All @@ -287,6 +310,9 @@ class USize(Type):
def to_serial(self) -> stys.USize:
return stys.USize()

def type_bound(self) -> TypeBound:
return TypeBound.Copyable


@dataclass(frozen=True)
class Alias(Type):
Expand All @@ -298,6 +324,9 @@ class Alias(Type):
def to_serial(self) -> stys.Alias:
return stys.Alias(name=self.name, bound=self.bound)

def type_bound(self) -> TypeBound:
return self.bound


@dataclass(frozen=True)
class FunctionType(Type):
Expand All @@ -309,6 +338,9 @@ class FunctionType(Type):
output: TypeRow
extension_reqs: ExtensionSet = field(default_factory=ExtensionSet)

def type_bound(self) -> TypeBound:
return TypeBound.Copyable

def to_serial(self) -> stys.FunctionType:
return stys.FunctionType(
input=ser_it(self.input),
Expand Down Expand Up @@ -362,6 +394,9 @@ class PolyFuncType(Type):
params: list[TypeParam]
body: FunctionType

def type_bound(self) -> TypeBound:
return TypeBound.Copyable

def to_serial(self) -> stys.PolyFuncType:
return stys.PolyFuncType(
params=[p.to_serial_root() for p in self.params], body=self.body.to_serial()
Expand All @@ -385,9 +420,15 @@ def to_serial(self) -> stys.Opaque:
bound=self.bound,
)

def type_bound(self) -> TypeBound:
return self.bound


@dataclass
class _QubitDef(Type):
def type_bound(self) -> TypeBound:
return TypeBound.Any

def to_serial(self) -> stys.Qubit:
return stys.Qubit()

Expand Down

0 comments on commit bd5ba47

Please sign in to comment.