pennylane icon indicating copy to clipboard operation
pennylane copied to clipboard

Compute expectation value

Open ludmilaasb opened this issue 1 year ago • 7 comments

Context:

It would be nice to have a function to calculate the expectation values of an operator $A$ given a state vector $\vert\psi\rangle$. This can be also used for computing the fidelity between a mixed and a pure state in simple way, avoiding eigendecomposition problems.

Description of the Change:

Added a overlap calculation between state vectors and density matrices

Benefits:

It is faster then computing $\text{Tr}(A \vert\psi\rangle\langle\psi\vert)$. No need for eigendecomposition, therefore, it is a way to avoid issues regarding differentiation as pointed on issue #4373.

Possible Drawbacks:

It does not work if the user pass as input two state vectors or two matrices. It is tailored to first input as (batched) matrices and the second one as (batched) state vectors.

Related GitHub Issues: #4373

ludmilaasb avatar Aug 16 '23 18:08 ludmilaasb

One open question to me personally is whether we want to restrict to Hermitian matrices or not, and--connected to that--what output data type we want (real or complex). This topic comes up repeatedly in the context of QNode expectation values of Hamiltonians, as far as I know, so it might deserve some discussion.

Could we not have the Hermitian restriction, but also have a qml.math.real_if_close that wraps the return?

trbromley avatar Aug 17 '23 12:08 trbromley

Could we not have the Hermitian restriction, but also have a qml.math.real_if_close that wraps the return?

data types have been causing quite a bit of headaches in the past, so having them be changed automatically scares me personally a little bit. Also, we get value dependent type changes that might not go well with JITting?

dwierichs avatar Aug 17 '23 13:08 dwierichs

Thank you for the careful review! 😄

So, about the Hermitian matrices, it depends on what kind of operators you would like to work with. For instance, if the goal is exclusive "physically measurable quantities", i.e., observables and POVMs, then yes, they need to be Hermitian.

On the other side, if you consider things like annihilation and creation operators, they are non-hermitian. But I don't know if you guys want to go in this direction. 🤔

ludmilaasb avatar Aug 17 '23 18:08 ludmilaasb

On the other side, if you consider things like annihilation and creation operators, they are non-hermitian. But I don't know if you guys want to go in this direction.

Good point. I can't judge what's needed here... We could also have "a physicist's expectation" as default, expecting a Hermitian and returning a real value. Then there could be a keyword argument is_hermitian that can be switched off to allow non-Hermitian obs.

dwierichs avatar Aug 18 '23 07:08 dwierichs

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.67%. Comparing base (1bf7682) to head (3d2210c). Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4484      +/-   ##
==========================================
- Coverage   99.68%   99.67%   -0.01%     
==========================================
  Files         421      421              
  Lines       40437    40161     -276     
==========================================
- Hits        40309    40031     -278     
- Misses        128      130       +2     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Aug 23 '23 22:08 codecov[bot]

[sc-43438]

trbromley avatar Sep 13 '23 18:09 trbromley

Thanks @ludmilaasb! I'll request some reviewers, though note that one failure in the CI is a reminder to add an entry to the changelog.

trbromley avatar Jan 31 '24 15:01 trbromley

Thanks everyone for getting this merged!

trbromley avatar Jun 18 '24 19:06 trbromley