Hello, I want to optimize a cost function that takes de distance between two desity matrices, one of them being the result of a circuit. But using np.linalg.norm() seems not to work correctly, since the opt.step() function gives me the same parameter at each step:
import pennylane as qml
from pennylane import numpy as np
@qml.qnode(device, interface="autograd")
def circuit(params):
#qml.RY(np.pi,wires=[0,1])
qml.RY(params[0],wires=0)
qml.RY(params[0],wires=1)
qml.Barrier(wires = range(4))
qml.RY(params[1],wires=1)
qml.RY(params[1],wires=2)
qml.Barrier(wires = range(4))
qml.RY(params[2],wires=2)
qml.RY(params[2],wires=3)
return qml.density_matrix(wires=range(4))
def cost_func(params):
densityMatrix = circuit(params)
fidelity = np.linalg.norm(np.array(densityMatrix-((1/16.)*(np.identity(16))), dtype=float))
return fidelity
The optimization is
init_params = np.array([np.pi/2,5,0], requires_grad=True)
opt = qml.GradientDescentOptimizer(stepsize=0.05)
steps = 5
params = init_params
for i in range(steps):
params = opt.step(cost,params)
print(params, cost(params))
As you can check the print() shows the same parameters. This also happens for different initial conditions, and different steps, and also if a define my norm function manually. What could be the problem here? Thanks in advance.