Hello!
Recently, one of my students reached out to me with an error using KerasLayer
. After a brief discussion, I tried her code and got the same error. I updated PennyLane to version 0.35, but there is still the same error.
I took a look at the class using
qml.qnn.KerasLayer??
In a notebook, I tested the code provided in the class description. See above:
import pennylane as qml
import tensorflow as tf
import sklearn.datasets
n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)
@qml.qnode(dev)
def qnode(inputs, weights):
qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))
weight_shapes = {"weights": (3, n_qubits, 3)}
qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
clayer1 = tf.keras.layers.Dense(2)
clayer2 = tf.keras.layers.Dense(2, activation="softmax")
model = tf.keras.models.Sequential([clayer1, qlayer, clayer2])
data = sklearn.datasets.make_moons()
X = tf.constant(data[0])
Y = tf.one_hot(data[1], depth=2)
opt = tf.keras.optimizers.SGD(learning_rate=0.5)
model.compile(opt, loss='mae')
And I obtained the same error as my student. The parameter dynamic: True
. It’s strange because this parameter doesn’t appear in the class. My intuition is around a version problem, but I’m not sure. KerasLayer
isn’t in my toolbox, but it’s the first time I’ve used it. Here is the full output message:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[9], line 16
12 return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))
14 weight_shapes = {"weights": (3, n_qubits, 3)}
---> 16 qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
17 clayer1 = tf.keras.layers.Dense(2)
18 clayer2 = tf.keras.layers.Dense(2, activation="softmax")
File ~/Concordia/lib/python3.10/site-packages/pennylane/qnn/keras.py:324, in KerasLayer.__init__(self, qnode, weight_shapes, output_dim, weight_specs, **kwargs)
320 self.weight_specs = weight_specs if weight_specs is not None else {}
322 self.qnode_weights = {}
--> 324 super().__init__(dynamic=True, **kwargs)
326 # no point in delaying the initialization of weights, since we already know their shapes
327 self.build(None)
File ~/Concordia/lib/python3.10/site-packages/keras/src/layers/layer.py:265, in Layer.__init__(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
263 self._input_shape_arg = input_shape_arg
264 if kwargs:
--> 265 raise ValueError(
266 "Unrecognized keyword arguments "
267 f"passed to {self.__class__.__name__}: {kwargs}"
268 )
270 self.built = False
271 self.dtype_policy = dtype_policies.get(dtype)
ValueError: Unrecognized keyword arguments passed to KerasLayer: {'dynamic': True}
We are not even trying to train the model… Any clue?
My version packages:
Name: PennyLane
Version: 0.35.0
Summary: PennyLane is a cross-platform Python library for quantum computing, quantum machine learning, and quantum chemistry. Train a quantum computer the same way as a neural network.
Home-page: https://github.com/PennyLaneAI/pennylane
Author:
Author-email:
License: Apache License 2.0
Location: /home/ubuntu/Concordia/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane_Lightning
Platform info: Linux-5.15.0-89-generic-x86_64-with-glibc2.35
Python version: 3.10.9
Numpy version: 1.26.0
Scipy version: 1.11.3
Installed devices:
- default.clifford (PennyLane-0.35.0)
- default.gaussian (PennyLane-0.35.0)
- default.mixed (PennyLane-0.35.0)
- default.qubit (PennyLane-0.35.0)
- default.qubit.autograd (PennyLane-0.35.0)
- default.qubit.jax (PennyLane-0.35.0)
- default.qubit.legacy (PennyLane-0.35.0)
- default.qubit.tf (PennyLane-0.35.0)
- default.qubit.torch (PennyLane-0.35.0)
- default.qutrit (PennyLane-0.35.0)
- null.qubit (PennyLane-0.35.0)
- lightning.qubit (PennyLane_Lightning-0.35.0)
Thanks for your time.