2

I'm trying to use the following function of QuTip library:

coherent_dm(N=10, alpha = c, offset=0, method='operator')

It's output is a matrix and it's input is a complex number "c", however I want to get the matrices for a list of multiple "c" numbers, but if I define "c" as:

t = np.linspace(0,2*np.pi,100)
c = 2*np.exp(-t*1j) + 0.1*10*(1 - np.exp(-t*1j))

And run the code it gives me "dimension mismatch" error. In this case I tried to run a list of a 100 complex numbers as input "c" expecting an output of a 100 matrices. Any ideas on how to solve this problem would be appreciated.

Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
  • 1
    It appears the argument list of `coherent_dm` can only be one state at a time (http://qutip.org/downloads/4.5.0/qutip-doc-4.5.pdf), so you'll need to run it once per state of interest. – jpf Mar 25 '21 at 22:08
  • Not exactly related to your question, but looking through the source really irritated me, so I submitted this PR to fix the way the parameters are being handled: https://github.com/qutip/qutip/pull/1469 – Mad Physicist Mar 25 '21 at 22:36

1 Answers1

2

coherent_dm returns a qutip.Qobj instance. This is a specialized object that fulfills the __array__ interface, but is not actually a numpy array. Internally, it appears to contain a sparse matrix. That means that you can not readily make coherent_dm return more than one matrix at a time, or even concatenate the results of multiple calls into a single array.

Your best bet is therefore probably to use a list comprehension:

result = [coherent_dm(N=10, alpha=i, offset=0, method='operator') for i in c]
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Thank you very much man I didn't know what a list of comprehension was and not only it works in this case but I think it'll be helpful later too. Again thanks you helped me a lot in my work. – Bruno Piveta Mar 26 '21 at 14:48
  • @BrunoPiveta. Glad it worked out. List comprehensions are a fundamental part of python and worth reading about for a couple of minutes. – Mad Physicist Mar 26 '21 at 14:50