Quanlse/Simulator/PulseSimQCQ.py
def computeBlockDiagonalize(mat: ndarray, blockDim: int = 4) -> [ndarray, ndarray]:
    """
    Block diagonalize the qubit-qubit subspace matrix and get the computational subspace block matrix

    :param mat: Qubit-Qubit subspace matrix
    :param blockDim: dimension of the computational subspace matrix (default: 4)

    :return: the block diagonalized matrix and transformation unitary
    """
    matCache = copy.deepcopy(mat)
    eigenVals, eigenVecs = eigenSystem(matCache)
    subVecList = createBasis(mat.shape[0], blockDim)
    indexList = findIndex(eigenVecs, subVecList)

    # Fix findIndex error
    if len(indexList) == 4:
        if indexList[0] == indexList[1]:
            if indexList[0] == mat.shape[0] - 1:
                indexList[0] = mat.shape[0] - 2
            elif indexList[0] == 0:
                indexList[1] = 1
            else:
                leftDiff = abs(eigenVals[indexList[0] - 1] - eigenVals[indexList[0]])
                righrDiff = abs(eigenVals[indexList[0]] - eigenVals[indexList[0] + 1])
                if leftDiff < righrDiff:
                    indexList[0] = indexList[0] - 1
                else:
                    indexList[0] = indexList[0] + 1
        elif indexList[2] == indexList[3]:
            if indexList[2] == mat.shape[0] - 1:
                indexList[2] = mat.shape[0] - 2
            elif indexList[2] == 0:
                indexList[3] = 1
            else:
                leftDiff = abs(eigenVals[indexList[2] - 1] - eigenVals[indexList[2]])
                rightDiff = abs(eigenVals[indexList[2]] - eigenVals[indexList[2] + 1])
                if leftDiff < rightDiff:
                    indexList[2] = indexList[2] - 1
                else:
                    indexList[2] = indexList[2] + 1
    elif len(indexList) == 2:
        if indexList[0] == indexList[1]:
            leftDiff = abs(eigenVals[indexList[0] - 1] - eigenVals[indexList[0]])
            rightDiff = abs(eigenVals[indexList[0]] - eigenVals[indexList[0] + 1])
            if indexList[0] == indexList[1]:
                if indexList[0] == mat.shape[0] - 1:
                    indexList[0] = mat.shape[0] - 2
                elif indexList[0] == 0:
                    indexList[1] = 1
            else:
                if leftDiff < rightDiff:
                    indexList[0] = indexList[0] - 1
                else:
                    indexList[0] = indexList[0] + 1

    matBD, transMat = blockDiag(matCache, indexList)

    return matBD, transMat

    Block diagonalize the qubit-qubit subspace matrix and get the computational subspace block matrix

    :param mat: Qubit-Qubit subspace matrix
    :param blockDim: dimension of the computational subspace matrix (default: 4)

    :return: the block diagonalized matrix and transformation unitary
    
Block diagonalize the qubit-qubit subspace matrix and get the computational subspace block matrix
Block diagonalize the qubit - qubit subspace matrix and get the computational subspace block matrix
def computeBlockDiagonalize(mat: ndarray, blockDim: int = 4) -> [ndarray, ndarray]:
    matCache = copy.deepcopy(mat)
    eigenVals, eigenVecs = eigenSystem(matCache)
    subVecList = createBasis(mat.shape[0], blockDim)
    indexList = findIndex(eigenVecs, subVecList)
    if len(indexList) == 4:
        if indexList[0] == indexList[1]:
            if indexList[0] == mat.shape[0] - 1:
                indexList[0] = mat.shape[0] - 2
            elif indexList[0] == 0:
                indexList[1] = 1
            else:
                leftDiff = abs(eigenVals[indexList[0] - 1] - eigenVals[indexList[0]])
                righrDiff = abs(eigenVals[indexList[0]] - eigenVals[indexList[0] + 1])
                if leftDiff < righrDiff:
                    indexList[0] = indexList[0] - 1
                else:
                    indexList[0] = indexList[0] + 1
        elif indexList[2] == indexList[3]:
            if indexList[2] == mat.shape[0] - 1:
                indexList[2] = mat.shape[0] - 2
            elif indexList[2] == 0:
                indexList[3] = 1
            else:
                leftDiff = abs(eigenVals[indexList[2] - 1] - eigenVals[indexList[2]])
                rightDiff = abs(eigenVals[indexList[2]] - eigenVals[indexList[2] + 1])
                if leftDiff < rightDiff:
                    indexList[2] = indexList[2] - 1
                else:
                    indexList[2] = indexList[2] + 1
    elif len(indexList) == 2:
        if indexList[0] == indexList[1]:
            leftDiff = abs(eigenVals[indexList[0] - 1] - eigenVals[indexList[0]])
            rightDiff = abs(eigenVals[indexList[0]] - eigenVals[indexList[0] + 1])
            if indexList[0] == indexList[1]:
                if indexList[0] == mat.shape[0] - 1:
                    indexList[0] = mat.shape[0] - 2
                elif indexList[0] == 0:
                    indexList[1] = 1
            else:
                if leftDiff < rightDiff:
                    indexList[0] = indexList[0] - 1
                else:
                    indexList[0] = indexList[0] + 1
    matBD, transMat = blockDiag(matCache, indexList)
    return matBD, transMat
