LSTM with Qnode using Keras layer

Hello I am attempting to use the new Keras quantum layer feature, but I am struggling to pass the output of the LSTM into the Qnode. My attempt is below. I get the error that " TypeError: QNode must include an argument with name inputs for inputting data". Thanks for your time and help.

from pennylane.qnn import KerasLayer

# state vector simualtator 
dev = qml.device('default.qubit', wires=2)
n_qubits=2

@qml.qnode(dev,interface='tf')

@qml.qnode(dev)


def circuit(inputs, weights_0, weight_1,weight_2,weight_3,weight_4,weight_5):
    
    #qml.RX(weights_0, wires=0)
    #qml.RX(weight_1, wires=1)
    #qml.RY(weight_2, wires=0)
    #qml.RY(weight_3, wires=1)
    #qml.RZ(weight_4, wires=0)
    #qml.RZ(weight_5, wires=1)
    qml.RZ(inputs,wires=0)
    qml.RZ(inputs,wires=1)
    
    qml.rot(weights_0,weight_1,weight_2,wires=0)
    qml.rot(weight_3,weight_4,weight_5,wires=1)
    
    # span all quantum states and create entanglement 
    qml.CNOT(wires=[0, 1])
    
    # return expectation value as output of QNode
    return qml.expval(qml.PauliZ(0))


# creating a dictionary and initializing value for our quantum parameters
weight_shapes = {"weights_0": 1,"weight_1": 1,"weight_2": 1,"weight_3": 1,"weights_4": 1,"weights_5": 1 }


# now create the quantum node in keras
qlayer = qml.qnn.KerasLayer(circuit, weight_shapes, output_dim=1)


# A sequential model with LSTM layer and quantum node as output node
LSTM=keras.layers.LSTM( 
  units=128,
  input_shape=(X_train.shape[1], X_train.shape[2])
)
    
clayer3=keras.layers.Dense(units=1)


model = tf.keras.models.Sequential([LSTM, qlayer, clayer3])
# Compile model and choose cost function/optimizer
model.compile(
  loss='mean_squared_error',
  optimizer=keras.optimizers.Adam(0.001)
)


model.summary()
print(X_train.shape[1])

# training model and choosing hyperparameters of training process
history = model.fit(
    X_train, y_train,
    epochs=30,
    batch_size=16,
    validation_split=0.1,
    verbose=1,
    shuffle=False
)

I am new to this I’m sorry, I did not think directly copying and pasting would make this post look so unappealing visually. Sorry for that again.

Hi @diracdyson. Thanks for using pennylane! In the weight_shapes dictionary, you have defined weights_4 and weights_5 while in your circuit you have used weight_4 and weight_5 (note the “s” in weight"s"). Also, the qnode decorator is used twice in the code. I removed the extra “s” and used only one qnode decorator and the error disappears:

import pennylane as qml
from pennylane.qnn import KerasLayer

n_qubits = 2
dev = qml.device('default.qubit', wires=n_qubits)

@qml.qnode(dev)
def circuit(inputs, weights_0, weight_1, weight_2, weight_3, weight_4, weight_5):
    qml.RZ(inputs,wires=0)
    qml.RZ(inputs,wires=1)    
    qml.rot(weights_0,weight_1,weight_2,wires=0)
    qml.rot(weight_3,weight_4,weight_5,wires=1)    
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0))

weight_shapes = {"weights_0": 1, "weight_1": 1, "weight_2": 1,
                 "weight_3": 1, "weight_4": 1, "weight_5": 1 }

qlayer = qml.qnn.KerasLayer(circuit, weight_shapes, output_dim=1)

I could only run your code up to this point because X_train is not defined. Hope this helps!