-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cirq.Simulator
final_state_vector is not normalized
#6402
Comments
import cirq q0 = cirq.LineQubit(0) state_vector_simulator = cirq.Simulator() Normalize the final state vectornormalized_final_state = final_state / cirq.linalg.norm(final_state) Now you can use the normalized_final_state for further analysis |
cirq-cync: proposed solution to normalize the output when we call |
I can take this on. |
@smburdick thanks for volunteering to help with this, assigned to you |
Hi hello. I was looking into some first issues for Cirq and came across this issue. I hope it is okay to give my two cents. q0 = cirq.LineQubit(0)
circuit = cirq.Circuit(
cirq.S(q0),
cirq.H(q0),
cirq.S(q0),
cirq.Y(q0),
cirq.S(q0),
cirq.H(q0),
cirq.S(q0),
cirq.measure(q0),
)
state_vector_simulator_c64 = cirq.Simulator() #default dtype for Simulator() is np.complex64, two 32-bit precision floats
state_vector_simulator_c128 = cirq.Simulator(dtype=np.complex128) #now two 64-bit precision floats
sim64 = state_vector_simulator_c64.simulate(circuit).final_state_vector
sim128 = state_vector_simulator_c128.simulate(circuit).final_state_vector
print(sim64)
print(sim128) [0.+0.j 0.+0.99999994j] #issues with precision
[0.+0.j 0.+1.j] #no issues with precision, normalization is fine Note that the same issue occurs with cirq.final_density_matrix as mentioned in #5916, or just with np.matmul using equivalent matrices if you wanted (not shown). fin_64 = cirq.final_density_matrix(circuit, dtype=np.complex64)
fin_128 = cirq.final_density_matrix(circuit, dtype=np.complex128)
print(fin_64)
print(fin_128) [[0. +0.j 0. +0.j]
[0. +0.j 0.9999999+0.j]] #issues with precision
[[0.+0.j 0.+0.j]
[0.+0.j 1.+0.j]] #no issues with precision It is mentioned that np.complex128 is an accepted dtype for the Simulator class, should this be the default to circumvent this issue? |
As per #6402 (comment) the decision here was to normalize the Regardless of the precision of the type used in simulation there will always be numbers that can't be represented exactly leading to an accumulation of errors. The final normalization step should make the output look nicer and closer to what we expect. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days |
Perhaps the simulator could provide an estimate for the accumulated round-off errors given the simulation dtype - then we could raise some warning if the state vector norm was too far off. As a quick estimate, the example above has 7 non-measure operations, there are 6 flops per matrix-vector multiplication so the epsilon on state vector coefficient would be ~ That said, I currently don't see a quick and easy API to add this in. |
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 quantumlib#6522 and quantumlib#6402
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days |
…mlib#6556) 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 quantumlib#6522 and quantumlib#6402
…mlib#6556) 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 quantumlib#6522 and quantumlib#6402
Description of the issue
The final state vector of
cirq.Simulator.simulate
may acccumulate enough numerical error that it may not have unit norm. similar to #5916How to reproduce the issue
Cirq version
1.2.0
The text was updated successfully, but these errors were encountered: