-
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(hugr-py): move std extension types/ops in to
std
module (#1288)
Not complete, see #1287 Quantum operations left in tests as they are not part of std extensions set (maybe they should be...?)
- Loading branch information
Showing
9 changed files
with
148 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Types and operations from the standard extension set.""" |
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,25 @@ | ||
"""Floating point types and operations.""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from hugr import tys, val | ||
|
||
#: HUGR 64-bit IEEE 754-2019 floating point type. | ||
FLOAT_T = tys.Opaque( | ||
extension="arithmetic.float.types", | ||
id="float64", | ||
args=[], | ||
bound=tys.TypeBound.Copyable, | ||
) | ||
|
||
|
||
@dataclass | ||
class FloatVal(val.ExtensionValue): | ||
"""Custom value for a floating point number.""" | ||
|
||
v: float | ||
|
||
def to_value(self) -> val.Extension: | ||
return val.Extension("float", FLOAT_T, self.v) |
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,71 @@ | ||
"""HUGR integer types and operations.""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass, field | ||
|
||
from hugr import tys, val | ||
from hugr.ops import Custom | ||
|
||
|
||
def int_t(width: int) -> tys.Opaque: | ||
"""Create an integer type with a given log bit width. | ||
Args: | ||
width: The log bit width of the integer. | ||
Returns: | ||
The integer type. | ||
Examples: | ||
>>> int_t(5).id # 32 bit integer | ||
'int' | ||
""" | ||
return tys.Opaque( | ||
extension="arithmetic.int.types", | ||
id="int", | ||
args=[tys.BoundedNatArg(n=width)], | ||
bound=tys.TypeBound.Eq, | ||
) | ||
|
||
|
||
#: HUGR 32-bit integer type. | ||
INT_T = int_t(5) | ||
|
||
|
||
@dataclass | ||
class IntVal(val.ExtensionValue): | ||
"""Custom value for an integer.""" | ||
|
||
v: int | ||
|
||
def to_value(self) -> val.Extension: | ||
return val.Extension("int", INT_T, self.v) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class IntOps(Custom): | ||
"""Base class for integer operations.""" | ||
|
||
extension: tys.ExtensionId = "arithmetic.int" | ||
|
||
|
||
_ARG_I32 = tys.BoundedNatArg(n=5) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class _DivModDef(IntOps): | ||
"""DivMod operation, has two inputs and two outputs.""" | ||
|
||
num_out: int = 2 | ||
extension: tys.ExtensionId = "arithmetic.int" | ||
op_name: str = "idivmod_u" | ||
signature: tys.FunctionType = field( | ||
default_factory=lambda: tys.FunctionType(input=[INT_T] * 2, output=[INT_T] * 2) | ||
) | ||
args: list[tys.TypeArg] = field(default_factory=lambda: [_ARG_I32, _ARG_I32]) | ||
|
||
|
||
#: DivMod operation. | ||
DivMod = _DivModDef() |
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,38 @@ | ||
"""HUGR logic operations.""" | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from hugr import tys | ||
from hugr.ops import Command, Custom | ||
|
||
if TYPE_CHECKING: | ||
from hugr.ops import ComWire | ||
|
||
|
||
@dataclass(frozen=True) | ||
class LogicOps(Custom): | ||
"""Base class for logic operations.""" | ||
|
||
extension: tys.ExtensionId = "logic" | ||
|
||
|
||
_NotSig = tys.FunctionType.endo([tys.Bool]) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class _NotDef(LogicOps): | ||
"""Not operation.""" | ||
|
||
num_out: int = 1 | ||
op_name: str = "Not" | ||
signature: tys.FunctionType = _NotSig | ||
|
||
def __call__(self, a: ComWire) -> Command: | ||
return super().__call__(a) | ||
|
||
|
||
#: Not operation | ||
Not = _NotDef() |
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
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
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