Hello
I’m trying to make a neural networks using the interface Pytorch Pennylane. To do so I have to build a complex pytorch which will have to to have some actions on the device.
I firstly just tried to recode the rotation tutorial but using a model and I end up with an error I can not understand.
import pennylane as qml
import torch
import torch.autograd
import torch.nn as nn
import torch.nn.functional as F
class test_Rot(nn.Module):
def __init__(self, n_wire):
super(test_Rot, self).__init__()
self.n_wire = n_wire
self.phi = torch.tensor(0.011, requires_grad = True)
self.theta = torch.tensor(0.012, requires_grad = True)
def forward(self,):
print(self.phi,self.theta)
qml.RX(self.phi, wires=0)
qml.RY(self.theta, wires=0)
dev1 = qml.device('default.qubit', wires=1)
testcl = test_Rot(dev1,1)
@qml.qnode(dev1, interface='torch')
def circuit(classifier):
classifier()
return qml.expval.PauliZ(0)
print('testcircuit')
print(circuit(testcl))
That’s where it stop, even if the rest of the code should be :
def cost(classifier):
return(circuit() - 1)
opt = torch.optim.Adam([testcl.phi, testcl.theta], lr = 0.1)
for i in range(400):
print(i)
opt.zero_grad()
loss = cost(testcl)
loss.backward()
opt.step()
My error message is:
testcircuit
Traceback (most recent call last):
File "tests.py", line 43, in <module>
print(circuit(testcl))
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\interfaces\torch.py", line 390, in custom_apply
return _TorchQNode.apply(keyword_values, *args)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\interfaces\torch.py", line 307, in forward
res = qnode(*ctx.args, **ctx.kwargs)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\qnode.py", line 457, in __call__
return self.evaluate(args, **kwargs) # args as one tuple
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\autograd\tracer.py", line 48, in f_wrapped
return f_raw(*args, **kwargs)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\qnode.py", line 471, in evaluate
self.construct(args, **kwargs)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\qnode.py", line 274, in construct
variables = unflatten(temp, args)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\utils.py", line 99, in unflatten
res, tail = _unflatten(np.asarray(flat), model)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\utils.py", line 88, in _unflatten
val, flat = _unflatten(flat, x)
File "C:\Users\barthelemy\.conda\envs\penny\lib\site-packages\pennylane\utils.py", line 92, in _unflatten
raise TypeError('Unsupported type in the model: {}'.format(type(model)))
TypeError: Unsupported type in the model: <class '__main__.test_Rot'>
I have put a print at the entrance of the circuit function, the error comes before that.
I’m not sure to see what I did wrong.
Best regards
Barthélémy