def generate_qstate(qid_0, qid_1, entangle=True): # random pure state (entangle or not) if entangle == True: dim = 2**len(qid_0 + qid_1) vec = np.array([0.0] * dim) vec[0] = 1.0 mat = unitary_group.rvs(dim) vec = np.dot(mat, vec) qs = QState(vector=vec) else: dim_0 = 2**len(qid_0) dim_1 = 2**len(qid_1) vec_0 = np.array([0.0] * dim_0) vec_1 = np.array([0.0] * dim_1) vec_0[0] = 1.0 vec_1[0] = 1.0 mat_0 = unitary_group.rvs(dim_0) mat_1 = unitary_group.rvs(dim_1) vec_0 = np.dot(mat_0, vec_0) vec_1 = np.dot(mat_1, vec_1) qs_0 = QState(vector=vec_0) qs_1 = QState(vector=vec_1) qs = qs_0.tenspro(qs_1) return qs
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 faulttolerant_t(qs_in): A = [0, 1, 2, 3, 4, 5, 6] # registers for ancila B = [7, 8, 9, 10, 11, 12, 13] # registers for output quantum state C = [14, 15, 16, 17, 18, 19, 20] # registers for input quantum state qs_A = QState(7) qs_B = logical_zero() qs_AB = qs_A.tenspro(qs_B) qs_ABC = qs_AB.tenspro(qs_in) # -H-T- qs_ABC.h(A[0]) [qs_ABC.cx(A[0], A[i]) for i in range(1, 7)] [qs_ABC.cx(A[i], B[i]).cs(A[i], B[i]).cz(A[i], B[i]) for i in range(7)] [qs_ABC.cx(A[0], A[i]) for i in range(1, 7)] qs_ABC.t_dg(A[0]).h(A[0]) mval = qs_ABC.m(qid=[A[0]]).last if mval == '1': [qs_ABC.z(q) for q in B] qs_ABC.reset(qid=A) # -CNOT- [qs_ABC.cx(B[i], C[i]) for i in range(7)] # -M-, -SX- qs_ABC.h(A[0]) [qs_ABC.cx(A[0], A[i]) for i in range(1, 7)] [qs_ABC.cz(A[i], C[i]) for i in range(7)] [qs_ABC.cx(A[0], A[i]) for i in range(1, 7)] qs_ABC.h(A[0]) mval = qs_ABC.m(qid=[A[0]]).last if mval == '1': [qs_ABC.x(q).s(q).z(q) for q in B] qs_out = qs_ABC.partial(qid=B) return qs_out
def hadamard_test_imag(): print("== expectaion value prediction (imaginary part) == ") # prepare initial state qs_0 = QState(1) qs_psi = QState(2).h(0).h(1) qs = qs_0.tenspro(qs_psi) # circuit for hadamard test qs.h(0).s(0) qs.ch(0, 1).crx(0, 2, phase=0.25) qs.h(0) shots = 1000 md = qs.m([0], shots=shots) p0 = md.frq[0] / shots p1 = md.frq[1] / shots exp_pred = p1 - p0 print("expectation value (predict) = {0:.3f}".format(exp_pred)) # theoretical expactation value qs_op = qs_psi.clone() qs_op.h(0).rx(1, phase=0.25) exp_theo = qs_psi.inpro(qs_op).imag print("expectation value (theoretical) = {0:.3f}".format(exp_theo))
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 = MyDensOp(qstate=[qs]) de_fin = de_ini.clone() # QState.free_all(qs_C, qs_S, qs) return de_ini, de_fin
def test_tenspro(self): """test 'tenspro' """ qs_A = QState(qubit_num=1).h(0) qs_B = QState(qubit_num=2).h(0).cx(0, 1) actual = qs_A.tenspro(qs_B) expect = QState(qubit_num=3).h(0).h(1).cx(1, 2) ans = equal_qstates(actual, expect) self.assertEqual(ans, True)
def test_inheritance_3(self): """test 'inheritance_3' """ qs_A = QState(qubit_num=1) qs_B = QState(qubit_num=1) qs_expect = qs_A.tenspro(qs_B).h(0).cx(0, 1).x(0) qs_C = MyQState(qubit_num=1) qs_D = MyQState(qubit_num=1) qs_actual = qs_C.tenspro(qs_D) qs_actual.bell(0, 1).x(0) fid = qs_expect.fidelity(qs_actual) self.assertEqual(abs(fid - 1.0) < EPS, True)
def faulttolerant_m(qs_in): anc = [0, 1, 2, 3, 4, 5, 6] # ancila cod = [7, 8, 9, 10, 11, 12, 13] # steane code qs_anc = QState(7) qs_total = qs_anc.tenspro(qs_in) 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]) md = qs_total.m(qid=[0], shots=10000) return md
def main(): print("== convergence on engenstate ==") # prepare initial state qs_0 = QState(1) qs_psi = QState(2) qs = qs_0.tenspro(qs_psi) # circuit for hadamard test iter = 100 for _ in range(iter): qs.h(0) qs.ch(0, 1).crx(0, 2, phase=0.25) qs.h(0) qs.m(id=[0], shots=1) print("- final |psi>") qs.show(id=[1, 2]) print("- unitary operated |psi>") qs.h(1).rx(2, phase=0.25).show(id=[1, 2])
def main(): print("== phase prediction == ") # prepare initial state qs_0 = QState(1) qs_psi = QState(1).h(0) qs = qs_0.tenspro(qs_psi) # circuit for hadamard test qs.h(0) qs.crx(0, 1, phase=0.5) qs.h(0) shots = 1000 md = qs.m([0], shots=shots) p0 = md.frq[0] / shots p1 = md.frq[1] / shots print("[predicted] cos(gamma) = {0:.3f}".format(p0 - p1)) print("[theoretical] cos(gamma) = {0:.3f}".format(math.cos(-0.25 * math.pi)))
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