Exemplo n.º 1
0
def create_densop():

    mat_1 = np.array([[0.75, 0.25], [0.25, 0.25]])
    de_1 = DensOp(matrix=mat_1)
    de_ini = de_1.composite(num=4)
    # de_1.free()

    return de_ini
Exemplo n.º 2
0
def decoder(de_comp, id_all, theta, iperm_mat):

    qs_0 = QState(1)
    de_0 = DensOp(qstate=[qs_0])
    de_fin = de_0.tenspro(de_comp)  # add 1-qubit (|0>)
    de_fin.apply(iperm_mat)
    [de_fin.ry(i, phase=theta) for i in id_all]

    # qs_0.free()
    # de_0.free()

    return de_fin
Exemplo n.º 3
0
def make_densop(expect, qubit_num, pauli_mat):

    dim = 2**qubit_num
    measure_num = len(expect)
    matrix = np.zeros((dim,dim))
    for i in range(measure_num):
        pauli_prod = make_pauli_product(i,measure_num,pauli_mat)
        matrix = matrix + expect[i] * pauli_prod
    matrix = matrix / (2.0**qubit_num)
    
    return DensOp(matrix=matrix)
Exemplo n.º 4
0
def random_mixed_state(qubit_num, mixed_num):

    # random probabilities
    r = [random.random() for _ in range(mixed_num)]
    s = sum(r)
    prob_mixed = [r[i] / s for i in range(mixed_num)]

    # random quantum states
    vec_A = [random_vector(qubit_num) for _ in range(mixed_num)]
    qs_A = [QState(vector=vec_A[i]) for i in range(mixed_num)]

    # random density operator
    de_A = DensOp(qstate=qs_A, prob=prob_mixed)

    return de_A
Exemplo n.º 5
0
def holevo_quantity(X, de):

    samp_num = len(X)
    code_num = len(de)

    prob = np.zeros(code_num)
    for x in X:
        prob[x] += 1.0
    prob = prob / sum(prob)

    de_total = DensOp.mix(densop=de, prob=prob)

    holevo = de_total.entropy()
    for i in range(code_num):
        holevo -= prob[i] * de[i].entropy()

    return holevo
Exemplo n.º 6
0
def random_densop(qnum_tar, qnum_ref, qnum_env):

    dim_pur = 2**(qnum_tar + qnum_ref)
    vec_pur = np.array([0.0] * dim_pur)
    vec_pur[0] = 1.0
    mat_pur = unitary_group.rvs(dim_pur)
    vec_pur = np.dot(mat_pur, vec_pur)

    dim_env = 2**qnum_env
    vec_env = np.array([0.0] * dim_env)
    vec_env[0] = 1.0

    vec_whole = np.kron(vec_pur, vec_env)

    qs = QState(vector=vec_whole)
    de = DensOp(qstate=[qs], prob=[1.0])

    return de
Exemplo n.º 7
0
def purification(de_A):

    # representation of the input state with orthogonal basis of system A
    coef, basis_A = eigen_values_vectors(de_A)
    rank = len(basis_A)

    # make computational basis of system R
    qnum_R = int(math.log2(rank))
    if 2**qnum_R != rank:
        qnum_R += 1
    basis_R = computational_basis(qnum_R)

    # orthogonal basis of system A+R
    basis_AR = [np.kron(basis_A[i], basis_R[i]) for i in range(rank)]

    # representation of the purified state with orthogonal basis of system A+R
    vec_AR = np.array([0] * len(basis_AR[0]))
    for i in range(rank):
        vec_AR = vec_AR + (math.sqrt(coef[i]) * basis_AR[i])

    qs_AR = QState(vector=vec_AR)
    de_AR = DensOp(qstate=[qs_AR], prob=[1.0])

    return de_AR
Exemplo n.º 8
0
    return id_A,id_B

def eigen_values(densop):

    matrix = densop.get_elm()
    eigvals = eigh(matrix, eigvals_only=True)
    eigvals_out = [eigvals[i] for i in range(len(eigvals)) if eigvals[i] > MIN_DOUBLE]

    return eigvals_out
    
if __name__ == '__main__':

    # whole quantum state
    qubit_num = 5
    qs = random_qstate(qubit_num)
    de = DensOp(qstate=[qs], prob=[1.0])  # pure state

    # partial density operators (system A and B)
    id_A, id_B = random_qubit_id(qubit_num)
    de_A = de.patrace(id_B)
    de_B = de.patrace(id_A)

    # eigen-values of density operators (system A and B)
    eval_A = eigen_values(de_A)
    eval_B = eigen_values(de_B)

    print("== system A ==")
    print("- qubit id =", id_A)
    print("- square trace = ", de_A.sqtrace())
    print("- eigen values =", eval_A)
    print("- rank =", len(eval_A))
