Search code examples
pythonnumpyscipyqutip

IndexError for scientific Python code


I have been working on some code that does integration, some manipulation, and then more integration. Here is the code (thanks @JRichardSnape!). Basically this code solves a matrix equation, which is what mesolve does. It takes in a Hamiltonian (a key physical matrix), an initial density matrix, rho0, and a tlist of times to evaluate rho(t) at (what we are solving for) and the collapse operators, L1, L2, L3, L4, L5, L6, L7. Then I extract the results and multiply by two other arrays and plot it.

I use the qutip quantum mechanics module since they have the solver I need: mesolve. The qutip module requires all matrices to be converted into a quantum object, which is done by Qobj(x).

I have added a line defining rho0=L1 right after the collapse operators definition. When this happens, it gives me an Index error:

IndexError: index 0 is out of bounds for axis 0 with size 0

I have tried to find out what is wrong. It goes wrong during the definition of f_t. The problem is when it is trying to index the array (n.data is a single element array of a complex128 number). What is going on?


Solution

  • This reproduces your error:

    In [34]: data = np.zeros((0,10))    
    In [35]: data
    Out[35]: array([], shape=(0, 10), dtype=float64)
    
    In [36]: data[0]
    ---------------------------------------------------------------------------
    IndexError                                Traceback (most recent call last)
    <ipython-input-36-88cae4a5bf31> in <module>()
    ----> 1 data[0]
    
    IndexError: index 0 is out of bounds for axis 0 with size 0
    

    Without delving to the the linked code, it is clear that the array in question has a 0 length 1st dimension. x.shape[0] is 0.

    You might get such an array be indexing another with an empty list,

    In [44]: data=np.ones((3,4))
    
    In [45]: data[[],...]
    Out[45]: array([], shape=(0, 4), dtype=float64)
    

    With the limited information you give it's hard to be more specific. Check the shape of all the suspected arrays.


    So Qobj is documented in http://qutip.org/docs/2.2.0/apidoc/classes.html

    and mesolve in http://qutip.org/docs/2.2.0/apidoc/functions.html#qutip.mesolve.mesolve

    and rho0 is expected to be rho0 : qutip.qobj.

    The underlying array for ground is a (7,1) shape, for L1 (the problem rho0?) is (7,7) and all 0's except for [0,0].

    Looks like this is a spin off of Integration not successful in Python QuTiP