コード例 #1
0
def main():

    # parameters for generating random state: |psi>
    alpha, beta, gamma = random.random(), random.random(), random.random()

    # reference state: T|psi>
    qs_expect = QState(1)
    qs_expect.rz(0, phase=alpha).rx(0, phase=beta).rz(0, phase=gamma).t(0)

    # prepare initial state
    qs = QState(3)
    qs.h(0).s(0)  # |Y>
    qs.h(1).t(1)  # |A>
    qs.rz(2, phase=alpha).rx(2, phase=beta).rz(2, phase=gamma)  # |psi>

    # T gate (only with X,Z,H,CNOT and measurement)
    qs.cx(1, 2)
    mval = qs.m(qid=[2]).last
    if mval == '1':
        qs.cx(1, 0).h(0).cx(1, 0).h(0)
        qs.x(1).z(1)
    qs_actual = qs.partial(qid=[1])

    # show the result
    print("== expect ==")
    qs_expect.show()
    print("== actual ==")
    qs_actual.show()
    print("== fidelity ==")
    print("{:.6f}".format(qs_actual.fidelity(qs_expect)))
コード例 #2
0
ファイル: test_QState.py プロジェクト: samn33/qlazy
 def test_operate_xyz(self):
     """test 'operate' (xyz)
     """
     qs_expect = QState(qubit_num=3)
     qs_actual = QState(qubit_num=3)
     pp = PauliProduct(pauli_str="XYZ", qid=[2, 0, 1])
     qs_expect.x(2).y(0).z(1)
     qs_actual.operate(pp=pp)
     ans = equal_qstates(qs_expect, qs_actual)
     self.assertEqual(ans, True)
コード例 #3
0
ファイル: test_QState.py プロジェクト: samn33/qlazy
 def test_operate_h_x(self):
     """test 'operate' (x followed by h)
     """
     qs_expect = QState(qubit_num=1).h(0)
     qs_actual = QState(qubit_num=1).h(0)
     pp = PauliProduct(pauli_str="X")
     qs_expect.x(0)
     qs_actual.operate(pp=pp)
     ans = equal_qstates(qs_expect, qs_actual)
     self.assertEqual(ans, True)
コード例 #4
0
qs.h(1).cx(1,2)

# initial state (before teleportation)
print("== Alice (initial) ==")
qs.show([0])
print("== Bob (initial) ==")
qs.show([2])
    
# Alice execute Bell-measurement to her qubits 0,1
print("== Bell measurement ==")
result = qs.mb([0,1],shots=1).lst

# Bob operate his qubit (id=2) according to the result
if result == BELL_PHI_PLUS:
    print("result: phi+")
elif result == BELL_PSI_PLUS:
    print("result: psi+")
    qs.x(2)
elif result == BELL_PSI_MINUS:
    print("result: psi-")
    qs.x(2).z(2)
elif result == BELL_PHI_MINUS:
    print("result: phi-")
    qs.z(2)

# final state (before teleportation)
print("== Alice (final) ==")
qs.show([0])
print("== Bob (final) ==")
qs.show([2])
コード例 #5
0
ファイル: t_gate.py プロジェクト: samn33/qlazy
def logical_zero():

    anc = [0, 1, 2, 3, 4, 5, 6]  # registers for ancila
    cod = [7, 8, 9, 10, 11, 12, 13]  # registers for steane code
    qs_total = QState(14)

    # g1
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cx(anc[0], cod[3]).cx(anc[1],
                                   cod[4]).cx(anc[2],
                                              cod[5]).cx(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.z(cod[0]).z(cod[1]).z(cod[2]).z(cod[3])
    qs_total.reset(qid=anc)

    # g2
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cx(anc[0], cod[1]).cx(anc[1],
                                   cod[2]).cx(anc[2],
                                              cod[5]).cx(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.z(cod[0]).z(cod[1]).z(cod[4]).z(cod[5])
    qs_total.reset(qid=anc)

    # g3
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cx(anc[0], cod[0]).cx(anc[1],
                                   cod[2]).cx(anc[2],
                                              cod[4]).cx(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.z(cod[2]).z(cod[4]).z(cod[6])
    qs_total.reset(qid=anc)

    # g4
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cz(anc[0], cod[3]).cz(anc[1],
                                   cod[4]).cz(anc[2],
                                              cod[5]).cz(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.x(cod[0]).x(cod[1]).x(cod[2]).x(cod[3])
    qs_total.reset(qid=anc)

    # g5
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cz(anc[0], cod[1]).cz(anc[1],
                                   cod[2]).cz(anc[2],
                                              cod[5]).cz(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.x(cod[0]).x(cod[1]).x(cod[4]).x(cod[5])
    qs_total.reset(qid=anc)

    # g6
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.cz(anc[0], cod[0]).cz(anc[1],
                                   cod[2]).cz(anc[2],
                                              cod[4]).cz(anc[3], cod[6])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 4)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': qs_total.x(cod[2]).x(cod[4]).x(cod[6])
    qs_total.reset(qid=anc)

    # g7
    qs_total.h(anc[0])
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 7)]
    [qs_total.cz(anc[i], cod[i]) for i in range(7)]
    [qs_total.cx(anc[0], anc[i]) for i in range(1, 7)]
    qs_total.h(anc[0])
    mval = qs_total.m(qid=[anc[0]]).last
    if mval == '1': [qs_total.x(q) for q in cod]
    qs_total.reset(qid=anc)

    qs = qs_total.partial(qid=cod)

    return qs
コード例 #6
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])