Add validation check for options passed to the `qml.qnode` decorator and to `qml.QNode`
See original GitHub issueFeature details
It would be nice to have a validation check for keyword arguments passed to qml.qnode
and qml.QNode
. Options that are unrecognized should raise a warning.
Consider e.g., the following:
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('qulacs.simulator', wires=2)
par = np.array(0.3, requires_grad=True)
@qml.qnode(dev, grad_method='adjoint')
def circuit(x):
qml.RY(x, wires=[0])
return qml.expval(qml.PauliZ(0))
qml.grad(circuit)(par)
As the keyword for choosing the differentiation method is diff_method
instead of grad_method
, the above snippet would execute well, although the 'qulacs.simulator'
device doesn’t support adjoint differentiation. Instead of the diff_method='adjoint'
, we’d be using diff_method='best'
without any warnings/errors raised, just because the keyword specified was grad_method
. Therefore the user doesn’t get informed that the computation doesn’t happen as intended.
Implementation
Likely, this will involve adding a validation check in qml.QNode
(and perhaps in the decorator). Would consider raising a warning.
Note on the implementation approach: it would make sense to rely on standard Python argument validation. So instead of using if statements in the code, the signature could be split into known arguments and kwargs.
List of tasks for completion:
- Add a validation check mechanism to creating qnodes with either the
qml.qnode
decorator or withqml.QNode
. The signature ofqml.qnode
and/orqml.QNode
should be changed such that known arguments are defined as separate parameters and there’s an option to pass keyword arguments too. - Passing options to
qml.qnode
andqml.QNode
that are unrecognized should raise a warning. Therefore, the example in the original description and in the Additional information section should raise a warning aboutgrad_method
being unrecognized. - Include unit and integration testing for the changes.
How important would you say this feature is?
2: Somewhat important. Needed this quarter.
Additional information
The following example should raise a warning for qml.QNode
, as grad_method
is an unrecognized option:
import pennylane as qml
from pennylane import numpy as np
dev = qml.device('qulacs.simulator', wires=2)
par = np.array(0.3, requires_grad=True)
def circuit(x):
qml.RY(x, wires=[0])
return qml.expval(qml.PauliZ(0))
qnode = qml.QNode(circuit, dev, grad_method='adjoint')
qml.grad(qnode)(par)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Thanks @ianmclean2011. Closed by #1591.
@antalszava I think my PR resolved this issue, so you might be able to close it now.