When I used it in the pytorch interface, and x = tensor([1]) it works. But when x = tensor(1) it does not work. It fails in this line: for item in x: in the function.
Hi @cubicgate, a couple of comments to get it working.
PennyLane currently only supports measuring each wire only once in the circuit, so you’ll only be able to return one of a0 or a1, but not both.
We also do not yet support default tensor/array arguments for the QNode. You can get around this by setting the default value to None, i.e., params=None, or by making params a positional argument.
Making these two changes, the above script now runs for me:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
import pennylane as qml
from pennylane.ops import *
dev_fock_1 = qml.device('strawberryfields.fock', wires=4, cutoff_dim=7)
num_vars = 24
params_1 = Variable(torch.tensor([0.01]*num_vars), requires_grad=True).view(num_vars,1)
@qml.qnode(dev_fock_1, interface="torch")
def work(x, params=None):
Displacement(x[0], 0, wires=0)
Displacement(x[1], 0, wires=1)
Displacement(x[2], 0, wires=2)
Displacement(x[3], 0, wires=3)
Displacement(params[0][0], params[1][0], wires=0)
a0 = qml.expval(NumberState(np.array([1, 0, 0, 0]), wires=[0, 1, 2, 3]))
return a0
vars_1 = Variable(torch.tensor([0.1, 0.1, 0.1, 0.1]), requires_grad=False)
me = work(vars_1, params=params_1)
Thanks @josh for your code. I tried it and got this error:
Traceback (most recent call last):
File “test_pennylane_1.py”, line 26, in
me = work(vars_1, params=params_1)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\interfaces\torch.py”, line 390, in custom_apply
return _TorchQNode.apply(keyword_values, *args)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\interfaces\torch.py”, line 307, in forward
res = qnode(*ctx.args, **ctx.kwargs)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\qnode.py”, line 514, in call
return self.evaluate(args, **kwargs) # args as one tuple
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\autograd\tracer.py”, line 48, in f_wrapped
return f_raw(*args, **kwargs)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\qnode.py”, line 591, in evaluate
ret = self.device.execute(self.queue, self.ev)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane_device.py”, line 208, in execute
self.apply(operation.name, operation.wires, operation.parameters)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\operation.py”, line 389, in parameters
return _unflatten(temp_val, self.params)[0]
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\utils.py”, line 89, in _unflatten
val, flat = _unflatten(flat, x)
File “C:\Users\wei\AppData\Local\Programs\Python\Python36\lib\site-packages\pennylane\utils.py”, line 93, in _unflatten
raise TypeError(‘Unsupported type in the model: {}’.format(type(model)))
TypeError: Unsupported type in the model: <class ‘pennylane.variable.Variable’>