|
| 1 | +from typing import no_type_check |
| 2 | + |
| 3 | +from guppylang.decorator import guppy |
| 4 | +from guppylang.module import GuppyModule |
| 5 | +from guppylang.std._internal.compiler.qsystem import ( |
| 6 | + RandomIntBoundedCompiler, |
| 7 | + RandomIntCompiler, |
| 8 | +) |
| 9 | +from guppylang.std._internal.compiler.quantum import ( |
| 10 | + QSYSTEM_RANDOM_EXTENSION, |
| 11 | + RNGCONTEXT_T, |
| 12 | +) |
| 13 | +from guppylang.std._internal.util import external_op |
| 14 | +from guppylang.std.builtins import nat, owned |
| 15 | +from guppylang.std.option import Option |
| 16 | + |
| 17 | +qsystem_random = GuppyModule("qsystem.random") |
| 18 | + |
| 19 | + |
| 20 | +@guppy.hugr_op( |
| 21 | + external_op("NewRNGContext", [], ext=QSYSTEM_RANDOM_EXTENSION), |
| 22 | + module=qsystem_random, |
| 23 | +) |
| 24 | +@no_type_check |
| 25 | +def _new_rng_context(seed: int) -> Option["RNG"]: ... |
| 26 | + |
| 27 | + |
| 28 | +@guppy(qsystem_random) |
| 29 | +def maybe_rng(seed: int) -> Option["RNG"]: # type: ignore[type-arg] # "Option" expects no type arguments, but 1 given |
| 30 | + """Safely create a new random number generator using a seed. |
| 31 | +
|
| 32 | + Returns `nothing` if RNG is already initialized.""" |
| 33 | + return _new_rng_context(seed) # type: ignore[no-any-return] # Returning Any from function declared to return "Option" |
| 34 | + |
| 35 | + |
| 36 | +@guppy.type(RNGCONTEXT_T, copyable=False, droppable=False, module=qsystem_random) |
| 37 | +class RNG: |
| 38 | + """Random number generator.""" |
| 39 | + |
| 40 | + @guppy(qsystem_random) # type: ignore[misc] # Unsupported decorated constructor type; Self argument missing for a non-static method (or an invalid type for self) |
| 41 | + def __new__(seed: int) -> "RNG": |
| 42 | + """Create a new random number generator using a seed.""" |
| 43 | + return _new_rng_context(seed).unwrap() # type: ignore[no-any-return] # Returning Any from function declared to return "RNGContext" |
| 44 | + |
| 45 | + @guppy.hugr_op( |
| 46 | + external_op("DeleteRNGContext", [], ext=QSYSTEM_RANDOM_EXTENSION), |
| 47 | + module=qsystem_random, |
| 48 | + ) |
| 49 | + @no_type_check |
| 50 | + def discard(self: "RNG" @ owned) -> None: ... |
| 51 | + |
| 52 | + @guppy.custom(RandomIntCompiler(), module=qsystem_random) |
| 53 | + @no_type_check |
| 54 | + def random_int(self: "RNG") -> int: ... |
| 55 | + |
| 56 | + @guppy(qsystem_random) |
| 57 | + def random_nat(self: "RNG") -> nat: |
| 58 | + """Generate a random 32-bit natural number.""" |
| 59 | + return nat(self.random_int()) # type: ignore[call-arg] # Too many arguments for "nat" |
| 60 | + |
| 61 | + @guppy.hugr_op( |
| 62 | + external_op("RandomFloat", [], ext=QSYSTEM_RANDOM_EXTENSION), |
| 63 | + module=qsystem_random, |
| 64 | + ) |
| 65 | + @no_type_check |
| 66 | + def random_float(self: "RNG") -> float: ... |
| 67 | + |
| 68 | + @guppy.custom(RandomIntBoundedCompiler(), module=qsystem_random) |
| 69 | + @no_type_check |
| 70 | + def random_int_bounded(self: "RNG", bound: int) -> int: ... |
0 commit comments