def quantum_strategy(trials=1000): win_cnt = 0 for _ in range(trials): # random bits by Charlie (x,y) x = random.randint(0,1) y = random.randint(0,1) # make entangled 2 qubits (one for Alice and another for Bob) qs = QState(2).h(0).cx(0,1) # response by Alice (a) if x == 0: # measurement of Z-basis (= Ry(0.0)-basis) sa = qs.m([0], shots=1, angle=0.0, phase=0.0).lst if sa == 0: a = 0 else: a = 1 else: # measurement of X-basis (or Ry(0.5*PI)-basis) sa = qs.mx([0], shots=1).lst # sa = qs.m([0], shots=1, angle=0.5, phase=0.0).lst if sa == 0: a = 0 else: a = 1 # response by Bob (b) if y == 0: # measurement of Ry(0.25*PI)-basis sb = qs.m([1], shots=1, angle=0.25, phase=0.0).lst if sb == 0: b = 0 else: b = 1 else: # measurement of Ry(-0.25*PI)-basis sb = qs.m([1], shots=1, angle=-0.25, phase=0.0).lst if sb == 0: b = 0 else: b = 1 # count up if win if (x and y) == (a+b)%2: win_cnt += 1 print("== result of quantum strategy (trials:{0:d}) ==".format(trials)) print("* win prob. = ", win_cnt/trials)
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 main(): a = random.uniform(0.0, 1.0) b = random.uniform(0.0, 1.0) phi = random.uniform(0.0, 1.0) print("a,b = {0:.4f}, {1:.4f}".format(a,b)) print("phi = {0:.4f}".format(phi)) print("** one-way quantum computing") # graph state qs_oneway = QState(2) qs_oneway.ry(0, phase=a).rz(0, phase=b) # input state (random) qs_oneway.h(1) qs_oneway.cz(0,1) # measurement s = qs_oneway.m([0], shots=1, angle=0.5, phase=phi) # result state qs_oneway.show([1]) print("** conventianal quantum gate") qs_gate = QState(1) qs_gate.ry(0, phase=a).rz(0, phase=b) # input state (random) qs_gate.rz(0, phase=-phi).h(0) qs_gate.show()
def test_m(self): """test 'm' (some angle and phase) """ qs = QState(qubit_num=2).ry(1, phase=0.2).rz(1, phase=0.2) md = qs.m([1], shots=10, angle=0.2, phase=0.2) self.assertEqual(md.frq[0], 10) self.assertEqual(md.frq[1], 0)
def test_m(self): """test 'm' (for bell state) """ qs = QState(qubit_num=2).h(0).cx(0, 1) md = qs.m(shots=10) self.assertEqual(md.frq[0] + md.frq[3], 10) self.assertEqual(md.frq[1], 0) self.assertEqual(md.frq[2], 0)
def measure(phase): qs = QState(1) qs.h(0) freq_list = qs.m([0], shots=100, angle=0.5, phase=phase).frq prob = freq_list[0] / 100 print("===") print("phase = {0:.4f} PI".format(phase)) print("[measured] prob. of up-spin = {0:.4f}".format(prob)) print("[theoretical] cos(phase/2)^2 = {0:.4f}".format((math.cos(phase*math.pi/2))**2))
def main(): print("== general rotation ==") alpha = random.uniform(0.0, 1.0) beta = random.uniform(0.0, 1.0) gamma = random.uniform(0.0, 1.0) print("(euler angle = {0:.4f}, {1:.4f}, {2:.4f})".format( alpha, beta, gamma)) print("** one-way quantum computing") # graph state qs_oneway = QState(5) qs_oneway.h(1).h(2).h(3).h(4) qs_oneway.cz(0, 1).cz(1, 2).cz(2, 3).cz(3, 4) # measurement alpha_oneway = alpha beta_oneway = beta gamma_oneway = gamma s0 = qs_oneway.m([0], shots=1, angle=0.5, phase=0.0).lst if s0 == 1: alpha_oneway = -alpha_oneway s1 = qs_oneway.m([1], shots=1, angle=0.5, phase=alpha_oneway).lst if s1 == 1: beta_oneway = -beta_oneway s2 = qs_oneway.m([2], shots=1, angle=0.5, phase=beta_oneway).lst if (s0 + s2) % 2 == 1: gamma_oneway = -gamma_oneway s3 = qs_oneway.m([3], shots=1, angle=0.5, phase=gamma_oneway).lst # result state qs_oneway.show([4]) print("** conventianal quantum gate") qs_gate = QState(1) qs_gate.rx(0, phase=alpha).rz(0, phase=beta).rx(0, phase=gamma) qs_gate.show()
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
return self.m(qid=[self.__anc[0]], shots=shots).frequency if __name__ == '__main__': shots = 1000 alpha, beta, gamma = random.uniform(0, 1), random.uniform( 0, 1), random.uniform(0, 1) print("- alpha, beta, gamma = {:.4f}, {:.4f}, {:.4f}".format( alpha, beta, gamma)) dat_left = CreateRegister(5) dat_right = CreateRegister(5) bnd = CreateRegister(1) anc = CreateRegister(1) qubit_num = InitRegister(dat_left, dat_right, bnd, anc) qs_logical = QStateLogical(qubit_num).set_register(dat_left, dat_right, bnd, anc) qs_logical.initialize(alpha=alpha, beta=beta, gamma=gamma) parity = qs_logical.merge() print("- parity =", parity) result_logical = qs_logical.Mz(shots=shots) print("- actual =", result_logical) qs = QState(qubit_num=1).u3(0, alpha=alpha, beta=beta, gamma=gamma) result = qs.m(shots=shots) print("- expect =", result.frequency)
# 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) # final state (before teleportation)