Skip to content

Time-independent Hamiltonian

The class file for time independent Hamiltonian.

A TIHamiltonian is stored as a sum of product terms: a coefficient times a product of basic operators. The products of basic operators are stored as a list of operators (currently strings).

In the worst case, this representation may take exponential resources. In practice, it is enough for nowadays machines.

Natural computation operations are overloaded. Pauli basis products are symbolically calculated, so that commutativity test is possible.

TIHamiltonian

The time-independent Hamiltonian

The underlying data-structure to store it is a list of tuples (h, c). Here h is a product Hamiltonian, represented by a list of site operators on the sites. c is the coefficient corresponding to h, and can either be a real number or an Expression.

The site operator algebras are symbolically dealt with here, since fermionic operators' algebra is position-related.

We can also test commutativity of Hamiltonians, and calculate the sites the Hamiltonian is acting non- trivially on.

Source code in SimuQ/simuq/hamiltonian.py
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
class TIHamiltonian :
    """ The time-independent Hamiltonian

    The underlying data-structure to store it is a list of
    tuples (h, c). Here h is a product Hamiltonian, 
    represented by a list of site operators on the sites. 
    c is the coefficient corresponding to h, and can either
    be a real number or an Expression. 

    The site operator algebras are symbolically dealt with 
    here, since fermionic operators' algebra is position-related.

    We can also test commutativity of Hamiltonians, and 
    calculate the sites the Hamiltonian is acting non-
    trivially on.
    """

    def __init__(self, sites_type, ham) :
        self.sites_type = sites_type
        self.ham = ham
        self.saved_t_sites = None
        self.cleanHam()

    @classmethod
    def empty(cls, sites_type) :
        ham = []
        return cls(sites_type, ham)

    @classmethod
    def identity(cls, sites_type) :
        num_sites = len(sites_type)
        prod = ['' for i in range(num_sites)]
        ham = [(prod, 1)]
        return cls(sites_type, ham)

    @classmethod
    def op(cls, sites_type, index, op) :
        num_sites = len(sites_type)
        prod = ['' for i in range(num_sites)]
        prod[index] = op
        ham = [(prod, 1)]
        return cls(sites_type, ham)

    def operAlgebra(self) :
        self.extend_ham_by_sites()

        i = 0
        while i < len(self.ham) :
            (h, coef) = self.ham[i]
            for j in range(len(h)) :
                if self.sites_type[j] == 'qubit' :
                    if h[j] == 'XX'  or  h[j] == 'YY'  or  h[j] == 'ZZ' :
                        h[j] = ''
                    elif h[j] == 'XY' :
                        h[j] = 'Z'
                        coef *= 1j
                    elif h[j] == 'YX' :
                        h[j] = 'Z'
                        coef *= -1j
                    elif h[j] == 'YZ' :
                        h[j] = 'X'
                        coef *= 1j
                    elif h[j] == 'ZY' :
                        h[j] = 'X'
                        coef *= -1j
                    elif h[j] == 'ZX' :
                        h[j] = 'Y'
                        coef *= 1j
                    elif h[j] == 'XZ' :
                        h[j] = 'Y'
                        coef *= -1j
                    self.ham[i]
            for j in range(len(h)) :
                if self.sites_type[j] == 'boson' :
                    s = h[j].find('ac')
                    while s != -1 :
                        h_copy = h.copy()
                        h_copy[j] = h_copy[j].replace('ac', 'ca', 1)
                        h[j] = h[j].replace('ac', '', 1)
                        self.ham.append((h_copy, coef))
                        s = h[j].find('ac')
                elif self.sites_type[j] == 'fermion' :
                    s = h[j].find('ac')
                    while s != -1 :
                        h_copy = h.copy()
                        h_copy[j] = h_copy[j].replace('ac', 'ca', 1)
                        h[j] = h[j].replace('ac', '', 1)
                        self.ham.append((h_copy, -coef))
                        s = h[j].find('ac')
            self.ham[i] = (h, coef)
            i += 1


    def cleanHam(self, tol = 1e-10) :
        self.operAlgebra()

        i = 0
        while i < len(self.ham) :
            for j in range(i) :
                if self.ham[j][0] == self.ham[i][0] :
                    self.ham[j] = (self.ham[j][0], self.ham[j][1] + self.ham[i][1])
                    del self.ham[i]
                    i -= 1
                    break
            i += 1
        i = 0

        while i < len(self.ham) :
            if isinstance(self.ham[i][1], Expression) :
                break
            if abs(self.ham[i][1]) < tol :
                del self.ham[i]
                continue
            i += 1

    def extend_ham_by_sites(self) :
        for i in range(len(self.ham)) :
            (h, coef) = self.ham[i]
            if len(h) < len(self.sites_type) :
                h += [''] * (len(self.sites_type) - len(h))
                self.ham[i] = (h, coef)

    def extend_sites(self, new_sites_type) :
        if len(new_sites_type) <= len(self.sites_type) :
            return
        for i in range(len(self.sites_type)) :
            if self.sites_type[i] != new_sites_type[i] :
                raise Exception("Sites type inconsistent!")
        add_num = len(new_sites_type) - len(self.sites_type)
        new_ham = [(self.ham[i][0] + [''] * add_num, self.ham[i][1]) for i in range(len(self.ham))]
        self.sites_type = new_sites_type
        self.ham = new_ham
        if self.saved_t_sites is not None :
            self.saved_t_sites += [0 for j in range(add_num)]

    def __neg__(self) :
        ham = copy(self.ham)
        for i in range(len(ham)) :
            ham[i] = (ham[i][0], -ham[i][1])
        h = TIHamiltonian(self.sites_type, ham)
        return h

    def __add__(self, other) :
        # need more typing restrictions
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            other = other * TIHamiltonian.empty()
        if self.sites_type != other.sites_type :
            self.extend_sites(other.sites_type)
            other.extend_sites(self.sites_type)
        ham = copy(self.ham)
        ham += other.ham
        h = TIHamiltonian(self.sites_type, ham)
        h.cleanHam()
        return h

    def __radd__(self, other) :
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            return self.__add__(other * TIHamiltonian.empty(self.sites_type))
        else :
            return NotImplemented

    def __sub__(self, other) :
        # need more typing restrictions
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            other = other * TIHamiltonian.empty(self.sites_type)
        if self.sites_type != other.sites_type :
            self.extend_sites(other.sites_type)
            other.extend_sites(self.sites_type)
        ham = copy(self.ham)
        ham += other.__neg__().ham
        h = TIHamiltonian(self.sites_type, ham)
        h.cleanHam()
        return h

    def __rsub__(self, other) :
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            return (other * TIHamiltonian.empty(self.sites_type)) - self
        else :
            return NotImplemented

    @staticmethod
    def strlist_mul(sites_type, a, b) :
        c = []
        coef = 1

        # Use a suffix sum to calculate the number of fermionic operators after site i.
        num_ferm_ope_backward = [0 for i in range(len(sites_type))]
        for i in range(len(sites_type) - 1, 1, -1) :
            num_ferm_ope_backward[i - 1] = num_ferm_ope_backward[i]
            if sites_type[i] == 'fermion' :
                num_ferm_ope_backward[i - 1] += len(a[i])

        for i in range(len(sites_type)) :
            if sites_type[i] == 'qubit' :
                c.append(a[i] + b[i])
            elif sites_type[i] == 'boson' :
                c.append(a[i] + b[i])
            elif sites_type[i] == 'fermion' :
                c.append(a[i] + b[i])
                if num_ferm_ope_backward[i] % 2 == 1  and  len(b[i]) % 2 == 1 :
                    coef *= -1

        return (c, coef)

    def scalar_mul(self, other) :
        ham = copy(self.ham)
        for i in range(len(ham)) :
            ham[i] = (ham[i][0], ham[i][1] * other)
        h = TIHamiltonian(self.sites_type, ham)
        return h

    def __mul__(self, other) :
        # need more typing restrictions
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            return self.scalar_mul(other)
        self.extend_ham_by_sites()
        other.extend_ham_by_sites()
        if self.sites_type != other.sites_type :
            self.extend_sites(other.sites_type)
            other.extend_sites(self.sites_type)
        ham = []
        for (prod1, coef1) in self.ham :
            for (prod2, coef2) in other.ham :
                (prod, coef3) = self.strlist_mul(self.sites_type, prod1, prod2)
                ham.append((prod, coef1 * coef2 * coef3))
        h = TIHamiltonian(self.sites_type, ham)
        h.cleanHam()
        return h

    def __truediv__(self, other) :
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            return self.scalar_mul(1/other)
        else :
            return NotImplemented

    def __rmul__(self, other) :
        if type(other) == int  or  type(other) == float  or  type(other) == complex  or  isinstance(other, Expression) :
            return self.scalar_mul(other)
        else :
            return NotImplemented

    def exp_eval(self, gvars, lvars) :
        ham = copy(self.ham)
        for i in range(len(ham)) :
            ham[i] = (ham[i][0], ham[i][1].exp_eval(gvars, lvars))
        h = TIHamiltonian(self.sites_type, ham)
        return h

    def is_empty(self) :
        abs_sum = 0
        for (prod, c) in self.ham :
            abs_sum += abs(c)
        if abs_sum < 1e-8 :
            return True
        else :
            return False

    @staticmethod
    def commutativity_test(h1, h2, derived = False) :
        if derived :
            t_sites1 = h1.touched_sites()
            t_sites2 = h2.touched_sites()
            for i in range(len(h1.sites_type)) :
                if t_sites1[i] == 1  and  t_sites2[i] == 1 :
                    return False
            return True
        return (h1 * h2 - h2 * h1).is_empty()

    def touched_sites(self) :
        if self.saved_t_sites != None :
            return self.saved_t_sites
        ret = [0 for i in range(len(self.sites_type))]
        for (prod, t) in self.ham :
            for i in range(len(self.sites_type)) :
                if prod[i] != '' :
                    ret[i] = 1
        self.saved_t_sites = ret
        return ret