Quanlse/Utils/Functions.py
def expect(matrix: ndarray, state: ndarray) -> float:
    """
    Return the expectation value of the matrix in the given state.

    :param matrix: the given matrix
    :param state: the given state (1-d state or 2-d density matrix)
    :return: expectation value of the matrix in given state
    """

    def isSquare(m: ndarray) -> bool:
        """
        This function is used to check whether the matrix is a square matrix
        :param m: input matrix
        :return: a bool value
        """
        return all(len(row) == len(m) for row in m)

    # We firstly need to check whether the matrix is square
    if not isSquare(matrix):
        raise ArgumentError('Matrix is not a square matrix')
    if state.shape[0] != matrix.shape[1]:
        raise ArgumentError('Dimension Mismatch')

    # is the input state ket or a density matrix?
    if state.shape[0] != state.shape[1]:
        # the input state is a pure state
        state = state @ state.conj().T
        expectValue = real(trace(dot(matrix, state)))

    else:
        # the input state is a density operator
        expectValue = real(trace(dot(matrix, state)))

    return expectValue

    Return the expectation value of the matrix in the given state.

    :param matrix: the given matrix
    :param state: the given state (1-d state or 2-d density matrix)
    :return: expectation value of the matrix in given state
    
Return the expectation value of the matrix in the given state.
Return the expectation value of the matrix in the given state .
def expect(matrix: ndarray, state: ndarray) -> float:
    def isSquare(m: ndarray) -> bool:
        return all(len(row) == len(m) for row in m)
    if not isSquare(matrix):
        raise ArgumentError('Matrix is not a square matrix')
    if state.shape[0] != matrix.shape[1]:
        raise ArgumentError('Dimension Mismatch')
    if state.shape[0] != state.shape[1]:
        state = state @ state.conj().T
        expectValue = real(trace(dot(matrix, state)))
    else:
        expectValue = real(trace(dot(matrix, state)))
    return expectValue
def expect ( matrix : ndarray , state : ndarray ) -> float : def isSquare ( m : ndarray ) -> bool : """
        This function is used to check whether the matrix is a square matrix
        :param m: input matrix
        :return: a bool value
        """ return all ( len ( row ) == len ( m ) for row in m ) if not isSquare ( matrix ) : raise ArgumentError ( 'Matrix is not a square matrix' ) if state . shape [ 0 ] != matrix . shape [ 1 ] : raise ArgumentError ( 'Dimension Mismatch' ) if state . shape [ 0 ] != state . shape [ 1 ] : state = state @ state . conj ( ) . T expectValue = real ( trace ( dot ( matrix , state ) ) ) else : expectValue = real ( trace ( dot ( matrix , state ) ) ) return expectValue
Return the expectation value of the matrix in the given state.
Return the expectation value of the matrix in the given state .
"""
    Return the expectation value of the matrix in the given state.

    :param matrix: the given matrix
    :param state: the given state (1-d state or 2-d density matrix)
    :return: expectation value of the matrix in given state
    """ """
        This function is used to check whether the matrix is a square matrix
        :param m: input matrix
        :return: a bool value
        """ # We firstly need to check whether the matrix is square # is the input state ket or a density matrix? # the input state is a pure state # the input state is a density operator
expectation value of the matrix in given state
expectation value of the matrix in given state
matrix
ndarray
the given matrix
the given matrix
state
ndarray
the given state (1-d state or 2-d density matrix)
the given state ( 1 - d state or 2 - d density matrix )