Skip to content

Instantly share code, notes, and snippets.

@garrison
Created October 28, 2026 19:04
Show Gist options
  • Select an option

  • Save garrison/802f6797eca223bd271da48afb1d86e9 to your computer and use it in GitHub Desktop.

Select an option

Save garrison/802f6797eca223bd271da48afb1d86e9 to your computer and use it in GitHub Desktop.
Notes on counter-based PRNG substream addressing and seed folding, re: Qiskit/qiskit#17346

Qiskit/qiskit#17346 proposes counter-based PRNG operations for quantum programs, and raises the question of how the choice of protocol should be represented. I've been using a counter-based generator in qiskit-addon-sqd-hpc for the same reasons, and arrived at the typed-key conclusion of JEP 9263 from the other direction. These are a few notes from that experience, in case any of it is useful.

Substream addressing needs stride, not adjacency. The proposal makes the counter the row-major flat index into the payload shape. That is safe as long as each output element consumes exactly one block, but it stops being safe once distributions are built on top. A rejection sampler's draw count per item is unbounded, and the draws consumed per sample by a given distribution are typically unspecified, so with adjacent counters item i's draws n..2n-1 are item i+1's first n draws, bit for bit. I put the item index in the most significant counter word instead, which gives each item a stride of 2^192 for philox4x64 rather than word_count outputs. It may be worth distinguishing "address a draw" from "address a substream" in the op semantics, since only the latter needs the headroom.

Worth noting that in C++, std::philox_engine::set_counter assigns the counter in reverse word order (X_k = c[n-1-k]), so c[0] is the most significant word. Easy to get backwards.

A user-supplied seed has to be folded into the key width, not truncated. qiskit.parameter takes a seed from the caller, presumably of unspecified width. Philox reduces the key modulo 2^word_size, and word_size can be narrower than the type carrying it — std::philox4x32 has a 64-bit result_type but 32-bit words. Mixing the seed is not sufficient on its own, because mixed is not the same as reduced: two seeds can avalanche to values agreeing in their low bits and differing only above, and masking then keys the identical substream. I measured base seeds 149694 and 149778 producing bit-identical std::philox4x32 streams when the high half was merely dropped or masked. XOR-folding the discarded part back down before truncation fixes it.

On protocol selection. The customizability motivation in JEP 9263 is the one I'd weigh most heavily here. JAX's first attempt selected the algorithm by a process-global config flag and they describe it as error-prone once more than one implementation is in use process-wide. Putting the protocol in the op name rather than a flag already avoids that, but it does leave the operand's shape protocol-dependent, which is the breakage the issue identifies. If the protocol travels with the key's type instead, downstream nodes stop depending on a trailing axis they shouldn't have been reading anyway.

The safety motivation seems at least as relevant given that broadcasting machinery is applied directly to key arrays. The transposed-buffer case in the JEP — vmap(uniform, in_axes=1)(keys) mapping over the buffer axis instead of the batch axis — is exactly the mistake a reserved trailing axis invites, and it fails silently with correlated streams rather than loudly.


These notes were drafted by Claude Opus 5 under my guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment