Skip to content

Improved exception handling#91

Merged
rafaelha merged 4 commits intomainfrom
rafaelha/improved_exception_handling
Apr 6, 2026
Merged

Improved exception handling#91
rafaelha merged 4 commits intomainfrom
rafaelha/improved_exception_handling

Conversation

@rafaelha
Copy link
Copy Markdown
Collaborator

@rafaelha rafaelha commented Apr 4, 2026

Slight normalization issues no longer raise an error but a warning. These can occur for circuits with arbitrary rotation gates, where fp32 is used instead of an exact representation.

Parsing errors for invalid Stim circuits now raise useful exceptions.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
2146 2073 97% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
src/tsim/circuit.py 94% 🟢
src/tsim/core/parse.py 93% 🟢
src/tsim/sampler.py 97% 🟢
src/tsim/utils/program_text.py 95% 🟢
TOTAL 95% 🟢

updated for commit: 59daecc by action🐍

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 4, 2026

PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-04-06 17:40 UTC

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

This PR improves error reporting and robustness around parsing tsim shorthand/Stim program text, and adjusts sampler behavior to warn (instead of error) on small normalization deviations.

Changes:

  • Add scientific-notation support for parametric angles in shorthand parsing and tag parsing, with new unit tests.
  • Enrich Stim parse errors to surface the problematic unconverted shorthand snippet in Circuit construction/append paths.
  • Change sampler normalization handling from raising to warning for small deviations, and update integration expectations; also refactor transversal encoder behavior (gate expansion removal).

Reviewed changes

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

Show a summary per file
File Description
src/tsim/utils/program_text.py Adds scientific-notation parsing and introduces enriched Stim error extraction helpers.
src/tsim/circuit.py Uses enriched parsing errors when constructing/appending circuits; replaces asserts with ValueError for invalid parametric usage.
src/tsim/core/parse.py Extends parametric tag parsing regex to allow scientific notation.
src/tsim/sampler.py Switches normalization failures from exception to warning (and introduces a new underflow ValueError path).
src/tsim/utils/encoder.py Removes logical gate expansion behavior from transversal encoding logic and constructor surface.
test/unit/utils/test_program_text.py Adds tests for scientific notation and for enriched parse-error snippets.
test/integration/test_sampler.py Updates expectations to warning (instead of raising) on normalization deviation.
Comments suppressed due to low confidence (1)

src/tsim/core/parse.py:58

  • parse_parametric_tag now accepts scientific notation via regex, but still does Fraction(param_match.group(2)). fractions.Fraction does not accept strings like "1e-2", so tags produced with scientific notation (e.g. theta=1e-6*pi) will raise instead of parsing. Convert via Decimal (e.g. Fraction(Decimal(value_str))) or via Fraction.from_float(float(value_str)).limit_denominator(...) to actually support the new accepted formats.
        # Match param=value*pi (value can be negative/decimal/scientific)
        param_match = re.match(
            r"^(\w+)=([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)\*pi$", param
        )
        if not param_match:
            return None
        param_name = param_match.group(1)
        value = Fraction(param_match.group(2))
        params[param_name] = value

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

@rafaelha rafaelha merged commit 42c7ff9 into main Apr 6, 2026
10 checks passed
@rafaelha rafaelha deleted the rafaelha/improved_exception_handling branch April 6, 2026 17:33
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.

2 participants