# Hybrid model: mismatch between output of Qnode and input of next classical layer

**URL:** <https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453>\
**Category:** PennyLane Help\
**Created:** [November 2, 2021, 3:37pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453 "2021-11-02T15:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NikSchet](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nikschet/32/786_2.png) [@NikSchet](https://discuss.pennylane.ai/u/NikSchet)\
**Post date:** [November 2, 2021, 3:37pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453/1 "2021-11-02T15:37:35Z")

</div>

Hello

I have a Hybrid (classical - quantum) model for classification:

_1st_ layer is a classical layer with **6-neurons**  
_2nd_ layer is a standard quantum node with **6-qubits**  
_3d_ decision layer is a **2-neuron** classical layer with a sigmoid function

The Quantum node is the same as in the tutorial “[https://pennylane.ai/qml/demos/tutorial\_qnn\_module\_tf.html](https://pennylane.ai/qml/demos/tutorial_qnn_module_tf.html)” but with 6 qubits.

It works fine but i would expect it not to work. Instead it makes sense to insert a classical layer of 6-neurons prior to the final decision layer.

My question is, should the output dimensions of the Qnode match the dimensions of the next classical layer? and if there is a mismatch how pennylane deals with it? thank you very much in advance!!!

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [November 2, 2021, 8:12pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453/2 "2021-11-02T20:12:43Z")

</div>

Hi @NikSchet, thanks for the question 🙂

In general, you’d likely want the output dimensions of the QNode to match the dimensions of the next classical layer. So if you had a final layer with 2 neurons, you would want to pass a two-dimensional tensor to that layer. You can create such a two-dimensional tensor from a QNode by having it return two measurements (e.g., expectation values of different observables). Note that it is not required that just because you have 6 qubits that the output of your QNode needs necessarily be 6-dimensional.

If you are able to provide a (condensed) version of your code, in particular showing the layer structure, it would potentially help us give a more specific answer 🙂

---

<div class="post-metadata">

**Author:** ![NikSchet](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nikschet/32/786_2.png) [@NikSchet](https://discuss.pennylane.ai/u/NikSchet)\
**Post date:** [November 2, 2021, 9:43pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453/3 "2021-11-02T21:43:15Z")

</div>

Ok let me try to provide the architecture:

I use this as a Qnode:

> ```
> @qml.qnode(dev, interface="tf", grad_method="backprop")
> def qnode(inputs, weights):
> for i in range(blocks):
> qml.templates.AngleEmbedding(inputs, wires=range(n_qubits))
> qml.templates.StronglyEntanglingLayers(weights[i], wires=range(n_qubits)) #STRONGLY ENTANGLING LAYERS
> return [qml.expval(qml.PauliZ(i)) for i in range(n_qubits)]
> 
> ```

The layers are:

clayer1 = tf.keras.layers.Dense(16, activation=“relu”)  
clayer2 = tf.keras.layers.Dense(6, activation=“relu”)  
qlayer = qml.qnn.KerasLayer(qnode, weight\_shapes, 6)  
clayerD = tf.keras.layers.Dense(1, activation=“sigmoid”)

And the model is defined as:

model = tf.keras.models.Sequential([clayer1,clayer2,qlayer,clayerD])  
So dimensions are (16neurons,6neurons,6qubits,1neuron)

In this case can you explain the procedure in which the outcome of the qlayer (6 qubits) is imported into the decision layer (1 neuron) ?

so your suggestion should be to use :

model=tf.keras.models.Sequential([clayer1,clayer2,qlayer,clayer2,clayerD])

Thank you for the fast reply!

---

<div class="post-metadata">

**Author:** ![nathan](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nathan/32/85_2.png) [@nathan](https://discuss.pennylane.ai/u/nathan)\
**Post date:** [November 3, 2021, 6:56pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453/4 "2021-11-03T18:56:22Z")

</div>

Hi @NikSchet,

Thanks for providing a snippet of your code. From this, I can see that you have properly specified a 6-dimensional `output_dim` to your `qml.qnn.KerasLayer`, which matches the output delivered by your QNode.

As for any “magic” on how things get connected together, this is actually all happening in `Keras`. From what I understand by looking at their [docs](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Dense), you only need to provide an output shape, and the input shape is inferred automatically. So in this case, when you do `tf.keras.layers.Dense(1, activation="sigmoid")`, you are saying that you want a dense layer with a _1D output_.

The necessary input shape is put together under the hood by Keras by looking at what layer proceeds `clayerD` in the input of `Sequential` (in your case, a layer with output dimension 6). So Keras infers that your final dense layer will have shape (6,1).

```nohighlight
model=tf.keras.models.Sequential([clayer1,clayer2,qlayer,clayer2,clayerD])

```

Nope, I didn’t really suggest that. What you were already doing seems to be correct, even if you didn’t quite know the details of what happens under the hood 🙂

---

<div class="post-metadata">

**Author:** ![NikSchet](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/nikschet/32/786_2.png) [@NikSchet](https://discuss.pennylane.ai/u/NikSchet)\
**Post date:** [November 3, 2021, 7:15pm UTC](https://discuss.pennylane.ai/t/hybrid-model-mismatch-between-output-of-qnode-and-input-of-next-classical-layer/1453/5 "2021-11-03T19:15:30Z")

</div>

ok noted! Thank you very much for your time! much appreciated 🙂
