Skip to content

Ensure the result of simulation is normalized #6522

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

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cirq-core/cirq/sim/state_vector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def __init__(

@cached_property
def final_state_vector(self) -> np.ndarray:
return self._get_merged_sim_state().target_tensor.reshape(-1)
ret = self._get_merged_sim_state().target_tensor.reshape(-1)
return ret / np.linalg.norm(ret)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we raise UserWarning if abs(norm(state_vector) - 1) > np.sqrt(np.finfo(state_vector.dtype).eps)?

That should be sufficient for an accumulated round off from ~1/eps operations, but give some warning in case the state vector would be widely off.


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