Skip to content

Allow expectation values for density matrix, MPS, Clifford #3990

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 5 commits into from

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Apr 3, 2021

Closes #3964.

More generically, this solves it for any SimulatesFinalState implementation whose TSimulationTrialResult type implements expectation_from_state.

@daxfohl daxfohl requested review from cduck, vtomole and a team as code owners April 3, 2021 22:35
@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Apr 3, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 4, 2021

@tonybruguier is there a quick way to add this to MPS as well? I see there is a state_vector field available on the trial result class. Could we add the same two lines there as I did here in state_vector_simulator and expect that to produce valid results?

@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 4, 2021

@95-martin-orion is it possible to do the same for Clifford simulators?

@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 4, 2021

@95-martin-orion oops, just saw you already had a fix. Up to you whether you'd rather use this one that generifies things or stick with the yagni approach.

@tonybruguier
Copy link
Contributor

@tonybruguier is there a quick way to add this to MPS as well? I see there is a state_vector field available on the trial result class. Could we add the same two lines there as I did here in state_vector_simulator and expect that to produce valid results?

I am not sure what is required. Do you want the simulator to take a density matrix as initial state and return a density matrix at the output of the circuit? That could be involved to write.

Or do you want the simulator to just output a density matrix?

@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 4, 2021

I am not sure what is required. Do you want the simulator to take a density matrix as initial state and return a density matrix at the output of the circuit? That could be involved to write.

I think I figured it out. Just needed to add a way to get an expectation value from MPSTrialResult, which turned out to be a one-liner. Took the tests from density matrix simulator tests (stolen from @95-martin-orion's PR), and changed them to use MPS, and they seem to pass.

@daxfohl daxfohl changed the title Allow expectation values for density matrix Allow expectation values for density matrix and MPS Apr 4, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 4, 2021

Looks like Clifford also provides a function to generate a state vector from a trial result, so we can add an expectation value there too. Will add that before merging.

@daxfohl daxfohl changed the title Allow expectation values for density matrix and MPS Allow expectation values for density matrix, MPS, Clifford Apr 5, 2021
@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 5, 2021

Clifford seems to be working now too.

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.

Regardless of how this pans out, I think we need to document the Simulator hierarchy somewhere (maybe at the top of simulator.py?) once it's done. Multiple nested base classes make it tricky to track which one supports what behavior.

@@ -225,6 +225,9 @@ def __str__(self) -> str:
final = self._final_simulator_state
return f'measurements: {samples}\noutput state: {final}'

def expectation_from_state(self, obs: 'cirq.PauliSum'):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suspect there is a more efficient way to extract expectiation values from an MPS state than converting it into its full state vector form.

@@ -169,6 +169,9 @@ def __str__(self) -> str:
final = self._final_simulator_state
return f'measurements: {samples}\noutput state: {final}'

def expectation_from_state(self, obs: 'cirq.PauliSum'):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to the MPS simulator, there should be a way of calculating expectation values from the Clifford tableau without expanding it to the full state vector.

@@ -372,6 +374,33 @@ def simulate_sweep(
"""
raise NotImplementedError()

def simulate_expectation_values_sweep(
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should include a note that this is only a default implementation; depending on the simulator's behavior, there may be a more efficient simulation method that bypasses simulating the full state.

result = self.simulate(
program, param_resolver, qubit_order=qubit_order, initial_state=initial_state
)
swept_evs.append([result.expectation_from_state(obs) for obs in pslist])
Copy link
Collaborator

Choose a reason for hiding this comment

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

My concern here: the fact that expectation_from_state has no default implementation means that SimulatesFullState does not fully implement SimulatesExpectationValues, which means user-created subclasses will have a broken API.

We could update the class docstring to say that users must implement expectation_from_state in their result class, but that's kind of unwieldy for a class whose base purpose does not include calculating expectation values. This is part of why I mentioned needing a SimulatesEVsAndFullState type for this in #3979.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Apr 5, 2021

@95-martin-orion I agree. I think go ahead and merge your solution for density matrix. I'll look at how to improve this as time comes available.

@95-martin-orion
Copy link
Collaborator

Closing as superseded by #3979. There is still interest in deduplicating this behavior (#4209), but at this point it's likely easier to do so in a fresh PR. (Feel free to reopen if that's not the case.)

# for free to join this conversation on GitHub. Already have an account? # 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