AmplitudeEmbedding error help

Hello,

I get an error for the program below.
Please correct if my attempt is wrong.

Thank you in advance.

Program
import pennylane as qml
from pennylane import numpy as np
from pennylane.templates.embeddings import AmplitudeEmbedding

dev = qml.device(‘default.qubit’, wires=2)

v = np.array([0.29, 0.62, 0.36])

AmplitudeEmbedding(v, wires=[0,1], pad=True, normalize=True)

amp = np.real(dev._state)

print("Amplitude vector: ", np.real(dev._state))

Error

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 @avinash_ch,

As a quantum operation, AmplitudeEmbedding should appear inside of a qnode function, e.g.,

@qml.qnode(dev)
def circuit(params):
    AmplitudeEmbedding(params, ...)
    return qml.expval.PauliZ(0)

Thank you @nathan. I will try that.

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'

Hi @avinash_ch,

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) 

This then produces the expected output:

>>> print(exp_val)
0.6399999999999999

Thank you @josh for your quick response.