What I am trying to do is first take an image and encode it into quantum states, for this I have taken an image from the MNIST dataset and then reshaped it to (4,4) and now I wrote the following circuit to prepare quantum states for it.


def quantum_encoding(image,params):

  for i in range(4):


  return qml.state()

Now I wanted to create a function that could take in two quantum states and then perform a swap test between them and return fidelity. So I tried it in the following manner.

def swap(state1,state2):


  for i in range(4):

  return qml.state()

An issue i faced was that I got this error -

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

As the output of the first function quantum_encoding is an 16 bit number or maybe like a 4qubit state ,

first I would like to know if my attempt to perform swap test between the two quantum encoded states is correct or not?

second I would like to know how to perform fidelity calculations with this and how to avoid the error?


1 Answer 1


As the error suggests, you cannot use QubitStateVector twice in the same QNode. Take, for instance, this simpler example that reproduces your error. Here, I want to initialize qubit 1 and 2 in a zero state:

import pennylane as qml
from pennylane import numpy as np


def circuit(state1, state2):
    qml.QubitStateVector(state1, wires=0)
    qml.QubitStateVector(state2, wires=1)

    return qml.state()

state1 = np.array([1,0])
state2 = np.array([1,0])

print(circuit(state1, state2))

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

What you must do is take the kronecker/tensor product between the separate states and feed that into one instance of QubitStateVector. In this case, the tensor product of both zero states is simply [1,0,0,0]:

def circuit(state):
    qml.QubitStateVector(state, wires=range(2))
    return qml.state()

state = np.array([1,0,0,0])


# [1.+0.j 0.+0.j 0.+0.j 0.+0.j]

That said, this might be a cool feature to have in PennyLane (i.e., being able to use QubitStateVector more than once in one QNode provided that the wires don't overlap). Maybe you can make a feature request, or even contribute to PennyLane yourself!

  • $\begingroup$ yea that works .But my goal is to find the fidelity between the two states obtained after performing SWAP test between the quantum states of the encoded image and hence I wanted to like try if I could get the density matrices of the 4 states (the one's on the 4 ancillary qubits) and then concatenate first two which would correspond to image_1 and then the other 2 correspond to image_2 and then I thought of using qml.math.fidelity between the two density matrices, but now I get the error,The state or density matrix cannot be returned in combination with other return types ,any workaround for it $\endgroup$
    – Pratyush
    Mar 13, 2023 at 16:30
  • $\begingroup$ Sorry,but I am kinda new to pennylane and quantum computing. What I have tried is,I did what you told,i found the kronecker product between the state1 and state2 and made modifications as you suggested, and I returned a density matrix on the 4 ancillary qubits. The output of this was a 16,16 shaped array. We will call this array as density_matrix. Now to this array i used qml.expval(qml.Hermitian(density_matrix,wires=[0,1,2,3]) this gave me my fidelity, I would like to know if this method is correct if not could you correct me and provide with a sample code if possible $\endgroup$
    – Pratyush
    Mar 13, 2023 at 17:13
  • $\begingroup$ No worries at all for being new to PennyLane @Pratyush! We all have to start from ground zero. You should be able to calculate the fidelity between two density matrices with qml.math.fidelity. There are some examples in that link. I think the error you were getting is stemming from you wanting to return multiple things from a QNode that cannot be returned simultaneously. $\endgroup$
    – isaac
    Mar 13, 2023 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.