I was doing a simple image classification with the help of pennylane quantum library.
this code used to work properly but now its not working from June 2023.
!pip install pennylane-qiskit
from qiskit.providers.fake_provider import FakeQuitoV2
import qiskit.providers.aer.noise as noise
import pennylane as qml
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from matplotlib import pyplot as plt
from pennylane import numpy as np
random_seed=5
np.random.seed(random_seed)
tf.random.set_seed(random_seed)
tf.keras.backend.set_floatx('float64')
t=[0,1]
(X_train,Y_train),(X_test,Y_test)=keras.datasets.mnist.load_data()
print(t)
binary_index=[i for i in range(len(Y_train)) if Y_train[i] in t]
X_train=X_train[binary_index]
Y_train=Y_train[binary_index]
binary_index=[i for i in range(len(Y_test)) if Y_test[i] in t]
X_test=X_test[binary_index]
Y_test=Y_test[binary_index]
n_train=200
n_test=50
X_train=X_train[:n_train]
Y_train=Y_train[:n_train]
X_test=X_test[:n_test]
Y_test=Y_test[:n_test]
X_train=X_train/255*np.pi
X_test =X_test/255*np.pi
X_train = np.array(X_train[...,tf.newaxis],requires_grad=False,dtype=np.float64)
X_test = np.array( X_test[...,tf.newaxis],requires_grad=False,dtype=np.float64)
n_epochs=30
batch_size=5
m=4
w1={"weights": (4,4)}
encoder_1= qml.device("qiskit.aer",wires=m,backend="aer_simulator_statevector")
@qml.qnode(encoder_1)
def encoderl(inputs,weights):
for i in range(m):
qml.RY(inputs[i],wires=i)
for i in range(m):
qml.RZ(inputs[m+i],wires=i)
for i in range(m):
qml.RX(inputs[2*m+i],wires=i)
for i in range(m):
qml.RY(inputs[3*m+i],wires=i)
qml.BasicEntanglerLayers(weights, wires=range(m))
return [qml.expval(qml.PauliZ(i)) for i in range(m)]
Encoding_layer=qml.qnn.KerasLayer(encoderl,w1,output_dim=m)
from keras.models import Sequential
model = Sequential([
keras.layers.Cropping2D(cropping=((2, 2), (2, 2))),
keras.layers.AveragePooling2D(pool_size=(6,6),strides=(6, 6)),
keras.layers.Flatten(),
Encoding_layer,
# keras.layers.Normalization(mean=0,variance=1,axis=None),
keras.layers.Dense(2,activation="softmax"),
])
model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics =['accuracy'])
print("Test training")
model.fit(X_train, Y_train,epochs = 1,batch_size = batch_size,validation_data=(X_test,Y_test),shuffle = True)
I am getting this error message now.
InvalidArgumentError Traceback (most recent call last)
<ipython-input-15-2257bd784bfb> in <cell line: 82>()
80 print("Test training")
81
---> 82 model.fit(X_train, Y_train,epochs = 1,batch_size = batch_size,validation_data=(X_test,Y_test),shuffle = True)
83
84
6 frames
/usr/local/lib/python3.10/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
/usr/local/lib/python3.10/dist-packages/pennylane/qnn/keras.py in call(self, inputs)
394 else:
395 # calculate the forward pass as usual
--> 396 results = self._evaluate_qnode(inputs)
397
398 # reshape to the correct number of batch dims
/usr/local/lib/python3.10/dist-packages/pennylane/qnn/keras.py in _evaluate_qnode(self, x)
416 **{k: 1.0 * w for k, w in self.qnode_weights.items()},
417 }
--> 418 res = self.qnode(**kwargs)
419
420 if isinstance(res, (list, tuple)):
/usr/local/lib/python3.10/dist-packages/pennylane/qnode.py in __call__(self, *args, **kwargs)
934
935 # construct the tape
--> 936 self.construct(args, kwargs)
937
938 cache = self.execute_kwargs.get("cache", False)
/usr/local/lib/python3.10/dist-packages/pennylane/qnode.py in construct(self, args, kwargs)
825 self.interface = qml.math.get_interface(*args, *list(kwargs.values()))
826
--> 827 self._tape = make_qscript(self.func, shots)(*args, **kwargs)
828 self._qfunc_output = self.tape._qfunc_output
829
/usr/local/lib/python3.10/dist-packages/pennylane/tape/qscript.py in wrapper(*args, **kwargs)
1480 def wrapper(*args, **kwargs):
1481 with AnnotatedQueue() as q:
-> 1482 result = fn(*args, **kwargs)
1483
1484 qscript = QuantumScript.from_queue(q, shots)
<ipython-input-15-2257bd784bfb> in encoderl(inputs, weights)
55 qml.RY(inputs[i],wires=i)
56 for i in range(m):
---> 57 qml.RZ(inputs[m+i],wires=i)
58 for i in range(m):
59 qml.RX(inputs[2*m+i],wires=i)
InvalidArgumentError: Exception encountered when calling layer 'keras_layer_6' (type KerasLayer).
{{function_node __wrapped__StridedSlice_device_/job:localhost/replica:0/task:0/device:GPU:0}} slice index 5 of dimension 0 out of bounds. [Op:StridedSlice] name: sequential_6/keras_layer_6/strided_slice/
Call arguments received by layer 'keras_layer_6' (type KerasLayer):
• inputs=tf.Tensor(shape=(5, 16), dtype=float64)