# Quantum circuit between classical layers with Tensorflow

**URL:** <https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821>\
**Category:** PennyLane Help\
**Created:** [February 4, 2021, 8:11pm UTC](https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821 "2021-02-04T20:11:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Ellis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/james_ellis/32/159_2.png) [@James\_Ellis](https://discuss.pennylane.ai/u/James_Ellis)\
**Post date:** [February 4, 2021, 8:11pm UTC](https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821/1 "2021-02-04T20:11:23Z")

</div>

@josh I am working on converting the transfer learning demo to Tensorflow. Could you possibly provide me with a very basic example of how to implement a quantum circuit in between two classical layers in Tensorflow? Thanks!

---

<div class="post-metadata">

**Author:** ![antalszava](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/antalszava/32/171_2.png) [@antalszava](https://discuss.pennylane.ai/u/antalszava)\
**Post date:** [February 5, 2021, 2:34pm UTC](https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821/2 "2021-02-05T14:34:14Z")

</div>

Hi @James_Ellis,

This can be a good case for using Keras in TensorFlow with [`KerasLayer` in PennyLane](https://pennylane.readthedocs.io/en/stable/code/api/pennylane.qnn.KerasLayer.html?highlight=keraslayer#pennylane.qnn.KerasLayer). The example in the docs defines a quantum circuit, creates a `qml.qnn.KerasLayer` and adds it to before a classical layer.

Working further on this, the example under `Usage Details` can give a good starting point for your specific case:

```python
import pennylane as qml
import tensorflow as tf
import sklearn.datasets

n_qubits = 2
dev = qml.device("default.qubit", wires=n_qubits)

@qml.qnode(dev)
def qnode(inputs, weights):
    qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
    qml.templates.StronglyEntanglingLayers(weights, wires=range(n_qubits))
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))

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

qlayer = qml.qnn.KerasLayer(qnode, weight_shapes, output_dim=2)
clayer1 = tf.keras.layers.Dense(2)
clayer2 = tf.keras.layers.Dense(2, activation="softmax")
model = tf.keras.models.Sequential([clayer1, qlayer, clayer2])

data = sklearn.datasets.make_moons()
X = tf.constant(data[0])
Y = tf.one_hot(data[1], depth=2)

opt = tf.keras.optimizers.SGD(learning_rate=0.5)
model.compile(opt, loss='mae')

```

Also suggest checking out some relevant other threads on the forum, e.g., [QNN KerasLayer with Model](http://discuss.pennylane.ai/t/qnn-keraslayer-with-model/413/2).

Hope this helps, let us know if you’d have further questions!

---

<div class="post-metadata">

**Author:** ![James\_Ellis](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/james_ellis/32/159_2.png) [@James\_Ellis](https://discuss.pennylane.ai/u/James_Ellis)\
**Post date:** [February 6, 2021, 9:32pm UTC](https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821/3 "2021-02-06T21:32:58Z")

</div>

Thanks for the help!

Can backpropagation instead of finite-difference be used with a KerasLayer?

---

<div class="post-metadata">

**Author:** ![josh](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/josh/32/100_2.png) [@josh](https://discuss.pennylane.ai/u/josh)\
**Post date:** [February 7, 2021, 2:57pm UTC](https://discuss.pennylane.ai/t/quantum-circuit-between-classical-layers-with-tensorflow/821/4 "2021-02-07T14:57:48Z")

</div>

> [@James\_Ellis](#):
>
> Can backpropagation instead of finite-difference be used with a KerasLayer?

Yep, with the latest version of PennyLane, the `KerasLayer` class supports backprop 🙂
