Esempio n. 1
0
class Hamiltonian:
    # ---------------------------------------------------------------------------------------------
    def __init__(self, capacity, cavity, RWA=True):
        Assert(isinstance(capacity, int), "capacity is not integer", FILE(),
               LINE())
        Assert(capacity > 0, "capacity <= 0", FILE(), LINE())

        Assert(isinstance(cavity, Cavity), "cavity is not Cavity", FILE(),
               LINE())

        self.capacity = capacity
        self.cavity = cavity
        self.n = cavity.n

        self.get_states()

        self.size = len(self.states)
        self.matrix = Matrix(self.size, self.size, dtype=np.complex128)

        # ------------------------------------------------------------------------------------------------------------------
        H_field = self.get_H_field()
        H_atoms = self.get_H_atoms()

        H = H_field + H_atoms

        if RWA:
            H += self.get_H_int_RWA()
        else:
            H += self.get_H_int_EXACT()

        self.matrix.data = np.matrix(H)
        # ------------------------------------------------------------------------------------------------------------------

    def get_H_field(self):
        # ------------------------------------------------------------------------------------------------------------------
        acrossa = AcrossA(self.capacity)
        # ------------------------------------------------------------------------------------------------------------------
        H_dim = (self.capacity + 1) * pow(2, self.cavity.n)
        # ------------------------------------------------------------------------------------------------------------------
        at_dim = pow(2, self.cavity.n)

        I_at = np.identity(at_dim)
        # ------------------------------------------------------------------------------------------------------------------
        H_field = self.cavity.wc * np.kron(acrossa, I_at)

        # ------------------------------------------------------------------------------------------------------------------
        return H_field

    def get_H_atoms(self):
        # ------------------------------------------------------------------------------------------------------------------
        sigmadiag = [1]

        sigmacross = np.diagflat(sigmadiag, -1)
        sigma = np.diagflat(sigmadiag, 1)
        sigmacrosssigma = np.dot(sigmacross, sigma)
        # ------------------------------------------------------------------------------------------------------------------
        ph_dim = self.capacity + 1

        I_ph = np.identity(ph_dim)
        # ------------------------------------------------------------------------------------------------------------------
        H_dim = (self.capacity + 1) * pow(2, self.cavity.n)

        H_atoms = np.zeros([H_dim, H_dim])
        # ------------------------------------------------------------------------------------------------------------------
        for i in range(1, self.cavity.n + 1):
            elem = sigmacrosssigma

            at_prev = np.identity(pow(2, i - 1))
            elem = np.kron(at_prev, elem)

            at_next = np.identity(pow(2, self.cavity.n - i))
            elem = np.kron(elem, at_next)

            H_atoms += self.cavity.wa * np.kron(I_ph, elem)
        # ------------------------------------------------------------------------------------------------------------------
        return H_atoms

    def get_H_int_RWA(self):
        # ------------------------------------------------------------------------------------------------------------------
        across = Across(self.capacity)
        a = A(self.capacity)
        acrossa = AcrossA(self.capacity)
        # ------------------------------------------------------------------------------------------------------------------
        sigmadiag = [1]

        sigmacross = np.diagflat(sigmadiag, -1)
        sigma = np.diagflat(sigmadiag, 1)
        sigmacrosssigma = np.dot(sigmacross, sigma)
        # ------------------------------------------------------------------------------------------------------------------
        H_dim = (self.capacity + 1) * pow(2, self.cavity.n)

        H_int = np.zeros([H_dim, H_dim])
        # ------------------------------------------------------------------------------------------------------------------
        for i in range(1, self.cavity.n + 1):
            # ------------------------------------------------
            elem = across

            before = np.identity(pow(2, i - 1))
            elem = np.kron(elem, before)

            elem = np.kron(elem, sigma)

            after = np.identity(pow(2, self.cavity.n - i))
            elem = np.kron(elem, after)

            H_int += self.cavity.g * elem
            # ------------------------------------------------
            elem = a

            before = np.identity(pow(2, i - 1))
            elem = np.kron(elem, before)

            elem = np.kron(elem, sigmacross)

            after = np.identity(pow(2, self.cavity.n - i))
            elem = np.kron(elem, after)

            H_int += self.cavity.g * elem
            # ------------------------------------------------
        # ------------------------------------------------------------------------------------------------------------------
        return H_int

    def get_H_int_EXACT(self):
        # ------------------------------------------------------------------------------------------------------------------
        across = Across(self.capacity)
        a = A(self.capacity)
        acrossa = np.dot(across, a)
        # ------------------------------------------------------------------------------------------------------------------
        sigmadiag = [1]

        sigmacross = np.diagflat(sigmadiag, -1)
        sigma = np.diagflat(sigmadiag, 1)
        sigmacrosssigma = np.dot(sigmacross, sigma)
        # ------------------------------------------------------------------------------------------------------------------
        H_dim = (self.capacity + 1) * pow(2, self.cavity.n)

        H_int = np.zeros([H_dim, H_dim], dtype=complex)
        # ------------------------------------------------------------------------------------------------------------------
        for i in range(1, self.cavity.n + 1):
            # ------------------------------------------------
            elem = (across + a)

            before = np.identity(pow(2, i - 1))
            elem = np.kron(elem, before)

            elem = np.kron(elem, sigmacross + sigma)

            after = np.identity(pow(2, self.cavity.n - i))
            elem = np.kron(elem, after)

            H_int += self.cavity.g * elem
            # ------------------------------------------------
        # ------------------------------------------------------------------------------------------------------------------
        return H_int

    # ---------------------------------------------------------------------------------------------
    def get_states(self):
        state = State(self.capacity, self.cavity.n)

        self.states = {}

        cnt = 0
        self.states[cnt] = copy.copy(state.state())

        while state.inc():
            cnt += 1
            self.states[cnt] = copy.copy(state.state())

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

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

        print()

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

        print()

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

    # ---------------------------------------------------------------------------------------------
    def print(self):
        print(self.matrix.data)

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

    # ---------------------------------------------------------------------------------------------
    def write_to_file(self, filename):
        self.matrix.write_to_file(filename)
