You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector #1454
[Bug] Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector
Issue Description:
Bug Summary
test_prepare_gaussian_state in src/openfermion/circuits/primitives/state_preparation_test.py fails probabilistically under certain random seeds (e.g. --randomly-seed=1921347655).
The failure manifests as a SciPy sparse matrix multiplication dimension mismatch: ValueError: matmul: dimension mismatch with signature (n,k=16),(k=2,1?)->(n,1?)
# Get the state using a circuit simulation
circuit = cirq.Circuit(
prepare_gaussian_state(qubits, quad_ham, occupied_orbitals, initial_state=initial_state)
)
if isinstance(initial_state, list):
initial_state = sum(1 << (n_qubits - 1 - i) for i in initial_state)
state = circuit.final_state_vector(initial_state=initial_state)
# Check that the result is an eigenstate with the correct eigenvalue
numpy.testing.assert_allclose(quad_ham_sparse.dot(state), energy * state, atol=atol)
E ValueError: matmul: dimension mismatch with signature (n,k=16),(k=2,1?)->(n,1?)
state = circuit.final_state_vector(initial_state=initial_state)
In Cirq, calling circuit.final_state_vector() without specifying qubit_order automatically infers the qubit order and active qubit set from circuit.all_qubits() (only the qubits that have operations applied to them).
For specific random Hamiltonians (e.g. under seed 1921347655), the state preparation circuit leaves 3 out of 4 qubits idle, applying operations to only 1 qubit.
Cirq therefore returns a 2-element state vector ($2^1 = 2$) instead of a 16-element state vector ($2^4 = 16$).
Multiplying quad_ham_sparse (dimension $16 \times 16$) by state (dimension $2$) raises ValueError: matmul: dimension mismatch.
I'd be happy to open a quick PR to fix this if no one else is currently working on it
changed the title [-][Bug] Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector[/-][+]Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector[/+]on Oct 9, 2026
Activity
anishbhujbal7 commentedon Sep 16, 2026
Opened PR #1455 to fix this by passing explicit qubit_order to final_state_vector. Verified locally with the failing seed
[-][Bug] Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector[/-][+]Flaky test_prepare_gaussian_state due to missing qubit_order in final_state_vector[/+]mhucka commentedon Oct 9, 2026
Fixed by #1455.
fix: pass explicit qubit_order to final_state_vector in state_prepara…