RNG and Shot-Dependent Examples

These examples exercise Guppy features that are specifically aimed at Quantinuum hardware-oriented workflows.

The RNG and shot-dependent functions used here are only available on Quantinuum hardware targets, so these examples are not intended as portable Guppy programs for arbitrary QIR backends. Note that not all RNG methods are currently supported; see Support matrix.

Quantum RNG example

Source file: guppy_examples/quantinuum-hardware-only/rng-quantum-rng-1.py

from typing import no_type_check

from guppylang import guppy, qubit
from guppylang.std.builtins import output
from guppylang.std.qsystem.random import RNG
from guppylang.std.quantum import h, measure


@guppy
@no_type_check
def main() -> None:
    q0 = qubit()
    q1 = qubit()
    h(q1)
    h(q1)
    r = RNG(11)
    if r.random_int() == 5:
        h(q1)
    r.discard()
    output("0", measure(q0).read())
    output("1", measure(q1).read())

Bounded quantum RNG example

Source file: guppy_examples/quantinuum-hardware-only/rng-quantum-rng-2.py

from typing import no_type_check

from guppylang import guppy, qubit
from guppylang.std.builtins import output
from guppylang.std.qsystem.random import RNG
from guppylang.std.quantum import h, measure


@guppy
@no_type_check
def main() -> None:
    q0 = qubit()
    q1 = qubit()
    h(q1)
    h(q1)
    r = RNG(11)
    if r.random_int_bounded(100) == 5:
        h(q1)
    r.discard()
    output("0", measure(q0).read())
    output("1", measure(q1).read())

Shot-dependent example

Source file: guppy_examples/quantinuum-hardware-only/rng-quantum-jobid-1.py

from typing import no_type_check

from guppylang import guppy, qubit
from guppylang.std.builtins import output
from guppylang.std.qsystem.utils import get_current_shot
from guppylang.std.quantum import h, measure


@guppy
@no_type_check
def main() -> None:
    q0 = qubit()
    q1 = qubit()
    h(q1)
    h(q1)
    if get_current_shot() == 5:
        h(q1)
    output("0", measure(q0).read())
    output("1", measure(q1).read())

Native PCG RNG example

Source file: guppy_examples/guppy-features/supported/native-pcg-random.py

from typing import no_type_check

from guppylang import guppy
from guppylang.std.builtins import output
from guppylang.std.num import nat
from guppylang.std.random import seeded_pcg32


@guppy
@no_type_check
def main() -> None:
    rng = seeded_pcg32(nat(7))
    output("pcg_bounded", rng.next_int_bounded(nat(6)))
    output("pcg_next", rng.next_int())

Unsupported: random_advance

Source file: guppy_examples/guppy-features/unsupported/rng-random-advance.py

from typing import no_type_check

from guppylang import guppy, qubit
from guppylang.std.builtins import output
from guppylang.std.qsystem.random import RNG
from guppylang.std.quantum import h, measure


@guppy
@no_type_check
def main() -> None:
    q0 = qubit()
    h(q0)
    r = RNG(11)
    r.random_advance(1)
    r.discard()
    output("0", measure(q0).read())

Expected error:

QIR generation failed. This may be the result of a bug but can also happen when trying to convert a feature in HUGR/Guppylang which is not supported in QIR. Error details: Failed to emit LLVM for function guppy_example_mod.main at node Node(1)

Caused by:
    0: Failed to emit LLVM for node N<tket.qsystem.random.RandomAdvance:Node(9)> with optype tket.qsystem.random.RandomAdvance
    1: Unknown op: RandomAdvance