Skip to content

feat: random_advance - #55

Merged
jake-arkinstall merged 2 commits into
mainfrom
feat/random_advance
Aug 19, 2025
Merged

jake-arkinstall merged 2 commits into
mainfrom
feat/random_advance

Conversation

@jake-arkinstall

Copy link
Copy Markdown
Collaborator
  • Add selene_random_advance(delta: u64) function to selene
    • This advances the random state by the specified number of 32 bit reads
    • As this is cyclic in 2^64, a negative value can be bitcast and accepted.
    • If no prng state is set, an error is raised (as with e.g. selene_random_u32)
  • Add random_advance to the QIS wrapper, calling the above.
  • Add pytest, though it is currently skipped as random_advance support isn't available in guppy yet.

Future work:

I do not like managing static RNG state in selene:

  • It's done this way because that's how QIS uses RNG.
  • It makes sense there. - but the static state is an antipattern in the context of selene itself.
  • Instead, Selene can offer stateful RNG, and we should handle state in the QIS wrapper.

…pped until guppy adds random_advance support)
@qartik

qartik commented Aug 18, 2025

Copy link
Copy Markdown
Member

LGTM, it will be nice to have @jordan-qtm or Alex review the expected behavior.

@jordan-qtm

Copy link
Copy Markdown
  • Add selene_random_advance(delta: u64) function to selene

    • This advances the random state by the specified number of 32 bit reads
    • As this is cyclic in 2^64, a negative value can be bitcast and accepted.
    • If no prng state is set, an error is raised (as with e.g. selene_random_u32)
  • Add random_advance to the QIS wrapper, calling the above.

  • Add pytest, though it is currently skipped as random_advance support isn't available in guppy yet.

Future work:

I do not like managing static RNG state in selene:

  • It's done this way because that's how QIS uses RNG.
  • It makes sense there. - but the static state is an antipattern in the context of selene itself.
  • Instead, Selene can offer stateful RNG, and we should handle state in the QIS wrapper.

Yes, this appears to have the correct behavior I would expect.

@qartik
qartik requested a review from Copilot August 18, 2025 18:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds random state advancement functionality to the selene simulator. It allows users to advance or rewind the PRNG state by a specified number of steps, which is useful for deterministic testing and debugging scenarios.

  • Adds random_advance function to advance PRNG state by a delta value
  • Implements FFI wrapper and QIS interface integration
  • Adds test coverage for the new functionality (currently skipped pending guppy support)

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
selene-sim/rust/selene_instance/rng.rs Implements core random_advance method and improves documentation
selene-sim/rust/ffi_interface.rs Adds FFI wrapper selene_random_advance for C interface
selene-sim/c/include/selene/selene.h Declares the new C API function
selene-ext/interfaces/helios_qis/c/src/interface.c Implements QIS wrapper function
selene-sim/python/tests/test_guppy.py Adds comprehensive test for advance functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


/// Advance the PRNG with a user-provided delta. As this
/// is cyclic, i64s with negative values have well defined
/// rewinding behaviour.

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 'i64s with negative values' but the function parameter is u64. This is confusing since u64 cannot have negative values. Consider clarifying that negative values should be cast to u64 or revising the documentation to match the actual parameter type.

Suggested change
/// rewinding behaviour.
/// is cyclic, advancing by large unsigned values will wrap around
/// the PRNG state as expected.

Copilot uses AI. Check for mistakes.
* Requires the PRNG to be seeded with random_seed,
* otherwise an error will be returned.
*/
struct selene_void_result_t selene_random_advance(struct SeleneInstance *instance, uint64_t delta);

Copilot AI Aug 18, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions 'i64s with negative values' but the function signature uses uint64_t. This creates confusion about how negative values should be handled. The documentation should clarify the casting behavior or use a signed parameter type.

Suggested change
struct selene_void_result_t selene_random_advance(struct SeleneInstance *instance, uint64_t delta);
* is cyclic, int64_t values (including negative values) have well defined
* rewinding behaviour.
*
* Requires the PRNG to be seeded with random_seed,
* otherwise an error will be returned.
*/
struct selene_void_result_t selene_random_advance(struct SeleneInstance *instance, int64_t delta);

Copilot uses AI. Check for mistakes.
@jake-arkinstall
jake-arkinstall merged commit 974b496 into main Aug 19, 2025
9 checks passed
@jake-arkinstall
jake-arkinstall deleted the feat/random_advance branch August 19, 2025 13:34
jake-arkinstall pushed a commit that referenced this pull request Aug 20, 2025
🤖 I have created a release *beep* *boop*
---


