Sync README.md from v8.1.35
Browse files623 unmodified lines624625626627628629623 unmodified lines## ▍ Changelog### v8.1.33- **Fixed**: `MPSSimulator._apply_nonlocal_2q` silently swapped which qubit acted as gate-argument-1 vs gate-argument-2 whenever a non-adjacent 2-qubit gate (`abs(q1-q2) != 1`) was called with `q1 > q2` -- e.g. `apply_cx(ctrl=3, tgt=1)` -- because the SWAP chain always ends up applying the gate at `(min(q1,q2), min(q1,q2)+1)` regardless of the caller's original argument order. For an asymmetric gate like CNOT this silently applied the wrong gate (control/target inverted) with no error or warning. Found via an independent code-review report, reproduced directly before trusting it: building a 4-qubit GHZ state via `H(0), CX(0,3), CX(3,1), CX(1,2)` gave fidelity 0.25 against the exact GHZ state (`(|0000⟩+|1001⟩)/√2` instead of `(|0000⟩+|1111⟩)/√2`). Fixed with the same q1>q2 normalization `apply_gate_2q`'s adjacent-qubit branch already used (transpose the gate's tensor axes, swap q1/q2) applied before the SWAP chain runs -- same fidelity check now reads 0.9999999657714559. Affects `apply_cx`/`apply_cz`/`apply_swap`/`apply_ccx` for any non-adjacent qubit pair called in decreasing order, reachable from `dashboard_core`'s `engine='mps'` option on any QASM circuit with such a gate. 6 new regression tests in `test_mps.py` (direct repro, cross-check against `DenseSVSimulator`, non-adjacent unordered-control Toffoli, both float32/float64).- **Checked, not changed**: the same external report also flagged `MPSSimulator.get_top_k_probable_states`'s amplitude summation as a potential bug -- independently re-verified against the exact contraction (matches to machine precision) and against the module's own documented history of already fixing exactly this class of bug. No live issue found. Its performance observation (the `while` loop in `_svd_truncate` isn't `jax.jit`-compiled) is real but is a speed question, not correctness, and is deliberately out of scope here.623 unmodified lines624625626627628629630631632633634635636637638639640623 unmodified lines## ▍ Changelog### v8.1.35- **Added**: `MPSSimulator.run_circuit_jit(ops)` -- a `jax.lax.scan`-fused, `@jax.jit`-compiled whole-circuit execution path for `MPSSimulator`, which previously had zero `@jax.jit` anywhere in the file: every gate ran as an eager JAX call, and the adaptive bond-dimension search inside `_svd_truncate` was a Python `while` loop forcing a host-device sync on every 2-qubit gate. Measured directly on a 60-qubit/428-gate stress circuit (the same one used to originally diagnose this): **88.9s (eager) -> 0.74s (fused, steady-state post-compile)**, ~120x faster, now within range of Qiskit Aer's MPS backend (0.64s) on the same circuit -- same physics exactly (`chi_used=16`, `budget_violations=0`, `avg_JSD=0.0000`, matching the eager run's own summary).- The two root causes, quantified before fixing: (1) zero JIT fusion -- confirmed via direct grep, not assumed; (2) non-adjacent 2-qubit gates expand into a SWAP chain at runtime, multiplying the real SVD-bearing gate count 2.4x on the stress circuit (187 logical 2-qubit gates -> 451 real `apply_gate_2q` calls, counted directly).- New technique (no precedent for this specific problem in `chunk.py`'s own earlier JIT fusion, issue #5, whose geometry never changes gate-to-gate): the bond-dimension search is now a single vectorized pass (JSD computed for every candidate truncation size at once via `jax.vmap`, replacing the incrementing `while` loop) over gamma/lambda tensors padded to a *fixed* `max_bond` size always, with the real bond dimension tracked as a traced scalar and everything beyond it zero-masked rather than dynamically sliced (`jax.lax.scan`'s carry requires constant shape/dtype every step). Non-adjacent gates are expanded into their SWAP-chain-equivalent adjacent-only op sequence at Python pre-compile time instead of via runtime dispatch, mirroring `chunk.py::_compile_multi_chunk_ops`'s "all gate-identity branching happens before tracing starts" principle.- `apply_gate_1q`/`apply_gate_2q`/`_apply_nonlocal_2q` (the eager path) are unchanged and still available -- `run_circuit_jit` is a new, additional entry point, not a replacement. Explicit, intentional trade-off: the fused path pads every gamma/lambda to `max_bond` for the rest of the instance's lifetime after the call, trading this module's adaptive-memory benefit (its whole point for very large, low-entanglement circuits) for speed -- use the eager methods directly when memory, not speed, matters more.- Verified in 5 separately-checked stages, each against real data before moving to the next: (1) the vectorized bond-dimension search matches the eager `while` loop's `(chi_new, jsd_val)` exactly across 171 real cases from real circuits, including the budget-violation fallback; (2) the SWAP-chain pre-expansion matches the real runtime dispatch's exact call sequence across all 330 `(q1, q2)` pairs for 3 qubit counts, plus an end-to-end final-state replay check; (3) the fused kernel matches `DenseSVSimulator` on real entangling circuits (TVD < 1e-6) and matches the eager path bit-for-bit (fidelity 1.0 to machine precision) even under a genuine `budget_violations` truncation scenario, both dtypes; (4) `entanglement_entropy`/`_bond_history`/`jsd_per_bond`/`truncation_errors`/`budget_violations` end up populated identically to the eager path (machine-precision match), sourced from `jax.lax.scan`'s stacked per-step diagnostics instead of Python list appends; (5) the real speed number above.- `contract_to_statevector` generalized (`.squeeze(axis=0)`/`.squeeze(axis=-1)` -> `[0]`-indexing) to work correctly on both the original unpadded boundary tensors (identical behavior, verified against the full existing test suite) and the new padded ones.### v8.1.34- **Fixed**: `QASMParser._eval_param` silently returned `0.0` for *any* gate-parameter expression it couldn't evaluate -- a malformed expression like `rx(pi * / 2) q[0];` parsed successfully and silently produced `rx(0.0)`, a different, valid circuit, with no signal a typo had happened. Found via an independent code-review report, reproduced directly (`ast.parse('pi * / 2', mode='eval')` itself correctly raises `SyntaxError` -- the bug was a blanket `except Exception: return 0.0` around it, added when the previous raw-`eval()` code-execution vulnerability was fixed, that swallowed genuine syntax errors along with rejected/malicious expressions). Now raises `ValueError` instead, for both cases -- malformed expressions and disallowed/malicious ones alike (still structurally blocked by the same AST node-type whitelist either way, just explicit about it instead of silent). Same class of silent-wrong-behavior issue already fixed for unknown gate names and mismatched parameter batches (v8.1.27, issues #4/#6). 2 existing security tests (`test_eval_param_blocks_sandbox_escapes`, the full-parse exploit test) updated to expect the raised exception instead of a silent `0.0`; 2 new regression tests added for the originally-reported malformed-expression case.### v8.1.33- **Fixed**: `MPSSimulator._apply_nonlocal_2q` silently swapped which qubit acted as gate-argument-1 vs gate-argument-2 whenever a non-adjacent 2-qubit gate (`abs(q1-q2) != 1`) was called with `q1 > q2` -- e.g. `apply_cx(ctrl=3, tgt=1)` -- because the SWAP chain always ends up applying the gate at `(min(q1,q2), min(q1,q2)+1)` regardless of the caller's original argument order. For an asymmetric gate like CNOT this silently applied the wrong gate (control/target inverted) with no error or warning. Found via an independent code-review report, reproduced directly before trusting it: building a 4-qubit GHZ state via `H(0), CX(0,3), CX(3,1), CX(1,2)` gave fidelity 0.25 against the exact GHZ state (`(|0000⟩+|1001⟩)/√2` instead of `(|0000⟩+|1111⟩)/√2`). Fixed with the same q1>q2 normalization `apply_gate_2q`'s adjacent-qubit branch already used (transpose the gate's tensor axes, swap q1/q2) applied before the SWAP chain runs -- same fidelity check now reads 0.9999999657714559. Affects `apply_cx`/`apply_cz`/`apply_swap`/`apply_ccx` for any non-adjacent qubit pair called in decreasing order, reachable from `dashboard_core`'s `engine='mps'` option on any QASM circuit with such a gate. 6 new regression tests in `test_mps.py` (direct repro, cross-check against `DenseSVSimulator`, non-adjacent unordered-control Toffoli, both float32/float64).- **Checked, not changed**: the same external report also flagged `MPSSimulator.get_top_k_probable_states`'s amplitude summation as a potential bug -- independently re-verified against the exact contraction (matches to machine precision) and against the module's own documented history of already fixing exactly this class of bug. No live issue found. Its performance observation (the `while` loop in `_svd_truncate` isn't `jax.jit`-compiled) is real but is a speed question, not correctness, and is deliberately out of scope here.
README.md+11