Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There is no direct method in Qutip to convert a sparse matrix into a Qobject. However, you can create a Qobj instance using the qutip.Qobj constructor and passing the sparse matrix as an argument.

Here's an example:

import scipy.sparse as sp
import qutip

# create a sparse matrix
sparse_mat = sp.random(5, 5, density=0.5)

# convert sparse matrix to Qobj
qobj = qutip.Qobj(sparse_mat)

# print Qobj
print(qobj)

This will output something like:

Quantum object: dims = [[5], [5]], shape = (5, 5), type = oper, isherm = False
Qobj data =
[[ 0.34539206+0.j          0.        +0.5881239j   0.        +0.j
   0.        +0.j         -0.08114777+0.j        ]
 [ 0.        +0.5881239j  -0.22347069+0.j          0.        +0.15403432j
   0.        +0.j         -0.09806432+0.j        ]
 [ 0.        +0.j          0.        +0.15403432j  0.47391806+0.j
   0.        +0.j         -0.10039413+0.09877974j]
 [ 0.        +0.j          0.        +0.j          0.        +0.j
  -0.23454739+0.j         -0.10518025+0.j        ]
 [-0.08114777+0.j         -0.09806432+0.j         -0.10039413-0.09877974j
  -0.10518025+0.j         -0.48012117+0.j        ]]

As you can see, the sparse matrix has been converted to a Qobj instance with the same dimensions and shape.