def matrix_to_density(mat):
 from sympy.physics.quantum.density import Density
 eigen = mat.eigenvects()
 args = [[matrix_to_qubit(Matrix([vector])), x[0]] for x in eigen for vector in x[2] if (x[0] != 0)]
 if (len(args) == 0):
  return 0
 else:
  return Density(*args)

def matrix_to_density ( mat ) : from sympy . physics . quantum . density import Density eigen = mat . eigenvects ( ) args = [ [ matrix_to_qubit ( Matrix ( [ vector ] ) ) , x [ 0 ] ] for x in eigen for vector in x [ 2 ] if ( x [ 0 ] != 0 ) ] if ( len ( args ) == 0 ) : return 0 else : return Density ( * args )
works by finding the eigenvectors and eigenvalues of the matrix .