def __init__(self, states, init_state, amplitude=1): Assert(isinstance(states, dict), "states is not dict", cf()) Assert(isinstance(init_state, list), "init_state is not list", cf()) Assert(len(states) > 1, "w_0 is not set", cf()) self.states = states k_found = None # print(states) # print(init_state) for k, v in states.items(): if init_state == v: k_found = k break Assert(k_found is not None, "w_0 is not set", cf()) super(WaveFunction, self).__init__(m=len(states), n=1, dtype=np.complex128) self.data[k_found] = amplitude
def __init__(self, m, n, dtype): Assert(m > 0, "m <= 0", cf()) Assert(n > 0, "n <= 0", cf()) self.m = m self.n = n self.data = np.matrix(np.zeros([m, n]), dtype=dtype)
def sigma(i, j, n_levels): Assert(i >= 0, "i < 0", cf()) Assert(j >= 0, "j < 0", cf()) Assert(n_levels > 0, "n_levels <= 0", cf()) sigma = SparseMatrix(m=n_levels, n=n_levels, orient='row') sigma.add((j, i), 1) return sigma
def remove_row(self, i): Assert(i >= 0, 'i < 0', cf()) Assert(i < self.m, 'i >= m', cf()) if i not in self.row.keys(): return self.count -= self.row[i]['count'] del self.row[i]
def swap_rows(self, i_1, i_2): Assert(i_1 >= 0, 'i < 0', cf()) Assert(i_1 < self.m, 'i >= m', cf()) Assert(i_2 >= 0, 'i < 0', cf()) Assert(i_2 < self.m, 'i >= m', cf()) if i_1 == i_2: return self.row[i_1], self.row[i_2] = self.row[i_2], self.row[i_1]
def add_row(self, i, k): Assert(i >= 0, 'i < 0', cf()) Assert(i < self.m, 'i >= m', cf()) if i not in self.row.keys(): return if k == 0: return for j_pos, j in enumerate(self.row[i]['ind']): self.add_item(i, j_pos, k)
def print_row(self, i, sep='\t', precision=0, file=sys.stdout): Assert(i >= 0, "i < 0", cf()) if i in self.row: row = [] for j in range(self.n): found = False for k, ind_j in enumerate(self.row[i]['ind']): # print(self.row[i]['ind'], 'ind_j=', ind_j, 'j=', j) if ind_j == j: row += [self.row[i]['items'][k]] found = True break if not found: row += [0] # re = format(value.real, "." + str(precision) + "f") # v = row_str = sep.join( [str(format(i, "." + str(precision) + "f")) for i in row]) else: row_str = sep.join(['0' for i in range(self.n)]) print(row_str, end='', file=file)
def __init__(self, n, wc, wa, g): Assert(isinstance(n, int), "n is not integer", cf()) Assert(isinstance(wc, (int, float)), "wc is not numeric", cf()) # Assert(isinstance(wa, (int, float)), "wa is not numeric", cf()) # Assert(isinstance(g, (int, float)), "g is not numeric", cf()) Assert(n > 0, "n <= 0", cf()) Assert(wc > 0, "wc <= 0", cf()) # Assert(wa > 0, "wa <= 0", cf()) # Assert(g > 0, "g <= 0", cf()) self.n = n self.wc = wc self.wa = wa self.g = g
def identity(n): Assert(n > 0, "n <= 0", cf()) I = SparseMatrix(orient='row') for i in range(n): I.add((i, i), 1) return I
def __init__(self, m=0, n=0, orient='row', heap_usage=False): Assert(m >= 0, "m < 0", cf()) Assert(n >= 0, "n < 0", cf()) self.m = m self.n = n self.count = 0 self.row = dict() self.col = dict() self.items = dict() self.ind = dict() self.orient = orient self.heap_usage = heap_usage self.heap = set()
def remove_from_heap(self, i, j): found = False for v in self.heap: if v[0] == (i, j): self.heap.remove(v) found = True break Assert(found, 'not found', cf())
def divide_row(self, i, k): Assert(k != 0, 'k == 0', cf()) Assert(i >= 0, 'i < 0', cf()) Assert(i < self.m, 'i >= m', cf()) if i not in self.row.keys(): return if k == 1: return if k == -1: for j_pos, j in enumerate(self.row[i]['ind']): self.row[i]['items'][j_pos] = -self.row[i]['items'][j_pos] else: for j_pos, j in enumerate(self.row[i]['ind']): self.div_item(i, j_pos, k) return
def check_states(self): try: Assert(len(self.states) > 0, "len(states) <= 0", cf()) for state in self.states.values(): ph = state[0] at = state[1] Assert(0 <= ph <= self.capacity, "incorrect state " + str(state), cf()) Assert(ph + np.sum(at) == self.capacity, "incorrect state " + str(state), cf()) for n_ in range(len(at)): Assert(0 <= at[n_] <= 1, "incorrect state " + str(state), cf()) except: print_error("incorrect states generation", cf()) exit(1) return
def set_ampl(self, state, amplitude=1): k_found = None for k, v in self.states.items(): print(state, v) if state == v: k_found = k break Assert(k_found is not None, "w_0 is not set", cf()) self.data[k_found] = amplitude
def check_unitarity(self): data = self.data data_H = self.data.getH() I = np.eye(len(data)) Assert( np.all(abs(data.dot(data_H) - I) < eps) and np.all(abs(data_H.dot(data) - I) < eps), "matrix is not unitary", cf()) return
def __init__(self, wc, wa, g, n_atoms, n_levels=2): Assert(isinstance(wc, (int, float)), "wc is not numeric", cf()) Assert(isinstance(wa, (int, float)), "wa is not numeric", cf()) Assert(isinstance(g, (int, float)), "g is not numeric", cf()) Assert(isinstance(n_atoms, int), "n_atoms is not integer", cf()) Assert(wc > 0, "wc <= 0", cf()) Assert(wa > 0, "wa <= 0", cf()) Assert(g > 0, "g <= 0", cf()) Assert(n_atoms > 0, "n <= 0", cf()) self.wc = wc self.wa = wa self.g = g self.n_atoms = n_atoms self.n_levels = n_levels
def sub_rows(self, i1, i2, jj): i1_set = set(self.row[i1]['ind']) i2_set = set(self.row[i2]['ind']) inter = i1_set & i2_set diff = i2_set - i1_set for c in inter: j_1 = self.row[i1]['ind'].index(c) j_2 = self.row[i2]['ind'].index(c) v_2 = self.row[i2]['items'][j_2] * self.row[i1]['items'][jj] self.sub_item(i1, j_1, v_2, autoremove=False) for c in diff: j_2 = c ind_2 = self.row[i2]['ind'].index(c) v_2 = self.row[i2]['items'][ind_2] self.add((i1, j_2), -v_2) for j_pos in range(len(self.row[i1]['items']))[::-1]: self.check_zero(i1, None, j_pos) # if i1 in self.row.keys(): # for j_pos, j in enumerate(self.row[i1]['items']): # # print(self.row[i1]['items']) # Assert(self.row[i1]['items'][j_pos] != 0, # 'nil' + str(self.row[i1]['items']), cf()) if i1 in self.row.keys(): self.row[i1]['count'] = len(self.row[i1]['items']) size = len(self.row[i1]['items']) count = self.row[i1]['count'] Assert( len(self.row[i1]['items']) == self.row[i1]['count'], 'count != size(items):' + str(count) + "!=" + str(size) + str(self.row[i1]['items']), cf())
def __init__(self, capacity, cavity): Assert(isinstance(cavity, Cavity), "cavity is not Cavity", cf()) Assert(isinstance(capacity, int), "capacity is not int", cf()) Assert(capacity > 0, "capacity <=0", cf()) self.cavity = cavity self.D = {} # ------------ n = cavity.n wc = cavity.wc wa = cavity.wa g = cavity.g # ------------ _min = min(capacity, n) self.states = {} # --------------------------------------- self.init_states(capacity, n) # --------------------------------------- self.size = len(self.states) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) i = 1 for i1 in range(0, _min + 1): for i2 in range(0, min(n, capacity - i1) + 1): j = 1 for j1 in range(0, _min + 1): for j2 in range(0, min(n, capacity - j1) + 1): if i1 != j1: p = [i1, j1] elif i2 != j2: p = [i2, j2] else: p = [1, 2] mi = min(p[0], p[1]) kappa = sqrt((n - mi) * (mi + 1)) if abs(i1 - j1) + abs(i2 - j2) == 1: _max = max(capacity - i1 - i2, capacity - j1 - j2) self.matrix.data[i - 1, j - 1] = g * \ sqrt(max(capacity - i1 - i2, capacity - j1 - j2)) * kappa elif abs(i1 - j1) + abs(i2 - j2) == 0: self.matrix.data[ i - 1, j - 1] = (capacity - (i1 + i2)) * wc + (i1 + i2) * wa else: self.matrix.data[i - 1, j - 1] = 0 j += 1 i += 1
def __init__(self, capacity, cavity): Assert(isinstance(capacity, int), "capacity is not integer", cf()) Assert(capacity > 0, "capacity <= 0", cf()) # Assert(isinstance(cavity, "BipartiteGeneral.Cavity.Cavity"), "capacity is not integer", cf()) self.capacity = capacity self.cavity = cavity self.n = cavity.n self.wc = cavity.wc self.wa = cavity.wa self.g = cavity.g self.states = self.get_states() self.size = len(self.states) # print(len(self.states)) # exit(1) self.get_states_bin() # self.print_bin_states() self.matrix = Matrix(self.size, self.size, dtype=np.complex128) # self.matrix_html = np.empty([self.size, self.size], dtype=">U900") for i in range(self.size): i_state = self.states[i] i_ph = i_state[0] i_at = i_state[1] for j in range(self.size): j_state = self.states[j] j_ph = j_state[0] j_at = j_state[1] if i_state == j_state: self.matrix.data[i, j] = self.wc * i_ph # self.matrix_html[i][j] += "wc" + DOT() + str(i_ph) + " " for n_ in range(len(i_at)): self.matrix.data[i, j] += self.wa[n_] * i_at[n_] # self.matrix_html[i][j] += "wa" + SUB(n_) + DOT() + str(i_at[n_]) + " " else: d_ph = j_ph - i_ph if abs(d_ph) == 1: diff_at_cnt = 0 for n_ in range(len(i_at)): d_at = j_at[n_] - i_at[n_] if d_ph + d_at == 0: diff_at_cnt += 1 diff_at_num = n_ elif d_at != 0: diff_at_cnt = 0 break if diff_at_cnt > 1: break if diff_at_cnt == 1: if d_ph > 0: k = a_cross(i_ph) * i_at[diff_at_num] self.matrix.data[i, j] = self.g[diff_at_num] * k else: k = a_cross(j_ph) * j_at[diff_at_num] self.matrix.data[i, j] = self.g[diff_at_num] * k # k = a_cross(i_ph) * i_at[diff_at_num] # self.matrix.data[i, j] = self.g[diff_at_num] self.matrix.check_hermiticity() return
def add(self, ind, value): Assert(len(ind) == 2, "len(ind) != 2", cf()) i = ind[0] j = ind[1] Assert(i >= 0, "i < 0", cf()) Assert(j >= 0, "j < 0", cf()) if self.n is None: self.n = j + 1 else: self.n = max(self.n, j + 1) if self.m is None: self.m = i + 1 else: self.m = max(self.m, i + 1) if value == 0: return if self.orient == 'row': if not (i in self.items): self.ind[i] = [] self.items[i] = [] # -------------------------------------- inserted = False for pos, v in enumerate(self.ind[i]): if j < v: self.ind[i].insert(pos, j) self.items[i].insert(pos, value) inserted = True break elif j == v: self.items[i][pos] = value inserted = True break if not inserted: self.ind[i].append(j) self.items[i].append(value) # -------------------------------------- if i not in self.row: count = 1 else: count = self.row[i]['count'] + 1 self.row[i] = { 'ind': self.ind[i], 'items': self.items[i], 'count': count } else: if not (j in self.items): self.ind[j] = [] self.items[j] = [] self.ind[j].append(i) self.items[j].append(value) self.col[i] = {'ind': self.ind[j], 'items': self.items[j]} if self.heap_usage: self.heap.add(((i, j), value)) self.count += 1 return
def __truediv__(self, k): Assert(k != 0, "k == 0", cf()) return self.__mul__(1.0 / k)
def check_hermiticity(ro): Assert(self.is_hermitian(), "not hermitian", cf())
def normalize(self): norm = np.linalg.norm(self.data) Assert(norm > 0, "norm <= 0", cf()) self.data /= norm
def __init__(self, capacity, cavity): Assert(isinstance(capacity, int), "capacity is not integer", cf()) Assert(capacity > 0, "capacity <= 0", cf()) # Assert(isinstance(cavity, "BipartiteGeneral.Cavity.Cavity"), "capacity is not integer", cf()) self.capacity = capacity self.cavity = cavity self.n = cavity.n self.wc = cavity.wc self.wa = cavity.wa self.g = cavity.g self.states = self.get_states() self.get_states_bin() # self.print_states() self.size = len(self.states) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) # self.matrix_html = np.empty([self.size, self.size], dtype=">U900") for i in range(self.size): i_state = self.states[i] i_ph = i_state[0] i_at = i_state[1] for j in range(self.size): j_state = self.states[j] j_ph = j_state[0] j_at = j_state[1] # self.matrix_html[i, j] = "" if i_state == j_state: self.matrix.data[i, j] = self.wc * i_ph # if(self.matrix_html[i][j] != ""): # self.matrix_html[i][j] += "+" # self.matrix_html[i][j] += "wc" + DOT() + str(i_ph) + " " for n_ in range(len(i_at)): if i_at[n_] != 0: self.matrix.data[i, j] += self.wa[n_] * i_at[n_] # if(self.matrix_html[i][j] != ""): # self.matrix_html[i][j] += "+" # self.matrix_html[i][j] += "wa" + SUB(n_) + DOT() + str(i_at[n_]) + " " else: d_ph = j_ph - i_ph if abs(d_ph) == 1: diff_at_cnt = 0 for n_ in range(len(i_at)): d_at = j_at[n_] - i_at[n_] if d_ph + d_at == 0: diff_at_cnt += 1 diff_at_num = n_ elif d_at != 0: diff_at_cnt = 0 break if diff_at_cnt > 1: break if diff_at_cnt == 1: if d_ph > 0: k = a_cross(i_ph) * i_at[diff_at_num] self.matrix.data[i, j] = self.g[diff_at_num] * k # self.matrix_html[i][j] = "g" + SUB(diff_at_num) + DOT() + A_CROSS(i_ph) + \ # """<math display="block"><mrow><msqrt><mn> · """ + \ # A_CROSS(i_ph) + """</mn></msqrt><mo>=</mo></mrow></math>""" else: k = a_cross(j_ph) * j_at[diff_at_num] self.matrix.data[i, j] = self.g[diff_at_num] * k # self.matrix_html[i][j] = "g" + SUB(diff_at_num) + DOT() + A_CROSS(j_ph) self.matrix.check_hermiticity() return
def check_hermiticity(self): Assert(np.all(abs(self.data - self.data.getH()) < eps), "matrix is not hermitian", cf())
def run_RWA(w0, H, t0, t1, initstate, certain_state, nt=200, ymin=0, ymax=1, RWA=True, title='title', color='blue'): # ------------------------------------------------------------------------------------------------------------------ if not (nt in range(0, 501)): return -1 Assert(t0 >= 0, "t0 < 0", cf()) Assert(t1 > 0, "t1 <= 0", cf()) Assert(t0 < t1, "t0 >= t1", cf()) if len(np.shape(H.matrix.data)) != 2: return -1 # if np.shape(H)[0] != np.shape(H)[1]: # return -1 # if np.shape(H)[0] != len(w0): # return -1 # ------------------------------------------------------------------------------------------------------------------ # nt = t1 * 100 # nt = 100 t = np.linspace(t0, t1, nt + 1) dt = t[1] - t[0] U = Unitary(H, dt) # ------------------------------------------------------------------------------------------------------------------ state = [] at_count = len(initstate[1]) for i in range(0, len(w0.data)): ph_count = int(i / pow(2, at_count)) st_number = i % pow(2, at_count) at_binary = bin(st_number)[2:].zfill(at_count) state.append('[' + str(ph_count) + '|' + at_binary + ']') # ------------------------------------------------------------------------------------------------------------------ w = [] wt = w0 for i in range(0, nt + 1): wt = get_wdt(wt.data, U.data) Assert(np.max(wt) <= 1, "np.max(wt) > 1", cf()) w.append(np.abs(wt)) w = np.array(w) w = w[:, :, 0] # ------------------------------------------------------------------------------------------------------------------ st = certain_state[0] * (pow(2, at_count)) at = 0 for i in range(0, at_count): at += pow(2, i) * certain_state[1][at_count - i - 1] # ------------------------------------------------------------------------------------------------------------------ animator.make_plot(t, t0, t1, ymin, ymax, w[:, st + at], color, title=title, X=r'$t,\ мкс$', Y=r'$Probability$ ') return
def run_w(w_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: fidelity = [] w_0 = np.matrix(w_0.data) w_t = np.array(w_0.data) # ---------------------------------------------------------------------------- dt_ = nt / (config.T / config.mks) / 20000 * 1000 nt_ = int(nt / dt_) z_0 = [] z_1 = [] z_max = [] # ind_0 = None # ind_1 = None # for k, v in H.states.items(): # if v == [0, H.n]: # ind_0 = k # elif v == [H.n, 0]: # ind_1 = k 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): w_t_arr = w_t.reshape(1, -1)[0] diag_abs = np.abs(w_t_arr)**2 trace_abs = np.sum(diag_abs) Assert(abs(1 - trace_abs) <= 0.1, "ro is not normed", cf()) # -------------------------------------------------------------------- if fidelity_mode: w_t = np.matrix(w_t) D = w_0.getH().dot(w_t).reshape(-1)[0, 0] fidelity_t = round(abs(D), 3) fidelity_t = "{:>.5f}".format(fidelity_t) fidelity.append(fidelity_t) # z_0.append("{:.5f}".format(diag_abs[ind_0])) # z_1.append("{:.5f}".format(diag_abs[ind_1])) # zmax = 0 # for i_ in range(0, len(diag_abs)): # if i_ != ind_0 and i_ != ind_1 and diag_abs[i_] > zmax: # # exit(1) # zmax = diag_abs[i_] # z_max.append(zmax) # z_1.append(round(diag_abs[ind_1], config.precision)) # z_1.append(float(diag_abs[ind_1])) # if t % nt_ == 0: writer.writerow(["{:.5f}".format(x) for x in diag_abs]) # -------------------------------------------------------------------- w_t = np.array(U.data.dot(w_t)) # ---------------------------------------------------------------------------- states = H.states write_x(states, config.x_csv, ind=[[1, [1, 1, 0]], [1, [0, 1, 1]], [1, [1, 0, 1]], [2, [0, 1, 0]], [2, [0, 0, 1]], [2, [1, 0, 0]], [3, [0, 0, 0]]]) # 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)
def __init__(self, n, wc, wa, g): Assert(isinstance(n, int), "n is not integer", cf()) Assert(isinstance(wc, (int, float)), "wc is not numeric", cf()) Assert(isinstance(wa, list), "wa is not list", cf()) Assert(isinstance(g, list), "g is not list", cf()) Assert(n > 0, "n <= 0", cf()) Assert(wc > 0, "wc <= 0", cf()) Assert(len(wa) == n, "len(wa) != n", cf()) for i in range(len(wa)): Assert(isinstance(wa[i], (int, float)), "wa[" + str(i) + "] is not numeric", cf()) Assert(wa[i] > 0, "wa[" + str(i) + "] <= 0", cf()) Assert(len(g) == n, "len(g) != n", cf()) for i in range(len(g)): Assert(isinstance(g[i], (int, float)), "g[" + str(i) + "] is not numeric", cf()) Assert(g[i] > 0, "g[" + str(i) + "] <= 0", cf()) self.n = n self.wc = wc self.wa = wa self.g = g