-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add pi constant #451
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c00759e
feat: Add angle type
mark-koch 91d3f7f
feat: Add pi constant
mark-koch cdf0b5b
Merge remote-tracking branch 'origin/main' into feat/angles
mark-koch 4d8ddaa
Fix extension ops
mark-koch f007296
Merge branch 'feat/angles' into feat/pi
mark-koch a873151
Merge remote-tracking branch 'origin/main' into feat/pi
mark-koch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,62 @@ | ||
import ast | ||
from dataclasses import dataclass, field | ||
|
||
from hugr import Node, Wire | ||
from hugr import val as hv | ||
from hugr.dfg import OpVar, _DefinitionBuilder | ||
|
||
from guppylang.ast_util import AstNode | ||
from guppylang.checker.core import Globals | ||
from guppylang.compiler.core import CompiledGlobals, DFContainer | ||
from guppylang.definition.common import CompilableDef, ParsableDef | ||
from guppylang.definition.value import CompiledValueDef, ValueDef | ||
from guppylang.tys.parsing import type_from_ast | ||
|
||
|
||
@dataclass(frozen=True) | ||
class RawConstDef(ParsableDef): | ||
"""A raw constant definition as provided by the user.""" | ||
|
||
type_ast: ast.expr | ||
value: hv.Value | ||
|
||
description: str = field(default="constant", init=False) | ||
|
||
def parse(self, globals: Globals) -> "ConstDef": | ||
"""Parses and checks the user-provided signature of the function.""" | ||
return ConstDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
type_from_ast(self.type_ast, globals, None), | ||
self.type_ast, | ||
self.value, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ConstDef(RawConstDef, ValueDef, CompilableDef): | ||
"""A constant with a checked type.""" | ||
|
||
def compile_outer(self, graph: _DefinitionBuilder[OpVar]) -> "CompiledConstDef": | ||
const_node = graph.add_const(self.value) | ||
return CompiledConstDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
self.ty, | ||
self.type_ast, | ||
self.value, | ||
const_node, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CompiledConstDef(ConstDef, CompiledValueDef): | ||
"""A constant that has been compiled to a Hugr node.""" | ||
|
||
const_node: Node | ||
|
||
def load(self, dfg: DFContainer, globals: CompiledGlobals, node: AstNode) -> Wire: | ||
"""Loads the extern value into a local Hugr dataflow graph.""" | ||
return dfg.builder.load(self.const_node) |
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,104 @@ | ||
"""Guppy standard module for dyadic rational angles.""" | ||
|
||
# mypy: disable-error-code="empty-body, misc, override" | ||
|
||
from typing import no_type_check | ||
|
||
from hugr import tys as ht | ||
from hugr import val as hv | ||
|
||
from guppylang.decorator import guppy | ||
from guppylang.module import GuppyModule | ||
from guppylang.prelude._internal.checker import CoercingChecker | ||
from guppylang.prelude._internal.compiler import ( | ||
AngleOpCompiler, | ||
) | ||
from guppylang.prelude.builtins import nat | ||
|
||
angles = GuppyModule("angles") | ||
|
||
|
||
_hugr_angle_type = ht.Opaque( | ||
"angle", ht.TypeBound.Copyable, [ht.BoundedNatArg(1)], "quantum.tket2" | ||
) | ||
|
||
|
||
def _hugr_angle_value(numerator: int, log_denominator: int) -> hv.Value: | ||
custom_const = { | ||
"log_denominator": log_denominator, | ||
"value": numerator, | ||
} | ||
return hv.Extension( | ||
name="ConstAngle", | ||
typ=_hugr_angle_type, | ||
val=custom_const, | ||
extensions=["quantum.tket2"], | ||
) | ||
|
||
|
||
pi = guppy.constant(angles, "pi", ty="angle", value=_hugr_angle_value(1, 1)) | ||
|
||
|
||
@guppy.type(angles, _hugr_angle_type) | ||
class angle: | ||
"""The type of angles represented as dyadic rational multiples of 2π.""" | ||
|
||
@guppy.custom(angles, AngleOpCompiler("afromrad"), CoercingChecker()) | ||
def __new__(radians: float) -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("aadd")) | ||
def __add__(self: "angle", other: "angle") -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("asub")) | ||
def __sub__(self: "angle", other: "angle") -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("aneg")) | ||
def __neg__(self: "angle") -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("atorad")) | ||
def __float__(self: "angle") -> float: ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("aeq")) | ||
def __eq__(self: "angle", other: "angle") -> bool: ... | ||
|
||
@guppy(angles) | ||
@no_type_check | ||
def __mul__(self: "angle", other: int) -> "angle": | ||
if other < 0: | ||
return self._nat_mul(nat(other)) | ||
else: | ||
return -self._nat_mul(nat(other)) | ||
|
||
@guppy(angles) | ||
@no_type_check | ||
def __rmul__(self: "angle", other: int) -> "angle": | ||
return self * other | ||
|
||
@guppy(angles) | ||
@no_type_check | ||
def __truediv__(self: "angle", other: int) -> "angle": | ||
if other < 0: | ||
return self._nat_div(nat(other)) | ||
else: | ||
return -self._nat_div(nat(other)) | ||
|
||
@guppy.custom(angles, AngleOpCompiler("amul")) | ||
def _nat_mul(self: "angle", other: nat) -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("aneg")) | ||
def _nat_div(self: "angle", other: nat) -> "angle": ... | ||
|
||
@guppy.custom(angles, AngleOpCompiler("aparts")) | ||
def _parts(self: "angle") -> tuple[nat, nat]: ... | ||
|
||
@guppy(angles) | ||
@no_type_check | ||
def numerator(self: "angle") -> nat: | ||
numerator, _ = self._parts() | ||
return numerator | ||
|
||
@guppy(angles) | ||
@no_type_check | ||
def log_denominator(self: "angle") -> nat: | ||
_, log_denominator = self._parts() | ||
return log_denominator |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: this will be "tket2.quantum" in next tket2 release