Quanlse/Utils/Functions.py
def blockDiag(matrix: ndarray, subIndex: List[int]):
    """
    Block diagonalize a given matrix using the principle of least action.

    :param matrix: The given matrix to be block diagonalized.
    :param subIndex: The indexes of sub-system.

    :return: Block diagonalization Matrix and transform Unitary
    """

    if matrix.shape[0] is not matrix.shape[1]:
        raise ArgumentError('Not a square matrix')

    if len(subIndex) > max(matrix.shape):
        raise ArgumentError(f'Number of indexes {len(subIndex)} exceeds matrix dimension len{matrix}')

    # Calculate the eigenvectors and eigenvalues of matrix.
    valsInit, vecsInit = linalg.eig(matrix)

    # Rearrange eigenvalues and corresponding eigenvectors in the ascending order.
    index = valsInit.argsort()
    vecs = vecsInit[:, index]
    vals = valsInit[index]

    s1 = vecs[0:len(subIndex), subIndex]
    s2 = vecs[len(subIndex):len(matrix), subIndex]
    X = -dagger(s2 @ linalg.inv(s1))

    XDag = X.T.conj()
    mat1 = block([identity(X.shape[0]), X])
    mat2 = block([-XDag, identity(XDag.shape[0])])
    U = block([[mat1], [mat2]])
    T = U @ linalg.sqrtm(linalg.inv(dagger(U) @ U))

    # Compute the block-diagonal matrix
    matBD = dagger(T) @ matrix @ T

    return matBD, T

    Block diagonalize a given matrix using the principle of least action.

    :param matrix: The given matrix to be block diagonalized.
    :param subIndex: The indexes of sub-system.

    :return: Block diagonalization Matrix and transform Unitary
    
Block diagonalize a given matrix using the principle of least action.
Block diagonalize a given matrix using the principle of least action .
def blockDiag(matrix: ndarray, subIndex: List[int]):
    if matrix.shape[0] is not matrix.shape[1]:
        raise ArgumentError('Not a square matrix')
    if len(subIndex) > max(matrix.shape):
        raise ArgumentError(f'Number of indexes {len(subIndex)} exceeds matrix dimension len{matrix}')
    valsInit, vecsInit = linalg.eig(matrix)
    index = valsInit.argsort()
    vecs = vecsInit[:, index]
    vals = valsInit[index]
    s1 = vecs[0:len(subIndex), subIndex]
    s2 = vecs[len(subIndex):len(matrix), subIndex]
    X = -dagger(s2 @ linalg.inv(s1))
    XDag = X.T.conj()
    mat1 = block([identity(X.shape[0]), X])
    mat2 = block([-XDag, identity(XDag.shape[0])])
    U = block([[mat1], [mat2]])
    T = U @ linalg.sqrtm(linalg.inv(dagger(U) @ U))
    matBD = dagger(T) @ matrix @ T
    return matBD, T
def blockDiag ( matrix : ndarray , subIndex : List [ int ] ) : if matrix . shape [ 0 ] is not matrix . shape [ 1 ] : raise ArgumentError ( 'Not a square matrix' ) if len ( subIndex ) > max ( matrix . shape ) : raise ArgumentError ( f'Number of indexes {len(subIndex)} exceeds matrix dimension len{matrix}' ) valsInit , vecsInit = linalg . eig ( matrix ) index = valsInit . argsort ( ) vecs = vecsInit [ : , index ] vals = valsInit [ index ] s1 = vecs [ 0 : len ( subIndex ) , subIndex ] s2 = vecs [ len ( subIndex ) : len ( matrix ) , subIndex ] X = - dagger ( s2 @ linalg . inv ( s1 ) ) XDag = X . T . conj ( ) mat1 = block ( [ identity ( X . shape [ 0 ] ) , X ] ) mat2 = block ( [ - XDag , identity ( XDag . shape [ 0 ] ) ] ) U = block ( [ [ mat1 ] , [ mat2 ] ] ) T = U @ linalg . sqrtm ( linalg . inv ( dagger ( U ) @ U ) ) matBD = dagger ( T ) @ matrix @ T return matBD , T
Block diagonalize a given matrix using the principle of least action.
Block diagonalize a given matrix using the principle of least action .
"""
    Block diagonalize a given matrix using the principle of least action.

    :param matrix: The given matrix to be block diagonalized.
    :param subIndex: The indexes of sub-system.

    :return: Block diagonalization Matrix and transform Unitary
    """ # Calculate the eigenvectors and eigenvalues of matrix. # Rearrange eigenvalues and corresponding eigenvectors in the ascending order. # Compute the block-diagonal matrix
Block diagonalization Matrix and transform Unitary
Block diagonalization Matrix and transform Unitary
matrix
ndarray
The given matrix to be block diagonalized.
The given matrix to be block diagonalized .
subIndex
List[int]
The indexes of sub-system.
The indexes of sub - system .