Hi @Jerry2001Qu, and thanks @angelinaG for your answer!
We recently introduced a new attribute to the device: dev.num_executions
, which makes it easy to track the number of device executions. You could do this on simulator before trying to run on hardware. This feature can be accessed by installing the development version of PennyLane.
For example, the following shows a benchmark of the number of device executions on a 4-qubit, 6-layer circuit:
import pennylane as qml
import torch
nqubits = 4
nlayers = 6
dev = qml.device("default.qubit", wires=nqubits)
@qml.qnode(dev, interface="torch")
def qcircuit(inputs, weights):
for i in range(nqubits):
qml.Hadamard(wires=i)
qml.RY(inputs[i], wires=i)
qml.templates.BasicEntanglerLayers(weights, wires=range(nqubits))
return [qml.expval(qml.PauliZ(i)) for i in range(nqubits)]
weight_shapes = {"weights": (nlayers, nqubits)}
inputs = torch.ones(nqubits, requires_grad=True)
qlayer = qml.qnn.TorchLayer(qcircuit, weight_shapes)
out = torch.sum(qlayer(inputs))
out.backward()
print(f"Number of executions: {dev.num_executions}")
n_exec_basic = nqubits * nlayers * 2
n_ry = nqubits * 2
n_expected = n_ry + n_exec_basic + 1 # the 1 comes from the forward pass
print(f"Expected number of executions: {dev.num_executions}")
The result is 57 device executions. We can also look at the dressed quantum circuit:
clayer1 = torch.nn.Linear(512, 4)
clayer2 = torch.nn.Linear(4, 2)
hybrid = torch.nn.Sequential(clayer1, qlayer, clayer2)
inputs = torch.ones(512, requires_grad=True)
dev._num_executions = 0
out = torch.sum(qlayer(inputs))
out.backward()
print(f"Number of executions: {dev.num_executions}")
This also gives 57 executions, so it doesn’t look like the hybrid element is increasing things (as expected).
In terms or training on IBMQ, we had a discussion on improving performance on another thread. I would say that this is quite a heavy task for optimization on hardware right now. One thing you could consider is training on simulator and testing (i.e., forward passes, which are much cheaper) on hardware.