pennylane
pennylane copied to clipboard
Compute expectation value
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
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?
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?
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. 🤔
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.
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.
[sc-43438]
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.
Thanks everyone for getting this merged!