# How to use Dask to parallelize QNode computations?

**URL:** <https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101>\
**Category:** PennyLane Help\
**Created:** [June 8, 2021, 9:21am UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101 "2021-06-08T09:21:00Z")\
**Posts on this page:** 7\
**Page:** 2

<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:** [August 4, 2021, 12:54pm UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/21 "2021-08-04T12:54:01Z")

</div>

Hi @gojiita_ku,

No worries!

It seems, that in this specific case the following could be adjusted:  
`@qml.qnode(dev, interface="torch",diff_method='adjoint',mutable=False)`.

I.e., we’d like to have `diff_method='adjoint'` be passed to `qml.qnode`, instead of `method='adjoint'`.

If we’re not passing `diff_method='adjoint'`, then the unknown keyword is ignored and we’re using the parameter-shift rule with the Torch interface.

Using the adjoint method resulted in a runtime of about ~110 seconds for me locally.

---

<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:** [August 5, 2021, 8:27am UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/22 "2021-08-05T08:27:10Z")

</div>

> It seems, that in this specific case the following could be adjusted:  
> `@qml.qnode(dev, interface="torch",diff_method='adjoint',mutable=False)` .
> 
> I.e., we’d like to have `diff_method='adjoint'` be passed to `qml.qnode` , instead of `method='adjoint'` .
> 
> If we’re not passing `diff_method='adjoint'` , then the unknown keyword is ignored and we’re using the parameter-shift rule with the Torch interface.
> 
> Using the adjoint method resulted in a runtime of about ~110 seconds for me locally.

Hi @antalszava,

Thanks so much for pointing out the error I made! Yes, after replacing `method='adjoint'` with `diff_method='adjoint'`, the new code also resulted in a runtime of about ~ 120 seconds in my local environment! This is an obvious speedup!

However, compared to classical image segmentation models, the quantum part still takes much more time (270s vs 0.16s for batch size of 2 in my case). So may I ask two more questions below?

1. Is there any other method to further reduce the run time of this quantum version of convolution? For example, could we exploit vectorization for pennylane?

2. If we use device as `default.qubit.tf`, intall tensorflow with gpu support and rebuild the model using tensorflow, would this quantum convolution be executed on gpu?

---

<div class="post-metadata">

**Author:** ![Romain\_Moyard](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/romain_moyard/32/567_2.png) [@Romain\_Moyard](https://discuss.pennylane.ai/u/Romain_Moyard)\
**Post date:** [August 5, 2021, 7:35pm UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/23 "2021-08-05T19:35:12Z")

</div>

Hi @gojiita_ku,  
Your questions are welcome!

1. I am looking into it and I will come back to you tomorrow.
2. Yes you are right, you can use `default.qubit.tf` with `diff_method=backprop` it supports classical back propagation on GPU.

---

<div class="post-metadata">

**Author:** ![Romain\_Moyard](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/romain_moyard/32/567_2.png) [@Romain\_Moyard](https://discuss.pennylane.ai/u/Romain_Moyard)\
**Post date:** [August 6, 2021, 1:36pm UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/24 "2021-08-06T13:36:05Z")

</div>

Hey @gojiita_ku,

For the first question, I spoke with my colleague @jmarrazola and unfortunately we do no think that there is a straightforward way of further further speeding up the execution of the present code. Indeed at the moment simulating quantum circuits is harder than implementing neural networks.

For vectorization techniques it would probably need to happen at the level of how the device operates, which would require changing most of the code base.

Feel free to ask any other questions!

---

<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:** [August 9, 2021, 2:49pm UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/25 "2021-08-09T14:49:15Z")

</div>

Hi @Romain_Moyard,

Thanks so much for your reply! I’m asking this question because I would like to make this type of hybrid quantum-classical models leap out of the lab and be more practical in certain real-world scenarios (e.g. medical imaging). Personally, I find the biggest obstacle I am facing is how to optimize the computation time of quantum parts of the model. Anyway, thanks for all of your supports! I believe this issue can be resolved soon in the future with some new features of Pennylane. 😃

By the way, may I further confirm one thing? Even though `default.qubit.tf` supports back-propagation method like TensorFlow, the computation of quantum circuits is still not based on vectorization, right? I mean quantum circuits in this case are still executed sequentially in for loops.

---

<div class="post-metadata">

**Author:** ![jmarrazola](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/jmarrazola/32/255_2.png) [@jmarrazola](https://discuss.pennylane.ai/u/jmarrazola)\
**Post date:** [August 9, 2021, 4:06pm UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/26 "2021-08-09T16:06:03Z")

</div>

Hi @gojiita_ku,

That’s right, devices in pennylane don’t perform a vectorized simulation of quantum circuits. Instead, the approach is to create a tape representing the circuit and subsequently apply each operation in the tape to an initial state vector. This follows essentially the same logic required to execute circuits on hardware.

Your suggestion of a fully-vectorized simulator is intriguing. In principle this should be possible, but would require a large change to how simulators are designed and built. It’s also not clear that the gain would be significant 🤔

Let us know if there’s anything else we can do to help!

Best,

Juan Miguel

---

<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:** [August 10, 2021, 4:07am UTC](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101/27 "2021-08-10T04:07:59Z")

</div>

Hi @jmarrazola,

Thank you so much for confirming my doubts! I’ll let you know if I got more questions about Pennylane. 😃

[Previous page](https://discuss.pennylane.ai/t/how-to-use-dask-to-parallelize-qnode-computations/1101.md?page=1)
