Quanlse/Utils/Functions.py
def population(rho: ndarray, subNum: int, dimList: List[int], plot=False) -> dict:
    """
    Output a dictionary to show population of multi-qubit matrix

    :param rho: density matrix
    :param subNum: number of qubits
    :param dimList: the dimension of each subsystem
    :param plot: an option to plot population
    :return: a dictionary illustrate population of each energy level
    """

    a = rho.shape
    if a[0] != a[1]:
        raise ArgumentError("The input matrix is invalid")

    if subNum != len(dimList):
        raise ArgumentError("Invalid dimList: dimList is inconsistent with subNum")

    dim = 1
    for j in range(len(dimList)):
        if dimList[j] == 1 or 0:
            raise ArgumentError("Invalid dimList: Subsystem cannot be one or zero dimension")
        else:
            dim *= dimList[j]

    if dim != a[0]:
        raise ArgumentError("Invalid dimList: dimList is inconsistent with rho's dimension")

    # generate a complete sequence of population
    maxi = max(dimList)
    l_ = zeros(maxi, dtype=int)
    for i in range(maxi):
        l_[i] = '%d' % i
    comp = array(list(product(l_, repeat=subNum)))

    # valuing invalid population
    for i in range(maxi ** subNum):
        for j in range(subNum):
            if comp[i][j] >= dimList[j]:
                comp[i] = 100

    def is_none(n):
        return n[0] == 100

    # filter invalid population, generating a complete sequence of valid population
    pop_name = itertools.filterfalse(is_none, list(comp))
    list_ = list(pop_name)

    popDict = {}

    P = []
    popList = []
    popName = []
    for k in range(dim):
        P.append(basis(dim, k) @ dagger(basis(dim, k)))

    for k in range(dim):
        atr = ''.join(str(i) for i in list_[k])
        pop = abs(trace(rho @ P[k]))
        popList.append(pop)
        popDict[atr] = pop
        popName.append(atr)

    if plot is True:
        # Draw the population of computational basis
        plotPop(popName, popList, xLabel="Computational Basis", yLabel="Population")

    return popDict

    Output a dictionary to show population of multi-qubit matrix

    :param rho: density matrix
    :param subNum: number of qubits
    :param dimList: the dimension of each subsystem
    :param plot: an option to plot population
    :return: a dictionary illustrate population of each energy level
    
Output a dictionary to show population of multi-qubit matrix
Output a dictionary to show population of multi - qubit matrix
def population(rho: ndarray, subNum: int, dimList: List[int], plot=False) -> dict:
    a = rho.shape
    if a[0] != a[1]:
        raise ArgumentError("The input matrix is invalid")
    if subNum != len(dimList):
        raise ArgumentError("Invalid dimList: dimList is inconsistent with subNum")
    dim = 1
    for j in range(len(dimList)):
        if dimList[j] == 1 or 0:
            raise ArgumentError("Invalid dimList: Subsystem cannot be one or zero dimension")
        else:
            dim *= dimList[j]
    if dim != a[0]:
        raise ArgumentError("Invalid dimList: dimList is inconsistent with rho's dimension")
    maxi = max(dimList)
    l_ = zeros(maxi, dtype=int)
    for i in range(maxi):
        l_[i] = '%d' % i
    comp = array(list(product(l_, repeat=subNum)))
    for i in range(maxi ** subNum):
        for j in range(subNum):
            if comp[i][j] >= dimList[j]:
                comp[i] = 100
    def is_none(n):
        return n[0] == 100
    pop_name = itertools.filterfalse(is_none, list(comp))
    list_ = list(pop_name)
    popDict = {}
    P = []
    popList = []
    popName = []
    for k in range(dim):
        P.append(basis(dim, k) @ dagger(basis(dim, k)))
    for k in range(dim):
        atr = ''.join(str(i) for i in list_[k])
        pop = abs(trace(rho @ P[k]))
        popList.append(pop)
        popDict[atr] = pop
        popName.append(atr)
    if plot is True:
        plotPop(popName, popList, xLabel="Computational Basis", yLabel="Population")
    return popDict
def population ( rho : ndarray , subNum : int , dimList : List [ int ] , plot = False ) -> dict : a = rho . shape if a [ 0 ] != a [ 1 ] : raise ArgumentError ( "The input matrix is invalid" ) if subNum != len ( dimList ) : raise ArgumentError ( "Invalid dimList: dimList is inconsistent with subNum" ) dim = 1 for j in range ( len ( dimList ) ) : if dimList [ j ] == 1 or 0 : raise ArgumentError ( "Invalid dimList: Subsystem cannot be one or zero dimension" ) else : dim *= dimList [ j ] if dim != a [ 0 ] : raise ArgumentError ( "Invalid dimList: dimList is inconsistent with rho's dimension" ) maxi = max ( dimList ) l_ = zeros ( maxi , dtype = int ) for i in range ( maxi ) : l_ [ i ] = '%d' % i comp = array ( list ( product ( l_ , repeat = subNum ) ) ) for i in range ( maxi ** subNum ) : for j in range ( subNum ) : if comp [ i ] [ j ] >= dimList [ j ] : comp [ i ] = 100 def is_none ( n ) : return n [ 0 ] == 100 pop_name = itertools . filterfalse ( is_none , list ( comp ) ) list_ = list ( pop_name ) popDict = { } P = [ ] popList = [ ] popName = [ ] for k in range ( dim ) : P . append ( basis ( dim , k ) @ dagger ( basis ( dim , k ) ) ) for k in range ( dim ) : atr = '' . join ( str ( i ) for i in list_ [ k ] ) pop = abs ( trace ( rho @ P [ k ] ) ) popList . append ( pop ) popDict [ atr ] = pop popName . append ( atr ) if plot is True : plotPop ( popName , popList , xLabel = "Computational Basis" , yLabel = "Population" ) return popDict
Output a dictionary to show population of multi-qubit matrix
Output a dictionary to show population of multi - qubit matrix
"""
    Output a dictionary to show population of multi-qubit matrix

    :param rho: density matrix
    :param subNum: number of qubits
    :param dimList: the dimension of each subsystem
    :param plot: an option to plot population
    :return: a dictionary illustrate population of each energy level
    """ # generate a complete sequence of population # valuing invalid population # filter invalid population, generating a complete sequence of valid population # Draw the population of computational basis
a dictionary illustrate population of each energy level
a dictionary illustrate population of each energy level
rho
ndarray
None
subNum
int
number of qubits
number of qubits
dimList
List[int]
the dimension of each subsystem
the dimension of each subsystem
plot
an option to plot population
an option to plot population