Fix collapse_state TypeError under numpy >= 2.4 - #1871
Merged
Conversation
sample_shots(probs, 1) returns a 1-element array, not a Python scalar. numpy < 2.4 implicitly converted it via int() (with a DeprecationWarning since 1.25); numpy >= 2.4 made that a hard TypeError, so any mid-circuit collapse measurement (gates.M(..., collapse=True)) crashed: TypeError: only 0-dimensional arrays can be converted to Python scalars Extract the scalar explicitly with shot[0] in both Backend._collapse_statevector and Backend._collapse_density_matrix. Adds test_measurement_collapse_density_matrix_state, since the existing density-matrix collapse test (test_measurement_collapse_density_matrix) is skipped for an unrelated reason and the statevector test doesn't exercise _collapse_density_matrix.
scarrazza
approved these changes
Aug 5, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1871 +/- ##
=======================================
Coverage 99.50% 99.50%
=======================================
Files 78 78
Lines 14109 14110 +1
=======================================
+ Hits 14039 14040 +1
Misses 70 70
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
gates.M(..., collapse=True)(mid-circuit measurement/collapse) crashes under numpy >= 2.4 with:Cause
MeasurementResult.add_shotreturnsbackend.sample_shots(probs, 1), which is a 1-element array (np.random.choice(..., size=1, ...)), never a 0-d array or Python scalar.Backend._collapse_statevectorandBackend._collapse_density_matrixthen doint(shot)on it to use as an index.numpy < 2.4 allowed
int()on a size-1 non-0-d array (deprecated since numpy 1.25,DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated...). numpy 2.4.0 made this a hardTypeError, so any circuit using mid-circuit collapse measurement now fails outright on current numpy.Reproduce on master:
Fix
Extract the scalar explicitly with
shot[0]instead of relying on the deprecated implicit conversion, in both_collapse_statevectorand_collapse_density_matrix.Tests
tests/test_measurements_collapse.py::test_measurement_collapse(existing, statevector path) already covers_collapse_statevectorand was failing under numpy 2.4+ before this fix.test_measurement_collapse_density_matrix_statefor_collapse_density_matrix, since the existing density-matrix test (test_measurement_collapse_density_matrix) is already skipped for an unrelated reason ("Problems with collapsing measurement and repeated execution") and nothing else in the suite exercises that path with real assertions.Ran locally under numpy 2.4.6 (numpy's first release where the old behavior became a hard error): both new/existing collapse tests pass; full
tests/test_measurements*.py,tests/test_models_circuit*.py,tests/test_result.py,tests/test_backends_hamming_weight.py(numpy backend) pass except two pre-existing, unrelated failures intest_models_circuit_noise.py::test_probabilities_repeated_execution(a separate numpy-2.4 issue inquantum_info/_quantum_info.py's_fill_tril, present identically with or without this change — happy to file that separately).