Doing some basic gradient descent with parameterised quantum circuits and been getting the following error:
AttributeError: 'tensor' object has no attribute '_id'
Any ideas as to why this may be? I have pasted my code below for more information:
import pennylane as qml
from pennylane import numpy as np
import tensorflow as tf
n_wires = 4
n_layers = 1
dev = qml.device('default.qubit', wires= n_wires)
dev1 = qml.device('default.qubit', wires= n_wires)
@qml.qnode(dev, interface="tf")
def phi_circ(weights): #generates the random vector phi
for i in range(4):
qml.RX(weights[i], wires=i)
qml.RY(weights[4+i], wires=i)
qml.RZ(weights[8+i], wires=i)
return [qml.expval(qml.Identity(wires=i)) for i in range(n_wires)]
@qml.template
def layer(weights, wires):
for i in range(4):
qml.RX(weights[i], wires=i)
for i in range(4):
qml.RZ(weights[4+i], wires=i)
qml.CZ(wires=[0,1])
qml.CZ(wires=[0,2])
qml.CZ(wires=[0,3])
qml.CZ(wires=[1,2])
qml.CZ(wires=[1,3])
qml.CZ(wires=[2,3])
@qml.qnode(dev1, interface="tf")
def psi_circ(thetas): #generates the parameterised vector psi
for i in range(n_layers):
layer(weights = thetas, wires= range(n_wires) )
return [qml.expval(qml.Identity(wires=i)) for i in range(n_wires)]
random = tf.constant( np.random.uniform(0, 2*np.pi, 12) )
phi_circ(random)
phi = tf.dtypes.cast(dev.state, tf.complex128)
init_thetas = np.random.uniform(0, 2*np.pi, 8)
thetas = tf.Variable(init_thetas)
def eplison(thetas):
psi_circ(thetas)
psi = tf.dtypes.cast(dev1.state, tf.complex128)
e = tf.dtypes.cast(np.vdot(psi - phi, psi - phi), tf.complex128)
return np.real(e)
opt = tf.keras.optimizers.SGD(0.4)
cost = lambda: eplison(thetas)
for step in range(50):
opt.minimize(cost, thetas)