KerasLayer Import Issue

Hello!

Recently, I am encountering with an error using KerasLayer. I tried the same code last week and got the imported the package. But from yesterday, I’m unable to import the KerasLayer from pennylane. Changing to custom keraslayer also leads to more errors and have also tried using the torch, but still it is not achieving the expected output and causes more errors.

#
import pennylane as qml
from pennylane.qnn.keras import KerasLayer

IMG_SIZE = (224,224)
N_QUBITS = 8
dev = qml.device("default.qubit", wires=N_QUBITS)

@qml.qnode(dev, interface="tf")
def quantum_circuit(inputs, weights):
    qml.templates.AngleEmbedding(inputs, wires=range(N_QUBITS))
    qml.templates.StronglyEntanglingLayers(weights, wires=range(N_QUBITS), ranges=[3, 3, 3])
    return [qml.expval(qml.PauliZ(i)) for i in range(N_QUBITS)]

weight_shapes = {"weights": (3, N_QUBITS, 3)}

qlayer = KerasLayer(quantum_circuit, weight_shapes=weight_shapes, output_dim=N_QUBITS)

Attaching the error i got,

# PennyLane: 0.38.0
NumPy: 1.23.5
TensorFlow: 2.12.0
--------------------------------------------------------------------------- 
ModuleNotFoundError                       Traceback (most recent call last)
/tmp/ipython-input-1-2185407428.py in <cell line: 0>()
----> 1 from pennylane.qnn.tensorflow import KerasLayer
      2 print("✅ KerasLayer loaded successfully!")

ModuleNotFoundError: No module named 'pennylane.qnn.tensorflow'

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

Kindly help me with this and let me know how to import the KerasLayer. I’m running this code in google colab.

Hi @Nathimalar_C , welcome to the Forum!

A few things are happening here.

On one hand the version of PennyLane you’re using is quite old. We’re now at PennyLane v0.42 while you’re using PennyLane v0.38. This makes it harder to use PennyLane since the docs and demos may not work with the version you’re using. You can install the latest PennyLane version in Colab with !pip install pennylane.

On the other hand, the KerasLayer class in qml.qnn.keras has been removed in PennyLane v0.42 because Keras 2 is no longer actively maintained (see our deprecations page for info on other deprecations and removals).

So the solution here is to move to Torch or JAX. If you’re familiar with JAX I would recommend this as the best option since it will give you more options for compilation which is increasingly important. If you prefer to use Torch here’s an example based on your code:

import pennylane as qml

IMG_SIZE = (224,224) # This image size is big. It may take a while to train. Better start smaller.
N_QUBITS = 8
dev = qml.device("default.qubit", wires=N_QUBITS)

@qml.qnode(dev) # No need to set the interface, this is automatic based on the data
def quantum_circuit(inputs, weights):
    qml.AngleEmbedding(inputs, wires=range(N_QUBITS))
    qml.StronglyEntanglingLayers(weights, wires=range(N_QUBITS), ranges=3) # If the range is always 3 there's no need to specify it three times
    return [qml.expval(qml.PauliZ(i)) for i in range(N_QUBITS)]

weight_shapes = {"weights": (3, N_QUBITS, 3)}

qlayer = qml.qnn.TorchLayer(quantum_circuit, weight_shapes=weight_shapes) # Remove output_dim

Let me know if this helps!