You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by "andrewmusselman (via GitHub)" <gi...@apache.org> on 2026/10/20 17:56:11 UTC

[PR] [Qumat] Fix two Braket backend parity bugs: circuit mutation and NumPy scalars [mahout]

andrewmusselman opened a new pull request, #1543:
URL: https://github.com/apache/mahout/pull/1543

   ### Purpose of PR
   Two common patterns work on Qiskit and Cirq but fail on Amazon Braket. Reading the state vector mutated the caller's circuit, so a later shot-based run raised; and NumPy scalar angles or bound values (as produced by NumPy/PyTorch training loops) were rejected. Both are fixed in `qumat/amazon_braket_backend.py`.
   
   ### Related Issues or PRs
   Closes #1541
   
   Reproducer (found by the backend parity sweep):
   ```python
   q.get_final_state_vector()   # inspect the state...
   q.execute_circuit()          # ...then sample it: Braket raises
   ```
   ("StateVector or Amplitude cannot be specified when shots>0")
   ```python
   q.apply_rx_gate(0, "t")
   q.execute_circuit({"t": np.float32(0.5)})   # Braket raises
   ```
   ("Cannot wrap 0.5 into literal type"; an `np.float32` angle passed directly raises "FreeParameter names must be strings")
   
   ### Changes Made
   - `get_final_state_vector()`: call `state_vector()` on `circuit.copy()` instead of the caller's circuit, as the Qiskit backend already does.
   - New `_as_angle()`: strings become `FreeParameter`, any `numbers.Real` becomes a plain `float`; replaces three copies of the `isinstance(angle, (int, float))` check in `apply_rx/ry/rz_gate`.
   - New `_bound_inputs()`: casts bound values to `float`; shared by `execute_circuit` and `get_final_state_vector`.
   - `apply_u_gate`: same numeric cast, angles annotated `SupportsFloat` (so the repo's `ty` hook passes); U stays non-parameterisable, as on the other backends.
   - `testing/qumat/test_amazon_braket_backend.py`: mocked tests assert the copy is used and the original untouched; end-to-end local-simulator regression; NumPy scalar tests for rotation and U gates and for binding `np.float32(pi)`.
   
   ### Testing
   - `testing/qumat`: 604 passed (main: 589).
   - The new tests fail against the old code (3 for the copy fix; 14 for the NumPy fix).
   - ruff, ruff format and `ty` clean (no new diagnostics vs main).
   - Not verified: AWS SV1 (cloud); the copy and float casts are device-independent.
   
   ### Merging
   Conflicts with the #1507 PR on one import line in `testing/qumat/test_amazon_braket_backend.py`. A rebased "#1507 after A1" version exists.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   https://claude.ai/code/session_01GuXmXRXdNF43h2FoQcNAZb
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@mahout.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org