# Qlm GradientDescent very slow with lightning.gpu

**URL:** <https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104>\
**Category:** PennyLane Help\
**Created:** [June 27, 2023, 8:23am UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104 "2023-06-27T08:23:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 27, 2023, 8:23am UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/1 "2023-06-27T08:23:07Z")

</div>

The problem is with Gradient Descent optimizer from pennylane and lightning.gpu compatibility I guess?

When I use default.qubit the optimizer step runs in a few seconds, when using lightning.gpu it hasn’t yet finished after 10 minutes with GPU or without GPU.

I don’t even know what to do, lightning.gpu without a GPU defaults to the CPU so I thought it should perform the same as the default.qubit.

SOLUTION: parameter-shift was just extremely slow

---

<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:** [June 27, 2023, 2:07pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/2 "2023-06-27T14:07:51Z")

</div>

Hey @ilmars_kuhtarskis! Welcome to the forum 😃!

It really depends on your application. It sounds like you’re running code that uses a relatively low number of qubits (\<\<20). In that regime, Lightning-GPU isn’t going to outperform lightning qubit, possibly not even default qubit. Lightning-GPU is really meant for high qubit counts :).

---

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 27, 2023, 7:11pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/3 "2023-06-27T19:11:40Z")

</div>

I could accept this answer if the speed difference wasn’t too big, but the default qubit takes 10 seconds for my code, but the lightning qubit hadn’t finished after 30 minutes.

---

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 27, 2023, 7:13pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/4 "2023-06-27T19:13:13Z")

</div>

How come lightning.qubit is so much better for everything else multiple x speedup, sometimes even more than 10x speedup, but here it is because it is ‘lightning qubit’. It is obviosuly better, I am just trying to find out what is the problem with the pennylane optimizer

---

<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:** [June 27, 2023, 7:20pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/5 "2023-06-27T19:20:34Z")

</div>

Does setting `diff_method="adjoint"` help? It could be that lightning is defaulting to parameter-shift, which is going to be slower.

---

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 27, 2023, 7:37pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/6 "2023-06-27T19:37:51Z")

</div>

I tried parameter-shift on default.qubit and it seems it is also taking really long now, I need to change my code for it to work for lightning.gpu becase some support issues… and check if it fixes the problem.

I think you are correct, I will update tomorrow!  
Thank you, I definitely would not have figured this out by myself.

---

<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:** [June 27, 2023, 7:49pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/7 "2023-06-27T19:49:53Z")

</div>

Great! Let us know if the behaviour still persists. At that point, it would be great to get a full code example and for you to share your package versions 🙂

---

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 28, 2023, 8:22am UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/8 "2023-06-28T08:22:19Z")

</div>

I couldn’t figure out how to get the exp val of all 0s state without support errors for the adjoint diff\_method, so I just switched to finite\_diff, hopefully it is not that bad.

If you have a solution it would be nice though 😃

I am using qml 0.30 cuda 11.7. I will try maybe param-shift isn’t that slow on a supercomputer If it is much more better than finite\_diff/adjoint.

Here is my circuit, I think it is just stolen from kernel tutorial:

```auto
def layer(x, params, wires, i0=0, inc=1):
    """Building block of the embedding ansatz"""
    i = i0
    for j, wire in enumerate(wires):
        qml.Hadamard(wires=[wire])
        qml.RZ(x[i % len(x)], wires=[wire])
        i += inc
        qml.RY(params[0, j], wires=[wire])

    qml.broadcast(unitary=qml.CRZ, pattern="ring", wires=wires, parameters=params[1])

def ansatz(x, params, wires):
    """The embedding ansatz"""
    for j, layer_params in enumerate(params):
        layer(x, layer_params, wires, i0=j * len(wires))

#check if I need batch_obs
dev = qml.device("lightning.gpu", wires=num_wires, shots=None)
wires = dev.wires.tolist()

@qml.qnode(dev, interface="autograd", diff_method="finite-diff")
def kernel(x1, x2, params):
    ansatz(x1, params, wires=wires)
    qml.adjoint(ansatz)(x2, params, wires=wires)
    #return probability of all 0 state
    return qml.expval(qml.Projector([0]*num_wires, wires=wires))

```

---

<div class="post-metadata">

**Author:** ![ilmars\_kuhtarskis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/ilmars_kuhtarskis/32/1756_2.png) [@ilmars\_kuhtarskis](https://discuss.pennylane.ai/u/ilmars_kuhtarskis)\
**Post date:** [June 28, 2023, 9:32am UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/9 "2023-06-28T09:32:39Z")

</div>

I got adjoint to work and it is just as fast as backprop on the CPU and GPU it’s amazing!

I replaced the kernel function with this:

```auto
@qml.qnode(dev, interface="autograd", diff_method="adjoint") 
def kernel_circuit(x1, x2, params):
    ansatz(x1, params, wires=wires)
    qml.adjoint(ansatz)(x2, params, wires=wires)
    return [qml.expval(qml.PauliZ(w)) for w in wires]

def kernel(x1, x2, params):
    expectations = kernel_circuit(x1, x2, params)
    return np.prod(expectations)

```

---

<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:** [June 28, 2023, 1:11pm UTC](https://discuss.pennylane.ai/t/qlm-gradientdescent-very-slow-with-lightning-gpu/3104/10 "2023-06-28T13:11:27Z")

</div>

Oh that’s awesome! Glad you were able to get this resolved 😁. If there’s anything else do let us know!
