Problem developing Non-Sequential Model with keras

I am trying to make a nonsequential model with help of pennylane-qiskit and keras but following error shows up

from keras.layers import *
from keras.models import Model

shape=X_test[0].shape
print(shape)
input=Input(shape=shape)

crop=Cropping2D(cropping=((2,2),(2,2)))(input)
pool=AveragePooling2D(pool_size=(6,6),strides=(6, 6))(crop)
flat=Flatten()(pool)

flat1,flat2=tf.split(flat,2,axis=1)

qlayer1=Encoding_layer1(flat1)

qlayer2=Encoding_layer2(flat2)

concatenated = tf.concat([qlayer1,qlayer2],axis=1)

norm=Normalization(mean=0,variance=1,axis=None)(concatenated)

dense=Dense(2,activation="softmax")(norm)

model=Model(inputs=input,outputs=dense)

model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics =['accuracy'])

hybrid = model.fit(X_train,
                    Y_train,
                    epochs = n_epochs,
                    batch_size = batch_size,
                    validation_data=(X_test,Y_test),
                    verbose=2,
                    shuffle = True,workers=cpu_count(), use_multiprocessing=True)

(28, 28, 1)
Epoch 1/30
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-24-2555657e97d3> in <cell line: 28>()
     26 model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics =['accuracy'])
     27 
---> 28 hybrid = model.fit(X_train,
     29                     Y_train,
     30                     epochs = n_epochs,

8 frames
/usr/local/lib/python3.10/dist-packages/pennylane/math/utils.py in requires_grad(tensor, interface)
    456             from tensorflow.python.eager.tape import should_record_backprop
    457         except ImportError:  # pragma: no cover
--> 458             from tensorflow.python.eager.tape import should_record as should_record_backprop
    459 
    460         return should_record_backprop([tf.convert_to_tensor(tensor)])

ImportError: Exception encountered when calling layer 'keras_layer_6' (type KerasLayer).

cannot import name 'should_record' from 'tensorflow.python.eager.tape' (/usr/local/lib/python3.10/dist-packages/tensorflow/python/eager/tape.py)

Call arguments received by layer 'keras_layer_6' (type KerasLayer):
  • inputs=tf.Tensor(shape=(5, 8), dtype=float64)

for the full code see this

!pip install qiskit==0.24.0
!pip install pennylane==0.30
!pip install pennylane-qiskit==0.31.0
!pip install qiskit_aer

from qiskit.providers.fake_provider import FakeQuitoV2
from qiskit.providers.fake_provider import FakeVigoV2

import qiskit_aer.noise as noise
import pennylane as qml
import pandas as pd
import tensorflow as tf
from tensorflow import keras
from matplotlib import pyplot as plt

from pennylane import numpy as np
from multiprocessing import cpu_count
from keras.models import Sequential

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]

folder ="/content/sample_data/local_runtime/"

n_train=200
n_test=50

n_epochs=30
batch_size=5

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)

m=2
w1={"weights": (4,2)}

print(cpu_count())
tf.config.list_physical_devices('GPU')

noisemodel1= noise.NoiseModel.from_backend(FakeQuitoV2())

encoder_1=qml.device("qiskit.aer",wires=m,backend="aer_simulator_statevector",noise_model =noisemodel1)

@qml.qnode(encoder_1)
def encoderl1(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_layer1=qml.qnn.KerasLayer(encoderl1,w1,output_dim=m)

noisemodel2= noise.NoiseModel.from_backend(FakeVigoV2())

encoder_2=qml.device("qiskit.aer",wires=m,backend="aer_simulator_statevector",noise_model =noisemodel2)

@qml.qnode(encoder_2)
def encoderl2(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_layer2=qml.qnn.KerasLayer(encoderl1,w1,output_dim=m)


from keras.layers import *
from keras.models import Model

shape=X_test[0].shape
print(shape)
input=Input(shape=shape)

crop=Cropping2D(cropping=((2,2),(2,2)))(input)
pool=AveragePooling2D(pool_size=(6,6),strides=(6, 6))(crop)
flat=Flatten()(pool)

flat1,flat2=tf.split(flat,2,axis=1)

qlayer1=Encoding_layer1(flat1)

qlayer2=Encoding_layer2(flat2)

concatenated = tf.concat([qlayer1,qlayer2],axis=1)

norm=Normalization(mean=0,variance=1,axis=None)(concatenated)

dense=Dense(2,activation="softmax")(norm)

model=Model(inputs=input,outputs=dense)

model.compile(optimizer='adam', loss = 'sparse_categorical_crossentropy', metrics =['accuracy'])

hybrid = model.fit(X_train,
                    Y_train,
                    epochs = n_epochs,
                    batch_size = batch_size,
                    validation_data=(X_test,Y_test),
                    verbose=2,
                    shuffle = True,workers=cpu_count(), use_multiprocessing=True)

all the necessary details are present in above code
in order to replicate this error you can copy paste above code in google colab current version.

Hi @Abhijeet_Panihar ,

Unfortunately the combination of package versions that you’re using is incompatible.

If you upgrade all of your imported packages you will notice that the error changes mentioning a dimension mismatch. Unfortunately your current code is very complex so it’s very hard to debug. My recommendation would be to simplify it as much as possible (possibly using a simpler dataset) for debugging purposes. This can help you find the source of your error.

Another option is starting from this demo on turning quantum nodes into Keras Layers and modifying lines one at a time. Again, only try this with the mnist dataset when everything else works.

Below is the code you need to run to upgrade the libraries.

!pip install qiskit --upgrade
!pip install pennylane --upgrade
!pip install pennylane-qiskit --upgrade
!pip install qiskit_aer --upgrade

I hope this helps you find the solution to your problem!

I am using this combination of package or else packages are not compactable.

the most simpler form of this error is given in first code cell

as for Turning quantum nodes into Keras Layers | PennyLane Demos page, I have used this page only for reference.

Hi @Abhijeet_Panihar,

The combination of package versions that you’re using is unfortunately incompatible. You will need to use the latest version of all packages in order to solve your current issue.

To install PennyLane locally it’s strongly recommended that you create a new virtual environment to prevent you from having any installation issues. You can create a virtual environment with Conda and install PennyLane as follows:

  1. Install Anaconda or Miniconda following the instructions here and here respectively.
  2. Open your terminal (mac) or command line (Windows).
  3. Create a new Conda environment with: conda create --name <name_of_your_environment> python=3.10
  4. Activate the environment: conda activate <name_of_your_environment>
  5. Install the needed packages: python -m pip install pennylane jupyter pennylane-qiskit

Note that you will be installing 3 packages here: PennyLane, Jupyter, and the PennyLane-Qiskit plugin. Also, note that where it says <name_of_your_environment> you can choose any name that you want.

After this you can write jupyter notebook in your terminal and you will be ready to create programs using PennyLane.

You can also watch the video here for detailed PennyLane installation instructions.

Alternatively you can use Google Colab.

When you open a new notebook in Google Colab you should run the following at the beginning of your notebook: !pip install pennylane pennylane-qiskit

Here you will be installing both PennyLane and the PennyLane-Qiskit plugin. If you needed other packages you would need to install them in the same way.

After they’re installed you will need to click on Runtime → Restart runtime
You can now run your code and debug any issues that arise.

Please let me know if you have any questions about this.

1 Like