Question about KerasLayer

I meet a question when I use qml.qnn.KerasLayer. I want to build a hybrid classical-quantum model. But when I run the code, a question appear. Here is my code.

import os,random
os.environ["KERAS_BACKEND"] = "tensorflow"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import numpy as np
import matplotlib.pyplot as plt
import as scio
import pickle,sys,h5py
import keras
import keras.backend as K
from keras.callbacks import LearningRateScheduler
import tensorflow as tf
import pennylane as qml
import mltools, dataset2016, dataset_train_alldb_allmods2
import csv
# from tensorflow.keras.layers import Input,Dense,Conv1D,MaxPool1D,ReLU,Dropout,Softmax,concatenate,Flatten,Reshape,Activation

#set Keras data format as channels_first

(mods, snrs, lbl), (X_train, Y_train, Y_train_hot), (X_val, Y_val, Y_val_hot), (X_test, Y_test, Y_test_hot), (train_idx, val_idx, test_idx), file_num = \

classes = mods
# Set up some params
nb_epoch = 3     # number of epochs to train on
batch_size = 512  # training batch size

n_qubits = 2
n_layers = 2
dev = qml.device("default.qubit", wires=n_qubits)
def qnode(inputs, weights):
    qml.RX(inputs[0], wires=0)
    qml.RX(inputs[1], wires=1)

    qml.RY(weights[0,0], wires=0)
    qml.RY(weights[1,1], wires=1)
    qml.CNOT(wires=[0, 1])
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

# model=mcl.DNN3()
dr =0.2

# print(weight_shapes)
weight_shapes = {"weights": (n_layers, n_qubits)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)

input   = tf.keras.layers.Input(shape=(5,))
Dense_1 = tf.keras.layers.Dense(5)
QNN_1   = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2, name="QNN_1")
output  = tf.keras.layers.Dense(2, activation="softmax")

model = tf.keras.models.Sequential([input, Dense_1, QNN_1, output])


# Train the framework (model)
filepath = 'weights/weights.h5'
history =,
    validation_data=(X_val, Y_val_hot),
    callbacks = [
                keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='auto'),
                keras.callbacks.EarlyStopping(monitor='val_loss', patience=30, verbose=1, mode='auto')

And the full error message below:

Traceback (most recent call last):
  File "E:\Postgraduate\Projects\AMC\AMR-Benchmark-main\HCQDNN3\", line 75, in <module>
    history =,
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\", line 1230, in fit
    callbacks.on_epoch_end(epoch, epoch_logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\", line 413, in on_epoch_end
    callback.on_epoch_end(epoch, logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\", line 1368, in on_epoch_end
    self._save_model(epoch=epoch, batch=None, logs=logs)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\", line 1422, in _save_model, overwrite=True, options=self._options)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\", line 2145, in save
    save.save_model(self, filepath, overwrite, include_optimizer, save_format,
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\", line 145, in save_model
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\", line 110, in save_model_to_hdf5
    model_metadata = saving_utils.model_metadata(model, include_optimizer)
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\", line 148, in model_metadata
    raise e
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\saving\", line 145, in model_metadata
    model_config['config'] = model.get_config()
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\", line 411, in get_config
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\utils\", line 508, in serialize_keras_object
    raise e
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\utils\", line 503, in serialize_keras_object
    config = instance.get_config()
  File "D:\Anaconda\envs\Tensorflow-gpu-python3.8\lib\site-packages\keras\engine\", line 727, in get_config
    raise NotImplementedError('Layer %s has arguments in `__init__` and '
NotImplementedError: Layer KerasLayer has arguments in `__init__` and therefore must override `get_config`.

Why I can’t build the model? I think I obey the rul when I use ‘qml.qnn.KerasLayer’
If I delete the QNN_1 , let model = tf.keras.models.Sequential([input, Dense_1, output]), the code can run successfully.

OMG! I find out the answer! When I installed pennylane, I install pennylane==0.13.0 instead of 0.23.0. Now I can build the hybird network successfully (o゜▽゜)o

1 Like

Hey @AHHil! Welcome to the forum :rocket:

Glad you were able to solve your issue :grin:. Indeed, upgrading your pennylane version is a good thing to check when you encounter issues. You can see the paper trail of new features, improvements, breaking changes, etc., in the release notes: Release notes — PennyLane 0.31.0 documentation

Also, I’m glad you upgraded to v0.23 and your code now works, but we’re on v0.31 as of writing this. Might be worth upgrading again :smile:. There are a lot of improvements to KerasLayer!

:smile:Thank you@isaacdevlugt! It’s my first time to use pennylane. Now I can successful build a simple hybird classical quantum netwok!
But I met a question about the version of pennylane again. Today I intstall pennylane0.31.0. But my hybird network can’t run under the new environment. I didn’t change my code. I don’t know the reason. I uninstall pennylane0.31.0 and install 0.28.0, then my code work again. Now I run my code under pennylane0.28.0 instead of 0.23. Is it necessary to update it to 0.31.0?

It’s not necessary to upgrade to the latest version of PennyLane. If you’re content with the functionality that v0.28 offers, then by all means keep it that way :slight_smile:

No problem! Thank you for your help! :smiley:

1 Like