Error while converting qnode output to ndarray

Hi @josh,

Please help me with the solution for the following error in the code.

Thanks.

    import pennylane as qml
    from pennylane import numpy as np

    x = random.randint(255, size=(4, 4))
    param = x.reshape(16,).tolist()

    dev = qml.device("default.qubit", wires=16)

    @qml.qnode(dev)
    def circuit(pix):
        for i in range(16):
            qml.RY(pix[i], wires=i)
            
        return [qml.expval(qml.PauliZ(j)) for j in range(16)]

    m = circuit(param)
    print(type(m))
    final = np.ndarray(m)

Output:
    <class 'pennylane.numpy.tensor.tensor'>
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-11-c394eabe543f> in <module>()
         17 m = circuit(param)
         18 print(type(m))
    ---> 19 final = np.ndarray(m)

    TypeError: only integer scalar arrays can be converted to a scalar index

Hi @avinash_ch,

PennyLane uses its own version of numpy under the hood, so the outputs of the circuit (as your print statement shows) are pennylane.numpy.tensor. They behave the same as regular numpy arrays for the most part; but if you’d like to explicitly extract the data as a numpy array, you can use the numpy() function, like so:

final = m.numpy()

Hope that helps, please let us know if you have any further questions!

Hi Olivia Di Matteo (@glassnotes),

That resolved the error. Thanks a lot for your prompt response.

I will get back to you if I have any questions after coding my idea.

No problem, happy to help!