# "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?"

**URL:** <https://discuss.pennylane.ai/t/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/7995>\
**Category:** PennyLane Help\
**Created:** [February 10, 2025, 8:01pm UTC](https://discuss.pennylane.ai/t/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/7995 "2025-02-10T20:01:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hch2984235287](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/hch2984235287/32/3422_2.png) [@hch2984235287](https://discuss.pennylane.ai/u/hch2984235287)\
**Post date:** [February 10, 2025, 8:01pm UTC](https://discuss.pennylane.ai/t/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/7995/1 "2025-02-10T20:01:43Z")

</div>

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

```auto
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:

```auto
---------------------------------------------------------------------------
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()`.

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [February 12, 2025, 12:11am UTC](https://discuss.pennylane.ai/t/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/7995/2 "2025-02-12T00:11:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vinayak19th](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vinayak19th/32/1805_2.png) [@vinayak19th](https://discuss.pennylane.ai/u/vinayak19th)\
**Post date:** [October 6, 2025, 7:18am UTC](https://discuss.pennylane.ai/t/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/7995/3 "2025-10-06T07:18:35Z")

</div>

You can try upgrading to keras 3 using this [[New Tutorial] Using Keras 3 with pennylane (including full multi-backend support) by vinayak19th · Pull Request #1555 · PennyLaneAI/qml · GitHub](https://github.com/PennyLaneAI/qml/pull/1555)
