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

Defensively copy density matrices in simulator #3109

Merged
merged 6 commits into from
Jun 25, 2020
Merged

Defensively copy density matrices in simulator #3109

merged 6 commits into from
Jun 25, 2020

Conversation

dabacon
Copy link
Collaborator

@dabacon dabacon commented Jun 24, 2020

This makes it so that by default when you access the density matrix during simulation, it makes a copy of the matrix. This can be overridden if one wants to get the speed benefits of not copying.

@googlebot googlebot added the cla: yes Makes googlebot stop complaining. label Jun 24, 2020
@dabacon
Copy link
Collaborator Author

dabacon commented Jun 24, 2020

Fixed #3101 and #3100

Copy link
Collaborator

@95-martin-orion 95-martin-orion left a comment

Choose a reason for hiding this comment

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

I think "copy on initialization" and "copy from intermediate states" are sufficient - "copy from final state" shouldn't be necessary, since the simulator shouldn't reuse the state beyond that point.

cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@cduck cduck left a comment

Choose a reason for hiding this comment

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

Looks good. Just a few nits and suggestions for more robust tests.

cirq/sim/density_matrix_simulator.py Show resolved Hide resolved
cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
cirq/sim/density_matrix_simulator_test.py Show resolved Hide resolved
cirq/sim/density_matrix_simulator_test.py Show resolved Hide resolved
Copy link
Collaborator Author

@dabacon dabacon left a comment

Choose a reason for hiding this comment

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

Thanks @cduck I didn't know about np.shares_memory, this is really useful!

cirq/sim/density_matrix_simulator.py Outdated Show resolved Hide resolved
cirq/sim/density_matrix_simulator_test.py Show resolved Hide resolved
cirq/sim/density_matrix_simulator_test.py Show resolved Hide resolved
@dabacon dabacon requested a review from cduck June 25, 2020 18:14
Copy link
Collaborator

@cduck cduck left a comment

Choose a reason for hiding this comment

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

LGTM after fixing the incorrect assertion.

@@ -1134,6 +1136,8 @@ def test_density_matrix_copy():
traces.append(np.trace(step.density_matrix(copy=False)))
assert any(not np.isclose(np.trace(x), 1.0) for x in matrices)
assert all(np.isclose(x, 1.0) for x in traces)
assert any(not np.shares_memory(x, y) for x, y in
Copy link
Collaborator

Choose a reason for hiding this comment

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

You mean all(not or not any(?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@dabacon dabacon added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 25, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 25, 2020
@CirqBot
Copy link
Collaborator

CirqBot commented Jun 25, 2020

Automerge cancelled: A required status check is not present.

Missing statuses: ['cla/google']

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 25, 2020
@cduck cduck added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 25, 2020
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 25, 2020
@CirqBot
Copy link
Collaborator

CirqBot commented Jun 25, 2020

Automerge cancelled: A required status check is not present.

Missing statuses: ['cla/google']

@CirqBot CirqBot removed automerge Tells CirqBot to sync and merge this PR. (If it's running.) front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. labels Jun 25, 2020
@dabacon dabacon added cla: no and removed cla: yes Makes googlebot stop complaining. labels Jun 25, 2020
@googlebot googlebot added cla: yes Makes googlebot stop complaining. and removed cla: no labels Jun 25, 2020
@dabacon
Copy link
Collaborator Author

dabacon commented Jun 25, 2020

@clabot this should be fine

@dabacon dabacon merged commit 5934b5a into quantumlib:master Jun 25, 2020
tai271828 pushed a commit to tai271828/Cirq that referenced this pull request Jun 26, 2020
tai271828 pushed a commit to tai271828/Cirq that referenced this pull request Jun 27, 2020
tonybruguier pushed a commit to tonybruguier/Cirq that referenced this pull request Aug 23, 2020
CirqBot pushed a commit that referenced this pull request Jan 12, 2021
During the (ongoing) review of #3630 95-martin-orion@ found a bug in my draft code. He said that I should copy state like in #3109. I changed my code and added a test in that PR.

However, I had inspired myself from the Clifford code which could have the same issue (I think), and so I am fixing the code in the present PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants