(matrix)
return result
Convert from the matrix repr. to a sum of Qubit objects.

    Parameters
    ----------
    matrix : Matrix, numpy.matrix, scipy.sparse
        The matrix to build the Qubit representation of. This works with
        sympy matrices, numpy matrices and scipy.sparse sparse matrices.

    Examples
    --------

    Represent a state and then go back to its qubit form:

        >>> from sympy.physics.quantum.qubit import matrix_to_qubit, Qubit
        >>> from sympy.physics.quantum.gate import Z
        >>> from sympy.physics.quantum.represent import represent
        >>> q = Qubit('01')
        >>> matrix_to_qubit(represent(q))
        |01>
Convert from the matrix repr. to a sum of Qubit objects.
Convert from the matrix repr . to a sum of Qubit objects .
def matrix_to_qubit(matrix):
    """Convert from the matrix repr. to a sum of Qubit objects.

    Parameters
    ----------
    matrix : Matrix, numpy.matrix, scipy.sparse
        The matrix to build the Qubit representation of. This works with
        sympy matrices, numpy matrices and scipy.sparse sparse matrices.

    Examples
    --------

    Represent a state and then go back to its qubit form:

        >>> from sympy.physics.quantum.qubit import matrix_to_qubit, Qubit
        >>> from sympy.physics.quantum.gate import Z
        >>> from sympy.physics.quantum.represent import represent
        >>> q = Qubit('01')
        >>> matrix_to_qubit(represent(q))
        |01>
    """
    # Determine the format based on the type of the input matrix
    format = 'sympy'
    if isinstance(matrix, numpy_ndarray):
        format = 'numpy'
    if isinstance(matrix, scipy_sparse_matrix):
        format = 'scipy.sparse'

    # Make sure it is of correct dimensions for a Qubit-matrix representation.
    # This logic should work with sympy, numpy or scipy.sparse matrices.
    if matrix.shape[0] == 1:
        mlistlen = matrix.shape[1]
        nqubits = log(mlistlen, 2)
        ket = False
        cls = QubitBra
    elif matrix.shape[1] == 1:
        mlistlen = matrix.shape[0]
        nqubits = log(mlistlen, 2)
        ket = True
        cls = Qubit
    else:
        raise QuantumError(
            'Matrix must be a row/column vector, got %r' % matrix
        )
    if not isinstance(nqubits, Integer):
        raise QuantumError('Matrix must be a row/column vector of size '
                           '2**nqubits, got: %r' % matrix)
    # Go through each item in matrix, if element is non-zero, make it into a
    # Qubit item times the element.
    result = 0
    for i in range(mlistlen):
        if ket:
            element = matrix[i, 0]
        else:
            element = matrix[0, i]
        if format == 'numpy' or format == 'scipy.sparse':
            element = complex(element)
        if element != 0.0:
            # Form Qubit array; 0 in bit-locations where i is 0, 1 in
            # bit-locations where i is 1
            qubit_array = [int(i & (1 << x) != 0) for x in range(nqubits)]
            qubit_array.reverse()
            result = result + element*cls(*qubit_array)

    # If sympy simplified by pulling out a constant coefficient, undo that.
    if isinstance(result, (Mul, Add, Pow)):
        result = result.expand()

    return result
def matrix_to_qubit ( matrix ) : # Determine the format based on the type of the input matrix format = 'sympy' if isinstance ( matrix , numpy_ndarray ) : format = 'numpy' if isinstance ( matrix , scipy_sparse_matrix ) : format = 'scipy.sparse' # Make sure it is of correct dimensions for a Qubit-matrix representation. # This logic should work with sympy, numpy or scipy.sparse matrices. if matrix . shape [ 0 ] == 1 : mlistlen = matrix . shape [ 1 ] nqubits = log ( mlistlen , 2 ) ket = False cls = QubitBra elif matrix . shape [ 1 ] == 1 : mlistlen = matrix . shape [ 0 ] nqubits = log ( mlistlen , 2 ) ket = True cls = Qubit else : raise QuantumError ( 'Matrix must be a row/column vector, got %r' % matrix ) if not isinstance ( nqubits , Integer ) : raise QuantumError ( 'Matrix must be a row/column vector of size ' '2**nqubits, got: %r' % matrix ) # Go through each item in matrix, if element is non-zero, make it into a # Qubit item times the element. result = 0 for i in range ( mlistlen ) : if ket : element = matrix [ i , 0 ] else : element = matrix [ 0 , i ] if format == 'numpy' or format == 'scipy.sparse' : element = complex ( element ) if element != 0.0 : # Form Qubit array; 0 in bit-locations where i is 0, 1 in # bit-locations where i is 1 qubit_array = [ int ( i & ( 1 << x ) != 0 ) for x in range ( nqubits ) ] qubit_array . reverse ( ) result = result + element * cls ( * qubit_array ) # If sympy simplified by pulling out a constant coefficient, undo that. if isinstance ( result , ( Mul , Add , Pow ) ) : result = result . expand ( ) return result