-2

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?

hpaulj
  • 221,503
  • 14
  • 230
  • 353
TanMath
  • 598
  • 1
  • 9
  • 27

1 Answers1

3

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

Community
  • 1
  • 1
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • 2
    Then you need to clarify your question. I explained the likely situation that is causing the error. I can't tell from the link, and the skimpy description, what line produced the error, or what variable might be involved. – hpaulj Aug 22 '15 at 03:35
  • A single element array would give a different error, `np.array(1.23)[0]` `0-d arrays cannot be index`, or no error `np.array([12.323])[0]`. – hpaulj Aug 22 '15 at 03:51
  • I clarified, is it better? – TanMath Aug 22 '15 at 22:53
  • 1
    Not much. I don't need to know the big picture; it's the details surrounding the faulty variable that matter. Things like portions of the stack trace, shape and type of variables, code with the problem area highlighted. There are plenty of SO examples of good debugging questions. – hpaulj Aug 22 '15 at 23:24
  • I'll make a guess. Early on the code defines `rho0 = ground*ground.dag()`, and you say the problem arose when you added `rho0=L1`. `L1` is a `Qobj` (or the result of calling that), so is `ground`. I don't know anything about `Qobj`. I don't know whether it is an array subclass, or whether it has a shape or allows indexing. But on the surface those two definitions of `rho0` do not look compatible. – hpaulj Aug 22 '15 at 23:27
  • Qobj is a class... Qobj(x) converts x, an array or list or matrix, into the Qobj object... – TanMath Aug 22 '15 at 23:32