Example #1
0
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)
        # 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 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 to_csv(self, filename):
        self.matrix.to_csv(filename)
        return

    # -------------------------------------------------------------------------------------------------

    # -------------------------------------------------------------------------------------------------
    def get_states(self):
        self.states = {}

        cnt = 0

        f = open("bin", "w")

        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)
                        # print(self.states[cnt])
                        f.write(''.join(str(i)
                                        for i in self.states[cnt][1]) + "\n")
                        # print(self.states[cnt], str.join(
                        #    "", self.states[cnt][1]))
                        cnt += 1

                    # print("[%2d, " % (v[0]), v[1], "]", sep="")
                    break

                if not inced:
                    break
        f.close()
        # 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
Example #2
0
class Hamiltonian:

    def __init__(self, capacity, cavity):
        Assert(isinstance(capacity, int), "capacity is not integer", FILE(), LINE())
        Assert(capacity >= 0, "capacity <= 0", FILE(), LINE())

        print("Hamiltonian", capacity)
        # Assert(isinstance(cavity, "BipartiteGeneral.Cavity.Cavity"), "capacity is not integer", FILE(), LINE())

        self.capacity = capacity
        self.cavity = cavity

        self.n_atoms = cavity.n_atoms

        self.wc = cavity.wc
        self.wa = cavity.wa
        self.g = cavity.g
        self.n_levels = cavity.n_levels
        self.states = self.get_states()
        self.get_states_bin()

        self.size = len(self.states)

        self.matrix = Matrix(self.size, self.size, dtype=np.longdouble)

        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

                    for n_ in range(len(i_at)):
                        if i_at[n_] != 0:
                            # print(i, j, i_at[n_], self.wa * i_at[n_])
                            self.matrix.data[i, j] += self.wa * i_at[n_]
                            # self.matrix.data[i, j] += self.wa[n_] * 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 * k
                            else:
                                k = a_cross(j_ph) * j_at[diff_at_num]

                                self.matrix.data[i,
                                                 # j] = self.g[diff_at_num] * k
                                                 j] = self.g * k

        # self.matrix.check_hermiticity()

        return
    # ---------------------------------------------------------------------------------------------

    def print(self):
        for i in range(self.size):
            for j in range(self.size):
                print(wc_str(self.matrix.data[i, j]), end='\t')

            print()

    # ---------------------------------------------------------------------------------------------
    def print_html(self, filename):
        states = self.states
        return
    # -------------------------------------------------------------------------------------------------

    def to_html(self, filename):
        self.matrix.states = self.states
        # f(self.matrix.data)
        # print(self.matrix.m, self.matrix.n)
        self.matrix.to_html(filename)

    # -------------------------------------------------------------------------------------------------
    def to_csv(self, filename):
        self.matrix.to_csv(filename)

        return
    # -------------------------------------------------------------------------------------------------

    # -------------------------------------------------------------------------------------------------
    def get_states(self):
        self.states = {}

        # state = [0, [0] * self.n_atoms]
        # state = [self.capacity, [0] * self.n_atoms]
        cnt = 1
        state = State(self.capacity, self.n_atoms, self.n_levels)
        self.states[cnt-1] = copy.deepcopy(state.state)

        # self.states[cnt] = copy.deepcopy(state)
        # cnt += 1

        # state[0] -= 1

        while(state.inc()):
            if state.state[0] + np.sum(state.state[1]) == self.capacity:
                # print("cnt:", cnt, state.state)
                self.states[cnt-1] = copy.deepcopy(state.state)
                cnt += 1

        # print(self.states)
        # exit(0)
        # while(1):
        #     inced = False

        #     for n_ in range(self.n_atoms - 1, -1, -1):
        #         if state[1][n_] == self.n_levels-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)
        #             print("cnt=", cnt, self.states[cnt])
        #             cnt += 1

        #         break

        #     if not inced:
        #         break

        # print(55)
        self.check_states()
        # print(66)
        # self.states_rev = {}

        # for k, v in self.states.items():
        #     self.states_rev[len(self.states)-1-k] = v

        # print(self.states)
        # self.states = self.states_rev
        # print(self.states_rev)
        return self.states
    # -------------------------------------------------------------------------------------------------

    # -------------------------------------------------------------------------------------------------
    def check_states(self):
        try:
            # print(self.capacity)
            # print(self.states)
            Assert(len(self.states) > 0, "len(states) <= 0", FILE(), LINE())

            for state in self.states.values():
                ph = state[0]
                at = state[1]

                Assert(0 <= ph <= self.capacity, "incorrect state " + str(state), FILE(), LINE())
                Assert(ph + np.sum(at) == self.capacity, "incorrect state " + str(state), FILE(), LINE())
                for n_ in range(len(at)):
                    Assert(0 <= at[n_] <= self.n_levels, "incorrect state " + str(state), FILE(), LINE())
        except:
            print_error("incorrect states generation", FILE(), LINE())
            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", FILE(), LINE())
            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_atoms / 2)]
            en2 = at_state[int(self.n_atoms / 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_atoms / 2) + 1):
            for k2 in range(int(self.n_atoms / 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
Example #3
0
    # print('n_atoms', n_atoms)

    for n_levels in range(N_LEVELS_1, N_LEVELS_2 + 1):
        # print('n_levels', n_levels)

        for i in range(n_levels):
            # print('i', i, end='')

            for j in range(i):
                # print('j', j)

                for i_atom in range(1, n_atoms + 1):
                    s = op.Sigma2(i_=i,
                                  j_=j,
                                  num=i_atom,
                                  n_atoms=n_atoms,
                                  n_levels=n_levels)

                    # filename = 'dark_dense/' + '_'.join(str(i)
                    # for i in [n_atoms, n_levels, i, j, i_atom]) + '.csv'
                    # print(filename)
                    m = Matrix(m=np.shape(s)[0], n=np.shape(s)[0], dtype=float)
                    # print(s)
                    m.data = s.todense()
                    m.to_csv('dark_dense/' + '_'.join(
                        str(i)
                        for i in [n_atoms, n_levels, i, j, i_atom]) + '.csv',
                             precision=0)
                    # for p in s.todense():
                    # print(p)
Example #4
0
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> &middot; """ + \
                                # 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 to_csv(self, filename):
        self.matrix.to_csv(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
Example #5
0
class Hamiltonian:
    def set_base(self, base):
        self.base = base

    def __init__(self, capacity, cavity, RWA=True, reduced=True):
        self.capacity = capacity
        self.cavity = cavity

        print("Hamiltonian naive", capacity)

        H_field = get_Hfield(capacity, cavity.n_atoms, cavity.n_levels,
                             cavity.wc, cavity.wa, cavity.g)
        print("get_Hfield", capacity)

        H_atoms = get_Hatoms(capacity, cavity.n_atoms, cavity.n_levels,
                             cavity.wc, cavity.wa, cavity.g)
        print("get_Hatoms", capacity)

        if RWA:
            H_int = get_Hint_RWA(
                capacity, cavity.n_atoms, cavity.n_levels, cavity.wc, cavity.wa, cavity.g)
        else:
            H_int = get_Hint_EXACT(
                capacity, cavity.n_atoms, cavity.n_levels, cavity.wc, cavity.wa, cavity.g)
        print("get_Hint", capacity)

        Assert(np.shape(H_field) == np.shape(H_atoms), "size mismatch", FILE(), LINE())
        Assert(np.shape(H_atoms) == np.shape(H_int), "size mismatch", FILE(), LINE())

        H = H_field + H_atoms + H_int
        # H = lil_matrix(H_field + H_atoms + H_int)

        self.size = np.shape(H)[0]

        self.matrix = Matrix(self.size, self.size, dtype=np.double)
        self.matrix.data = H

        at = AtomicBasis(count=cavity.n_atoms, n_levels=cavity.n_levels)
        base = Base(capacity, at)
        print("at, base", capacity)

        self.set_base(base)
        print("self.set_base(base)", capacity)

        if reduced:
            self.reduce()
        print("self.reduce()", capacity)

        self.set_states()
        print("self.set_states()", capacity)
        # print(self.states)
        # print(self.states)
        # self.to_html("H.html")

        H = H_Full(capacity, cavity)

        # H.matrix.data = np.abs(H.matrix.data)
        # print(H.matrix.data)
        # print(self.matrix.data)

        # for i in range(self.size):
        #     for j in range(self.size):
        #         if H.matrix.data[i, j] != self.matrix.data[i, j]:
        #             print(H.matrix.data[i, j], '!= ', self.matrix.data[i, j])
        #             return

        # if np.any(H.matrix.data != self.matrix.data):
        #     print("TC: Not equal")

        #     exit(0)

    # ---------------------------------------------------------------------------------------------

    def print(self):
        for i in range(self.size):
            for j in range(self.size):
                print(wc_str(self.matrix.data[i, j]), end='\t')

            print()
    # -------------------------------------------------------------------------------------------------

    def to_csv(self, filename):
        self.matrix.to_csv(filename)

        return
    # -------------------------------------------------------------------------------------------------

    def iprint(self):
        df = pd.DataFrame()

        for i in range(self.size):
            for j in range(self.size):
                df.loc[i, j] = wc_str(abs(self.matrix.data[i, j]))

        # df.index = df.columns = self.states_str
        df.index = df.columns = self.states
        print(self.states)
        # df.index = df.columns = self.base.base_str

        self.df = df

    # ---------------------------------------------------------------------------------------------

    def reduce(self):
        for k, v in list(enumerate(self.base.base))[::-1]:
            if v[0] + np.sum(v[1]) != self.capacity:
                self.matrix.data = np.delete(self.matrix.data, k, axis=0)
                self.matrix.data = np.delete(self.matrix.data, k, axis=1)
                self.base.base.remove(v)
                self.base.base_str.remove(str(v))

        self.size = np.shape(self.matrix.data)[0]
        self.matrix.m = self.matrix.n = self.size

    def set_states(self):
        self.states = {}

        for k, v in enumerate(self.base.base):
            self.states[k] = v
Example #6
0
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 to_csv(self, filename):
        self.matrix.to_csv(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 += "&radic;" + "<span style="text-decoration:overline;">" + str(abs(self.matrix.data[i, j]) / self.g) + "</span>"        #         else:
        # #             html += "&radic;" + "<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()
Example #7
0
class Hamiltonian:
    # ---------------------------------------------------------------------------------------------
    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 get_index(self, state):
        for k, v in self.states.items():
            if v == state:
                return k

    # ---------------------------------------------------------------------------------------------

    # ---------------------------------------------------------------------------------------------
    def to_csv(self, filename):
        self.matrix.to_csv(filename)

    # ---------------------------------------------------------------------------------------------

    # ---------------------------------------------------------------------------------------------
    def get_states(self):
        return self.states

    def init_states(self, capacity, n):
        _min = min(capacity, n)

        count = 0

        for i1 in range(0, _min + 1):
            for i2 in range(0, min(n, capacity - i1) + 1):
                self.states[count] = [i1, i2]

                count += 1

    # ---------------------------------------------------------------------------------------------
    def print_states(self, title="States:"):
        print(title, color="green")

        print()

        for k, v in self.states.items():
            print(v)

        print()