Unused parameters for the quantum layer in keras hybrid model

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)

More information about the error:

<ipython-input-10-3e198aa82acd> in qnode(inputs, weights_0, weights_1, weights_2, weights_3)
     11         qml.RY(np.arctan(inputs[i]),wires=i) #first 10 inputs apply in ry
     12         qml.Hadamard(i)
---> 13         qml.RX(np.arctan(inputs[10+i]),wires=i) #the 10-20 inputs apply in rx
     14 
     15     qml.CNOT(wires=[0, 1])

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)

Hi @aouie, it’s hard to tell for sure what’s going on but my impression is that you have an “inputs” parameter that goes into your qnode, but when you create the qlayer you only add the weight shapes so the qnode is getting confused there. I would suggest creating a minimum example in a few lines so that you can understand where the error comes from and it will make it easier for us to help you too.

I am trying this one but I changed the dataset that I am using, also, I am using 10 qubits.
Quantum-Counselor-for-Portfolio-Investment/pennylane_stock_price_hybrid_algorithm.ipynb at main · alejomonbar/Quantum-Counselor-for-Portfolio-Investment · GitHub

I am trying this because I want to perform Ry in the first 30% of my data, followed by Rx (second 30%) and Rz for the last 40%

Hi @aouie, from what I can see in their code they are also using 10 qubits. They have built their code for data that has a similar shape to theirs so you might have to seriously modify their code in order to run it with a very different data shape such as yours.

I think the best option for you is to modify your dataset so that it looks similar to theirs. First run their code with your modified data and then if it works try to add one new change at a time.

Okay! Thank you for that!