class Unitary: def __init__(self, H, dt): self.size = H.size H = np.array(H.matrix.data, dtype=np.complex128) self.data = np.matrix(lg.expm(-1j * H * dt), dtype=np.complex128) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) self.matrix.data = self.data def get_conj(self): return self.data.getH() def write_to_file(self, filename): U = Matrix(self.size, self.size) print(U.m) U.data = self.data U.write_to_file(filename) def print(self): print('U:') self.matrix.print() print() return
def __init__(self, H, dt): self.size = H.size H = np.array(H.matrix.data, dtype=np.complex128) self.data = np.matrix(lg.expm(-1j * H * dt), dtype=np.complex128) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) self.matrix.data = self.data
def __init__(self, capacity, cavity): self.cavity = cavity self.g = cavity.g self.D = {} self.count = 0 M = capacity self.n = n = cavity.n wc = cavity.wc wa = cavity.wa g = cavity.g _min = min(M, n) dime = (_min + 1)**2 # print("dime:", dime) H = np.array([np.zeros(dime) for i in range(0, dime)]) self.matrix = Matrix(dime, dime, dtype=np.complex128) i = 1 self.states = states = {} count = 0 for i1 in range(0, _min + 1): for i2 in range(0, min(n, M - i1) + 1): j = 1 states[count] = [i1, i2] count += 1 # D[count] = [I-i1-i2, i1, i2] for j1 in range(0, _min + 1): for j2 in range(0, min(n, M - 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(M - i1 - i2, M - j1 - j2) self.matrix.data[i - 1, j - 1] = g * sqrt( max(M - i1 - i2, M - j1 - j2)) * kappa # print(i1, j1, i2, j2, "H[", i-1, j-1, "] = ", H[i-1][j-1], ": g * # sqrt(", max(M - i1 - i2, M - j1 - j2), ") * sqrt(", (n - mi) * (mi + 1), # ")") elif abs(i1 - j1) + abs(i2 - j2) == 0: self.matrix.data[ i - 1, j - 1] = (M - (i1 + i2)) * wc + (i1 + i2) * wa # print("H[",i-1,",",j-1,"]=", self.matrix.data[i-1, j-1]) # print(i1, j1, i2, j2, "H[", i-1, j-1, "] = ", H[i-1][j-1], ": wc *", (M - (i1 + i2)), " + wa * ", (i1 + i2)) else: self.matrix.data[i - 1, j - 1] = 0 j += 1 i += 1 self.data = np.matrix(H) self.size = np.shape(self.data)[0]
class Hamiltonian: # --------------------------------------------------------------------------------------------- def __init__(self, capacity, cavity): self.cavity = cavity self.g = cavity.g self.D = {} self.count = 0 M = capacity self.n = n = cavity.n wc = cavity.wc wa = cavity.wa g = cavity.g _min = min(M, n) dime = (_min + 1)**2 # print("dime:", dime) H = np.array([np.zeros(dime) for i in range(0, dime)]) self.matrix = Matrix(dime, dime, dtype=np.complex128) i = 1 self.states = states = {} count = 0 for i1 in range(0, _min + 1): for i2 in range(0, min(n, M - i1) + 1): j = 1 states[count] = [i1, i2] count += 1 # D[count] = [I-i1-i2, i1, i2] for j1 in range(0, _min + 1): for j2 in range(0, min(n, M - 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(M - i1 - i2, M - j1 - j2) self.matrix.data[i - 1, j - 1] = g * sqrt( max(M - i1 - i2, M - j1 - j2)) * kappa # print(i1, j1, i2, j2, "H[", i-1, j-1, "] = ", H[i-1][j-1], ": g * # sqrt(", max(M - i1 - i2, M - j1 - j2), ") * sqrt(", (n - mi) * (mi + 1), # ")") elif abs(i1 - j1) + abs(i2 - j2) == 0: self.matrix.data[ i - 1, j - 1] = (M - (i1 + i2)) * wc + (i1 + i2) * wa # print("H[",i-1,",",j-1,"]=", self.matrix.data[i-1, j-1]) # print(i1, j1, i2, j2, "H[", i-1, j-1, "] = ", H[i-1][j-1], ": wc *", (M - (i1 + i2)), " + wa * ", (i1 + i2)) else: self.matrix.data[i - 1, j - 1] = 0 j += 1 i += 1 self.data = np.matrix(H) self.size = np.shape(self.data)[0] # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_index(self, state): for k, v in self.states.items(): if v == state: return k # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_states(self): return self.states # --------------------------------------------------------------------------------------------- def print_html(self, filename): # f = open(filename, "w") # html = """ <!DOCTYPE html> # <html> # <head> # <title> # States # </title> # </head> # <body> # <table border=1> # """ # html += "<tr>" html += "<td>" html += "</td>" # # for i in range(0, len(self.states)): # # html += "<td>" # html += "[" + str(self.states[i].n1) + "," + str(self.states[i].n2) + "]" # html += "</td>" # # html += "</tr>" # # for i in range(0, len(self.states)): # # html += "<tr>" # html += "<td>" # html += "[" + str(self.states[i].n1) + "," + str(self.states[i].n2) + "]" # html += "</td>" # # for j in range(0, len(self.states)): # # html += "<td>" # # if sqrt: # # html += "√" + "<span style="text-decoration:overline;">" + str(abs(self.matrix.data[i, j]) / self.g) + "</span>" # else: # # html += "√" + "<span style="text-decoration:overline;">" + str(abs(self.matrix.data[i, j])) + "</span>" # # html += "</td>" # # html += "</tr>" # html += """ </table> # </body> # </html> # """ # f.write(html) # f.close() # webbrowser.open(filename) return # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def init_states(self): self.states = [] s = St(self.cavity) self.states.append(copy.copy(s)) while (s.inc()): self.states.append(copy.copy(s)) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def print_states(self): print("States:", color="green") print() for k, v in self.states.items(): print(v) print()
def __init__(self, capacity, cavity): self.cavity = cavity self.states = {} count = 0 self.capacity = capacity M = capacity self.n = n = cavity.n wc = cavity.wc wa = cavity.wa g = cavity.g self.DIME = [] self.H_dims = {} # --------------------------------------- for I in range(M, -1, -1): _min = min(I, n) dime = (_min + 1)**2 self.DIME.append(dime) # for I in range(M, -1, -1): # _min = min(I, n) # count = 0 # for i1 in range(0, _min + 1): # for i2 in range(0, min(n, I - i1) + 1): # count += 1 # self.DIME.append(count) self.size = np.sum(self.DIME) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) d = 0 for I in range(M, -1, -1): i = 1 COUNT = count for i1 in range(0, min(I, n) + 1): for i2 in range(0, min(n, I - i1) + 1): j = 1 self.states[count] = [I - i1 - i2, i1, i2] for j1 in range(0, min(I, n) + 1): for j2 in range(0, min(n, I - 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)) count += (i == j) if abs(i1 - j1) + abs(i2 - j2) == 1: self.matrix.data[ COUNT + i - 1, COUNT + j - 1] = g * sqrt( max(I - i1 - i2, I - j1 - j2)) * kappa elif abs(i1 - j1) + abs(i2 - j2) == 0: self.matrix.data[ COUNT + i - 1, COUNT + j - 1] = (I - (i1 + i2)) * wc + (i1 + i2) * wa else: self.matrix.data[COUNT + i - 1, COUNT + j - 1] = 0 j += 1 i += 1 self.matrix.data = self.matrix.data[0:count, 0:count] self.data = self.matrix.data self.size = np.shape(self.matrix.data)[0] self.matrix.m = self.matrix.n = self.size
class Hamiltonian: # --------------------------------------------------------------------------------------------- def __init__(self, capacity, cavity): self.cavity = cavity self.states = {} count = 0 self.capacity = capacity M = capacity self.n = n = cavity.n wc = cavity.wc wa = cavity.wa g = cavity.g self.DIME = [] self.H_dims = {} # --------------------------------------- for I in range(M, -1, -1): _min = min(I, n) dime = (_min + 1)**2 self.DIME.append(dime) # for I in range(M, -1, -1): # _min = min(I, n) # count = 0 # for i1 in range(0, _min + 1): # for i2 in range(0, min(n, I - i1) + 1): # count += 1 # self.DIME.append(count) self.size = np.sum(self.DIME) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) d = 0 for I in range(M, -1, -1): i = 1 COUNT = count for i1 in range(0, min(I, n) + 1): for i2 in range(0, min(n, I - i1) + 1): j = 1 self.states[count] = [I - i1 - i2, i1, i2] for j1 in range(0, min(I, n) + 1): for j2 in range(0, min(n, I - 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)) count += (i == j) if abs(i1 - j1) + abs(i2 - j2) == 1: self.matrix.data[ COUNT + i - 1, COUNT + j - 1] = g * sqrt( max(I - i1 - i2, I - j1 - j2)) * kappa elif abs(i1 - j1) + abs(i2 - j2) == 0: self.matrix.data[ COUNT + i - 1, COUNT + j - 1] = (I - (i1 + i2)) * wc + (i1 + i2) * wa else: self.matrix.data[COUNT + i - 1, COUNT + j - 1] = 0 j += 1 i += 1 self.matrix.data = self.matrix.data[0:count, 0:count] self.data = self.matrix.data self.size = np.shape(self.matrix.data)[0] self.matrix.m = self.matrix.n = self.size # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_index(self, state): for k, v in self.states.items(): if v == state: return k # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_states(self): return self.states # --------------------------------------------------------------------------------------------- def print_html(self, filename): f = open(filename, "w") # html = """ <!DOCTYPE html> # <html> # <head> # <title> # States # </title> # </head> # <body> # <table border=1> # """ # html += "<tr>" html += "<td>" html += "</td>" # # for i in range(0, len(self.states)): # # html += "<td>" # html += "[" + str(self.states[i].n1) + "," + str(self.states[i].n2) + "]" # html += "</td>" # # html += "</tr>" # # for i in range(0, len(self.states)): # # html += "<tr>" # html += "<td>" # html += "[" + str(self.states[i].n1) + "," + str(self.states[i].n2) + "]" # html += "</td>" # # for j in range(0, len(self.states)): # # html += "<td>" # # if sqrt: # # html += "√" + "<span style="text-decoration:overline;">" + str(abs(self.matrix.data[i, j]) / self.g) + "</span>" # else: # # html += "√" + "<span style="text-decoration:overline;">" + str(abs(self.matrix.data[i, j])) + "</span>" # # html += "</td>" # # html += "</tr>" # html += """ </table> # </body> # </html> # """ # f.write(html) f.close() webbrowser.open(filename) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- # def init_states(self): # self.states = [] # s = St(self.cavity) # self.states.append(copy.copy(s)) # while(s.inc()): # self.states.append(copy.copy(s)) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def print_states(self): print("States:", color="green") print() for k, v in self.states.items(): print(v) print()
def write_to_file(self, filename): U = Matrix(self.size, self.size) print(U.m) U.data = self.data U.write_to_file(filename)
class Hamiltonian: def __init__(self, capacity, cavity, mu): Assert(isinstance(capacity, int), "capacity is not integer", cf()) Assert(capacity > 0, "capacity <= 0", cf()) Assert(isinstance(cavity, list), "capacity is not list", cf()) for _ in range(len(cavity)): Assert(isinstance(cavity[_], Cavity), "is not cavity", cf()) self.capacity = capacity self.cavity = cavity self.n = [] self.wc = [] self.wa = [] self.g = [] for i in range(len(cavity)): self.n.append(cavity[i].n) self.wc.append(cavity[i].wc) self.wa.append(cavity[i].wa) self.g.append(cavity[i].g) self.mu = mu self.states = self.get_states() self.get_states_bin() # print(self.states) # exit(1) self.size = len(self.states) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) for i in range(self.size): i_state = self.states[i] i_ph = [] i_at = [] i_ph.append(i_state[0][0]) i_at.append(i_state[0][1]) i_ph.append(i_state[1][0]) i_at.append(i_state[1][1]) for j in range(self.size): j_state = self.states[j] j_ph = [] j_at = [] j_ph.append(j_state[0][0]) j_at.append(j_state[0][1]) j_ph.append(j_state[1][0]) j_at.append(j_state[1][1]) if i_state == j_state: for _ in range(len(cavity)): self.matrix.data[i, j] += self.wc[_] * i_ph[_] for n_ in range(self.n[_]): self.matrix.data[i, j] += self.wa[_][n_] * i_at[_][n_] else: diff_cv = 0 for cv_ in range(2): if i_state[cv_] != j_state[cv_]: diff_cv += 1 cv = cv_ if diff_cv == 1: d_ph = j_ph[cv] - i_ph[cv] if abs(d_ph) == 1: diff_at_cnt = 0 for n_ in range(len(i_at[cv])): d_at = j_at[cv][n_] - i_at[cv][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: self.matrix.data[i, j] = self.g[cv][diff_at_num] * \ sqrt(max(i_ph[cv], j_ph[cv])) # print(self.states[i], '->', self.states[j], self.matrix.data[i, j]) else: d_ph_0 = j_ph[0] - i_ph[0] d_ph_1 = j_ph[1] - i_ph[1] if abs(d_ph_0) == 1 and (d_ph_1 + d_ph_0 == 0): if i_at[0] == j_at[0] and i_at[1] == j_at[1]: if j_ph[0] > i_ph[0]: self.matrix.data[i, j] += mu * \ a_plus(i_ph[0]) * a_minus(i_ph[1]) else: self.matrix.data[i, j] += mu * \ a_plus(i_ph[1]) * a_minus(i_ph[0]) # print(self.states[i], '->', self.states[j], self.matrix.data[i, j]) # for i in range(self.size): # for j in range(self.size): # if self.matrix.data[i, j] != self.matrix.data[j, i]: # print(self.states[i], self.states[j], self.matrix.data[i, j], self.matrix.data[j, i]) self.matrix.check_hermiticity() return # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def print_html(self): states = self.states st = list(states.values()) for i in range(0, len(st)): st[i] = str(st[i]) df = pd.DataFrame(self.matrix_n.data, columns=st, index=st, dtype=str) f = open("Hamiltonian.html", "w") style = """ <style> .dataframe th, td { text-align:center; # min-width:200px; white-space: nowrap; word-break:keep-all; } </style>""" f.write(style + df.to_html()) webbrowser.open("Hamiltonian.html") f.close() return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def get_states(self): state_0 = CV_state(self.capacity, self.n[0]) state_1 = CV_state(self.capacity, self.n[1]) self.states = {} cnt = 0 full_state = Full_state(self.capacity, [state_0, state_1]) self.states[cnt] = copy.deepcopy(full_state.state()) while (full_state.inc() != -1): self.states[cnt] = copy.deepcopy(full_state.state()) cnt += 1 print(cnt) for k, v in self.states.items(): print(k, v) # self.check_states() return self.states # ------------------------------------------------------------------------------------------------- def get_states_bin(self): states_bin = {} for k, v in self.states.items(): en1 = np.sum(v[0][1]) en2 = np.sum(v[1][1]) st = "[" + str(en1) + "," + str(en2) + "]" if not st in states_bin.keys(): states_bin[st] = [] states_bin[st].append(k) self.states_bin = {} self.states_bin_keys = [] for k1 in range(self.n[0] + 1): for k2 in range(self.n[1] + 1): if k1 + k2 > self.capacity: break k = "[" + str(k1) + "," + str(k2) + "]" self.states_bin_keys.append(k) self.states_bin[k] = states_bin[k] return self.states_bin # ------------------------------------------------------------------------------------------------- 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 print_states(self): print("Hamiltonian states:", color="yellow") print() try: for v in self.states.values(): print(v) print() except: print_error("states not set", cf()) exit(1) return # ------------------------------------------------------------------------------------------------- def print_bin_states(self): print("Hamiltonian bin states:", color="yellow") print() try: for k in self.states_bin.keys(): print(k) print() except: print_error("states not set", cf()) exit(1) return
def __init__(self, capacity, cavity, mu): Assert(isinstance(capacity, int), "capacity is not integer", cf()) Assert(capacity > 0, "capacity <= 0", cf()) Assert(isinstance(cavity, list), "capacity is not list", cf()) for _ in range(len(cavity)): Assert(isinstance(cavity[_], Cavity), "is not cavity", cf()) self.capacity = capacity self.cavity = cavity self.n = [] self.wc = [] self.wa = [] self.g = [] for i in range(len(cavity)): self.n.append(cavity[i].n) self.wc.append(cavity[i].wc) self.wa.append(cavity[i].wa) self.g.append(cavity[i].g) self.mu = mu self.states = self.get_states() self.get_states_bin() # print(self.states) # exit(1) self.size = len(self.states) self.matrix = Matrix(self.size, self.size, dtype=np.complex128) for i in range(self.size): i_state = self.states[i] i_ph = [] i_at = [] i_ph.append(i_state[0][0]) i_at.append(i_state[0][1]) i_ph.append(i_state[1][0]) i_at.append(i_state[1][1]) for j in range(self.size): j_state = self.states[j] j_ph = [] j_at = [] j_ph.append(j_state[0][0]) j_at.append(j_state[0][1]) j_ph.append(j_state[1][0]) j_at.append(j_state[1][1]) if i_state == j_state: for _ in range(len(cavity)): self.matrix.data[i, j] += self.wc[_] * i_ph[_] for n_ in range(self.n[_]): self.matrix.data[i, j] += self.wa[_][n_] * i_at[_][n_] else: diff_cv = 0 for cv_ in range(2): if i_state[cv_] != j_state[cv_]: diff_cv += 1 cv = cv_ if diff_cv == 1: d_ph = j_ph[cv] - i_ph[cv] if abs(d_ph) == 1: diff_at_cnt = 0 for n_ in range(len(i_at[cv])): d_at = j_at[cv][n_] - i_at[cv][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: self.matrix.data[i, j] = self.g[cv][diff_at_num] * \ sqrt(max(i_ph[cv], j_ph[cv])) # print(self.states[i], '->', self.states[j], self.matrix.data[i, j]) else: d_ph_0 = j_ph[0] - i_ph[0] d_ph_1 = j_ph[1] - i_ph[1] if abs(d_ph_0) == 1 and (d_ph_1 + d_ph_0 == 0): if i_at[0] == j_at[0] and i_at[1] == j_at[1]: if j_ph[0] > i_ph[0]: self.matrix.data[i, j] += mu * \ a_plus(i_ph[0]) * a_minus(i_ph[1]) else: self.matrix.data[i, j] += mu * \ a_plus(i_ph[1]) * a_minus(i_ph[0]) # print(self.states[i], '->', self.states[j], self.matrix.data[i, j]) # for i in range(self.size): # for j in range(self.size): # if self.matrix.data[i, j] != self.matrix.data[j, i]: # print(self.states[i], self.states[j], self.matrix.data[i, j], self.matrix.data[j, i]) self.matrix.check_hermiticity() return
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
class Hamiltonian: 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 print_html(self, filename): states = self.states # st = list(states.values()) # for i in range(0, len(st)): # st[i] = str(st[i]) # pd.options.display.max_colwidth = 999 # df = pd.DataFrame(self.matrix_html, columns=st, index=st, dtype=str) # f = open(filename, "w") # style = """ # <style > # .dataframe th, td { # padding: 10px; # text - align: center; # # min-width:200px; # white - space: nowrap; # word-break: keep-all;} # < / style > """ # script = """ # <script type = "text/javascript" src = "http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" > < / script > # """ # f.write(style + script + df.to_html(escape=False)) # webbrowser.open(filename) # f.close() return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def get_states(self): self.states = {} state = [self.capacity, [0] * self.n] cnt = 0 self.states[cnt] = copy.deepcopy(state) cnt += 1 state[0] -= 1 while (1): inced = False for n_ in range(self.n - 1, -1, -1): if state[1][n_] == 1: state[1][n_] = 0 continue if state[0] + np.sum(state[1]) > self.capacity: continue state[1][n_] += 1 inced = True state[0] = self.capacity - np.sum(state[1]) if state[0] >= 0: self.states[cnt] = copy.deepcopy(state) cnt += 1 break if not inced: break self.check_states() return self.states # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- 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 print_states(self): print("Hamiltonian states", color="green") try: for v in self.states.values(): print("[%2d, " % (v[0]), v[1], "]", sep="") print() except: print_error("states not set", cf()) exit(1) return # ------------------------------------------------------------------------------------------------- def print_bin_states(self): for k, v in self.states_bin.items(): print(k, v) def get_states_bin(self): states_bin = {} for k, v in self.states.items(): at_state = v[1] en1 = at_state[:int(self.n / 2)] en2 = at_state[int(self.n / 2):] st = "[" + str(np.sum(en1)) + "," + str(np.sum(en2)) + "]" if not st in states_bin.keys(): states_bin[st] = [] states_bin[st].append(k) self.states_bin = {} self.states_bin_keys = [] for k1 in range(int(self.n / 2) + 1): for k2 in range(int(self.n / 2) + 1): if k1 + k2 > self.capacity: break k = "[" + str(k1) + "," + str(k2) + "]" self.states_bin_keys.append(k) if k in states_bin.keys(): self.states_bin[k] = states_bin[k] self.states_bin_keys = sorted(self.states_bin_keys) return self.states_bin
class Hamiltonian: 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) self.get_states_bin() self.print_bin_states() # exit(1) 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 print_html(self, filename): # states = self.states"" # st = list(states.values())"" # for i in range(0, len(st)):" # st[i] = str(st[i])"" # df = pd.DataFrame(self.matrix.data, columns=st," # index=st, dtype=str)"" # f = open("Hamiltonian.html", "w")" # style = """" # <style>" # .dataframe th, td {" # text-align:center;" # # min-width:200px;" # white-space: nowrap;" # word-break:keep-all;" # }" # </style>""""" # f.write(style + df.to_html())" # webbrowser.open("Hamiltonian.html")"" # f.close()"" return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) return # ------------------------------------------------------------------------------------------------- # ------------------------------------------------------------------------------------------------- def get_states(self): self.states = {} cnt = 0 for capacity in range(self.capacity, -1, -1): state = [capacity, [0] * self.n] self.states[cnt] = copy.deepcopy(state) cnt += 1 state[0] -= 1 while(1): inced = False for n_ in range(self.n - 1, -1, -1): if state[1][n_] == 1: state[1][n_] = 0 continue if state[0] + np.sum(state[1]) > capacity: continue state[1][n_] += 1 inced = True state[0] = capacity - np.sum(state[1]) if state[0] >= 0: self.states[cnt] = copy.deepcopy(state) cnt += 1 break if not inced: break # self.states = {} # state = [self.capacity, [0] * self.n] # cnt = 0 # # self.states[cnt] = copy.deepcopy(state) # self.index1 = cnt # cnt += 1 # state[0] -= 1 # capacity = self.capacity # while(1): # inced = False # for n_ in range(self.n - 1, -1, -1): # if state[1][n_] == 1: # state[1][n_] = 0 # continue # if state[0] + np.sum(state[1]) > capacity: # continue # state[1][n_] += 1 # inced = True # print("cap ", state, capacity) # state[0] = capacity - np.sum(state[1]) # if state[0] >= 0: # self.states[cnt] = copy.deepcopy(state) # print(cnt, self.states[cnt]) # cnt += 1 # break # if not inced: # if capacity == 0: # self.states[cnt] = [0, [0] * self.n] # cnt += 1 # break # if capacity == self.capacity: # self.index2 = cnt - 1 # capacity -= 1 self.check_states() return self.states # ------------------------------------------------------------------------------------------------- def print_bin_states(self): for k, v in self.states_bin.items(): print(k, v) def get_states_bin(self): states_bin = {} for k, v in self.states.items(): at_state = v[1] en1 = at_state[: int(self.n / 2)] en2 = at_state[int(self.n / 2):] if v[0] + np.sum(en1) + np.sum(en2) != self.capacity: continue st = "[" + str(np.sum(en1)) + "," + str(np.sum(en2)) + "]" if not st in states_bin.keys(): states_bin[st] = [] states_bin[st].append(k) self.states_bin = {} self.states_bin_keys = [] for ph in range(self.capacity, -1, -1): for k1 in range(int(self.n / 2) + 1): for k2 in range(int(self.n / 2) + 1): if ph + k1 + k2 == self.capacity: k = "[" + str(k1) + "," + str(k2) + "]" self.states_bin_keys.append(k) if k in states_bin.keys(): self.states_bin[k] = states_bin[k] self.states_bin_keys = sorted(self.states_bin_keys) return self.states_bin # ------------------------------------------------------------------------------------------------- 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 print_states(self): # print("Hamiltonian states:", color="green") try: for v in self.states.values(): print("[%2d, " % (v[0]), v[1], "]", sep="") # print() except: print_error("states not set", cf()) exit(1) return
def __init__(self, capacity, cavity_1, cavity_2, mu): self.cavity_1 = cavity_1 self.capacity = capacity self.cavity_1 = cavity_1 self.cavity_2 = cavity_2 self.n_1 = n_1 = cavity_1.n self.n_2 = n_2 = cavity_2.n wc_1 = cavity_1.wc wc_2 = cavity_2.wc wa_1 = cavity_1.wa wa_2 = cavity_2.wa g_1 = cavity_1.g g_2 = cavity_2.g self.init_states() self.init_bin_states() # --------------------------------------- # --------------------------------------- self.matrix = Matrix(self.size, self.size, dtype=np.complex128) cnk = Cnk(capacity) for i in range(self.size): st_i = self.states[i] ph_i = [st_i[0][0], st_i[1][0]] at_i = np.array([st_i[0][1], st_i[1][1]]) for j in range(0, self.size): st_j = self.states[j] ph_j = [st_j[0][0], st_j[1][0]] at_j = np.array([st_j[0][1], st_j[1][1]]) if i == j: self.matrix.data[i, j] = wc_1 * \ ph_i[0] + wc_2 * ph_i[1] + wa_1 * \ at_i[0][0] + wa_2 * at_i[1][0] # print(self.states[i], '->', # self.states[j], self.matrix.data[i, j]) elif abs(ph_j[0] - ph_i[0]) == 1 and ( ph_j[0] - ph_i[0]) == -(ph_j[1] - ph_i[1]) and np.all( at_i == at_j): d_ph_0 = ph_j[0] - ph_i[0] d_ph_1 = ph_j[1] - ph_i[1] if abs(d_ph_0) == 1 and (d_ph_1 + d_ph_0 == 0): if at_i[0] == at_j[0] and at_i[1] == at_j[1]: a_1 = min(at_i[0][0], at_j[0][0]) a_2 = min(at_i[1][0], at_j[1][0]) if ph_j[0] > ph_i[0]: self.matrix.data[i, j] += mu * \ a_plus(ph_i[0]) * a_minus(ph_i[1] ) # * cnk.get(n_1, a_1+1) # * C_n_k(n_2, at_j[0]) else: self.matrix.data[i, j] += mu * \ a_plus(ph_i[1]) * a_minus(ph_i[0] ) # * cnk.get(n_2, a_2+1) # * C_n_k(n_2, at_j[0]) # self.matrix.data[i, j] *= cnk.get(n_1, a_1) * cnk.get(n_2, a_2) # self.matrix.data[i, j] = mu * sqrt(max(ph_j[0], ph_j[1])) # print(self.states[i], '->', # self.states[j], self.matrix.data[i, j]) else: if np.all(st_i[1] == st_j[1]): if abs(at_i[0][0] - at_j[0][0]) == 1: p = min(ph_i[0], ph_j[0]) a = min(at_i[0][0], at_j[0][0]) self.matrix.data[i, j] = g_1 * (n_1 - a) * sqrt( (p + 1) * cnk.get(n_1, a) / cnk.get(n_1, a + 1)) # print( # self.states[i], '->', self.states[j], self.matrix.data[i, j]) elif np.all(st_i[0] == st_j[0]): if abs(at_i[1][0] - at_j[1][0]) == 1: p = min(ph_i[1], ph_j[1]) a = min(at_i[1][0], at_j[1][0]) self.matrix.data[i, j] = g_2 * (n_2 - a) * sqrt( (p + 1) * cnk.get(n_2, a) / cnk.get(n_2, a + 1))
class Hamiltonian: # --------------------------------------------------------------------------------------------- def __init__(self, capacity, cavity_1, cavity_2, mu): self.cavity_1 = cavity_1 self.capacity = capacity self.cavity_1 = cavity_1 self.cavity_2 = cavity_2 self.n_1 = n_1 = cavity_1.n self.n_2 = n_2 = cavity_2.n wc_1 = cavity_1.wc wc_2 = cavity_2.wc wa_1 = cavity_1.wa wa_2 = cavity_2.wa g_1 = cavity_1.g g_2 = cavity_2.g self.init_states() self.init_bin_states() # --------------------------------------- # --------------------------------------- self.matrix = Matrix(self.size, self.size, dtype=np.complex128) cnk = Cnk(capacity) for i in range(self.size): st_i = self.states[i] ph_i = [st_i[0][0], st_i[1][0]] at_i = np.array([st_i[0][1], st_i[1][1]]) for j in range(0, self.size): st_j = self.states[j] ph_j = [st_j[0][0], st_j[1][0]] at_j = np.array([st_j[0][1], st_j[1][1]]) if i == j: self.matrix.data[i, j] = wc_1 * \ ph_i[0] + wc_2 * ph_i[1] + wa_1 * \ at_i[0][0] + wa_2 * at_i[1][0] # print(self.states[i], '->', # self.states[j], self.matrix.data[i, j]) elif abs(ph_j[0] - ph_i[0]) == 1 and ( ph_j[0] - ph_i[0]) == -(ph_j[1] - ph_i[1]) and np.all( at_i == at_j): d_ph_0 = ph_j[0] - ph_i[0] d_ph_1 = ph_j[1] - ph_i[1] if abs(d_ph_0) == 1 and (d_ph_1 + d_ph_0 == 0): if at_i[0] == at_j[0] and at_i[1] == at_j[1]: a_1 = min(at_i[0][0], at_j[0][0]) a_2 = min(at_i[1][0], at_j[1][0]) if ph_j[0] > ph_i[0]: self.matrix.data[i, j] += mu * \ a_plus(ph_i[0]) * a_minus(ph_i[1] ) # * cnk.get(n_1, a_1+1) # * C_n_k(n_2, at_j[0]) else: self.matrix.data[i, j] += mu * \ a_plus(ph_i[1]) * a_minus(ph_i[0] ) # * cnk.get(n_2, a_2+1) # * C_n_k(n_2, at_j[0]) # self.matrix.data[i, j] *= cnk.get(n_1, a_1) * cnk.get(n_2, a_2) # self.matrix.data[i, j] = mu * sqrt(max(ph_j[0], ph_j[1])) # print(self.states[i], '->', # self.states[j], self.matrix.data[i, j]) else: if np.all(st_i[1] == st_j[1]): if abs(at_i[0][0] - at_j[0][0]) == 1: p = min(ph_i[0], ph_j[0]) a = min(at_i[0][0], at_j[0][0]) self.matrix.data[i, j] = g_1 * (n_1 - a) * sqrt( (p + 1) * cnk.get(n_1, a) / cnk.get(n_1, a + 1)) # print( # self.states[i], '->', self.states[j], self.matrix.data[i, j]) elif np.all(st_i[0] == st_j[0]): if abs(at_i[1][0] - at_j[1][0]) == 1: p = min(ph_i[1], ph_j[1]) a = min(at_i[1][0], at_j[1][0]) self.matrix.data[i, j] = g_2 * (n_2 - a) * sqrt( (p + 1) * cnk.get(n_2, a) / cnk.get(n_2, a + 1)) # print( # self.states[i], '->', self.states[j], self.matrix.data[i, j]) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_index(self, state): for k, v in self.states.items(): if v == state: return k # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def write_to_file(self, filename): self.matrix.write_to_file(filename) # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def get_states(self): return self.states # --------------------------------------------------------------------------------------------- def print_html(self, filename): pass # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def init_states(self): self.states = dict() count = 0 for ph_1 in range(self.capacity + 1): for at_1 in range(self.cavity_1.n + 1): for ph_2 in range(self.capacity + 1): for at_2 in range(self.cavity_2.n + 1): if ph_1 + at_1 + ph_2 + at_2 != self.capacity: continue self.states[count] = [[ph_1, [at_1]], [ph_2, [at_2]]] count += 1 self.size = len(self.states) return # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def init_bin_states(self): self.bin_states = dict() count = 0 for at_1 in range(self.cavity_1.n + 1): for at_2 in range(self.cavity_2.n + 1): if at_1 + at_2 > self.capacity: continue self.bin_states[count] = [at_1, at_2] count += 1 return # --------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------- def print_states(self): print("States:", color="green") print() for k, v in self.states.items(): print(v) print()