Closed antalszava closed 3 years ago
Thanks for catching this @antalszava @mariaschuld.
I think this is a result of the old + new QNode variable marking system clashing.
Keyword arguments being non-differentiable is only assumed in the old core; PennyLane NumPy tensors are passed as-is to the device if passed as a keyword argument.
In tape mode, however, positional vs. keyword arg makes no difference. Any argument can be differentiable, if marked as so.
As a result, when tape-mode is default this issue should disappear. However, in the meantime, I suggest we add
if isinstance(x, qml.numpy.tensor) and x.ndim == 0:
x = x.unwrap()
either to the (old) autograd interface, the (old) BaseQNode
, or QubitDevice
, depending on which one makes more sense; this way we don't have to update all plugins.
My gut feeling is that it makes more sense to fix this in BaseQNode
, since it will be deleted anyway when tape mode is default.
Issue description
When a
pennylane.numpy.tensor
is passed to a QNode as a keyword argument, it is considered as a non-differentiable parameter. Unlike in the case when it is handled as a differentiable parameter, it is not cast into andarray
.When indexing into a
pennylane.numpy.tensor
, one can end up having a single element sequence. Certain devices (e.g.,qiskit.aer
,forest.wavefunction
), are not capable of supporting the application of a gate taking this type of argument and raise an error. Gates can take such apennylane.numpy.tensor
as gate parameter, when passed as a non-differentiable parameter.A special case using
RandomLayers
was tackled in PR #893. This is an example that is used in the Quanvolutional Neural Networks demonstration.Expected behavior: The above snippet executes without errors.
Actual behavior: Error is raised (Qiskit case):
RuntimeError: Invalid number of dimensions!
Name: PennyLane Version: 0.13.0.dev0 Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc. Home-page: https://github.com/XanaduAI/pennylane Author: None Author-email: None License: Apache License 2.0 Location: Requires: numpy, scipy, networkx, autograd, toml, appdirs, semantic-version Required-by: PennyLane-SF, PennyLane-Qchem, PennyLane-Forest, PennyLane-qsharp, pennylane-qulacs, PennyLane-lightning, PennyLane-qiskit, PennyLane-AQT, PennyLane-Cirq Platform info: Linux-4.19.11-041911-generic-x86_64-with-debian-buster-sid Python version: 3.7.6 Numpy version: 1.18.5 Scipy version: 1.4.1 Installed devices:
Source code and tracebacks
Additional information
A solution could be to create a case for
pennylane.numpy.tensor
type sequence parameters in each plugin device implementation (see https://github.com/PennyLaneAI/pennylane/pull/893#issuecomment-725935113 containing a suggestion).Seems that this problem does not arise in tape mode.