Error in MNIST data encoding using default.mixed (WORKING FINE WITH default.qubit)

# Put code here
point,label=generate_data2(train_images[i],train_labels[i])

def generate_data2(img,val):
    """Generate training and testing data."""

    dev = qml.device("default.mixed", wires=8)
    @qml.qnode(dev)
    def circuit(img):

        for i in range(2):
            for j  in range(8):
                qml.RY(img[j,4*i+0][0],j)
            for j  in range(8):

                qml.RX(img[j,4*i+1][0],j)
            for j  in range(8):
                qml.RZ(img[j,4*i+2][0],j)
            for j  in range(8):

                qml.RY(img[j,4*i+3][0],j)
            density_matrices = []
        for i in range(8):
          density_matrices.append(qml.density_matrix([i]))    
        return density_matrices
    return circuit(img),val

# Put full error message here
(60000, 28, 28) (10000, 28, 28)
---------------------------------------------------------------------------
QuantumFunctionError                      Traceback (most recent call last)
<ipython-input-10-44f35fdc5028> in <cell line: 104>()
    103 resy=[]
    104 for i in range(n_test):
--> 105     point=generate_data2(test_images[i])
    106     point = np.array(point)
    107     print(i,"/",n_test,"\r")

9 frames
/usr/local/lib/python3.10/dist-packages/pennylane/_qubit_device.py in statistics(self, circuit, shot_range, bin_size)
    645             elif isinstance(m, StateMP):
    646                 if len(measurements) > 1:
--> 647                     raise qml.QuantumFunctionError(
    648                         "The state or density matrix cannot be returned in combination "
    649                         "with other return types"

QuantumFunctionError: The state or density matrix cannot be returned in combination with other return types
Name: PennyLane
Version: 0.34.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /usr/local/lib/python3.10/dist-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning, PennyLane-qiskit

Platform info:           Linux-6.1.58+-x86_64-with-glibc2.35
Python version:          3.10.12
Numpy version:           1.25.2
Scipy version:           1.11.4
Installed devices:
- qiskit.aer (PennyLane-qiskit-0.34.1)
- qiskit.basicaer (PennyLane-qiskit-0.34.1)
- qiskit.ibmq (PennyLane-qiskit-0.34.1)
- qiskit.ibmq.circuit_runner (PennyLane-qiskit-0.34.1)
- qiskit.ibmq.sampler (PennyLane-qiskit-0.34.1)
- qiskit.remote (PennyLane-qiskit-0.34.1)
- default.gaussian (PennyLane-0.34.0)
- default.mixed (PennyLane-0.34.0)
- default.qubit (PennyLane-0.34.0)
- default.qubit.autograd (PennyLane-0.34.0)
- default.qubit.jax (PennyLane-0.34.0)
- default.qubit.legacy (PennyLane-0.34.0)
- default.qubit.tf (PennyLane-0.34.0)
- default.qubit.torch (PennyLane-0.34.0)
- default.qutrit (PennyLane-0.34.0)
- null.qubit (PennyLane-0.34.0)
- lightning.qubit (PennyLane-Lightning-0.34.0)

Hey @Himanshu_Sahu_Ph.D_C, welcome to the forum :rocket:

QuantumFunctionError: The state or density matrix cannot be returned in combination with other return types

As the error suggests, you cannot return multiple instances of qml.state or (in your case) qml.density_matrix. This QNode in your code is calculating a density matrix object 8 times:

    @qml.qnode(dev)
    def circuit(img):

        for i in range(2):
            for j  in range(8):
                qml.RY(img[j,4*i+0][0],j)
            for j  in range(8):

                qml.RX(img[j,4*i+1][0],j)
            for j  in range(8):
                qml.RZ(img[j,4*i+2][0],j)
            for j  in range(8):

                qml.RY(img[j,4*i+3][0],j)
            density_matrices = []
        for i in range(8):
          density_matrices.append(qml.density_matrix([i]))    
        return density_matrices

I suggest returning the full density matrix (i.e., for all 8 qubits), and then doing any post-processing or partial tracing outside of the QNode (you can use qml.qinfo.reduced_dm for this: qml.qinfo.transforms.reduced_dm — PennyLane 0.34.0 documentation)

Hope this helps!