Hi, I hope you are doing well!
I am trying to retrieve a target state introducing an input Hamiltonian with two trainable parameters and using the infidelity as cost function. Here is a snippet of the code
import numpy as np
import pennylane as qml
from pennylane import numpy as qnp
# Target state
target = np.array([0,0,0,1])
# Circuit + cost function definition
@qml.qnode(device=qml.device('default.qubit', wires=2), interface='autograd')
def circuit(params):
H = sum([params[i]*(qml.PauliX(i) + qml.PauliZ(i)) for i in range(2)])
qml.exp(H, coeff=-0.5j)
return qml.state()
def cost_fn(params):
return 1-abs(np.dot(circuit(params), target))**2 # infidelity
# Select optimizer + trainable parameters initialization
opt = qml.GradientDescentOptimizer()
theta = qnp.array([0.0, 0.0], requires_grad=True)
# Store infidelities to track convergence
infidelity = [cost_fn(theta)]
max_iterations = 100
conv_tol = 1e-03
for n in range(max_iterations):
theta, prev_infid = opt.step_and_cost(cost_fn, theta)
infidelity.append(cost_fn(theta))
conv = np.abs(infidelity[-1] - prev_infid)
if n % 2 == 0:
print(f"Step = {n}, Infidelity = {infidelity[-1]:.8f}")
if conv <= conv_tol:
break
print("\n" f"Final infidelity = {infidelity[-1]:.8f}")
The error shown is
TypeError: Can't differentiate w.r.t. type <class 'pennylane.ops.qubit.hamiltonian.Hamiltonian'>
Since I am new using PennyLane, probably I am not coding it properly. I have tried but I did not find references regarding this error.
Thanks in advance for your time and have a nice day.