-
Notifications
You must be signed in to change notification settings - Fork 5
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(qsystem): add Random number generation module #822
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #822 +/- ##
==========================================
+ Coverage 93.06% 93.11% +0.05%
==========================================
Files 72 74 +2
Lines 8564 8633 +69
==========================================
+ Hits 7970 8039 +69
Misses 594 594 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Nice, thank you!
I don't follow why this is sorry - the |
I also tried using tests/integration/test_qsystem.py:8: in <module>
from guppylang.std.qsystem.random import RNG
guppylang/std/qsystem/random.py:26: in <module>
class RNG:
guppylang/std/qsystem/random.py:34: in RNG
@guppy(qsystem_random)
guppylang/decorator.py:141: in <lambda>
return lambda s: dec(s, arg)
guppylang/decorator.py:118: in dec
return module.register_func_def(f)
guppylang/module.py:229: in register_func_def
defn = RawFunctionDef(DefId.fresh(self), f.__name__, None, f, get_py_scope(f))
guppylang/module.py:404: in get_py_scope
code = f.__code__
E AttributeError: 'staticmethod' object has no attribute '__code__'. Did you mean: '__call__'? which perhaps suggests that static methods are not yet supported in Guppy? |
ah that's fine (indeed static methods are not supported) - just have it available as a standalone function ( |
See CQCL/guppylang#822 BREAKING CHANGE: To be compatible with Guppy's convention of implicitly returning `self` as the second value of the tuple, the following signatures are updated: ```diff - /// `fn random_int(RNGContext) -> (RNGContext, u32)` + /// `fn random_int(RNGContext) -> (u32, RNGContext)` - /// `fn random_float(RNGContext) -> (RNGContext, f32)` + /// `fn random_float(RNGContext) -> (f32, RNGContext)` - /// `fn random_int_bounded(RNGContext, bound: u32) -> (RNGContext, u32)` + /// `fn random_int_bounded(RNGContext, bound: u32) -> (u32, RNGContext)` ```
@ss2165 is it accurate that we need CQCL/hugr#1946 and another PR for truncation to be able to Or am I missing how to perform type conversions between 32-bit and 64-bit ints? |
I think that's right - I didn't realise they weren't being emitted apologies |
c40616b
to
348ca54
Compare
Thanks to Mark for feedback on design
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.
Nice, looks good!
I have a patch to take this to merge, but it might require #846 diff --git a/pyproject.toml b/pyproject.toml
index b5b31b2..5a826c0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -39,7 +39,7 @@ dependencies = [
"pydantic >=2.7.0b1,<3",
"typing-extensions >=4.9.0,<5",
"hugr >=0.10.3,<0.11",
- "tket2-exts>=0.4.0,<0.5",
+ "tket2-exts>=0.5.0,<0.6",
]
[project.optional-dependencies]
@@ -65,7 +65,7 @@ test = [
"pytest-notebook >=0.10.0,<0.11",
"pytest-snapshot >=0.9.0,<1",
"ipykernel >=6.29.5,<7",
- "tket2>=0.6.1",
+ "tket2>=0.7.0",
"pytest-benchmark>=5.1.0",
]
execution = [
@@ -86,7 +86,7 @@ execute-llvm = { workspace = true }
# Uncomment these to test the latest dependency version during development
# hugr = { git = "https://github.com/CQCL/hugr", subdirectory = "hugr-py", rev = "e40b6c7" }
-tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", rev = "633ebd7"}
+# tket2-exts = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-exts", rev = "633ebd7"}
# tket2 = { git = "https://github.com/CQCL/tket2", subdirectory = "tket2-py", rev = "259cf88"} |
Hi @ss2165, I think I recall anticipating this issue in #822 (comment) but we hadn't tested full stack then. |
@qartik The issue is not seeding the rng twice, it is a lack of guarantee that you have a unique handle to rng state, so reproducibility isn't guaranteed because rng calls could be reordered. The failure at runtime should be from calling initialisation twice without discarding first. |
I am unsure which runtime we are referring to. If the platform call doesn't fail, how can we detect this ordering violation? Since |
For random_nat, see: quantinuum-dev/hugrverse#138 Discussion on maybe_rng at #822 (comment) BREAKING CHANGE: removed `random.RNG.random_nat()` and `random.maybe_rng()`
Closes: #769
Depends on:
RNGContext
type, swap returns tket2#786None
of an optional qubit #810Some changes from the issue:
RNG
guppy type, instead of theRNGContext
guppy struct.def __new__(seed: int) -> "RNG":
instead of__init__(seed: int) -> "RNGContext"
maybe_rng
instead ofinit_safe(seed: int) -> Option["RNGContext"]
Other thoughts:
Unsure if this interface is the best we can do:
It might be nice to provide a context manager interface such as:
which would enforce the linear discipline on
RNG
. Mark confirms it's currently not possible within guppy.This comment override removes the
release-as
flag in the original commit:BEGIN_COMMIT_OVERRIDE
feat(qsystem): add Random number generation module
END_COMMIT_OVERRIDE