# Making quanvolutional neural net weights trainable

**URL:** <https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509>\
**Category:** PennyLane Help\
**Created:** [August 7, 2020, 9:24am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509 "2020-08-07T09:24:07Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![dymat](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dymat/32/383_2.png) [@dymat](https://discuss.pennylane.ai/u/dymat)\
**Post date:** [November 20, 2020, 8:39am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/21 "2020-11-20T08:39:59Z")

</div>

Hi @glassnotes,

I updated the code in the post above [post above](http://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/19).

It should work now out of the box. If not, check out my repo at github: [https://github.com/dymat/TrainableQuanvolution](https://github.com/dymat/TrainableQuanvolution)

Best regards,  
~dymat

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [November 20, 2020, 9:56am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/22 "2020-11-20T09:56:18Z")

</div>

@dymat thank you for posting the code I will try to run it and get back to you asap 🙂

---

<div class="post-metadata">

**Author:** ![Tom\_Bromley](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/tom_bromley/32/129_2.png) [@Tom\_Bromley](https://discuss.pennylane.ai/u/Tom_Bromley)\
**Post date:** [November 20, 2020, 10:44pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/23 "2020-11-20T22:44:36Z")

</div>

Thanks @dymat, it’s very cool that you had some positive results training on MNIST (at least for the 1 quantum layer). If you’re interested, I think this would make a great demo! We have just launched a [community space](https://pennylane.ai/qml/demos_community.html) for demos. Since you already have a Jupyter Notebook, is should just be a case of creating an issue [here](https://github.com/PennyLaneAI/qml/issues) (see [instructions](https://pennylane.ai/qml/demos_submission.html)).

On to your question about the gradient for two layers, I had a quick look and unfortunately nothing jumped out as a problem. I might have a chance to dig a bit deeper next week, however for now I would suggest some debugging. For example, what if we swapped out the quantum circuit (`self.circuit`) with a simple [torch function](https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)? If you still aren’t getting a gradient in the first layer, it’s probably an issue with the big `for` loop in `forward()`. Otherwise, we can look at if it’s an issue on the PL side.

Hope this helps a bit!  
Tom

---

<div class="post-metadata">

**Author:** ![vijpandaturtle](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/vijpandaturtle/32/156_2.png) [@vijpandaturtle](https://discuss.pennylane.ai/u/vijpandaturtle)\
**Post date:** [November 23, 2020, 5:32am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/24 "2020-11-23T05:32:52Z")

</div>

@dymat went through your code and ran it a few times with some changes. I haven’t found the source of the gradient problem yet. But will still keep looking and update if i find something!

---

<div class="post-metadata">

**Author:** ![dymat](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dymat/32/383_2.png) [@dymat](https://discuss.pennylane.ai/u/dymat)\
**Post date:** [November 23, 2020, 8:18am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/25 "2020-11-23T08:18:04Z")

</div>

Hi @Tom_Bromley,

publishing our notebook as a demo sounds cool. I would use the one-layer example. Can we change the demo notebook later on, if we find the source of the gradient problem in two-layer networks?

Best regards,  
D

---

<div class="post-metadata">

**Author:** ![dymat](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dymat/32/383_2.png) [@dymat](https://discuss.pennylane.ai/u/dymat)\
**Post date:** [November 23, 2020, 2:29pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/26 "2020-11-23T14:29:26Z")

</div>

> [@dymat](#):
>
> Can we change the demo notebook later on, if we find the source of the gradient problem in two-layer networks?

Ok, I’ve checked the instructions and see that my question is obsolete, since the code lives in my repo.

---

<div class="post-metadata">

**Author:** ![glassnotes](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/glassnotes/32/327_2.png) [@glassnotes](https://discuss.pennylane.ai/u/glassnotes)\
**Post date:** [November 23, 2020, 9:18pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/27 "2020-11-23T21:18:28Z")

</div>

Hi @dymat, awesome, we’re looking forward to seeing your demo! Let us know if you have any further questions while getting it ready.

---

<div class="post-metadata">

**Author:** ![dymat](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/dymat/32/383_2.png) [@dymat](https://discuss.pennylane.ai/u/dymat)\
**Post date:** [November 23, 2020, 10:32pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/28 "2020-11-23T22:32:03Z")

</div>

Hi @glassnotes, the demo request is submitted ([https://github.com/PennyLaneAI/qml/issues/165](https://github.com/PennyLaneAI/qml/issues/165)).

Best regards,  
D

---

<div class="post-metadata">

**Author:** ![gojiita\_ku](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/gojiita_ku/32/566_2.png) [@gojiita\_ku](https://discuss.pennylane.ai/u/gojiita_ku)\
**Post date:** [February 17, 2023, 3:45pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/29 "2023-02-17T15:45:55Z")

</div>

Hi @dymat @Tom_Bromley , has this issue of quantum layer 0 not getting any gradients been solved? Any update?

---

<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 24, 2023, 5:40pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/30 "2023-02-24T17:40:03Z")

</div>

Hi @gojiita_ku, thank you for your question and sorry for the delay in responding!  
I haven’t seen gradient issues recently. Are you experiencing a similar issue?

If so, please share with us a minimal version of your code, the output of `qml.about()`, and your full error traceback.

If you run our [quanvolutional neural networks demo](https://pennylane.ai/qml/demos/tutorial_quanvolution.html) as-is, does it work properly?

I’ll be looking out for your answer!

---

<div class="post-metadata">

**Author:** ![gojiita\_ku](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/gojiita_ku/32/566_2.png) [@gojiita\_ku](https://discuss.pennylane.ai/u/gojiita_ku)\
**Post date:** [February 26, 2023, 5:06pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/32 "2023-02-26T17:06:48Z")

</div>

> [@Tom\_Bromley](#):
>
> Thanks @dymat, it’s very cool that you had some positive results training on MNIST (at least for the 1 quantum layer). If you’re interested, I think this would make a great demo! We have just launched a [community space](https://pennylane.ai/qml/demos_community.html) for demos. Since you already have a Jupyter Notebook, is should just be a case of creating an issue [here](https://github.com/PennyLaneAI/qml/issues) (see [instructions](https://pennylane.ai/qml/demos_submission.html)).
> 
> On to your question about the gradient for two layers, I had a quick look and unfortunately nothing jumped out as a problem. I might have a chance to dig a bit deeper next week, however for now I would suggest some debugging. For example, what if we swapped out the quantum circuit (`self.circuit`) with a simple [torch function](https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)? If you still aren’t getting a gradient in the first layer, it’s probably an issue with the big `for` loop in `forward()`. Otherwise, we can look at if it’s an issue on the PL side.
> 
> Hope this helps a bit!  
> Tom

Hi @Tom_Bromley and @CatalinaAlbornoz,

Could you please give a code example in this case about how to swap out the quantum circuit (`self.circuit` ) with a simple [torch function](https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)as suggested by Tom?

> For example, what if we swapped out the quantum circuit (`self.circuit` ) with a simple [torch function](https://pytorch.org/docs/stable/autograd.html#torch.autograd.Function)?

Many thanks!

---

<div class="post-metadata">

**Author:** ![gojiita\_ku](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/gojiita_ku/32/566_2.png) [@gojiita\_ku](https://discuss.pennylane.ai/u/gojiita_ku)\
**Post date:** [February 27, 2023, 9:49am UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/33 "2023-02-27T09:49:17Z")

</div>

Hi @CatalinaAlbornoz. I run [this notebook](https://github.com/dymat/TrainableQuanvolution/blob/main/StackedQuanvolutions.ipynb) and found parameters in both of two QonvLayers were updated successfully every epoch. But if I change the code a little bit (e.g. build the model using `nn.Module` instead of `nn.Sequential`), I found parameters in the first QonvLayer were not updated. I don’t know the reason. I’ve uploaded my notebook file. Could you please take a look if you have free time? Many thanks!

[StackedQuanvolutions.py](https://discuss.pennylane.ai/uploads/short-url/aBGFmlwoohNsNpGNzS1x0masiiG.py) (5.3 KB)

Here is the output of` qml.about()`:

```auto
Name: PennyLane
Version: 0.28.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /Users/chenyixiong/opt/anaconda3/lib/python3.9/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, retworkx, scipy, semantic-version, toml
Required-by: PennyLane-Lightning, PennyLane-qiskit, pennylane-qulacs
Platform info: macOS-10.16-x86_64-i386-64bit
Python version: 3.9.13
Numpy version: 1.21.5
Scipy version: 1.9.1
Installed devices:
- lightning.qubit (PennyLane-Lightning-0.28.2)
- qiskit.aer (PennyLane-qiskit-0.28.0)
- qiskit.basicaer (PennyLane-qiskit-0.28.0)
- qiskit.ibmq (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.28.0)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.28.0)
- qulacs.simulator (pennylane-qulacs-0.28.0)
- default.gaussian (PennyLane-0.28.0)
- default.mixed (PennyLane-0.28.0)
- default.qubit (PennyLane-0.28.0)
- default.qubit.autograd (PennyLane-0.28.0)
- default.qubit.jax (PennyLane-0.28.0)
- default.qubit.tf (PennyLane-0.28.0)
- default.qubit.torch (PennyLane-0.28.0)
- default.qutrit (PennyLane-0.28.0)
- null.qubit (PennyLane-0.28.0)

```

---

<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:** [June 21, 2023, 9:12pm UTC](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509/34 "2023-06-21T21:12:22Z")

</div>

Hi @gojiita_ku,

I’m really sorry I somehow didn’t see your question when you asked it. Are you still having the same problem? Do you still need our help?

[Previous page](https://discuss.pennylane.ai/t/making-quanvolutional-neural-net-weights-trainable/509.md?page=1)
