Hi, I am trying to train a simple hybrid network with a quantum layer composed of 3 strongly entangling layers on 4 qubits connected to a classical layer with 2 output neurons. I am using pytorch. The problem is that it is really slow to obtain gradients of this network and I am using the simulator, not quantum hardware, so i suppose that backpropagation is being used.
If I just use the quantum layer and the pennylane optimizers, the training is fast and i notice a big difference in using default.qubit.autograd rather than default.qubit. In the hybrid network however, i didn’t notice any difference. When i use the qulacs simulator, instead of faster results, it gets even worse.
Any suggestions would be appreciated.
Thanks for your help!
class Hybrid_Network(nn.Module):
def __init__(self, nqubits, nlayers , output_size):
super(Hybrid_Network, self).__init__()
device = qml.device('default.qubit', wires=nqubits)
@qml.qnode(device)
def qnode(inputs,weights):
qml.templates.AngleEmbedding(inputs, wires=range(nqubits))
#strongly entangling layer - weights = {(n_layers , n_qubits, n_parameters)}
qml.templates.StronglyEntanglingLayers(weights,wires=range(nqubits))
#return expectation value
return [qml.expval(qml.PauliZ(i)) for i in range(nqubits)]
# weights of the quantum layer are randomly initialized by PyTorch using the uniform distribution over [0,2pi]
#{(n_layers , n_qubits, n_parameters)}
weight_shapes = {"weights":(nlayers,N,3)}
self.qlayer = qml.qnn.TorchLayer(qnode,weight_shapes)
self.clayer = nn.Linear(4,output_size)
self.hybrid_network = nn.Sequential(
self.qlayer,
self.clayer
)
def forward(self, input):
return self.hybrid_network(input)