Skip to content
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!: Support implicit modules for all decorators and turn builtins into implicit module #476

Merged
merged 23 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3c3f157
feat!: Support implicit modules for all decorators and turn builtins …
mark-koch Sep 11, 2024
fcf6984
Lints
mark-koch Sep 11, 2024
359c1fc
Fix test
mark-koch Sep 11, 2024
2b21a44
Fix more tests
mark-koch Sep 11, 2024
d25dab1
Fix test
mark-koch Sep 12, 2024
6f6a564
Merge remote-tracking branch 'origin/main' into feat/implicit-decorators
mark-koch Sep 12, 2024
faa5b64
feat!: Add functions to quantum module and make quantum_functional in…
mark-koch Sep 13, 2024
d99d694
Fix tket tests
mark-koch Sep 13, 2024
f5cff19
Merge remote-tracking branch 'origin/main' into feat/quantum-module
mark-koch Sep 13, 2024
6d869e4
Merge remote-tracking branch 'origin/feat/quantum-module' into feat/i…
mark-koch Sep 13, 2024
37162f9
Fix tests
mark-koch Sep 13, 2024
9287b39
Fmt
mark-koch Sep 13, 2024
f24183d
Fix test
mark-koch Sep 13, 2024
133888f
tweak comment
acl-cqc Sep 16, 2024
3f04c01
quantum.py import only no_type_check from typing
acl-cqc Sep 16, 2024
fc29e84
fix: Fix implicit imports in notebooks (#495)
mark-koch Sep 16, 2024
2f70db7
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Sep 16, 2024
17b5647
Merge 'origin/main' including 'feat/quantum-module' into HEAD
acl-cqc Sep 16, 2024
d4532ac
Add @overloads to help document _with_optional_module; use Decorator …
acl-cqc Sep 16, 2024
e3c4453
fix overloads, oops, and type-ignore, heh
acl-cqc Sep 16, 2024
15e5c48
Pull stuff out of loop in _get_python_caller
acl-cqc Sep 16, 2024
bafadfe
assert discarded buffer is empty
acl-cqc Sep 16, 2024
8f75828
make assert mypy-ok
acl-cqc Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 163 additions & 60 deletions guppylang/decorator.py

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion guppylang/ipython_inspect.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Tools for inspecting source code when running in IPython."""

import ast
from typing import NamedTuple, cast
from typing import Any, NamedTuple, cast


def is_running_ipython() -> bool:
Expand Down Expand Up @@ -54,3 +54,11 @@ def find_ipython_def(name: str) -> IPythonDef | None:
cell_name = f"In [{len(cell_sources) - i}]"
return IPythonDef(node, cell_name, cell_source)
return None


def get_ipython_globals() -> dict[str, Any]:
"""Returns the globals of the current IPython kernel."""
try:
return get_ipython().user_ns # type: ignore[name-defined, no-any-return]
except NameError:
raise RuntimeError("Not running in IPython") from None
1 change: 1 addition & 0 deletions guppylang/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from guppylang.definition.ty import TypeDef
from guppylang.error import GuppyError, pretty_errors

PyClass = type
PyFunc = Callable[..., Any]
PyFuncDefOrDecl = tuple[bool, PyFunc]

Expand Down
36 changes: 16 additions & 20 deletions guppylang/prelude/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
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.angle import AngleOpCompiler
from guppylang.prelude.builtins import nat

angles = GuppyModule("angles")


_hugr_angle_type = ht.Opaque("angle", ht.TypeBound.Copyable, [], "tket2.quantum")


Expand All @@ -32,68 +28,68 @@ def _hugr_angle_value(numerator: int, log_denominator: int) -> hv.Value:
)


pi = guppy.constant(angles, "pi", ty="angle", value=_hugr_angle_value(1, 1))
pi = guppy.constant("pi", ty="angle", value=_hugr_angle_value(1, 1))


@guppy.type(angles, _hugr_angle_type)
@guppy.type(_hugr_angle_type)
class angle:
"""The type of angles represented as dyadic rational multiples of 2π."""

@guppy.custom(angles, AngleOpCompiler("afromrad"), CoercingChecker())
@guppy.custom(AngleOpCompiler("afromrad"), CoercingChecker())
def __new__(radians: float) -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("aadd"))
@guppy.custom(AngleOpCompiler("aadd"))
def __add__(self: "angle", other: "angle") -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("asub"))
@guppy.custom(AngleOpCompiler("asub"))
def __sub__(self: "angle", other: "angle") -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("aneg"))
@guppy.custom(AngleOpCompiler("aneg"))
def __neg__(self: "angle") -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("atorad"))
@guppy.custom(AngleOpCompiler("atorad"))
def __float__(self: "angle") -> float: ...

@guppy.custom(angles, AngleOpCompiler("aeq"))
@guppy.custom(AngleOpCompiler("aeq"))
def __eq__(self: "angle", other: "angle") -> bool: ...

@guppy(angles)
@guppy
@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)
@guppy
@no_type_check
def __rmul__(self: "angle", other: int) -> "angle":
return self * other

@guppy(angles)
@guppy
@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"))
@guppy.custom(AngleOpCompiler("amul"))
def _nat_mul(self: "angle", other: nat) -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("aneg"))
@guppy.custom(AngleOpCompiler("aneg"))
def _nat_div(self: "angle", other: nat) -> "angle": ...

@guppy.custom(angles, AngleOpCompiler("aparts"))
@guppy.custom(AngleOpCompiler("aparts"))
def _parts(self: "angle") -> tuple[nat, nat]: ...

@guppy(angles)
@guppy
@no_type_check
def numerator(self: "angle") -> nat:
numerator, _ = self._parts()
return numerator

@guppy(angles)
@guppy
@no_type_check
def log_denominator(self: "angle") -> nat:
_, log_denominator = self._parts()
Expand Down
Loading
Loading