Beispiel #1
0
class Hamiltonian:
    def __init__(self, capacity, cavity, RWA=True, reduced=True):
        self.capacity = capacity
        self.cavity = cavity

        self.size = 0

        HC = {}

        size_start = 0

        self.states = {}

        for c in range(capacity, -1, -1):
            print("c=", c)
            Hc = H(capacity=c, cavity=cavity, RWA=RWA, reduced=reduced)
            HC[c] = Hc

            for k, v in Hc.states.items():
                self.states[size_start + k] = v

            size_start += HC[c].size

            self.size += Hc.size
        print("_H done")
        I = np.zeros([self.size, self.size], dtype=np.complex128)

        size_start = 0

        for c in range(capacity, -1, -1):
            I[size_start:size_start + HC[c].size,
              size_start:size_start + HC[c].size] = HC[c].matrix.data
            size_start += HC[c].size

        self.matrix = Matrix(self.size, self.size, dtype=np.complex128)
        self.matrix.data = I

    def print_states(self):
        for k, v in self.states.items():
            print(k, ": ", v, sep="")

    # def iprint(self):
    # self.matrix.ipri
    # 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 = [str(v) for v in self.states.values()]

    # #     self.df = df

    def to_html(self, filename):
        self.matrix.states = self.states
        self.matrix.to_html(filename)
Beispiel #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