-
Notifications
You must be signed in to change notification settings - Fork 625
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
base: master
Are you sure you want to change the base?
Conversation
Hello. You may have forgotten to update the changelog!
|
qml.expval
return typeqml.expval
to correctly cast value type
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. |
@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) |
There was a problem hiding this comment.
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
.
Context:
Prior to this PR we had the expectation value being silently converted to a real type,
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,
Possible Drawbacks: None.
Related GitHub Issues: Fixes #6076