This is in reference to a youtube tutorial on optimization:
if I have a function such as:
circuit (theta, argument 2)
then I cannot do
theta, prev_cost = opt.step_and cost(circuit,theta) as my circuit function need two parameters to be passed. what can be done in this case? what should I do for that argument 2
According to the documentation for optimizers, we can accept a variable length list of arguments. So as long as your circuit expects two arguments, then the optimizer will accept 3 arguments (the circuit, plus the two arguments).
Here’s an example:
import pennylane as qml
from pennylane import numpy as np
dev = qml.device("default.qubit", wires=1)
theta = np.array(0.5, requires_grad = True)
phi = np.array(0.1, requires_grad = True)
@qml.qnode(dev)
def circuit(theta, phi):
qml.RX(theta, wires=0)
qml.RY(phi, wires=0)
return qml.expval(qml.PauliZ(0))
circuit(theta, phi)
opt = qml.GradientDescentOptimizer(0.1)
# Note here that some care is needed to capture and unpack the outputs when you have multiple arguments
(new_theta, new_phi), cost_val = opt.step_and_cost(circuit, theta, phi)
print(new_theta, new_phi, cost_val)