Hi @Kaushik,
Can you please write a minimal example which shows this error?
Something that I can use to try to replicate your error.
Below is an example of how to perform an optimization within a class (taken from another Forum thread). It might help you get an idea on how to set up your program.
import pennylane as qml
from pennylane import numpy as np
class MyClass:
def __init__(self, n_qubits):
self.n_qubits = n_qubits
device = qml.device("default.qubit", wires=n_qubits)
self.quantum_node = qml.QNode(self.quantum_function, device)
def quantum_function(self, params):
for i in range(len(params)):
qml.RX(params[i], wires=i)
return [qml.expval(qml.PauliZ(i)) for i in range(len(params))]
def cost(self, params):
return -np.sum(self.quantum_node(params))
def do_optimization(self):
params = np.random.uniform(0, np.pi, size=(self.n_qubits,), requires_grad=True)
opt = qml.GradientDescentOptimizer(0.1)
for n in range(10):
params = opt.step(self.cost, params)
print(self.cost(params))
obj = MyClass(4)
obj.do_optimization()
I hope this helps you solve your question!