File “/usr/local/lib/python3.6/dist-packages/PennyLane-0.6.0.dev0-py3.6.egg/pennylane/operation.py”, line 334, in init
if set(wires) != set(range(QNode._current_context.num_wires)):
AttributeError: ‘NoneType’ object has no attribute ‘num_wires’
Hi @nathan…
Is there still a mistake in my attempt? Please help.
Thank you.
Program
import pennylane as qml
from pennylane import numpy as np
from pennylane.templates.embeddings import AmplitudeEmbedding
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def testAmp(v):
AmplitudeEmbedding(v, wires=[0,1], pad=True, normalize=True)
return qml.expval.PauliZ(0)
v = np.array([0.4, 0.5, 0.3])
exp_val = testAmp(v)
print(exp_val)
**Error**
File "/usr/local/lib/python3.6/dist-packages/PennyLane-0.6.0.dev0-py3.6.egg/pennylane/templates/embeddings.py", line 141, in AmplitudeEmbedding
if normalize and np.linalg.norm(features, 2) != 1:
File "/home/avinash/.local/lib/python3.6/site-packages/numpy/linalg/linalg.py", line 2450, in norm
sqnorm = dot(x, x)
TypeError: unsupported operand type(s) for +: 'Variable' and 'Variable'
This is because the AmplitudeEncoding function accepts non-differentiable data (the state you want to embed). In PennyLane, differentiable parameters are always passed by postional arguments, while non-differentiable parameters are passed via keyword arguments.
So changing the QNode to pass a keyword argument to AmplitudeEncoding works:
import pennylane as qml
from pennylane import numpy as np
from pennylane.templates.embeddings import AmplitudeEmbedding
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev)
def testAmp(data=None):
AmplitudeEmbedding(data, wires=[0,1], pad=True, normalize=True)
return qml.expval(qml.PauliZ(0))
v = np.array([0.4, 0.5, 0.3])
exp_val = testAmp(data=v)