Random¶
- class Random(seed: int | None = None)[source][source]¶
Bases:
objectPython mirror of
qm.qua.Random.A
Randomand a QUARandomthat start from the same state produce the same values, provided the host callsrand_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 awhile_whose length depends on runtime data, cannot be replayed on the host. Seeding and reseeding are the user’s responsibility on both sides: everyqua_rng.set_seed(...)in the QUA program must be mirrored by aset_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 libraryrandom.randrange(2**28 - 1), as QUA does for an unseededRandom(), and is available asinitial_seedso it can be passed to QUA. Preferdeclare_qua()over constructing an unseeded QUARandom(), whose compile-time seed is invisible to the host.
- 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 resetdraws.- 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_intis not positive.
- rand_fixed() FixedPoint[source][source]¶
Mirror
qua_rng.rand_fixed(): a pseudorandom 4.28 fixed in[0, 1).- Returns:
A
FixedPointwhose raw value is the new 28-bit state. Usefloat(x)to convert it.
- rand_int_sequence(max_int: int, n: int) list[int][source][source]¶
Draw
nintegers in order, asnsuccessiverand_int()calls.
- rand_fixed_sequence(n: int) list[FixedPoint][source][source]¶
Draw
nfixed values in order, asnsuccessiverand_fixed()calls.
- skip(n: int) None[source][source]¶
Advance the generator by
ndraws 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 updatex -> a*x + cwith itself.- Parameters:
n – Number of draws to skip, non-negative.
- Raises:
ValueError – If
nis negative.
- declare_qua() QuaRandom[source][source]¶
Declare a QUA
Randomstarting 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 withqua_rng.set_seed(...)and mirror each reseed withset_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.Randomwhose seed variable is initialized tostate.