Optimisation routine yields the same loss values

Hi @CatalinaAlbornoz , thank you for your reply! No it is not related to that question. It is more related to this question for which you have helped me resolve the issue. Thank you once again for that.

I have the simpler version of my code here. So my circuit is dependent on 2 sets of parameters w and x. My cost function depends on the gradient of the circuit w.r.t the parameter x. I want to optimize the parameters w by minimizing the cost function. The way I have defined the cost function seems differentiable with respect to w when I am using default.qubit device and training is done without any warning and the cost function changes in every step. However, when I try to use lightning.qubit I get the userwarning as shown below.

I also tried to use res = circuit(w, x) where I simply ask the circuit to minimize the expectation value. This case works well and I also observe speedup when I am using lightning.qubit. Somehow, the gradient g1(w, x) is making cost function not differentiable w.r.to w. However, as I said before, the code works fine with default.qubit. I want to know how I can make it work using lightning plugin since the bigger version of my code is very slow since it has many layers and qubits and I would benefit from using lightning. Thanks in advance for you help!

Please see my code below

import os
os.environ["OMP_NUM_THREADS"] = "4"
import pennylane as qml
from pennylane import numpy as np
import time


start = time.time()

np.random.seed(42)

npoints = 5
batch_size = 5
max_steps = 10 # number of iterations in training

dev = qml.device('lightning.qubit', wires=1)

@qml.qnode(dev)
def circuit(w, x):
    qml.RX(w[0], wires = 0)
    qml.RX(x, wires = 0)
    qml.RX(w[1], wires = 0)
    return qml.expval(qml.PauliZ(wires=0))

g1 = qml.grad(circuit, argnum=1) #gradient of the circuit w.r.t x

def loss(w, x):
    '''Loss function which minimizes the gradient of teh ciruit w.r.t x at a given point x'''
    res = g1(w, x) 
    
    return res**2


# defining cost function
def cost(w, xlist):
    '''cost function which computes the mean of loss function for all the points in xlist'''
    loss1 = 0
    for i in xlist:
        loss1 += loss(w, i)
    loss1 = loss1/len(xlist)
    return loss1

X = np.linspace(0, 1, npoints, requires_grad=True) #values for parameter x

weights = np.array([0.1, 0.2], requires_grad=True) #initial weights

lr = 0.01
opt = qml.AdamOptimizer(lr)

for step in range(max_steps):
    # select batch of data
    batch_index = np.random.randint(0, len(X), (batch_size,))
    x_batch = X[batch_index]

    # update the weights by one optimizer step
    weights = opt.step(lambda w: cost(w, x_batch), weights)
    c = cost(weights, X)

    if (step + 1) % 1 == 0:
        print("Cost at step {0:3}: {1}".format(step + 1, c))

end = time.time()

print('run time:'+ str(end-start))

Here is the output when using lightning plugin

C:\Users\hedge\PycharmProjects\venv\Lib\site-packages\autograd\tracer.py:14: UserWarning: Output seems independent of input.
  warnings.warn("Output seems independent of input.")
Cost at step   1: 0.511200264393484
Cost at step   2: 0.511200264393484
Cost at step   3: 0.511200264393484
Cost at step   4: 0.511200264393484
Cost at step   5: 0.511200264393484
Cost at step   6: 0.511200264393484
Cost at step   7: 0.511200264393484
Cost at step   8: 0.511200264393484
Cost at step   9: 0.511200264393484
Cost at step  10: 0.511200264393484
run time:0.10907101631164551

Here is the output of qml.about()

Name: PennyLane
Version: 0.37.0
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: C:\Users\hedge\PycharmProjects\venv\Lib\site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, packaging, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Rigetti, PennyLane_Lightning

Platform info:           Windows-10-10.0.22631-SP0
Python version:          3.11.9
Numpy version:           1.26.4
Scipy version:           1.14.1
Installed devices:
- default.clifford (PennyLane-0.37.0)
- default.gaussian (PennyLane-0.37.0)
- default.mixed (PennyLane-0.37.0)
- default.qubit (PennyLane-0.37.0)
- default.qubit.autograd (PennyLane-0.37.0)
- default.qubit.jax (PennyLane-0.37.0)
- default.qubit.legacy (PennyLane-0.37.0)
- default.qubit.tf (PennyLane-0.37.0)
- default.qubit.torch (PennyLane-0.37.0)
- default.qutrit (PennyLane-0.37.0)
- default.qutrit.mixed (PennyLane-0.37.0)
- default.tensor (PennyLane-0.37.0)
- null.qubit (PennyLane-0.37.0)
- lightning.qubit (PennyLane_Lightning-0.37.0)
- rigetti.numpy_wavefunction (PennyLane-Rigetti-0.36.0.post0)
- rigetti.qpu (PennyLane-Rigetti-0.36.0.post0)
- rigetti.qvm (PennyLane-Rigetti-0.36.0.post0)
- rigetti.wavefunction (PennyLane-Rigetti-0.36.0.post0)
None