Skip to content

Commit

Permalink
Avoid state vector normalization if it worsens the round offs
Browse files Browse the repository at this point in the history
This fixes failure of

check/pytest -n0 cirq-core/cirq/circuits/circuit_test.py::test_final_state_vector

which happened because normalization of a state vector at `np.complex64`
precision can subtly increase the overall round-off error.

Follow up to #6522 and #6402
  • Loading branch information
pavoljuhas committed Apr 9, 2024
1 parent 1bec6b5 commit 43cb287
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cirq-core/cirq/sim/state_vector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ def final_state_vector(self) -> np.ndarray:
"skipping renormalization"
)
return ret
return ret / norm
# normalize only if doing so improves the round-off on total probability
ret_norm = ret / norm
round_off_change = abs(np.vdot(ret_norm, ret_norm) - 1) - abs(np.vdot(ret, ret) - 1)
result = ret_norm if round_off_change < 0 else ret
return result

def state_vector(self, copy: bool = False) -> np.ndarray:
"""Return the state vector at the end of the computation.
Expand Down

0 comments on commit 43cb287

Please sign in to comment.