Exemplo n.º 9
0
if __name__ == '__main__':

    print("== parameter ==")

    gamma = 0.5
    H = make_hamiltonian()
    print("gamma =", gamma)

    print("== initial density operator ==")

    u_1 = math.sqrt(1 / 3)
    u_2 = math.sqrt(1 / 3)
    u_3 = math.sqrt(1 / 3)
    D = make_densop_matrix(u_1, u_2, u_3)
    de = DensOp(matrix=D)

    de.show()
    print("square trace =", de.sqtrace())
    print("(u_1,u_2,u_3) = ({0:.3f},{1:.3f},{2:.3f})".format(u_1, u_2, u_3))
    print("expect value of energy =", de.expect(matrix=H))

    [M_0, M_1] = make_kraus(gamma=gamma)

    print("== finail density operator ==")

    de.instrument(kraus=[M_0, M_1])

    de.show()
    print("square trace =", de.sqtrace())
    (u_1, u_2, u_3) = get_coordinate(densop=de)
Exemplo n.º 10
0
def random_qstate(qnum):  # random pure state

    dim = 2**qnum
    vec = np.array([0.0] * dim)
    vec[0] = 1.0
    mat = unitary_group.rvs(dim)
    vec = np.dot(mat, vec)
    qs = QState(vector=vec)

    return qs


if __name__ == '__main__':

    qnum_A = 2
    qnum_B = 2

    id_A = list(range(qnum_A))
    id_B = [i + qnum_A for i in range(qnum_B)]

    qs = random_qstate(qnum_A + qnum_B)
    de = DensOp(qstate=[qs], prob=[1.0])

    ent = de.entropy()
    ent_A = de.entropy(id_A)
    ent_B = de.entropy(id_B)

    print("** S(A,B)  = {:.4f}".format(ent))
    print("** S(A)    = {:.4f}".format(ent_A))
    print("** S(B)    = {:.4f}".format(ent_B))
Exemplo n.º 11
0
if __name__ == '__main__':

    qnum_A = 2
    qnum_B = 3

    id_A = list(range(qnum_A))
    id_B = [i + qnum_A for i in range(qnum_B)]

    ## simple random pure state
    #qs = random_qstate(qnum_A+qnum_B)

    # random pure state represented by schmidt decomposition
    qs = random_qstate_schmidt(qnum_A, qnum_B)

    # density operator for the pure state
    de = DensOp(qstate=[qs], prob=[1.0])

    ent = de.entropy()
    ent_A = de.entropy(id_A)
    ent_B = de.entropy(id_B)

    # 'ent_A' is equal to 'ent_B', if state for system A+B is pure state
    print(
        "- entropy (system A+B):                S(A,B)  = {:.4f}".format(ent))
    print("- entanglement entropy (system A):     S(A)    = {:.4f}".format(
        ent_A))
    print("- entanglement entropy (system B):     S(B)    = {:.4f}".format(
        ent_B))

    ent_Q = entropy_from_qstate(qs, id_A, id_B, 1000)
Exemplo n.º 12
0
from qlazy import QState, DensOp

qs = QState(4)

qs.h(0).h(1)  # unitary operation for 0,1-system
qs.x(2).z(3)  # unitary operation for 2,3-system

de1 = DensOp(qstate=[qs], prob=[1.0]) # product state
de1_reduced = de1.patrace([0,1])   # trace-out 0,1-system

print("== partial trace of product state ==")
print(" * trace = ", de1_reduced.trace())
print(" * square trace = ", de1_reduced.sqtrace())
    
qs.cx(1,3).cx(0,2)  # entangle between 0,1-system and 2,3-system

de2 = DensOp(qstate=[qs], prob=[1.0])  # entangled state
de2_reduced = de2.patrace([0,1])    # trace-out 0,1-system

print("== partial trace of entangled state ==")
print(" * trace = ", de2_reduced.trace())
print(" * square trace = ", de2_reduced.sqtrace())

print("== partial state of entangled state ==")
qs.show([2,3])
Exemplo n.º 13
0
from qlazy import QState, DensOp

