def get_estimate_for_k(k, ub = 8192, lb = 64):
    m = C_EPSILON * MULTIPLICATIVE_CONSTANT * k
    while True:
        mb = (ub + lb) / 2
        sl = log2(mb) * log2(log2(mb)) * m
        if abs(sl - mb) < 1:
            return ub
        
        if sl > mb:
            lb = mb
        else:
            ub = mb
Example #2
0
def negativity(rho, subsys, method='tracenorm', logarithmic=False):
    """
    Compute the negativity for a multipartite quantum system described
    by the density matrix rho. The subsys argument is an index that
    indicates which system to compute the negativity for.

    .. note::

        Experimental.
    """
    mask = [idx == subsys for idx, n in enumerate(rho.dims[0])]
    rho_pt = partial_transpose(rho, mask)

    if method == 'tracenorm':
        N = ((rho_pt.dag() * rho_pt).sqrtm().tr().real - 1) / 2.0
    elif method == 'eigenvalues':
        l = rho_pt.eigenenergies()
        N = ((abs(l) - l) / 2).sum()
    else:
        raise ValueError("Unknown method %s" % method)

    if logarithmic:
        return log2(2 * N + 1)
    else:
        return N
Example #3
0
def calculate_efficiency(length):

    efficiency = 0.0
    for i in original_p_dict:
        efficiency += original_p_dict[i] * log2(original_p_dict[i])
    efficiency = -1 * efficiency
    return round(efficiency / length, 7)
Example #4
0
def printTreeToConsole(tree):
    levels = int(log2(len(tree)))
    space_for_elements = math.pow(2, levels) * 3
    heapKeyPos = 1; level = 0; levelBuffer = ''
    for heapKey in tree:
        if(int(log2(heapKeyPos)) > level):
            print levelBuffer + '\n' ; level +=1 ; levelBuffer ='' ;
            
        heapKeyStr = str(heapKey)
        if heapKey is None:
            heapKeyStr = "x"
            
        space_for_element = int((space_for_elements/(math.pow(2, level))))
        right_spacer = left_spacer = ' ' * ((space_for_element-len(heapKeyStr))/2)
        
        if (space_for_element-len(heapKeyStr)) % 2 == 1:   
            right_spacer += ' '
                
        levelBuffer += (left_spacer + heapKeyStr + right_spacer)
        heapKeyPos +=1
    
    if len(levelBuffer) > 0:
        print levelBuffer + '\n'
Example #5
0
def _entropy_relative(rho, sigma, base=e, sparse=False):
    """
    ****NEEDS TO BE WORKED ON****

    Calculates the relative entropy S(rho||sigma) between two density
    matrices.

    Parameters
    ----------
    rho : qobj
        First density matrix.
    sigma : qobj
        Second density matrix.
    base : {e,2}
        Base of logarithm.

    Returns
    -------
    rel_ent : float
        Value of relative entropy.

    """
    if rho.type != 'oper' or sigma.type != 'oper':
        raise TypeError("Inputs must be density matrices..")
    # sigma terms
    svals = sp_eigs(sigma.data, sigma.isherm, vecs=False, sparse=sparse)
    snzvals = svals[svals != 0]
    if base == 2:
        slogvals = log2(snzvals)
    elif base == e:
        slogvals = log(snzvals)
    else:
        raise ValueError("Base must be 2 or e.")
    # rho terms
    rvals = sp_eigs(rho.data, rho.isherm, vecs=False, sparse=sparse)
    rnzvals = rvals[rvals != 0]
    # calculate tr(rho*log sigma)
    rel_trace = float(real(sum(rnzvals * slogvals)))
    return -entropy_vn(rho, base, sparse) - rel_trace
Example #6
0
def entropy_vn(rho, base=e, sparse=False):
    """
    Von-Neumann entropy of density matrix

    Parameters
    ----------
    rho : qobj
        Density matrix.
    base : {e,2}
        Base of logarithm.
    sparse : {False,True}
        Use sparse eigensolver.

    Returns
    -------
    entropy : float
        Von-Neumann entropy of `rho`.

    Examples
    --------
    >>> rho=0.5*fock_dm(2,0)+0.5*fock_dm(2,1)
    >>> entropy_vn(rho,2)
    1.0

    """
    if rho.type == 'ket' or rho.type == 'bra':
        rho = ket2dm(rho)
    vals = sp_eigs(rho.data, rho.isherm, vecs=False, sparse=sparse)
    nzvals = vals[vals != 0]
    if base == 2:
        logvals = log2(nzvals)
    elif base == e:
        logvals = log(nzvals)
    else:
        raise ValueError("Base must be 2 or e.")
    return float(real(-sum(nzvals * logvals)))
def is_estimate_valid(k):
    n = get_estimate_for_k(k)
    return (1.0 / (k * log2(n)))**(log2(log2(n))) <= (1 - EPSILON) / 2 
def Entropy_value(frequency, length):
    entropy = 0
    for i in frequency:
        entropy -= (frequency[i] / length * log2(frequency[i] / length))
    return entropy
            count[c] = 1
        else:
            count[c] += 1
    #print(count)
    for c in sorted(count):
        print(c, "=>", count[c] / len(message))

    #Alphabet
    print("Count of different symblos(alphabet): ", len(count))

    #Entropy
    print("Entropy value: ", Entropy_value(count, len(message)))

    #Equal Code
    print("Equal code: ")
    med = math.ceil(log2(len(count)))
    print("Code length : ", med)
    equable_dict = dict.fromkeys(count, "")
    code = 0
    for i in equable_dict:
        equable_dict[i] = bin(code)
        equable_dict[i] = equable_dict[i][2:len(equable_dict[i]):1]
        while len(equable_dict[i]) != med:
            equable_dict[i] = "0" + equable_dict[i]
        code += 1
    for i in sorted(equable_dict):
        print(i, "=", equable_dict[i])
    code_mes = ""
    for i in message:
        code_mes += equable_dict[i]
    print("Message length in code: ", len(code_mes), "\nMessage code: ",