Skip to content

fix: expand user-defined QASM gates inline to prevent simulator panic#33

Merged
splch merged 1 commit intomainfrom
fix/expand-custom-gates
Mar 19, 2026
Merged

fix: expand user-defined QASM gates inline to prevent simulator panic#33
splch merged 1 commit intomainfrom
fix/expand-custom-gates

Conversation

@splch
Copy link
Copy Markdown
Owner

@splch splch commented Mar 19, 2026

Custom gate definitions (e.g. gate oracle q0, q1 { ... }) were stored but never expanded. When invoked, they became opaqueGate objects with nil Matrix(), causing the statevector simulator to panic on nil slice indexing. This crashed the WASM instance, producing "undefined is not valid JSON" on first run and "Go program has already exited" on retry.

Parser: add expandGateCall() that re-parses gate body tokens with qubit and parameter substitution, inlining the operations. Empty gate bodies correctly become no-ops (identity).

Simulator: add nil-matrix safety checks in Apply, applyOp, and all dispatch fallback paths to prevent panics from any remaining opaque gates (e.g. user-defined gates called with modifiers).

Custom gate definitions (e.g. `gate oracle q0, q1 { ... }`) were stored
but never expanded. When invoked, they became opaqueGate objects with
nil Matrix(), causing the statevector simulator to panic on nil slice
indexing. This crashed the WASM instance, producing "undefined is not
valid JSON" on first run and "Go program has already exited" on retry.

Parser: add expandGateCall() that re-parses gate body tokens with qubit
and parameter substitution, inlining the operations. Empty gate bodies
correctly become no-ops (identity).

Simulator: add nil-matrix safety checks in Apply, applyOp, and all
dispatch fallback paths to prevent panics from any remaining opaque
gates (e.g. user-defined gates called with modifiers).
@splch splch merged commit 3b5b83f into main Mar 19, 2026
6 checks passed
@splch splch deleted the fix/expand-custom-gates branch March 19, 2026 20:58
@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 48.14815% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.34%. Comparing base (3f6415b) to head (a65e77a).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
qasm/parser/parser.go 57.89% 10 Missing and 6 partials ⚠️
sim/statevector/kernel_controlled.go 0.00% 3 Missing and 1 partial ⚠️
sim/statevector/dynamic.go 50.00% 1 Missing and 1 partial ⚠️
sim/statevector/kernel2q.go 0.00% 1 Missing and 1 partial ⚠️
sim/statevector/kernel3q.go 0.00% 1 Missing and 1 partial ⚠️
sim/statevector/sim.go 50.00% 1 Missing and 1 partial ⚠️

❌ Your patch status has failed because the patch coverage (48.14%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #33      +/-   ##
==========================================
- Coverage   71.43%   71.34%   -0.10%     
==========================================
  Files         165      165              
  Lines       17491    17543      +52     
==========================================
+ Hits        12495    12516      +21     
- Misses       4040     4062      +22     
- Partials      956      965       +9     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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