-
Notifications
You must be signed in to change notification settings - Fork 627
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
[Return-types Fix #4] Update Hamiltonian expand transform #3232
Conversation
Hello. You may have forgotten to update the changelog!
|
Codecov Report
@@ Coverage Diff @@
## master #3232 +/- ##
=======================================
Coverage 99.70% 99.70%
=======================================
Files 277 277
Lines 24542 24544 +2
=======================================
+ Hits 24469 24471 +2
Misses 73 73
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
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.
Thanks for the fix @eddddddy, it is looking good! 💯 Do you know if we should worry about multiple measurement and expectations values of different hamiltonian or different measurements? (see the example below). I think it should be separated in two different cases but I am not totally sure.
@qnode(dev, diff_method=diff_method, mode=mode, max_diff=max_diff)
def circuit(data, weights, coeffs):
weights = weights.reshape(1, -1)
qml.templates.AngleEmbedding(data, wires=[0, 1])
qml.templates.BasicEntanglerLayers(weights, wires=[0, 1])
H = qml.Hamiltonian(coeffs, obs)
H.compute_grouping()
H_2= qml.Hamiltonian(coeffs_2, obs_2)
H_2.compute_grouping()
return qml.expval(H), qml.expval(H2)
Hi @rmoyard, it seems that we don't have to worry about multiple Hamiltonians being measured: pennylane/pennylane/_device.py Lines 737 to 743 in cf195af
Running your code, we get the following Exception: Traceback (most recent call last):
File "C:\Users\Edward\Documents\pennylane\pennylane\_device.py", line 738, in batch_transform
circuits, hamiltonian_fn = qml.transforms.hamiltonian_expand(circuit, group=False)
File "C:\Users\Edward\Documents\pennylane\pennylane\transforms\hamiltonian_expand.py", line 128, in hamiltonian_expand
raise ValueError(
ValueError: Passed tape must end in `qml.expval(H)`, where H is of type `qml.Hamiltonian`
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "../scripts/return.py", line 159, in <module>
main()
File "../scripts/return.py", line 154, in main
debug2()
File "../scripts/return.py", line 146, in debug2
circuit()
File "C:\Users\Edward\Documents\pennylane\pennylane\qnode.py", line 622, in __call__
res = qml.execute(
File "C:\Users\Edward\Documents\pennylane\pennylane\interfaces\execution.py", line 575, in execute
return _execute_new(
File "C:\Users\Edward\Documents\pennylane\pennylane\interfaces\execution.py", line 347, in _execute_new
tapes, batch_fn = qml.transforms.map_batch_transform(device.batch_transform, tapes)
File "C:\Users\Edward\Documents\pennylane\pennylane\transforms\batch_transform.py", line 471, in map_batch_transform
new_tapes, fn = transform(t)
File "C:\Users\Edward\Documents\pennylane\pennylane\_device.py", line 741, in batch_transform
raise ValueError(
ValueError: Can only return the expectation of a single Hamiltonian observable |
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.
Thanks @eddddddy 💯
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.
@eddddddy a nice fix! 👏 Just 2 questions.
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.
Looks good! 👍 Thanks for the answers.
Context:
Part of the ongoing return types project
Description of the Change:
Update the
hamiltonian_expand
transform to work with the new return types.