Skip to content
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

Closed
NoureldinYosri opened this issue Jan 8, 2024 · 9 comments
Closed

cirq.Simulator final_state_vector is not normalized #6402

NoureldinYosri opened this issue Jan 8, 2024 · 9 comments
Assignees
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" kind/bug-report Something doesn't seem to work.

Comments

@NoureldinYosri
Copy link
Collaborator

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 #5916

How to reproduce the issue

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 = cirq.Simulator()
state_vector_simulator.simulate(circuit).final_state_vector
array([0.+0.j        , 0.+0.99999994j], dtype=complex64)

Cirq version
1.2.0

@NoureldinYosri NoureldinYosri added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque kind/bug-report Something doesn't seem to work. labels Jan 8, 2024
@sid-112
Copy link

sid-112 commented Jan 10, 2024

import cirq

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 = cirq.Simulator()
final_state = state_vector_simulator.simulate(circuit).final_state_vector

Normalize the final state vector

normalized_final_state = final_state / cirq.linalg.norm(final_state)

Now you can use the normalized_final_state for further analysis

@NoureldinYosri NoureldinYosri added good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Jan 17, 2024
@NoureldinYosri
Copy link
Collaborator Author

cirq-cync: proposed solution to normalize the output when we call final_state_vector

@smburdick
Copy link
Contributor

I can take this on.

@NoureldinYosri
Copy link
Collaborator Author

@smburdick thanks for volunteering to help with this, assigned to you

@StrangeM4tter
Copy link

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.
I think the origin of this stems from a float precision problem, which I'll demonstrate using the given code block from @NoureldinYosri

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?

@NoureldinYosri
Copy link
Collaborator Author

As per #6402 (comment) the decision here was to normalize the final_state_vector.

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.

Copy link

github-actions bot commented Mar 9, 2024

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

@pavoljuhas
Copy link
Collaborator

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 ~ sqrt(7 * 6 * eps**2) = 8e-7 (for np.finfo(np.complex64).eps = 1.2e-7). The difference of the state vector norm from 1 is 6e-8, well below the round-offs estimate.

That said, I currently don't see a quick and easy API to add this in.

pavoljuhas added a commit to pavoljuhas/Cirq that referenced this issue Apr 9, 2024
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
pavoljuhas added a commit that referenced this issue Apr 10, 2024
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
Copy link

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

jselig-rigetti pushed a commit to jselig-rigetti/Cirq that referenced this issue May 28, 2024
…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
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
…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
@mhucka mhucka added T status/stale This has been closed due to inactivity for an extended period of time. triage/stale and removed triage/stale labels Nov 18, 2024
@mhucka mhucka removed the status/stale This has been closed due to inactivity for an extended period of time. label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue This issue can be resolved by someone who is not familiar with the codebase. A good starting issue. good for learning For beginners in QC, this will help picking up some knowledge. Bit harder than "good first issues" kind/bug-report Something doesn't seem to work.
Projects
None yet
Development

No branches or pull requests

6 participants