Design a swap test classifier with amplitude embedding

i want to design a swap classifier using amplitude encoding method Pennylane. But i got this error:

pennylane._device.DeviceError: Operation QubitStateVector cannot be used after other Operations have already been applied on a default.qubit.autograd device.

Tom explained to me before about the source of issue About amplitude embedding. Here is my code:

@qml.qnode(dev)
def swap_test(x1, x2):

    # load the two inputs into two different registers
    qml.templates.embeddings.AmplitudeEmbedding(x1, wires=[1, 2, 3, 4, 5])
    qml.templates.embeddings.AmplitudeEmbedding(x2, wires=[6, 7, 8, 9, 10])
   
    qml.Hadamard(wires=0)
    
    qml.CSWAP(wires=[0, 1, 6])
    qml.CSWAP(wires=[0, 2, 7])
    qml.CSWAP(wires=[0, 3, 8])
    qml.CSWAP(wires=[0, 4, 9])
    qml.CSWAP(wires=[0, 5, 10])

    qml.Hadamard(wires=0)

    return qml.expval(qml.PauliZ(0))

as you can see the second qml.templates.embeddings.AmplitudeEmbedding(x2, wires=[6, 7, 8, 9, 10]) is at the first of circuit. It should work without error. But it did’t. Do you know how i can solve this problem.

Hi @sassan_moradi! Hmm, this appears to be a bug in PennyLane — it is assuming that only the first operation can be a state preparation operation. Would you mind making a quick issue on the PennyLane GitHub? Thanks!

Sure. i will do it now.

1 Like

@sassan_moradi since the conversation continued on GitHub, I will close this topic for now – feel free to open up a new one for any additional questions!