Esempio n. 2
0
class Hamiltonian:
    def diag(self, i, j):
        self.matrix.data[i, j] = 0

        value = []

        for cv in range(0, len(self.cv_chain.cavities)):
            self.matrix.data[i, j] += self.cv_chain.cavity(cv).wc * \
                self.states[i][cv][0]

            if self.states[i][cv][0] > 0:
                if self.states[i][cv][0] == 1:
                    value.append('wc' + str(cv + 1))
                else:
                    value.append('wc' + str(cv + 1) + '*' +
                                 str(self.states[i][cv][0]))

            at_sum = np.sum(self.states[i][cv][1])

            self.matrix.data[i, j] += self.cv_chain.cavity(cv).wa * \
                at_sum

            if at_sum > 0:
                if at_sum == 1:
                    value.append('wa' + str(cv + 1))
                else:
                    value.append('wa' + str(cv + 1) + '*' + str(at_sum))

        self.matrix_symb.data[i, j] = ' + '.join(value)

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

    def __init__(self, capacity, cv_chain, RWA, mu):
        Assert(isinstance(capacity, int), "capacity is not integer", cf())
        Assert(capacity > 0, "capacity <= 0", cf())

        Assert(isinstance(cv_chain, CavityChain),
               "cv_chain is not CavityChain", cf())
        # for cv in cavities:
        #     Assert(isinstance(cv_chain, CavityChain),
        #            "cavity is not Cavity", cf())

        self.capacity = capacity
        self.cv_chain = cv_chain
        # self.n = cavity.n

        self.get_states()

        self.size = len(self.states)
        # print("size =", self.size)
        # exit(0)
        self.matrix = Matrix(self.size, self.size, dtype=np.complex128)
        self.matrix_symb = Matrix(self.size, self.size, dtype=str)

        for i in range(self.size):
            i_state = self.states[i]

            for j in range(self.size):
                j_state = self.states[j]

                if i == j:
                    self.diag(i, j)
                else:
                    # # jump
                    ne_cv = []

                    ne_cnt = 0

                    for cv in range(0, len(self.cv_chain.cavities)):
                        if i_state[cv] != j_state[cv]:
                            ne_cnt += 1

                            if ne_cnt > 2:
                                break

                            ne_cv.append(cv)

                    if ne_cnt == 2:
                        d_ph_i = i_state[ne_cv[0]][0] - \
                            i_state[ne_cv[1]][0]
                        d_ph_j = j_state[ne_cv[0]][0] - \
                            j_state[ne_cv[1]][0]

                        if abs(d_ph_i) == 1 and d_ph_i == - d_ph_j \
                                and np.all(i_state[ne_cv[0]][1] == j_state[ne_cv[1]][1]):
                            # print(self.states[i], self.states[j])
                            # exit(0)
                            self.matrix.data[i, j] = mu
                            self.matrix_symb.data[i, j] = 'mu'
                            continue
                    elif ne_cnt == 1:
                        # g
                        at_i = i_state[ne_cv[0]][1]
                        at_j = j_state[ne_cv[0]][1]

                        ne_at_cnt = 0
                        ne_at_ind = []

                        for at_ind in range(0, len(at_i)):
                            if at_i[at_ind] != at_j[at_ind]:
                                ne_at_ind.append(at_ind)
                                ne_at_cnt += 1
                                ne_at = at_ind

                                if ne_at_cnt > 2:
                                    break

                        if ne_at_cnt == 2:
                            if at_i[ne_at_ind[0]] ^ at_i[ne_at_ind[1]]:
                                # print(at_i, at_j)
                                self.matrix.data[i, j] = self.cv_chain.cavity(
                                    ne_cv[0]).g
                                self.matrix_symb.data[i,
                                                      j] = 'g' + str(ne_cv[0] +
                                                                     1)
                                continue

                    self.matrix_symb.data[i, j] = '0'

        # self.matrix.data = np.matrix(H)
        # ------------------------------------------------------------------------------------------------------------------

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

    def get_states(self):
        states = get_full_base(capacity=self.capacity,
                               cv_chain=self.cv_chain,
                               limit=False)

        cnt = 0

        self.states = {}

        for i in states:
            self.states[cnt] = i
            cnt += 1

        self.states_str = list(self.states.values())
        self.states_str = [str(i) for i in self.states_str]

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

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

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

        print()

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

        print()

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

    # ---------------------------------------------------------------------------------------------
    def print(self):
        print(self.matrix.data)

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

    # ---------------------------------------------------------------------------------------------
    def iprint(self):
        import pandas as pd

        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

        self.df = df

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

    # ---------------------------------------------------------------------------------------------
    def iprint_symb(self):
        import pandas as pd

        df = pd.DataFrame()

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

        df.index = df.columns = self.states_str

        self.df = df

        df.to_html("H.html")

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

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