Skip to content

Reference sample#88

Merged
rafaelha merged 13 commits intomainfrom
rafaelha/reference_sample
Apr 1, 2026
Merged

Reference sample#88
rafaelha merged 13 commits intomainfrom
rafaelha/reference_sample

Conversation

@rafaelha
Copy link
Copy Markdown
Collaborator

@rafaelha rafaelha commented Apr 1, 2026

Closes #30

This allows users to draw a noiseless reference sample (to match Stim's behavior).

This is disabled by default, since reference samples can shift the probability distribution in the non-Clifford case.

rafaelha and others added 9 commits April 1, 2026 08:53
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
- Added overloads to `_sample_batches` method to handle reference sample computation.
- Implemented logic to conditionally XOR detector and observable results with noiseless reference samples.
- Introduced warnings for cross-batch reference sample discrepancies.
- Updated unit tests to validate new reference sample behavior and ensure existing functionality remains unchanged.
- Updated README and demo notebook to clarify the behavior of TSIM regarding actual detector and observable samples.
- Modified the sampler implementation to introduce `use_detector_reference_sample` and `use_observable_reference_sample` parameters, allowing users to compute noiseless reference samples.
- Adjusted unit tests to validate the new sampling behavior and ensure correct functionality with the updated parameters.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-04-01 21:33 UTC

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
2118 2054 97% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/tsim/sampler.py 97% 🟢
TOTAL 97% 🟢

updated for commit: 15dce6b by action🐍

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds optional “reference sample” behavior to TSIM’s detector sampler so users can XOR sampled detector/observable outputs against a noiseless baseline (matching Stim’s “detection flips” convention), and introduces a runtime check to detect probability-normalization underflow issues during autoregressive sampling.

Changes:

  • Add use_detector_reference_sample / use_observable_reference_sample options to CompiledDetectorSampler.sample, implemented by sampling an extra noiseless row and XORing selected outputs.
  • Add a marginal-normalization deviation check in component sampling and raise an error when deviation exceeds a threshold.
  • Update docs/examples and add unit/integration tests covering the new behavior and the underflow guard.

Reviewed changes

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

Show a summary per file
File Description
src/tsim/sampler.py Implements reference-sample XOR options and adds normalization-deviation detection during sampling.
test/unit/test_sampler.py Adds unit tests for reference-sample XOR behavior (including bit-packed output and default behavior).
test/integration/test_sampler.py Adds an integration test ensuring normalization deviation triggers an error; fixes a debug plotting argument.
README.md Documents the new reference-sample flags to match Stim behavior.
docs/demos/from_stim_to_tsim.ipynb Updates demo text/code to show default vs reference-sample behavior.
src/tsim/core/exact_scalar.py Minor TODO wording change.
Comments suppressed due to low confidence (1)

src/tsim/sampler.py:141

  • max_norm_deviation is a JAX scalar (jax.Array) returned from sample_component. Using it directly in a Python if and formatting it with :.1e is likely to fail (JAX arrays generally can't be used as booleans/format specifiers without converting to a host scalar). Convert it explicitly (e.g., dev = float(max_norm_deviation) / .item()) before comparing/formatting, and consider treating NaN as an error (NaN comparisons will bypass the threshold check).
    for component in program.components:
        samples, key, max_norm_deviation = sample_component(component, f_params, key)
        if max_norm_deviation > 1e-5:
            raise AssertionError(
                "A marginal probability was not normalized correctly "
                f"(normalization deviated from 1 by {max_norm_deviation:.1e}). "
                "This is likely the result of an underflow error. Please report this "
                "as a bug at https://github.com/QuEraComputing/tsim/issues/new."

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

Comment thread docs/demos/from_stim_to_tsim.ipynb
rafaelha added 2 commits April 1, 2026 14:14
… samples

- Adjusted the calculation of total shots and batch sizes when `compute_reference` is enabled.
- Updated the logic to ensure correct batch sizing based on available leeway.
- Added unit tests to validate the new batch size behavior with and without reference samples.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 4 out of 4 changed files in this pull request and generated 4 comments.


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

Comment thread src/tsim/sampler.py
Comment thread src/tsim/sampler.py
Comment thread test/unit/test_sampler.py
Comment thread docs/demos/from_stim_to_tsim.ipynb Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@rafaelha rafaelha merged commit acaae7f into main Apr 1, 2026
10 checks passed
@rafaelha rafaelha deleted the rafaelha/reference_sample branch April 1, 2026 21:29
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.

Support reference sample in simulators

2 participants