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)))
def test_operate_controlled_xyz(self): """test 'operate' (controlled_xyz) """ qs_expect = QState(qubit_num=4) qs_actual = QState(qubit_num=4) pp = PauliProduct(pauli_str="XYZ", qid=[2, 0, 1]) qs_expect.cx(3, 2).cy(3, 0).cz(3, 1) qs_actual.operate(pp=pp, ctrl=3) ans = equal_qstates(qs_expect, qs_actual) self.assertEqual(ans, True)
def cost(phi): qs = QState(2) qs.rx(0, phase=phi[0]) qs.rz(0, phase=phi[1]) qs.rx(1, phase=phi[2]) qs.rz(1, phase=phi[3]) qs.cx(1, 0) qs.rz(1, phase=phi[4]) qs.rx(1, phase=phi[5]) exp = ExpectVal(M, qs) # qs.free() return exp
def distillation_Y(qs_list): data = CreateRegister(1) # data qubit code = CreateRegister(7) # logical qubit for Steane code anci = CreateRegister(1) # ancilla qubit for S gate qubit_num = InitRegister(data, code, anci) qs = QState(qubit_num=qubit_num - len(anci)) # bell state qs.h(data[0]).cx(data[0], code[6]) # steane code qs.h(code[0]).h(code[1]).h(code[2]) qs.cx(code[6], code[3]).cx(code[6], code[4]) qs.cx(code[2], code[3]).cx(code[2], code[4]).cx(code[2], code[5]) qs.cx(code[1], code[4]).cx(code[1], code[5]).cx(code[1], code[6]) qs.cx(code[0], code[3]).cx(code[0], code[5]).cx(code[0], code[6]) # S gates and measurements mval_list = [] for i in range(7): qs_total = qs.tenspro(qs_list[i]) qs_total.cx(code[i], anci[0]).h(anci[0]).cx(code[i], anci[0]).h(anci[0]) mval = int(qs_total.mx(qid=[code[i]]).last) mval_list.append(mval) qs = qs_total.partial(qid=data + code) if measure_X_stab(mval_list) == False: return None qs_pat = qs_total.partial(qid=data) if measure_logical_X(mval_list) == True: qs_pat.z(0) return qs_pat
def main(): print("== CNOT gate ==") print("** one-way quantum computing") # graph state qs_oneway = QState(15) qs_oneway.h(0) qs_oneway.h(1).h(2).h(3).h(4).h(5).h(6).h(7) qs_oneway.h(9).h(10).h(11).h(12).h(13).h(14) qs_oneway.cz(0, 1).cz(1, 2).cz(2, 3).cz(3, 4).cz(4, 5).cz(5, 6) qs_oneway.cz(3, 7).cz(7, 11) qs_oneway.cz(8, 9).cz(9, 10).cz(10, 11).cz(11, 12).cz(12, 13).cz(13, 14) # measurement qs_oneway.mx([0], shots=1) qs_oneway.my([1], shots=1) qs_oneway.my([2], shots=1) qs_oneway.my([3], shots=1) qs_oneway.my([4], shots=1) qs_oneway.my([5], shots=1) qs_oneway.my([7], shots=1) qs_oneway.mx([8], shots=1) qs_oneway.mx([9], shots=1) qs_oneway.mx([10], shots=1) qs_oneway.my([11], shots=1) qs_oneway.mx([12], shots=1) qs_oneway.mx([13], shots=1) qs_oneway.show([6, 14]) print("** conventianal quantum gate") qs_gate = QState(2) qs_gate.h(0) qs_gate.cx(0, 1) qs_gate.show()
def distillation_A(qs_list): data = CreateRegister(1) # data qubit code = CreateRegister(15) # logical qubit for Steane code anci = CreateRegister(1) # ancilla qubit for S gate qubit_num = InitRegister(data, code, anci) qs = QState(qubit_num=qubit_num-len(anci)) # bell state qs.h(data[0]).cx(data[0], code[14]) # Reed-Muler code [qs.h(code[q-1]) for q in [1,2,4,8]] [qs.cx(code[8-1], code[q-1]) for q in [9,10,11,12,13,14,15]] [qs.cx(code[4-1], code[q-1]) for q in [5,6,7,12,13,14,15]] [qs.cx(code[2-1], code[q-1]) for q in [3,6,7,10,11,14,15]] [qs.cx(code[1-1], code[q-1]) for q in [3,5,7,9,11,13,15]] [qs.cx(code[15-1], code[q-1]) for q in [3,5,6,9,10,12]] # T_dag gates and measurements mval_list = [] for i in range(15): qs_total = qs.tenspro(qs_list[i]) m = qs_total.cx(anci[0], code[i]).m(qid=[code[i]]).last if m == '1': qs_total.x(anci[0]) else: qs_total.s_dg(anci[0]) mval = int(qs_total.mx(qid=[anci[0]]).last) mval_list.append(mval) qs = qs_total.partial(qid=data+code) if measure_X_stab(mval_list) == False: return None qs_pat = qs_total.partial(qid=data) if measure_logical_X(mval_list) == False: qs_pat.z(0) return qs_pat
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
qs = QState(3) # prepare qubit (id=0) that Alice want to send to Bob by rotating around X,Z qs.ry(0, phase=0.3).rz(0, phase=0.4) # make entangled 2 qubits (id=1 for Alice, id=2 for Bob) 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 qs.cx(0, 1).h(0) b0 = qs.m([0], shots=1).lst b1 = qs.m([1], shots=1).lst print("== Bell measurement ==") print("b0,b1 = ", b0, b1) # Bob operate his qubit (id=2) according to the result if b0 == 0 and b1 == 0: # phi+ pass elif b0 == 0 and b1 == 1: # psi+ qs.x(2) elif b0 == 1 and b1 == 0: # psi- qs.z(2) elif b0 == 1 and b1 == 1: # phi- qs.x(2).z(2)
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])
from qlazy import QState qs = QState(3) # prepare qubit (id=0) that Alice want to send to Bob by rotating around X,Z qs.ry(0, phase=0.3).rz(0, phase=0.4) # make entangled 2 qubits (id=1 for Alice, id=2 for Bob) qs.h(1).cx(1, 2) # initial state (before teleportation) print("== Alice (initial) ==") qs.show([0]) print("== Bob (initial) ==") qs.show([2]) # equivalent to quantum teleportation qs.cx(0, 1).h(0) qs.cx(1, 2) qs.cz(0, 2) # final state (before teleportation) print("== Alice (final) ==") qs.show([0]) print("== Bob (final) ==") qs.show([2])