##
[0.2.2](selene-sim-v0.2.1...selene-sim-v0.2.2)
(2025-08-20)


### Features

* random_advance ([#55](#55))
([974b496](974b496))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
jake-arkinstall added a commit that referenced this pull request Jun 12, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.0-alpha.1](selene-sim-v0.3.0-alpha.0...selene-sim-v0.3.0-alpha.1)
(2026-06-12)


### ⚠ BREAKING CHANGES

* Use struct exports for all plugin types, separate error model and
simulator ([#169](#169))
* Add handling for an additional gateset
([#119](#119))

### Features

* Add __version__ attributes
([#137](#137))
([379ae01](379ae01))
* Add event hook which records measurement results
([#104](#104))
([01300ee](01300ee)),
closes [#103](#103)
* Add handling for an additional gateset
([#119](#119))
([5180b80](5180b80))
* Add simulate_delay functionality
([#139](#139))
([cca97fa](cca97fa))
* Add support for object files provided as bytes
([#94](#94))
([c4cfac6](c4cfac6))
* add timing to builtin runtimes and batching options to softrz runtime
([#158](#158))
([049e123](049e123))
* Better exception handling for parse_shots=False
([#70](#70))
([3caf530](3caf530))
* Build improvements
([#142](#142))
([12f399b](12f399b))
* Bump selene-core dependency and relax guppylang testing dependency
([#120](#120))
([6375791](6375791))
* Cleanup error'd processes before log collection
([#98](#98))
([77e698e](77e698e))
* **compiler:** Bump tket version; add wasm + gpu to the hugr-qis
registry
([c69155d](c69155d))
* correct shot end strategy and error processing
([#91](#91))
([93eaeb0](93eaeb0))
* Emit a nicer error when trying to emulate unsupported pytket ops
([#72](#72))
([d88a28a](d88a28a))
* Fine-grained timeout configuration
([#69](#69))
([072842e](072842e))
* Interactive use of Selene from python
([#135](#135))
([db3028d](db3028d))
* Make interfaces shared, add ArgReader
([#171](#171))
([143e742](143e742))
* QIR support using QIR-QIS
([#114](#114))
([70ab294](70ab294))
* Quest performance improvements
([#133](#133))
([af640e9](af640e9))
* random_advance ([#55](#55))
([974b496](974b496))
* Record simulated delays in event hooks
([#154](#154))
([608884a](608884a))
* Result stream handling refactor
([#93](#93))
([607a55e](607a55e))
* Stim improvements (more ops + state printing)
([#115](#115))
([867d5e5](867d5e5))
* Support state-dump passthrough on quantum replay simulator
([#108](#108))
([1b01a01](1b01a01))
* test on QIS instead of relying on the upper stack
([#150](#150))
([b80a9c4](b80a9c4))
* Test on QIS snapshots
([#134](#134))
([c4d58ab](c4d58ab))
* Traces for analytics
([#160](#160))
([24b9978](24b9978))
* update to tket-qsystem 0.20
([#66](#66))
([7191b07](7191b07))
* Use mingw instead of msvc for windows wheels
([#143](#143))
([3d91514](3d91514))
* Use SeleneStartupError for errors before shot_start
([#170](#170))
([85c2907](85c2907))
* Use struct exports for all plugin types, separate error model and
simulator ([#169](#169))
([c44a5c9](c44a5c9))


### Bug Fixes

* add `___barrier` to Helios QIS for QIR emulation
([#136](#136))
([dfbc4c3](dfbc4c3))
* Add ENDING to allowed shot state on receiving meta information
([#100](#100))
([fc4d673](fc4d673))
* Add numpy dependency back
([#127](#127))
([c9a01eb](c9a01eb))
* avoid using qir_major_version string for QIR detection
([#123](#123))
([deaa0dc](deaa0dc))
* classify lowered qir-qis bitcode as helios
([#157](#157))
([67fca60](67fca60))
* **compiler:** error when entrypoint has arguments
([#84](#84))
([604b131](604b131))
* **compiler:** update tket-qystem to fix CZ bug
([#78](#78))
([3991f11](3991f11))
* correct post_runtime duration metric
([#74](#74))
([0bef66a](0bef66a))
* make qir-qis optional and stabilize CI
([#164](#164))
([e52c0b2](e52c0b2))


### Documentation

* add repository citation guidance and metadata
([#146](#146))
([880a58b](880a58b)),
closes [#145](#145)
* Correct URL to guppy repository
([#121](#121))
([27ded00](27ded00))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: Jake Arkinstall <65358059+jake-arkinstall@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants