# How to use lightning.gpu to solve parameter optimization problems

**URL:** <https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563>\
**Category:** PennyLane Lightning\
**Created:** [October 18, 2023, 5:35am UTC](https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563 "2023-10-18T05:35:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@sun](https://discuss.pennylane.ai/u/sun)\
**Post date:** [October 18, 2023, 5:35am UTC](https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563/1 "2023-10-18T05:35:44Z")

</div>

I recently did an experiment on quantum machine learning, but I wanted to use GPU to accelerate the parameter optimization problem of quantum machine learning, so I used lightning.gpu, but reported the following errors:

ValueError: Computing the gradient of circuits that return the state with the parameter-shift rule gradient transform is not supported, as it is a hardware-compatible method.

So I’d like to ask a few questions here:  
(1) lightning.gpu does not support solving the problem of using the optimizer for gradient descent to optimize parameters  
(2) How to use ansatz built on pennylane platform and combined with gpu to use gradient descent method for quantum machine learning? Can you give me an example, thank you，lol

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [October 18, 2023, 2:36pm UTC](https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563/2 "2023-10-18T14:36:11Z")

</div>

Hey @sun! Welcome to the forum 🕶

This error message doesn’t have anything to do with PennyLane lightning actually 🙂. The same error happens with `default.qubit`:

```python
import pennylane as qml
from pennylane import numpy as np

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, diff_method="parameter-shift")
def circuit(params):
    qml.RX(params[0], wires=0)
    qml.RY(params[1], wires=0)
    return qml.state()

params = np.array([0.1, 0.2], requires_grad=True)
qml.jacobian(circuit)(params)

```

```pycon
ValueError: Computing the gradient of circuits that return the state with the parameter-shift rule gradient transform is not supported, as it is a hardware-compatible method.

```

The incompatibility here is due to the fact that the parameter-shift rule is intrinsically hardware compatible; it relies on the measurement being something that a real quantum computer can churn out, and the full quantum state is not one of those things 🙂. Check out this page for more info: [qml.gradients.param\_shift — PennyLane 0.32.0 documentation](https://docs.pennylane.ai/en/stable/code/api/pennylane.gradients.param_shift.html)

If you return, say, an expectation value, then using the parameter-shift rule is all fine and dandy!

```auto
import pennylane as qml
from pennylane import numpy as np

dev = qml.device("lightning.qubit", wires=2)
@qml.qnode(dev, diff_method="parameter-shift")
def circuit(params):
    qml.RX(params[0], wires=0)
    qml.RY(params[1], wires=0)
    qml.RX(params[2], wires=0)
    return qml.expval(qml.PauliZ(0))
    
params = np.array([0.1, 0.2, 0.3], requires_grad=True)
qml.jacobian(circuit)(params)

```

```auto
array([-0.3875172 , -0.18884787, -0.38355704])

```

Let me know if this helps 🙂

---

<div class="post-metadata">

**Author:** ![sun](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@sun](https://discuss.pennylane.ai/u/sun)\
**Post date:** [October 19, 2023, 6:21am UTC](https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563/4 "2023-10-19T06:21:27Z")

</div>

I really appreciate your reply. I’ll look into it

---

<div class="post-metadata">

**Author:** ![isaacdevlugt](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/isaacdevlugt/32/1159_2.png) [@isaacdevlugt](https://discuss.pennylane.ai/u/isaacdevlugt)\
**Post date:** [October 19, 2023, 1:25pm UTC](https://discuss.pennylane.ai/t/how-to-use-lightning-gpu-to-solve-parameter-optimization-problems/3563/5 "2023-10-19T13:25:36Z")

</div>

My pleasure! Let me know if you have any more questions 🙂
