I wonder whether I can add the variables into the qnode, such as the “layer_num” below:
dev = qml.device("default.qubit", wires = 2)
@qml.qnode(dev)
def qnode(inputs, weights, layer_num):
qml.AmplitudeEmbedding(features=inputs, wires=range(2), normalize=True)
for ii in range(layer_num):
qml.RY(weights[ii][0], wires=0)
qml.RY(weights[ii][1], wires=1)
return qml.probs(wires=list(range(4)))
inputs = torch.range(0,3)
layer_num = 10
weight_shape = {"weights": (layer_num,2)}
qgen = qml.qnn.TorchLayer(qnode, weight_shape)
I got the error message ValueError: Must specify a shape for every non-input parameter in the QNode after running this code. Please help me solve this problem, thank you
For your second code example, you don’t need to specify layer_num since it’s tied to the dimension of weights — simply passing in weights does the trick, then you can just loop over len(weights):
dev = qml.device("default.qubit", wires=2)
@qml.qnode(dev)
def qnode(inputs, weights):
qml.AmplitudeEmbedding(features=inputs, wires=range(2), normalize=True)
for ii in range(len(weights)):
qml.RY(weights[ii][0], wires=0)
qml.RY(weights[ii][1], wires=1)
return qml.probs(wires=list(range(4)))
inputs = torch.range(0, 3)
layer_num = 10
weight_shape = {"weights": (layer_num, 2)}
qgen = qml.qnn.TorchLayer(qnode, weight_shape)
Thank you so much!
But I still have a problem with whether I can give the variables while adding QNode into Torch Layer.
For instance, I only want to apply RY gates on specific wires which depends on the given variable.