Closed ghost closed 2 years ago
Hi @JoeGibbs88! To give you the short answer, this is a known restriction of the currently released version of PennyLane - you cannot include classical processing within a QNode if you want to compute the metric tensor, which is needed for the QNG optimizer. ('Repeating of parameters' here counts as classical processing, since it results in the product rule being required!).
However, the development version of PL has upgraded the metric tensor function to now support this workflow π So if you install the latest version of PL from GitHub,
pip install git+https://github.com/PennyLaneAI/pennylane.git#egg=pennylane
then your code example with the second line uncommented will work well (I just tested this myself)! You can switch over to the development version now, or wait for the 0.19 release of PL (which will be coming soon).
In case you are interested in the slightly longer answer:
The metric tensor proper is only defined for the quantum circuit gate arguments, while the optimizer is instead optimizing the QNode arguments.
This is a bit of subtlety, and is caused by repeating of the parameter in your example. You have an array of parameters of shape (1,)
as input to the QNode, but due to the parameter repitition, the resulting quantum circuit has 2 trainable gate arguments. As a result, the resulting metric tensor will be of size [2, 2]
!
This will confuse the optimizer, which won't be able to apply the QNG update step due to the shape mismatch.
The reason it has been implemented like this is that the introductory paper https://arxiv.org/abs/1909.02108 does not consider QNG optimization in the context of classical pre-processing of parameters; convergence is only proven when optimizing the gate arguments directly.
However, as noted above, in the new PL development version, the qml.metric_tensor
function has been modified to take into account classical processing between QNode args and gate args π
@JoeGibbs88 let me know if the development PL version works for you, or if you have any other questions. In the meantime, I will close this issue, since the underlying issue is already solved in the codebase
Your solution also worked for me, thanks for the help :)
I would like to use the QNGoptimizer to train an ansatz that uses the same parameter as an input to multiple rotation gates. Here is an example.
The code above runs fine, however, if the second line in circuit() is uncommented, I get the error:
Is there a method I am unaware of that allows the QNGoptimizer to be applied to ansatzes with shared parameters, or is this incompatible in its common form?