PennyLaneAI / pennylane

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.
https://pennylane.ai
Apache License 2.0
2.21k stars 576 forks source link

[BUG] `ValueError: Unrecognized keyword arguments passed to KerasLayer: {'dynamic': True}` #5608

Open Top-Gun-Maxverick opened 2 months ago

Top-Gun-Maxverick commented 2 months ago

Expected behavior

Successful creation of a KerasLayer

Actual behavior

ValueError: Unrecognized keyword arguments passed to KerasLayer: {'dynamic': True}

Additional information

This issue arises when using Pennylane version 0.30+ and Keras 3.

Source code

return qml.qnn.KerasLayer(V8136, weight_shapes={"weights": (layers, qubits)}, output_dim=16)

class Encoder(keras.Model):
    def __init__(self, qubits: int, L: int, **kwargs):
        super().__init__(**kwargs)
        self.embed = EquivarEmbed(qubits=16, layers=L)
        self.linear = layers.Dense(units=2*latent_dim, activation="relu", name="dense")
        self.dropout = layers.Dropout(rate=0.2, name="dropout")
        self.z_mean = layers.Dense(units=latent_dim, activation="relu", name="z_mean")
        self.log_var = layers.Dense(units=latent_dim, activation="relu", name="log_var")

    def call(self, inputs):
        adjacency = self.embed(inputs)
        x = self.linear(adjacency)
        x = self.dropout(x)
        z_mean = self.z_mean(x)
        log_var = self.log_var(x)
        return [z_mean, log_var]

E = Encoder(qubits=16, L=1)

Tracebacks

ValueError                                Traceback (most recent call last)
<ipython-input-93-0785c3d4688a> in <cell line: 1>()
----> 1 E = Encoder(qubits=16, L=1)
      2 E.build(input_shape=(1, adj_shape, adj_shape))

3 frames
<ipython-input-91-74aa4fd00ddb> in __init__(self, qubits, L, **kwargs)
      2     def __init__(self, qubits: int, L: int, **kwargs):
      3         super().__init__(**kwargs)
----> 4         self.embed = EquivarEmbed(qubits=16, layers=L)
      5         self.linear = layers.Dense(units=2*latent_dim, activation="relu", name="dense")
      6         self.dropout = layers.Dropout(rate=0.2, name="dropout")

<ipython-input-90-17e156e41642> in EquivarEmbed(qubits, layers)
     16         return [qml.expval(qml.PauliX(i)) for i in range(qubits)]
     17 
---> 18     return qml.qnn.KerasLayer(V8136, weight_shapes={"weights": (layers, qubits)}, output_dim=16)

/usr/local/lib/python3.10/dist-packages/pennylane/qnn/keras.py in __init__(self, qnode, weight_shapes, output_dim, weight_specs, **kwargs)
    322         self.qnode_weights = {}
    323 
--> 324         super().__init__(**kwargs)
    325 
    326         # no point in delaying the initialization of weights, since we already know their shapes

/usr/local/lib/python3.10/dist-packages/keras/src/layers/layer.py in __init__(self, activity_regularizer, trainable, dtype, autocast, name, **kwargs)
    262             self._input_shape_arg = input_shape_arg
    263         if kwargs:
--> 264             raise ValueError(
    265                 "Unrecognized keyword arguments "
    266                 f"passed to {self.__class__.__name__}: {kwargs}"

ValueError: Unrecognized keyword arguments passed to KerasLayer: {'dynamic': True}

System information

Name: PennyLane
Version: 0.35.1
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: /usr/local/lib/python3.10/dist-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-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- default.clifford (PennyLane-0.35.1)
- default.gaussian (PennyLane-0.35.1)
- default.mixed (PennyLane-0.35.1)
- default.qubit (PennyLane-0.35.1)
- default.qubit.autograd (PennyLane-0.35.1)
- default.qubit.jax (PennyLane-0.35.1)
- default.qubit.legacy (PennyLane-0.35.1)
- default.qubit.tf (PennyLane-0.35.1)
- default.qubit.torch (PennyLane-0.35.1)
- default.qutrit (PennyLane-0.35.1)
- null.qubit (PennyLane-0.35.1)
- lightning.qubit (PennyLane_Lightning-0.35.1)

Existing GitHub issues

albi3ro commented 2 months ago

Thanks for opening the issue @Top-Gun-Maxverick . Looks like this same problem was also discussed in our forum:

https://discuss.pennylane.ai/t/keraslayer-error-parameter-dynamic-true/4253/4

It seems like the solution for now is downgrading tensorflow.

We have been investigating adding Keras 3 support, but have not gotten it working quite yet.