Hi, I want to utilize the library of multiprocessing to run circuits simultaneously. The toy code is as below:
from multiprocessing import Pool
import pennylane as qml
from pennylane import numpy as np
d_node, weight = param
dev = qml.device("default.qubit", wires=n_wires)
d_node1 = qml.grad(qml.QNode(circuit, dev))
d_node2 = qml.grad(qml.QNode(circuit, dev))
pool = Pool(2)
params = np.random.random(n_wires, requires_grad=True)
result = pool.map(cal_grad, [(d_node1, params), (d_node2, params)])
However, errors happen as below:
AttributeError: 'tensor' object has no attribute 'requires_grad'
Any suggestions are welcome. Thanks in advance!
Hi @Yang and welcome to the forum!
You can use the QnodeCollection class to create a collection of independent Qnodes that can be simultaneously evaluated. The collection can be created as:
qnode = qml.QNodeCollection([qnode1, qnode2])
The qnodes within the QNodeCollection are executed sequentially by default but you can use the
parallel=True keyword argument to activate asynchronous evaluation. However, the best speedup is achieved with external hardware devices or external simulators as explained in further details here under the “Asynchronous Evaluation” section. You may also find this previous discussion helpful. Please feel free to let us know if you have any further questions.
Thanks for you advice. It works for multi circuits. I notice that
QnodeCollection class takes
QNode as input. What about
qml.grad? How could I calculate the gradient of multiple inputs in parallel as demonstrated in the example code above?
If I am not mistaken, computing the gradient in parallel is not possible with the QNodeCollection. But PennyLane integrates nicely with libraries like dask (which is actually used by the QNodeCollection). You should be able to evaluate the
qml.grad function asynchronously with this library…
I hope this helps?
@Yang, In addition to what Maria said, I noticed that in your code copied above, generating parameters explicitly as:
params = [0.54, 0.12]
might help to resolve the AttributeError.
@Maria_Schuld @sjahangiri That’s really helpful. I will try both methods. Thank you!