Convert a Qobj into numpy array

9,549 views
Skip to first unread message

Caco Somoza

unread,
Mar 20, 2014, 8:11:01 AM3/20/14
to qu...@googlegroups.com
Hi there! I just started learning Qutip and my python skills are still novice. I was wondering if there's any Qobj method to convert an operator into a simple numpy array or related. I'm using Spyder since the variable explorer lets me see matrices in a friendly way (studying density matrices in the console is a nightmare). If you guys know any other way to display matrices inline in a neat way I would be very pleased to hear.

Thanks!

Paul Nation

unread,
Mar 20, 2014, 8:22:43 AM3/20/14
to qu...@googlegroups.com
To get a Qobj to an array use: 

Q.full()

Paul
On Mar 20, 2014, at 9:11 PM, Caco Somoza <cacos...@gmail.com> wrote:

Hi there! I just started learning Qutip and my python skills are still novice. I was wondering if there's any Qobj method to convert an operator into a simple numpy array or related. I'm using Spyder since the variable explorer lets me see matrices in a friendly way (studying density matrices in the console is a nightmare). If you guys know any other way to display matrices inline in a neat way I would be very pleased to hear.

Thanks!

--
You received this message because you are subscribed to the Google Groups "qutip" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qutip+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vishvendra Punia

unread,
Mar 20, 2014, 8:49:17 AM3/20/14
to qu...@googlegroups.com
Other useful operations on Quantum Objects. http://qutip.org/docs/2.2.0/guide/guide-basics.html

FunctionCommandDescription
ConjugateQ.conj()Conjugate of quantum object.
Dagger (adjoint)Q.dag()Returns adjoint (dagger) of object.
DiagonalQ.diag()Returns the diagonal elements.
EigenenergiesQ.eigenenergies()Eigenenergies (values) of operator.
EigenstatesQ.eigenstates()Returns eigenvalues and eigenvectors.
ExponentialQ.expm()Matrix exponential of operator.
Full Q.full()Returns full (not sparse) array of Q’s data.
Groundstate Q.groundstate()Eigenval & eigket of Qobj groundstate.
Matrix ElementQ.matrix_element(bra,ket) Matrix element <bra|Q|ket>
NormQ.norm() Returns L2 norm for states, trace norm for operators.
Partial TraceQ.ptrace(sel) Partial trace returning components selected using ‘sel’ parameter.
Sqrt Q.sqrtm()Matrix sqrt of operator.
TidyupQ.tidyup()Removes small elements from Qobj.
TraceQ.tr()Returns trace of quantum object.
TransformQ.transform(inpt)A basis transformation defined by matrix or list of kets ‘inpt’ .
TransposeQ.trans()Transpose of quantum object.
UnitQ.unit()Returns normalized (unit) vector Q/Q.norm().

Joe Bowen

unread,
Sep 30, 2015, 9:18:36 AM9/30/15
to QuTiP: Quantum Toolbox in Python
Hi there,

I want to take the cosine of a Qobj. My way of doing this was to convert it to an numpy array then use np.cos(). How would I then change it back to a Qobj? I would appreciate any help or alternative ways of doing this.

Joe
qobj_numpy.py

nonher...@gmail.com

unread,
Sep 30, 2015, 9:31:26 AM9/30/15
to qu...@googlegroups.com
You would have to take the resulting array and pass it to the Qobj class

Out = Qobj(array)

Of course if you have tensor structure and other things these would have to be passed back to the class as well. 

Paul
--
You received this message because you are subscribed to the Google Groups "QuTiP: Quantum Toolbox in Python" group.

To unsubscribe from this group and stop receiving emails from it, send an email to qutip+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
<qobj_numpy.py>

Joe Bowen

unread,
Sep 30, 2015, 10:18:02 AM9/30/15
to QuTiP: Quantum Toolbox in Python
Yes, so my tensor structure in the original Qobj is;

        dims = [[10, 10], [10, 10]], shape = [100, 100], type = oper, isherm = True

I'm not sure how to pass the appropriate tensor structure back.

        Q.permute(order)

What would I pass into 'order'?

Thanks

nonher...@gmail.com

unread,
Sep 30, 2015, 11:24:49 AM9/30/15
to qu...@googlegroups.com
You can pass that information to the Qobj class just as you typed it. Or just use

Qobj.dims
Qobj.shape

etc from the old Qobj. 

Another possibility is to extract the array from the object and then set the resulting array after calculations as the Qobj data

my_array = Qobj.full()

Do stuff here

Qobj.data = sp.csr_matrix(array)

Where sp stands for the scipy.sparse module. 
--

Alex Pitchford

unread,
Oct 1, 2015, 1:05:26 PM10/1/15
to QuTiP: Quantum Toolbox in Python
You could perhaps try subclassing to create your own Qobj with addition methods, e.g.

class QobjTrig(Qobj):

  def cos(self):

    out = Qobj()

    out.data = sp.csr_matrix(np.cos(self.data))

    out.dims = [self.dims[1], self.dims[0]]

    return out


This probably doesn't offer much over Paul's method unless you will be wanting to use the function over again and perhaps do some other trig functions as well.
Reply all
Reply to author
Forward
0 new messages