-
Notifications
You must be signed in to change notification settings - Fork 634
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
[BUG] QNSPSAOptimizer
improperly handles optimization on lightning.qubit
#5460
Comments
QNSPSAOptimizer
improperly handles differentiation on lightning.qubit
QNSPSAOptimizer
improperly handles optimization on lightning.qubit
Hi @isaacdevlugt, the problem seems to not concern
The same happens with functions that take a circuit as an argument. |
The issue is that Line 441 strictly assumes the new device interface. While |
Hi everyone! I was going to fix the bug, but when I tried to run the above code, I didn't see an error. Can you please confirm if the issue is still present? Thanks! |
@PietropaoloFrisoni we discussed internally, but the issue is no longer present with lightning because lightning now uses the new device API (PennyLaneAI/pennylane-lightning#646). But, |
@isaacdevlugt Thanks, Isaac! That's precisely what I did to reproduce and fix the issue (you can see it in the description of #5497 ) |
….qubit (#5497) **Context:** The `QNSPSAOptimizer` improperly handles optimization on devices that don't follow the new API design. **Description of the Change:** In the workflow of the `step_and_cost` method of `QNSPSAOptimizer`, the code now checks whether the device follows the new API guidelines before calling the `preprocess` function (which is not present in the old device interface). If this is not the case, the code simply calls the `qml.execute` function. **Benefits:** Now, `qml.QNSPSAOptimizer` can handle legacy devices that do not follow the new API design, such as `default.qubit.legacy`. Therefore, the following code now works: ``` from pennylane import numpy as np import pennylane as qml dev = qml.device("default.qubit.legacy", wires=2) @qml.qnode(dev) def cost(params): qml.RX(params[0], wires=0) qml.CRY(params[1], wires=[0, 1]) return qml.expval(qml.Z(0) @ qml.Z(1)) opt = qml.QNSPSAOptimizer(stepsize=5e-2) params = np.random.rand(2) for _ in range(10): params, loss = opt.step_and_cost(cost, params) ``` **Possible Drawbacks:** None that I can think of. **Related GitHub Issues:** #5460 [sc-60727], [sc-60328]
Expected behavior
No difference in behaviour with default.qubit and lightning.qubit when optimizing a circuit with QNSPSAOptimizer.
Actual behavior
QNSPSAOptimizer mishandles the case where lightning qubit is used.
Additional information
Might be related to #5437? Not sure.
Source code
Tracebacks
System information
Name: PennyLane Version: 0.35.1 Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network. Home-page: https://github.com/PennyLaneAI/pennylane Author: Author-email: License: Apache License 2.0 Location: /Users/isaac/.virtualenvs/pennylane-stable/lib/python3.11/site-packages Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions Required-by: PennyLane_Lightning Platform info: macOS-14.4.1-arm64-arm-64bit Python version: 3.11.8 Numpy version: 1.26.4 Scipy version: 1.12.0 Installed devices: - default.clifford (PennyLane-0.35.1) - default.gaussian (PennyLane-0.35.1) - default.mixed (PennyLane-0.35.1) - default.qubit (PennyLane-0.35.1) - default.qubit.autograd (PennyLane-0.35.1) - default.qubit.jax (PennyLane-0.35.1) - default.qubit.legacy (PennyLane-0.35.1) - default.qubit.tf (PennyLane-0.35.1) - default.qubit.torch (PennyLane-0.35.1) - default.qutrit (PennyLane-0.35.1) - null.qubit (PennyLane-0.35.1) - lightning.qubit (PennyLane_Lightning-0.35.1)
Existing GitHub issues
The text was updated successfully, but these errors were encountered: