Random

class Random(seed: int | None = None)[source][source]

Bases: object

Python mirror of qm.qua.Random.

A Random and a QUA Random that start from the same state produce the same values, provided the host calls rand_int() / rand_fixed() the same number of times, in the same order, as the QUA program does.

Replication is only possible when the number and order of draws is fixed ahead of time. A draw placed under a measurement-dependent if_, or inside a while_ whose length depends on runtime data, cannot be replayed on the host. Seeding and reseeding are the user’s responsibility on both sides: every qua_rng.set_seed(...) in the QUA program must be mirrored by a set_seed() call with the same value at the same point of the draw sequence.

Parameters:

seed – Initial seed. When None, a seed is drawn with the standard library random.randrange(2**28 - 1), as QUA does for an unseeded Random(), and is available as initial_seed so it can be passed to QUA. Prefer declare_qua() over constructing an unseeded QUA Random(), whose compile-time seed is invisible to the host.

property initial_seed: int

Seed given at construction (int32-wrapped).

property state: int

Current raw generator state, i.e. the value of QUA’s seed variable.

property draws: int

Number of draws since construction or the last set_seed().

set_seed(seed: int) → None[source][source]

Mirror qua_rng.set_seed(seed): overwrite the state and reset draws.

Parameters:

seed – New seed, wrapped to a signed 32-bit integer like a QUA int.

rand_int(max_int: int) → int[source][source]

Mirror qua_rng.rand_int(max_int): a pseudorandom integer in [0, max_int).

Parameters:

max_int – Exclusive upper bound, must be positive.

Returns:

The drawn integer.

Raises:

ValueError – If max_int is not positive.

rand_fixed() → FixedPoint[source][source]

Mirror qua_rng.rand_fixed(): a pseudorandom 4.28 fixed in [0, 1).

Returns:

A FixedPoint whose raw value is the new 28-bit state. Use float(x) to convert it.

rand_int_sequence(max_int: int, n: int) → list[int][source][source]

Draw n integers in order, as n successive rand_int() calls.

rand_fixed_sequence(n: int) → list[FixedPoint][source][source]

Draw n fixed values in order, as n successive rand_fixed() calls.

skip(n: int) → None[source][source]

Advance the generator by n draws without producing values.

Useful to step over draws the QUA program makes but the host does not need (their count must still be known). Runs in O(log n) by composing the affine update x -> a*x + c with itself.

Parameters:

n – Number of draws to skip, non-negative.

Raises:

ValueError – If n is negative.

copy() → Random[source][source]

Return an independent generator with the same state and draw count.

declare_qua() → QuaRandom[source][source]

Declare a QUA Random starting from this generator’s current state.

Must be called inside with program():. The next draw of the returned QUA generator matches the next draw of this Python instance, whether or not the Python instance has already been used. From there on the QUA instance is owned by the user: reseed it with qua_rng.set_seed(...) and mirror each reseed with set_seed() on the host.

Calling this twice in one program declares two independent QUA states with the same seed; mirror each with its own copy().

Returns:

A qm.qua.Random whose seed variable is initialized to state.