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

Update qml.expval to correctly cast value type #6939

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

andrijapau
Copy link
Contributor

@andrijapau andrijapau commented Feb 7, 2025

Context:

Prior to this PR we had the expectation value being silently converted to a real type,

obs = (0.2 + 0.7j) * qml.PauliZ(0)

@qml.qnode(qml.device("default.qubit", 1))
def qnode():
    return qml.expval(obs)

>>> qnode()
0.2

Description of the Change:

This PR stops casting to a real number when calculating expval and instead casts to whatever the eigenvalue type is.

Benefits:

Code is now correct with the following behaviour,

obs = (0.2 + 0.7j) * qml.PauliZ(0)

@qml.qnode(qml.device("default.qubit", 1))
def qnode():
    return qml.expval(obs)

>>> qnode()
(0.2+0.7j)

Possible Drawbacks: None.

Related GitHub Issues: Fixes #6076

@andrijapau andrijapau changed the title Fix expval return type Fix qml.expval return type Feb 7, 2025
Copy link
Contributor

github-actions bot commented Feb 7, 2025

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@andrijapau andrijapau changed the title Fix qml.expval return type Update qml.expval to correctly cast value type Feb 7, 2025
Copy link

codecov bot commented Feb 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.59%. Comparing base (0ecf816) to head (e3a953c).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6939      +/-   ##
==========================================
- Coverage   99.59%   99.59%   -0.01%     
==========================================
  Files         480      480              
  Lines       45505    45504       -1     
==========================================
- Hits        45320    45319       -1     
  Misses        185      185              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +51 to +60
@pytest.mark.parametrize("coeffs", [1, 0.5, 0.5j, 0.5 + 0.5j])
def test_expected_dtype(self, coeffs):
"""Test that the return type of the expval function is correct"""

@qml.qnode(qml.device("default.qubit"))
def circuit(coeffs):
return qml.expval(coeffs * qml.PauliZ(0))

res = circuit(coeffs)
assert np.allclose(res, coeffs)
Copy link
Contributor

Choose a reason for hiding this comment

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

This test doesn't need to do a device execution. You can manually check that the correct expval is returned using ExpectationMP.process_state and ExpectationMP.process_samples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] imaginary part of expectation values of observables with complex coefficients is silently discarded
2 participants