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

Density matrix simulator EVs #3979

Merged
merged 4 commits into from
Jun 15, 2021
Merged

Conversation

95-martin-orion
Copy link
Collaborator

Fixes #3964. CC @zaqqwerty

This is a near-exact copy of the behavior and tests for sparse_simulator. If we see the need in the future, this code could probably be folded into a SimulatesEVsAndFullState interface as the default simulate_expectation_values_sweep behavior, but that doesn't seem particularly urgent at the moment.

Relevant changes from the sparse_simulator code:

  • obs.expectation_from_density_matrix(result.final_density_matrix, qmap) instead of state-vector equivalent
  • added test_simulate_noisy_expectation_values test
  • that's it

@95-martin-orion 95-martin-orion requested review from cduck, vtomole and a team as code owners March 30, 2021 15:20
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Mar 30, 2021
@95-martin-orion
Copy link
Collaborator Author

Assigning to @balopat (the next triage expert) for review, since I can't review my own PRs 😞

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

I would like to reduce the duplication introduced here.

1.) production code: I would experiment with pulling out the repeated lines and have a new "protected" method expectation_value_from_result(obs: PauliSum, result:TSimulationResult) -> float which then can be called from the abstract method

2.) test code: I would move factor out the expectation value calculation logic to a separate test.py file and parameterized it with the different simulator types.

@95-martin-orion
Copy link
Collaborator Author

1.) production code: I would experiment with pulling out the repeated lines and have a new "protected" method expectation_value_from_result(obs: PauliSum, result:TSimulationResult) -> float which then can be called from the abstract method

Packing the duplicated code in simulate_expectation_values_sweep into a Result-targeted method would force us to repeat the construction of pslist for every result. Probably not a huge overhead relative to simulation costs, but if we can avoid it I think we should.

I stand by my top-of-PR comment: the correct way to de-dupe this would be something closer to #3990. (Probably the cleanest solution would involve converting *_sweep methods to return Iterable, but that's a separate discussion.)

@balopat
Copy link
Contributor

balopat commented Apr 28, 2021

I stand by my top-of-PR comment: the correct way to de-dupe this would be something closer to #3990.

Ah, cool, I missed that, and oh, nice, yes that is exactly the direction I was thinking - having an expectation_value_from_state (I don't like expectation_from_state) implemented - though you're also right that maybe not all Simulators should be mandated to implement EV calculation, and isolating that responsibility to a separate interface like SimulatesEVandFullState or similar could be useful to make EV calculation implementation opt-in.

Also, note that #3990 adds yet another test with exactly the same cases at a first blink...maybe we should think about extracting a cirq.testing.assert_consistently_calculates_expectation_value(simulator) function that could be reused in tests for all the different implementations!

I'm okay to get this merged but I would love to have an issue created for deduping both impl and tests for this and referenced in a comment in all these places.

(Probably the cleanest solution would involve converting *_sweep methods to return Iterable, but that's a separate discussion.)

This sounds interesting but I just don't see why yet, can you elaborate on this idea?

Copy link
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

LGTM - given we add an issue and a comment for deduping tests & design

@balopat
Copy link
Contributor

balopat commented Jun 8, 2021

ping @95-martin-orion we could merge this, just add in a comment so we don't forget to dedupe

@95-martin-orion
Copy link
Collaborator Author

Opened #4209 to track deduplication.

@95-martin-orion 95-martin-orion added the automerge Tells CirqBot to sync and merge this PR. (If it's running.) label Jun 15, 2021
@CirqBot CirqBot added the front_of_queue_automerge CirqBot uses this label to indicate (and remember) what's being merged next. label Jun 15, 2021
@CirqBot CirqBot merged commit 72993b5 into quantumlib:master Jun 15, 2021
@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 15, 2021
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
Fixes quantumlib#3964. CC @zaqqwerty

This is a near-exact copy of the behavior and tests for `sparse_simulator`. If we see the need in the future, this code could probably be folded into a `SimulatesEVsAndFullState` interface as the default `simulate_expectation_values_sweep` behavior, but that doesn't seem particularly urgent at the moment.

Relevant changes from the `sparse_simulator` code:
- `obs.expectation_from_density_matrix(result.final_density_matrix, qmap)` instead of state-vector equivalent
- added `test_simulate_noisy_expectation_values` test
- that's it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/expectation-value cla: yes Makes googlebot stop complaining.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Density matrix simulator support for SimulatesExpectationValues
4 participants