Keras 3 Support

Hey team.
I’m not sure what the current state of KerasLayer development for Keras 3 is at, but I have a temp workaround that sorta works with the Keras 3 generic backend prototype.

class DataReuploader(Layer):
    def __init__(self,qnode,qubits,name,sum=False,weight_specs=None,**kwargs):
        weight_shapes = {"weights": (qubits,2)}
        self.qubits = qubits
        if k.backend.backend() =="tensorflow":
            self.circ = qml.qnn.KerasLayer(circuit,weight_shapes,output_dim=qubits)
        elif k.backend.backend() =="torch":
            self.circ = qml.qnn.TorchLayer(circuit,weight_shapes)
        super().__init__(name=name,*kwargs)
        self.sum = sum
        self.name = name
        self.weight_specs = weight_specs if weight_specs is not None else {}

    @property
    def trainable_weights(self):
        return self.circ.trainable_weights

    @property
    def trainable_variables(self):
        return self.circ.trainable_variables

    def get_config(self):
        config = super().get_config()
        config.update(self.circ.get_config())
        config.update({"name":self.name,"sum_units":self.sum})
        return config

    def compute_output_shape(self,input_shape):
           return self.circ.compute_output_shape(input_shape)
            
    def call(self, x):
        x = self.circ(x)
        return x

Essentially a hacky way of leveraging the exist TFKeras and Torch wrappers for qnodes to extend the functionallity to Keras 3.

Currently testing just replacing all the tf.** calls on the source code with keras.op.* to see if that also work with keras 3

Hi @vinayak19th ,

Thanks for sharing this!

I’ve shared it with our PennyLane dev team to see if we can support Keras3.
At the moment we don’t know if we can fully support it but your workaround may indeed help others!

Hi @CatalinaAlbornoz, I’m working on my own to modify the current KerasLayer api to work in Keras 3 with both tensorflow and keras backends. I’ll submit a pull request when it’s done.

Hopefully that’ll help a lot of people and simply further usage

Thanks for making the Pull Request @vinayak19th !

I think it might help a lot of people.

Got it fully working in a jupyter custom layer environment. Needs more work to be integrated into the library itself. Posted it as a demo in the meantime - [New Tutorial] Using Keras 3 with pennylane (including full multi-backend support) by vinayak19th · Pull Request #1555 · PennyLaneAI/qml

Hi @vinayak19th ,

Thank you for following up here. I’ve responded in the other Forum thread that you opened (#9060) so we can keep the conversation there, or in the issue if you decide to open one.