def computeBlockDiagonalize ( mat : ndarray , blockDim : int = 4 ) -> [ ndarray , ndarray ] : matCache = copy . deepcopy ( mat ) eigenVals , eigenVecs = eigenSystem ( matCache ) subVecList = createBasis ( mat . shape [ 0 ] , blockDim ) indexList = findIndex ( eigenVecs , subVecList ) if len ( indexList ) == 4 : if indexList [ 0 ] == indexList [ 1 ] : if indexList [ 0 ] == mat . shape [ 0 ] - 1 : indexList [ 0 ] = mat . shape [ 0 ] - 2 elif indexList [ 0 ] == 0 : indexList [ 1 ] = 1 else : leftDiff = abs ( eigenVals [ indexList [ 0 ] - 1 ] - eigenVals [ indexList [ 0 ] ] ) righrDiff = abs ( eigenVals [ indexList [ 0 ] ] - eigenVals [ indexList [ 0 ] + 1 ] ) if leftDiff < righrDiff : indexList [ 0 ] = indexList [ 0 ] - 1 else : indexList [ 0 ] = indexList [ 0 ] + 1 elif indexList [ 2 ] == indexList [ 3 ] : if indexList [ 2 ] == mat . shape [ 0 ] - 1 : indexList [ 2 ] = mat . shape [ 0 ] - 2 elif indexList [ 2 ] == 0 : indexList [ 3 ] = 1 else : leftDiff = abs ( eigenVals [ indexList [ 2 ] - 1 ] - eigenVals [ indexList [ 2 ] ] ) rightDiff = abs ( eigenVals [ indexList [ 2 ] ] - eigenVals [ indexList [ 2 ] + 1 ] ) if leftDiff < rightDiff : indexList [ 2 ] = indexList [ 2 ] - 1 else : indexList [ 2 ] = indexList [ 2 ] + 1 elif len ( indexList ) == 2 : if indexList [ 0 ] == indexList [ 1 ] : leftDiff = abs ( eigenVals [ indexList [ 0 ] - 1 ] - eigenVals [ indexList [ 0 ] ] ) rightDiff = abs ( eigenVals [ indexList [ 0 ] ] - eigenVals [ indexList [ 0 ] + 1 ] ) if indexList [ 0 ] == indexList [ 1 ] : if indexList [ 0 ] == mat . shape [ 0 ] - 1 : indexList [ 0 ] = mat . shape [ 0 ] - 2 elif indexList [ 0 ] == 0 : indexList [ 1 ] = 1 else : if leftDiff < rightDiff : indexList [ 0 ] = indexList [ 0 ] - 1 else : indexList [ 0 ] = indexList [ 0 ] + 1 matBD , transMat = blockDiag ( matCache , indexList ) return matBD , transMat
Block diagonalize the qubit-qubit subspace matrix and get the computational subspace block matrix
Block diagonalize the qubit - qubit subspace matrix and get the computational subspace block matrix
"""
    Block diagonalize the qubit-qubit subspace matrix and get the computational subspace block matrix

    :param mat: Qubit-Qubit subspace matrix
    :param blockDim: dimension of the computational subspace matrix (default: 4)

    :return: the block diagonalized matrix and transformation unitary
    """ # Fix findIndex error
the block diagonalized matrix and transformation unitary
the block diagonalized matrix and transformation unitary
mat
ndarray
Qubit-Qubit subspace matrix
Qubit - Qubit subspace matrix
blockDim
int
dimension of the computational subspace matrix (default: 4)
dimension of the computational subspace matrix ( default : 4 )