Hello!
I tried adding forward pass through my model before printing the summary but I encounter this error:
InvalidArgumentError: Exception encountered when calling layer "keras_layer" (type KerasLayer).
slice index 10 of dimension 0 out of bounds. [Op:StridedSlice] name: sequential/keras_layer/strided_slice/
Call arguments received:
inputs=tf.Tensor(shape=(2, 48, 1), dtype=float32)
This is the shape of my data:
print(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
(1709, 48, 1) (1709, 24) (427, 48, 1) (427, 24)
This is my model:
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Input(shape=()))
model.add(qlayer)
model.add(tf.keras.layers.Reshape((48,32)))
model.add(tf.keras.layers.GRU(units=36, return_sequences=True, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.GRU(units=36, return_sequences=True))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.GRU(units=24))
model.add(tf.keras.layers.Dense(units=24))
model(X_test[:2])
And this is my qlayer:
n_qubits = 10
dev = qml.device("default.qubit.tf", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs,weights_0,weights_1,weights_2,weights_3):
for i in range(10):
qml.RY(np.arctan(inputs[i]),wires=i)
qml.Hadamard(i)
qml.RX(np.arctan(inputs[10+i]),wires=i)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[2, 3])
qml.CNOT(wires=[4, 5])
qml.CNOT(wires=[6, 7])
qml.CNOT(wires=[8, 9])
for i in range(10):
qml.RZ(np.arctan(inputs[20+i]),wires=i)
for j,w in enumerate(weights_0):
qml.RY(w, wires=j)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[4, 3])
qml.CNOT(wires=[3, 2])
qml.CNOT(wires=[2, 1])
list_index = [1,3,5,7,9]
for j,w in enumerate(weights_1):
qml.RY(w, wires=list_index[j])
qml.Hadamard(1)
qml.Hadamard(3)
qml.Hadamard(5)
qml.Hadamard(7)
qml.Hadamard(9)
for j,w in enumerate(weights_2):
qml.RX(w, wires=j)
qml.CNOT(wires=[0, 1])
qml.CNOT(wires=[4, 3])
qml.CNOT(wires=[3, 2])
qml.CNOT(wires=[2, 1])
list_index = [1,3,5,7,9]
for j,w in enumerate(weights_3):
qml.RX(w, wires=list_index[j])
qml.Hadamard(1)
qml.Hadamard(3)
qml.Hadamard(5)
qml.Hadamard(7)
qml.Hadamard(9)
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliZ(2)), qml.expval(qml.PauliZ(3)), qml.expval(qml.PauliZ(4)),qml.expval(qml.PauliZ(5)), qml.expval(qml.PauliZ(6)), qml.expval(qml.PauliZ(7)), qml.expval(qml.PauliZ(8)), qml.expval(qml.PauliZ(9))
weight_shapes = {"weights_0": 10, "weights_1": 5, "weights_2": 10, "weights_3": 5}#,"weights_5": 1,"weights_6": 1,"weights_7": 1,"weights_8": 1,"weights_9": 1}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=(48,32))
I’m not sure if my output shape is correct because when I try to fit my model, I get this error:
InvalidArgumentError: Exception encountered when calling layer "keras_layer" (type KerasLayer).
slice index 10 of dimension 0 out of bounds. [Op:StridedSlice] name: sequential_1/keras_layer/strided_slice/
Call arguments received:
inputs=tf.Tensor(shape=(32, 48, 1), dtype=float32)