Skip to content

Make stim sampling parallel by default - #108

Merged
Roger-luo merged 13 commits into
mainfrom
david/stim-rayon
Jun 20, 2026
Merged

Roger-luo merged 13 commits into
mainfrom
david/stim-rayon

Conversation

@david-pl

@david-pl david-pl commented Jun 8, 2026 •

Copy link
Copy Markdown
Collaborator

This PR updates ppvm-stim multi-shot sampling so that (when compiled with the optional rayon feature) it automatically uses parallel execution for large shot batches, and updates the Rust/Python user-facing docs + tests to reflect the new "shot-indexed factory" API and deterministic seeding strategy.

Changes

  • Refactors Stim sampling to support sample_serial, sample_parallel (rayon-gated), and a default sample that chooses serial vs parallel based on batch size and thread count.
  • Updates Rust examples/docs/tests to use a factory closure Fn(usize) -> GeneralizedTableau (shot index provided for deterministic per-shot seeds).
  • Enables rayon-backed sampling in the Python native extension and adds Python/Rust tests for reproducibility and serial/parallel equivalence.
  • Splits sample into two #[cfg]-gated definitions so non-rayon builds do not require Sync on the factory closure.
  • Renames prepare → validate and execute_prepared → execute_validated throughout for naming clarity.
  • Adds sample_serial_validated, sample_parallel_validated, and sample_validated variants that accept pre-validated &[ExtendedInstruction] + measurement_count and skip re-validation. The Python sample() binding uses sample_validated() so validation runs only once at StimProgram.parse() time instead of on every sampling call.
  • Documents that per-shot seeds use wrapping (mod 2⁶⁴) arithmetic.

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 updates ppvm-stim multi-shot sampling so that (when compiled with the optional rayon feature) it automatically uses parallel execution for large shot batches, and updates the Rust/Python user-facing docs + tests to reflect the new “shot-indexed factory” API and deterministic seeding strategy.

Changes:

  • Refactors Stim sampling to support sample_serial, sample_parallel (rayon-gated), and a default sample that chooses serial vs parallel based on batch size and thread count.
  • Updates Rust examples/docs/tests to use a factory closure Fn(usize) -> GeneralizedTableau (shot index provided for deterministic per-shot seeds).
  • Enables rayon-backed sampling in the Python native extension and adds Python/Rust tests for reproducibility and serial/parallel equivalence.

Reviewed changes

Copilot reviewed 10 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
skills/ppvm-usage/SKILL.md Updates user documentation for the new shot-indexed factory and parallel sampling behavior.
skills/ppvm-usage/examples/rust/src/bin/stim_sample.rs Updates example to use the new factory closure signature and explains deterministic seeding.
ppvm-python/test/generalized_tableau/test_stim.py Adds a reproducibility test intended to cover the parallel sampling path.
ppvm-python/src/ppvm/generalized_tableau.py Documents parallel sampling semantics and deterministic seed + i behavior.
crates/ppvm-stim/tests/executor.rs Updates sampling tests for the new factory signature and adds serial-vs-parallel equivalence test (rayon).
crates/ppvm-stim/src/lib.rs Re-exports sample_serial and (feature-gated) sample_parallel; updates doc example.
crates/ppvm-stim/src/executor.rs Implements sample_serial, sample auto-dispatch, and sample_parallel (rayon).
crates/ppvm-stim/Cargo.toml Adds optional rayon feature/dependency.
crates/ppvm-python-native/src/interface_tableau.rs Routes Python sampling through ppvm_stim::sample with per-shot deterministic seeding and GIL release.
crates/ppvm-python-native/Cargo.toml Enables ppvm-stim’s rayon feature for the Python native extension.
Cargo.lock Records rayon as a dependency of ppvm-stim.
.gitignore Ignores /debug.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/ppvm-stim/src/executor.rs Outdated
Comment thread crates/ppvm-stim/src/executor.rs Outdated
Comment thread crates/ppvm-stim/src/executor.rs
@github-actions

github-actions Bot commented Jun 8, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-06-20 05:17 UTC

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 16:29
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

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