qs_pure = QState(1).h(0)  # (|0> + |1>) / sqrt(2.0)
de_pure = DensOp(qstate=[qs_pure], prob=[1.0])

qs_pure_1 = QState(1)  # |0>
qs_pure_2 = QState(1).x(0)  # |1>
de_mixed = DensOp(qstate=[qs_pure_1, qs_pure_2], prob=[0.5, 0.5])

print("== pure state ==")
de_pure.show()
print("* trace =", de_pure.trace())
print("* square trace =", de_pure.sqtrace())
print("")
print("== pure state (only non-zero) ==")
de_pure.show(nonzero=True)
print("* trace =", de_pure.trace())
print("* square trace =", de_pure.sqtrace())
print("")
print("== mixed state ==")
de_mixed.show()
print("* trace =", de_mixed.trace())
print("* square trace =", de_mixed.sqtrace())
print("== mixed state (only non-zero) ==")
de_mixed.show(nonzero=True)
print("* trace =", de_mixed.trace())
print("* square trace =", de_mixed.sqtrace())
Exemplo n.º 14
0
            de_tmp.mul(prob[i])
            densop_est = de_tmp.clone()
        else:
            de_tmp.mul(prob[i])
            densop_est.add(de_tmp)

    return densop_est
    
if __name__ == '__main__':

    # settings
    qubit_num = 1
    mixed_num = 2
    shots = 100

    # quantum state ensemble (original)
    qstate, prob = random_qstate_ensemble(qubit_num, mixed_num)

    # density operator (original)
    densop_ori = DensOp(qstate=qstate, prob=prob)

    # density operator estimation only from quantum state ensemble
    # (quantum state tomography)
    densop_est = estimate_densop(prob,qstate,shots)

    print("** density operator (original)")
    densop_ori.show()
    print("** density operator (estimated)")
    densop_est.show()
    print("** fidelity =",densop_ori.fidelity(densop_est))
Exemplo n.º 15
0
    s = math.sin(theta * math.pi)
    c = math.cos(theta * math.pi)
    f = 1.0 / (1.0 + c)

    E0 = f * np.array([[s * s, -c * s], [-c * s, c * c]])
    E1 = f * np.array([[0, 0], [0, 1]])
    E2 = np.eye(2) - E0 - E1

    return (E0, E1, E2)


if __name__ == '__main__':

    theta = 0.4  # -0.5 <= theta <= 0.5
    print("prob. of success =", 1.0 - math.cos(theta * math.pi))

    qs = [QState(1), QState(1).ry(0, phase=theta * 2.0)]
    de = [
        DensOp(qstate=[qs[0]], prob=[1.0]),
        DensOp(qstate=[qs[1]], prob=[1.0])
    ]

    povm = make_povm(theta)

    print("theta = {0:}*PI".format(theta))
    print("     [0]   [1]   [?]")
    for i in range(2):
        prob = de[i].probability(povm=povm)
        print("[{0:}] {1:.3f} {2:.3f} {3:.3f}".format(i, prob[0], prob[1],
                                                      prob[2]))
Exemplo n.º 16
0
def make_densop(basis):

    qs = [QState(vector=b) for b in basis]
    de = [DensOp(qstate=[q], prob=[1.0]) for q in qs]

    return de
Exemplo n.º 17
0
    # random schmidt coefficients
    coef = normalized_random_list(rank)

    # random probabilities for mixing the pure states
    prob = normalized_random_list(mixed_num)

    # basis for system A+B
    dim_AB = dim_A * dim_B
    basis_AB = [None] * mixed_num
    for i in range(mixed_num):
        basis_AB[i] = np.zeros(dim_AB)
        for j in range(dim_A):
            basis_AB[i] = basis_AB[i] + \
                math.sqrt(coef[j]) * np.kron(basis_A[j],basis_B[i*dim_A+j])

    # construct the density operator
    matrix = np.zeros((dim_AB, dim_AB))
    for i in range(mixed_num):
        matrix = matrix + prob[i] * np.outer(basis_AB[i], basis_AB[i])
    de = DensOp(matrix=matrix)

    # calculate the entropies
    ent = de.entropy()
    ent_A = de.entropy(id_A)
    ent_B = de.entropy(id_B)

    print("** S(A,B)    = {:.4f}".format(ent))
    print("** S(A)      = {:.4f}".format(ent_A))
    print("** S(B)      = {:.4f}".format(ent_B))
    print("** S(B)-S(A) = {:.4f}".format(ent_B - ent_A))