I’ve recently started using pennylane. Thanks for the great tool!
I am trying (unsuccessfully) to build a custom Keras layer in TensorFlow 2 that includes a QNode. The goal is to use QNodes in a “standard” TensorFlow 2 workflow, i.e. define a Keras model, compile and fit. The Keras model should first process input data via a classical NN, then feed it into the parameters of a quantum circuit (all inside the model).
My first attempt failed due to AttributeError: 'Tensor' object has no attribute 'numpy', which seems to be related to pennylane assuming eager mode but Keras assuming that layers can be both executed as a graph and in eager.
Then I tried using dynamic=True in the layer init to tell Keras that the layer only works with eager, but this fails with NotImplementedError: in Keras.
Hi @Matthias_Rosenkranz! You’re right that PennyLane only currently works with TensorFlow eager (not autograph), so dynamic=True is currently required when using Keras.
I haven’t used PennyLane with Keras personally, however I can link you a few resources that have:
On our tutorials page, we have quite a few tutorials using PyTorch and TensorFlow directly, but none using Keras so far, this would be a great addition if you wanted to add a demonstration once your code is working!
For reference, here is a working Colab including the Keras-style training via compile and fit. It’s not a very useful model in itself as it just fits a classical NN to output a parameter for R_y that flips the initial |0\rangle state.
We have now added a new KerasLayer feature to PennyLane that converts QNodes to a Keras Layer. This should make it easy to interface more with other elements of Keras.
You can access this feature by installing the latest version of PennyLane: