question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add validation check for options passed to the `qml.qnode` decorator and to `qml.QNode`

See original GitHub issue

Feature 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:

  1. Add a validation check mechanism to creating qnodes with either the qml.qnode decorator or with qml.QNode. The signature of qml.qnode and/or qml.QNode should be changed such that known arguments are defined as separate parameters and there’s an option to pass keyword arguments too.
  2. Passing options to qml.qnode and qml.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 about grad_method being unrecognized.
  3. 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:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
thisaccommented, Sep 14, 2021

Thanks @ianmclean2011. Closed by #1591.

1reaction
ianmclean2011commented, Sep 14, 2021

@antalszava I think my PR resolved this issue, so you might be able to close it now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

qml.qnode — PennyLane 0.27.0 documentation
The interface that will be used for classical backpropagation. This affects the types of objects that can be passed to/returned from the QNode....
Read more >
Specialized QValidator and QML UI changes - Stack Overflow
As the validator is only concerned about validation it does not contain any state about the data it validates. As I must get...
Read more >
pennylane/test_qnode.py at master - GitHub
PennyLane is a cross-platform Python library for differentiable programming of quantum computers. Train a quantum computer the same way as a neural network....
Read more >
pennylane Changelog - pyup.io
`qml.enable_return()` now supports QNodes returning multiple measurements, ... Added an `are_pauli_words_qwc` function which checks if certain Pauli words ...
Read more >
Qt 6 - Episode 29 - Enabling the QML designer - YouTube
In this video series we will cover Qt 6. In this we will show how to enable the designer and walk through some...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found