-
Notifications
You must be signed in to change notification settings - Fork 575
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
Error when creating qnode with backprop and finite-shots #1588
Conversation
Hello. You may have forgotten to update the changelog!
|
[ch8003] |
Codecov Report
@@ Coverage Diff @@
## master #1588 +/- ##
=======================================
Coverage 99.14% 99.14%
=======================================
Files 196 196
Lines 14245 14247 +2
=======================================
+ Hits 14123 14125 +2
Misses 122 122
Continue to review full report at Codecov.
|
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.
Hi @albi3ro, thanks! 💯 Had one major comment for a test case, the rest is looking good (minor suggestions).
tests/tape/test_qnode.py
Outdated
dev = qml.device("default.qubit", wires=1, shots=3) | ||
|
||
with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): | ||
QNode._validate_backprop_method(dev, "doesnt matter") |
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.
Wondering if it's worth inputting valid interface names here. Just by looking at the test case, the error might arise due to the invalid interface. Contrary to that, we'd like to make sure that we get an error with every valid interface. So maybe good to parameterize the test.
Co-authored-by: antalszava <antalszava@gmail.com>
Hi @albi3ro what's the status on this?
Could they simply just evaluate the QNode without trying to differentiate it? If so, maybe we could refer to that in the error message. |
ac05674
to
c6fc3aa
Compare
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.
Great, I've left some comments. 💯 I think we should add a new test, because a deleted test that was previously covering some jax functionalities was deleted.
@@ -342,6 +342,12 @@ def _validate_backprop_method(device, interface): | |||
qml.QuantumFunctionError: if the device does not support backpropagation, or the | |||
interface provided is not compatible with the device | |||
""" | |||
if device.shots is not None: |
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.
Great 👍
|
||
dev = qml.device("default.qubit", wires=1, shots=3) | ||
|
||
with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): |
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.
Nice test 💯
return qml.sample(qml.PauliZ(0)) | ||
|
||
a = tf.Variable(0.54) | ||
with pytest.raises(qml.QuantumFunctionError, match="Devices with finite shots"): |
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.
Should we keep this test? Maybe changing the description to better fit the error message?
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.
Since we test elsewhere that this error is raised, I'm deleting this test.
@rmoyard what do we need to add? |
@albi3ro I think one the test using jax was deleted. |
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.
Great, it seems good to me 💯
Fixes Issue #1481
Right now, you can create a qnode that uses finite shots and backpropagation. This is fine on qnode execution on the forward pass, but will always fail and raise some sort of unhelpful error message of the backward pass/ gradient computation.
For example:
For context, that error is:
With this change, during QNode creation, a
UserWarning
is raise. I decided to use a warning instead of a Error, as users may still want to calculate finite shot forward passes on an interface device. This way, they at least have an explanation for the strange errors they will end up getting.