"KerasLayer requires a Keras version lower than 3. After downgrading Keras to version 2.3.1, the previous data processing code (not shown) stopped working. Is there any way to resolve the version compatibility issue in the displayed code section?"

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments
n_qubits = 4  # Corresponding to the output dimension of Dense(4)
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev, interface="tf")
def quantum_circuit(inputs, weights):
    # Encode input data (from the output of Dense(4))
    for j in range(n_qubits):
        qml.RY(np.pi * inputs[j], wires=j)
    
    # Apply a custom CNOT structure (fixed pattern)
    ops = [[1, 2, 3], [2, 3, 0], [3, 0, 1], [0, 1, 2]]
    for t in range(4):
        control = t
        targets = ops[t]
        for q in targets:
            qml.CNOT(wires=[control, q])
        qml.Barrier(wires=range(n_qubits))
    
    # Measure the expectation values of all qubits
    return [qml.expval(qml.PauliZ(j)) for j in range(n_qubits)]
weight_shapes = {"weights": (0,)}
qlayer = qml.qnn.KerasLayer(
    quantum_circuit,
    weight_shapes=weight_shapes,
    output_dim=n_qubits
)

If you want help with diagnosing an error, please put the full error message below:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[12], line 2
      1 weight_shapes = {"weights": (0,)}
----> 2 qlayer = qml.qnn.KerasLayer(
      3     quantum_circuit,
      4     weight_shapes=weight_shapes,
      5     output_dim=n_qubits
      6 )

File ~\AppData\Roaming\Python\Python39\site-packages\pennylane\qnn\keras.py:321, in KerasLayer.__init__(self, qnode, weight_shapes, output_dim, weight_specs, **kwargs)
    313     raise ImportError(
    314         "KerasLayer requires TensorFlow version 2 or above. The latest "
    315         "version of TensorFlow can be installed using:\n"
    316         "pip install tensorflow --upgrade\nAlternatively, visit "
    317         "https://www.tensorflow.org/install for detailed instructions."
    318     )
    320 if not CORRECT_KERAS_VERSION:
--> 321     raise ImportError(
    322         "KerasLayer requires a Keras version lower than 3. For instructions on running with Keras 2,"
    323         "visit https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility."
    324     )
    326 self.weight_shapes = {
    327     weight: (tuple(size) if isinstance(size, Iterable) else (size,) if size > 1 else ())
    328     for weight, size in weight_shapes.items()
    329 }
    331 self._signature_validation(qnode, weight_shapes)

ImportError: KerasLayer requires a Keras version lower than 3. For instructions on running with Keras 2,visit https://keras.io/getting_started/#tensorflow--keras-2-backwards-compatibility.

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Hi @hch2984235287,

It looks like your version didn’t downgrade. You will need to restart the kernel for this to happen. Let us know if this fixes your issue.