コード例 #1
0
ファイル: steane_code_1.py プロジェクト: katou-boop/qlazy
def generate_qstate(qid_C, qid_S):

    a = np.random.rand() + np.random.rand() * 1.j
    b = np.random.rand() + np.random.rand() * 1.j

    print("== quantum state (a |0L> + b |1L>) ==")
    print("- a = {:.4f}".format(a))
    print("- b = {:.4f}".format(b))

    qvec = np.full(2**len(qid_C), 0. + 0.j)
    for s in Steane_0:
        qvec[int(s, 2)] = a
    for s in Steane_1:
        qvec[int(s, 2)] = b

    norm = np.linalg.norm(qvec)
    qvec = qvec / norm

    qs_C = QState(vector=qvec)
    qs_S = QState(len(qid_S))
    qs = qs_C.tenspro(qs_S)
    de_ini = DensOp(qstate=[qs])
    de_fin = de_ini.clone()

    QState.free_all(qs_C, qs_S, qs)
    return de_ini, de_fin
コード例 #2
0
ファイル: schumacher.py プロジェクト: katou-boop/qlazy
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
コード例 #3
0
ファイル: schumacher.py プロジェクト: katou-boop/qlazy
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
コード例 #4
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])

    # free memory
    qs_AR.free()

    return de_AR
コード例 #5
0
ファイル: holevo.py プロジェクト: katou-boop/qlazy
def make_densop(basis):

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

    for n in range(len(qs)):
        qs[n].free()

    return de
コード例 #6
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)
コード例 #7
0
ファイル: holevo.py プロジェクト: katou-boop/qlazy
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()

    de_total.free()

    return holevo
コード例 #8
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)

    # free memory
    for i in range(mixed_num):
        qs_A[i].free()

    return de_A
コード例 #9
0
ファイル: trace_distance.py プロジェクト: katou-boop/qlazy
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])

    qs.free()
    return de
コード例 #10
0
ファイル: densop.py プロジェクト: katou-boop/qlazy
from qlazypy 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("== mixed state ==")
de_mixed.show()
print("* trace =", de_mixed.trace())
print("* square trace =", de_mixed.sqtrace())

qs_pure.free()
qs_pure_1.free()
qs_pure_2.free()

de_pure.free()
de_mixed.free()
コード例 #11
0
    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))
コード例 #12
0
    # 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))

    de.free()
コード例 #13
0
ファイル: steane_code_1.py プロジェクト: katou-boop/qlazy
    # correction
    for i, row in enumerate(Hamming_T):
        [self.x(qid_S[j]) if row[j] == 0 else False for j in range(len(row))]
        self.mcx(qid=qid_S + [qid_C[i]])
        [self.x(qid_S[j]) if row[j] == 0 else False for j in range(len(row))]

    if kind == 'phase_flip': [self.h(q) for q in qid_C]

    return self


if __name__ == '__main__':

    # add custom gates
    DensOp.add_method(noise)
    DensOp.add_method(correct)

    # set registers
    qid_C = DensOp.create_register(7)  # registers for code space
    qid_S = DensOp.create_register(3)  # registers for error syndrome
    DensOp.init_register(qid_C, qid_S)

    # generate initial quantum state (density operator)
    de_ini, de_fin = generate_qstate(qid_C, qid_S)

    # add noise
    de_fin.noise(kind='depolarize', qid=[qid_C[3]], prob=1.0)

    # error correction
    de_fin.correct('bit_flip', qid_C, qid_S)
コード例 #14
0
def correct(self):

    self.cx(0, 2).cx(0, 1)
    self.cx(3, 5).cx(3, 4)
    self.cx(6, 8).cx(6, 7)
    self.ccx(2, 1, 0).ccx(5, 4, 3).ccx(8, 7, 6)
    self.h(0).h(3).h(6)
    self.cx(0, 3).cx(0, 6)
    self.ccx(6, 3, 0)
    return self


if __name__ == '__main__':

    # add custom gate
    DensOp.add_method(encode)
    DensOp.add_method(noise)
    DensOp.add_method(correct)

    # settings
    kind = 'depolarize'  # bit_flip,phase_flip,bit_phase_flip,depolarize,amp_dump,phase_dump
    prob = 1.0
    qid = [0]
    print("== settings ==")
    print("* kind of noise        =", kind)
    print("* probability of noise =", prob)
    print("* noisy channels       =", qid)

    # error correction (shor code)
    de_ini, de_fin = create_densop()
    de_fin.encode()
コード例 #15
0
def create_densop():

    de_ini = DensOp(qubit_num=9).h(0)
    de_fin = de_ini.clone()
    return de_ini, de_fin
コード例 #16
0
ファイル: amplitude_dumping.py プロジェクト: katou-boop/qlazy
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)
コード例 #17
0
    matrix = (Sigma_0+u_1*Sigma_1+u_2*Sigma_2+u_3*Sigma_3) / 2.0

    return matrix

if __name__ == '__main__':

    
    print("== parameter ==")
    prob = 0.1
    print("prob =", prob)
    
    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)

    print("== phase dump ==")
    #de.bit_flip(0, prob=prob)
    #de.phase_flip(0, prob=prob)
    #de.bit_phase_flip(0, prob=prob)
    #de.depolarize(0, prob=prob)
    #de.amp_dump(0, prob=prob)
    de.phase_dump(0, prob=prob)

    (u_1,u_2,u_3) = get_coordinate(densop=de)
    print("(u_1,u_2,u_3) = ({0:},{1:},{2:})".format(u_1,u_2,u_3))

    de.free()
コード例 #18
0
from qlazypy 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])

qs.free()
de1.free()
de2.free()
de1_reduced.free()
de2_reduced.free()
コード例 #19
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)

    # 'ent_Q' is equal to 'ent_A','ent_B',
    # if the pure state is represented by schmidt decomposition
    print("- entanglement entropy (predict of A): S(A)'   = {:.4f}".format(ent_Q))
コード例 #20
0
    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))

    for q in qstate:
        q.free()
    densop_ori.free()
    densop_est.free()
コード例 #21
0
ファイル: entropy1.py プロジェクト: katou-boop/qlazy
    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))

    qs.free()
    de.free()
コード例 #22
0
ファイル: povm.py プロジェクト: katou-boop/qlazy
def make_povm(theta=0.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]))
            
        de[i].free()
        qs[i].free()