def run_ro(ro_0, H, config): # --------------------------------------------------------------------------------------------- U = Unitary(H, config.dt) # U.write_to_file(U_csv) U_conj = U.get_conj() # --------------------------------------------------------------------------------------------- ro_t = ro_0 ro_0_sqrt = lg.fractional_matrix_power(ro_0, 0.5) fidelity = [] with open(config.fid_csv, 'w') as csv_file: writer = csv.writer( csv_file, lineterminator='\n') st = [] for i in H.states.values(): st.append(str(i)) writer.writerow(st) for t in range(0, config.nt + 1): fidelity_t = Fidelity(ro_sqrt=ro_0_sqrt, sigma=ro_t) # print(fidelity_t) # exit(0) ro_t = U.data.dot(ro_t).dot(U_conj) # print(np.around(ro_t, 3)) # if t > 3: # exit(1) # fidelity_t = D(ro_0, ro_t) # fidelity_t = D_HS(ro_0, ro_t) # fidelity_t = round(fidelity_t, 5) # print(np.abs(np.diag(ro_t.data))) # exit(0) fidelity.append(fidelity_t) # exit(0) print('Min fidelity:', np.min(fidelity)) print('Max fidelity:', np.max(fidelity)) list_to_csv(config.fid_csv, fidelity, header=['fidelity'])
def run(ro_0, H, dt, nt, config, fidelity_mode=False): # -------------------------------------------------------- U = Unitary(H, dt) if __debug__: U.write_to_file(config.U_csv) U_conj = U.conj() # -------------------------------------------------------- if fidelity_mode: ro_0_sqrt = lg.fractional_matrix_power(ro_0.data, 0.5) fidelity = [] ro_t = ro_0.data # ---------------------------------------------------------------------------- with open(config.z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt): diag_abs = np.abs(np.diag(ro_t)) trace_abs = np.sum(diag_abs) Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) writer.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------------------------- if fidelity_mode: fidelity_t = round(Fidelity(ro_0_sqrt, ro_t), 5) fidelity_t = "{:.5f}".format(fidelity_t) fidelity.append(fidelity_t) # -------------------------------------------------------------------- ro_t = U.data.dot(ro_t).dot(U_conj) # ---------------------------------------------------------------------------- states = H.states write_x(states, config.x_csv, ind_1=[0, H.n], ind_2=[H.n, 0]) write_t(T_str_v(config.T), config.nt, config.y_csv) # ---------------------------------------------------------- if fidelity_mode: list_to_csv(config.fid_csv, fidelity, header=["fidelity"])
def run_ro(ro_0, H, dt, nt): # --------------------------------------------------------------------------------------------- U = Unitary(H, dt) if __debug__: U.write_to_file(U_csv) U_conj = U.conj() # ---------------------------------------------------- ro_t = ro_0.data ro_0_sqrt = lg.fractional_matrix_power(ro_0.data, 0.5) # ---------------------------------------------------- FIDELITY = [] # ---------------------------------------------------- with open(z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt): p = np.abs(np.diag(ro_t)) trace = np.sum(p) Assert(abs(1 - trace) <= 0.1, "ro is not normed", cf()) writer.writerow(["{:.5f}".format(x) for x in p]) # -------------------------------------------------- fidelity_t = Fidelity(ro_0_sqrt, ro_t) FIDELITY.append(fidelity_t) # -------------------------------------------------- ro_t = U.data.dot(ro_t).dot(U_conj) # --------------------------------------------------------------------------------------------- states = H.states write_x(states, x_csv, ind_1=[0, H.n], ind_2=[H.n, 0]) write_t(T_str_v(T), nt, y_csv) # write_t(T / mks / 1e-2, nt, y_csv) # # ------------------------- list_to_csv(fid_csv, FIDELITY, header=["fidelity"])
def run(ro_0, H, dt, nt, l, config, fidelity_mode=False): # -------------------------------------------------------- a = get_a(H, H.capacity, H.n) _a = Matrix(H.size, H.size, dtype=np.complex128) _a.data = a.data # _a.write_to_file('a') # print(a) # if __debug__: # _a.write_to_file(a_csv) a_cross = a.getH() across_a = np.dot(a_cross, a) _a_cross_a = Matrix(H.size, H.size, dtype=np.complex128) _a_cross_a.data = across_a.data # print(a) # exit(1) # print("H:\n", color="yellow") # H.matrix.set_header(H.states) # H.matrix.print_pd() # _a.set_header(H.states) # print("a:\n", color="yellow") # _a.print_pd() # _a_cross_a.set_header(H.states) # print("acrossa:\n", color="yellow") # _a_cross_a.print_pd() # for i in range(a.shape[0]): # for j in range(a.shape[1]): # if a[i, j]: # print(H.states[i], H.states[j], a[i, j]) # exit(1) # print(np.matrix(a)) # if __debug__: # _a_cross_a.write_to_file(a_cross_a_csv) # -------------------------------------------------------- U = Unitary(H, dt) # if __debug__: # U.write_to_file(config.U_csv) U_conj = U.conj() # -------------------------------------------------------- ro_t = ro_0.data for k, v in H.states.items(): if v == [H.capacity, 0, 0]: index1 = k if v == [0, H.n, 0]: index2 = k if fidelity_mode: ro_0_sqrt = lg.fractional_matrix_power( ro_0.data[index1:index2 + 1, index1:index2 + 1], 0.5) fidelity = [] # print(np.round(np.diag(ro_t), 3)) # print() # print("ρ(0):\n", color="yellow") # ro_0.set_header(H.states) # ro_0.print_pd() H.print_states() # ---------------------------------------------------------------------------- with open(config.z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt + 1): # print(t, nt) # diag_abs = np.abs(np.diag(ro_t)[index1:index2 + 1]) # trace_abs = np.sum(diag_abs) diag = np.diag(ro_t) p = np.abs(diag[index1:index2 + 1]) # p = np.asarray(p).reshape(-1) # p = np.around(p, precision) norm = np.sum(np.abs(diag)) # print(t, "norm:", norm) Assert( abs(1 - norm) <= 0.1, str(t) + " " + str(norm) + ": ro is not normed", cf()) # Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) writer.writerow(["{:.3f}".format(x) for x in p]) # writer.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------------------------- if fidelity_mode: fidelity_t = Fidelity( ro_0_sqrt, ro_t[index1:index2 + 1, index1:index2 + 1]) fidelity.append(fidelity_t) # -------------------------------------------------------------------- # for i in np.diag(ro_t): # v = str(np.round(i, 3)) # # v = str(np.round(i, 3)) # print(v.rjust(5, ' '), end=' ') # v = str(np.round(np.sum(np.abs(diag[:-1])), 3)) # print(v) L = get_L(ro_t, a, a_cross, across_a) ro_t = U.data.dot(ro_t).dot(U_conj) + dt * (config.l * L) # ---------------------------------------------------------------------------- states = {} # print(np.round(np.diag(ro_t), 3)) # ro_0.data = ro_t # ro_0.set_header(H.states) # print("ρ(t):\n", color="yellow") # ro_0.print_pd() # ro_0.data = L # print("L(t):\n", color="yellow") # ro_0.write_to_file("L") # ro_0.print_pd() # exit(1) cnt = 0 for k in range(index1, index2 + 1): states[cnt] = (H.states[k])[1:] cnt += 1 print(states) write_x(states, config.x_csv, ind=[[0, H.n], [H.n, 0]]) write_t(T_str_v(config.T), config.nt, config.y_csv) # write_t(config.T / config.mks / 1e-2, config.nt, config.y_csv) # ---------------------------------------------------------- if fidelity_mode: list_to_csv(config.fid_csv, fidelity, header=["fidelity"])
def run(ro_0, H, dt, nt, config, fidelity_mode=False): # -------------------------------------------------------- U = Unitary(H, dt) if __debug__: U.write_to_file(config.U_csv) U_conj = U.conj() # -------------------------------------------------------- if fidelity_mode: ro_0_sqrt = lg.fractional_matrix_power(ro_0.data, 0.5) fidelity = [] ro_t = ro_0.data # ---------------------------------------------------------------------------- p_bin = dict.fromkeys(H.states_bin_keys) # -------------------------------------------------------- with open(config.z_csv, "w") as csv_file: writer = csv.writer(csv_file, quoting=csv.QUOTE_NONE, lineterminator="\n") with open(config.z_all_csv, "w") as csv_all_file: writer_all = csv.writer(csv_all_file, quoting=csv.QUOTE_NONE, lineterminator="\n") for t in range(0, nt): diag_abs = np.abs(np.diag(ro_t)) trace_abs = np.sum(diag_abs) Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) for k, v in p_bin.items(): p_bin[k] = 0 for k, v in H.states_bin.items(): for ind in v: p_bin[k] += diag_abs[ind] v_bin = [p_bin[k] for k in H.states_bin_keys] # -------------------------------------------------- writer.writerow(["{:.5f}".format(x) for x in v_bin]) # if __debug__: # writer_all.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------- if fidelity_mode: fidelity_t = Fidelity(ro_0_sqrt, ro_t) fidelity.append(fidelity_t) # -------------------------------------------------- ro_t = U.data.dot(ro_t).dot(U_conj) # -------------------------------------------------------------- states_bin = {} cnt = 0 for k in H.states_bin_keys: if k == "[" + str(0) + "," + str(int( config.n / 2)) + "]" or k == "[" + str(int( config.n / 2)) + "," + str(0) + "]": states_bin[cnt] = str(k) else: states_bin[cnt] = "" cnt += 1 # ---------------------------------------------------------- states = {} cnt = 0 for v in H.states_bin_keys: states[cnt] = v cnt += 1 # ---------------------------------------------------------- write_x(states, config.x_csv) write_t(config.T / config.mks, config.nt, config.y_csv) # ---------------------------------------------------------- if fidelity_mode: list_to_csv(fid_csv, fidelity, header=["fidelity"])