Copilot reviewed 10 out of 12 changed files in this pull request and generated 4 comments.

Comment thread crates/ppvm-stim/src/executor.rs
Comment thread crates/ppvm-python-native/src/interface_tableau.rs Outdated
Comment thread ppvm-python/src/ppvm/generalized_tableau.py Outdated
Comment thread crates/ppvm-python-native/src/interface_tableau.rs Outdated
Copilot AI review requested due to automatic review settings June 11, 2026 16:37
@david-pl
david-pl removed the request for review from Copilot June 11, 2026 16:37
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 16:41

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

Copilot reviewed 10 out of 12 changed files in this pull request and generated 3 comments.

Comment thread crates/ppvm-python-native/src/interface_tableau.rs Outdated
Comment thread crates/ppvm-stim/tests/executor.rs
Comment thread skills/ppvm-usage/SKILL.md Outdated
Copilot AI added 2 commits June 11, 2026 16:55
…ute_validated; add validated sample variants

- Rename prepare.rs -> validate.rs and function prepare() -> validate()
- Rename execute_prepared() -> execute_validated() throughout
- Add sample_serial_validated / sample_parallel_validated / sample_validated
  that skip re-validation for callers that already hold a validated program
- Python binding sample() now uses sample_validated() via the pre-validated
  StimProgram, avoiding redundant re-validation on every sampling call
- Update test file prepare.rs -> validate.rs
- Update all doc comments and unreachable! messages
Copilot AI review requested due to automatic review settings June 11, 2026 16:58
@david-pl
david-pl removed the request for review from Copilot June 11, 2026 16:58
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 17:05
Comment thread crates/ppvm-stim/tests/executor.rs
Comment thread ppvm-python/src/ppvm/generalized_tableau.py Outdated

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

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

Comments suppressed due to low confidence (1)

crates/ppvm-stim/src/validate.rs:21

  • validate() currently only checks probability ranges for measurement instructions and MPAD, but it does not validate probabilities for noise instructions (e.g. DEPOLARIZE1(p), X_ERROR(p), etc.) nor for I_ERROR[loss]/[correlated_loss]. This means invalid probabilities like DEPOLARIZE1(2.0) can pass validation and then be executed, despite ExecError::InvalidProbability existing for this purpose.

Comment thread crates/ppvm-python-native/src/interface_tableau.rs
Comment thread ppvm-python/src/ppvm/generalized_tableau.py Outdated
…ngs; update AGENTS.md and dev guide with docstring convention
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 17:28

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

Copilot reviewed 17 out of 19 changed files in this pull request and generated 1 comment.

Comment thread crates/ppvm-python-native/src/interface_tableau.rs
Copilot AI review requested due to automatic review settings June 20, 2026 03:38

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

Copilot reviewed 17 out of 19 changed files in this pull request and generated 1 comment.

Comment thread ppvm-python/test/generalized_tableau/test_stim.py Outdated
Merging main (#101 classical mixtures + sampling) added a
`T::Coeff: PartialOrd` bound to `execute_validated`, but the
`sample_serial_validated`, `sample`, `sample_validated`, and
`sample_parallel*` wrappers only carried `PartialOrd<f64>`, so the crate
stopped compiling. Add the missing `+ PartialOrd` to all seven affected
functions.

Also resolve outstanding review feedback:
- Disambiguate the `[validate]` intra-doc links (now ambiguous between the
  `validate` module and `validate` fn after the prepare->validate rename)
  via the `fn@` disambiguator, silencing the rustdoc warning.
- Rename `test_sample_stim_seeded_is_reproducible_across_the_parallel_path`
  -> `..._for_large_batches` and reword the comment: it asserts
  reproducibility whether rayon runs the batch serially or in parallel
  (the cutoff depends on pool size, which the test does not control).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Roger-luo
Roger-luo merged commit 2de425e into main Jun 20, 2026
9 checks passed
@Roger-luo
Roger-luo deleted the david/stim-rayon branch June 20, 2026 05:17
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