Skip to content

Fix collapse_state TypeError under numpy >= 2.4 - #1871

Merged
scarrazza merged 1 commit into
qiboteam:masterfrom
martejedor:fix/collapse-state-numpy2
Aug 5, 2026
Merged

Fix collapse_state TypeError under numpy >= 2.4#1871
scarrazza merged 1 commit into
qiboteam:masterfrom
martejedor:fix/collapse-state-numpy2

Conversation

@martejedor

Copy link
Copy Markdown
Contributor

Bug

gates.M(..., collapse=True) (mid-circuit measurement/collapse) crashes under numpy >= 2.4 with:

TypeError: only 0-dimensional arrays can be converted to Python scalars

Cause

MeasurementResult.add_shot returns backend.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_statevector and Backend._collapse_density_matrix then do int(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 hard TypeError, so any circuit using mid-circuit collapse measurement now fails outright on current numpy.

Reproduce on master:

from qibo import Circuit, gates

qc = Circuit(2)
qc.add(gates.H(0))
qc.add(gates.CNOT(0, 1))
qc.add(gates.M(0, collapse=True))
qc.add(gates.M(0, 1))
qc(nshots=100)  # TypeError: only 0-dimensional arrays can be converted to Python scalars

Fix

Extract the scalar explicitly with shot[0] instead of relying on the deprecated implicit conversion, in both _collapse_statevector and _collapse_density_matrix.

Tests

  • tests/test_measurements_collapse.py::test_measurement_collapse (existing, statevector path) already covers _collapse_statevector and was failing under numpy 2.4+ before this fix.
  • Added test_measurement_collapse_density_matrix_state for _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 in test_models_circuit_noise.py::test_probabilities_repeated_execution (a separate numpy-2.4 issue in quantum_info/_quantum_info.py's _fill_tril, present identically with or without this change — happy to file that separately).

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.
@martejedor
martejedor requested review from a team August 4, 2026 10:10
@scarrazza scarrazza added the run-workflow Forces github workflow execution label Aug 5, 2026
@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.50%. Comparing base (5c537a0) to head (07370f8).

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           
Flag Coverage Δ
unittests 99.50% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@scarrazza
scarrazza merged commit 377bb55 into qiboteam:master Aug 5, 2026
17 checks passed
@martejedor
martejedor deleted the fix/collapse-state-numpy2 branch August 6, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-workflow Forces github workflow execution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants