def test_trimer_hamiltonian(): # ------------------------------------------------------------------ # -- Hubbard atom with two bath sites, Hamiltonian beta = 2.0 V1 = 2.0 V2 = 5.0 epsilon1 = 0.00 epsilon2 = 4.00 mu = 2.0 U = 1.0 # -- construction using triqs operators up, do = 0, 1 docc = c_dag(up,0) * c(up,0) * c_dag(do,0) * c(do,0) nA = c_dag(up,0) * c(up,0) + c_dag(do,0) * c(do,0) nB = c_dag(up,1) * c(up,1) + c_dag(do,1) * c(do,1) nC = c_dag(up,2) * c(up,2) + c_dag(do,2) * c(do,2) H_expr = -mu * nA + epsilon1 * nB + epsilon2 * nC + U * docc + \ V1 * (c_dag(up,0)*c(up,1) + c_dag(up,1)*c(up,0) + \ c_dag(do,0)*c(do,1) + c_dag(do,1)*c(do,0) ) + \ V2 * (c_dag(up,0)*c(up,2) + c_dag(up,2)*c(up,0) + \ c_dag(do,0)*c(do,2) + c_dag(do,2)*c(do,0) ) # ------------------------------------------------------------------ fundamental_operators = [ c(up,0), c(do,0), c(up,1), c(do,1), c(up,2), c(do,2)] rep = SparseMatrixRepresentation(fundamental_operators) H_mat = rep.sparse_matrix(H_expr) # -- explicit construction class Dummy(object): def __init__(self): pass op = Dummy() op.cdagger = rep.sparse_operators.c_dag op.c = np.array([cdag.getH() for cdag in op.cdagger]) op.n = np.array([ cdag*cop for cop, cdag in zip(op.c, op.cdagger)]) H_ref = -mu * (op.n[0] + op.n[1]) + \ epsilon1 * (op.n[2] + op.n[3]) + \ epsilon2 * (op.n[4] + op.n[5]) + \ U * op.n[0] * op.n[1] + \ V1 * (op.cdagger[0] * op.c[2] + op.cdagger[2] * op.c[0] + \ op.cdagger[1] * op.c[3] + op.cdagger[3] * op.c[1] ) + \ V2 * (op.cdagger[0] * op.c[4] + op.cdagger[4] * op.c[0] + \ op.cdagger[1] * op.c[5] + op.cdagger[5] * op.c[1] ) # ------------------------------------------------------------------ # -- compare compare_sparse_matrices(H_mat, H_ref)
def test_sparse_matrix_representation(): up, do = 0, 1 fundamental_operators = [c(up, 0), c(do, 0)] rep = SparseMatrixRepresentation(fundamental_operators) # -- Test an operator O_mat = rep.sparse_matrix(c(up, 0)) O_ref = rep.sparse_operators.c_dag[0].getH() compare_sparse_matrices(O_mat, O_ref) # -- Test O_mat = rep.sparse_matrix(c(do, 0)) O_ref = rep.sparse_operators.c_dag[1].getH() compare_sparse_matrices(O_mat, O_ref) # -- Test expression H_expr = c(up, 0) * c(do, 0) * c_dag(up, 0) * c_dag(do, 0) H_mat = rep.sparse_matrix(H_expr) c_dag0, c_dag1 = rep.sparse_operators.c_dag c_0, c_1 = c_dag0.getH(), c_dag1.getH() H_ref = c_0 * c_1 * c_dag0 * c_dag1 compare_sparse_matrices(H_mat, H_ref)
class TriqsExactDiagonalization(object): """ Exact diagonalization for Triqs operator expressions. """ # ------------------------------------------------------------------ def __init__(self, H, fundamental_operators, beta): self.beta = beta self.rep = SparseMatrixRepresentation(fundamental_operators) self.ed = SparseExactDiagonalization(self.rep.sparse_matrix(H), self.rep.blocks, beta) # ------------------------------------------------------------------ def get_expectation_value(self, op): return self.ed.get_expectation_value(self.rep.sparse_matrix(op)) # ------------------------------------------------------------------ def get_free_energy(self): return self.ed.get_free_energy() def get_partition_function(self): return self.ed.get_partition_function() def get_density_matrix(self): return self.ed.get_density_matrix() def get_ground_state_energy(self): return self.ed.get_ground_state_energy() # ------------------------------------------------------------------ def set_g2_tau(self, g_tau, op1, op2): assert (type(g_tau.mesh) == MeshImTime) assert (self.beta == g_tau.mesh.beta) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) tau = np.array([tau.value for tau in g_tau.mesh]) g_tau.data[:, 0, 0] = self.ed.get_tau_greens_function_component( tau, op1_mat, op2_mat) #self.set_tail(g_tau, op1_mat, op2_mat) # ------------------------------------------------------------------ def set_g2_tau_matrix(self, g_tau, op_list): assert (g_tau.target_shape == tuple([len(op_list)] * 2)) for (i1, o1), (i2, o2) in mpi_op_comb(op_list, repeat=2): self.set_g2_tau(g_tau[i1, i2], o1, dagger(o2)) g_tau = mpi_all_reduce_g(g_tau) return g_tau # ------------------------------------------------------------------ def set_g2_iwn(self, g_iwn, op1, op2): assert (self.beta == g_iwn.mesh.beta) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) iwn = np.array([iwn.value for iwn in g_iwn.mesh]) g_iwn.data[:, 0, 0] = self.ed.get_frequency_greens_function_component( iwn, op1_mat, op2_mat, self.xi(g_iwn.mesh)) #self.set_tail(g_iwn, op1_mat, op2_mat) # ------------------------------------------------------------------ def set_g2_iwn_matrix(self, g_iwn, op_list): assert (g_iwn.target_shape == tuple([len(op_list)] * 2)) for (i1, o1), (i2, o2) in mpi_op_comb(op_list, repeat=2): self.set_g2_iwn(g_iwn[i1, i2], o1, dagger(o2)) g_iw = mpi_all_reduce_g(g_iwn) return g_iwn # ------------------------------------------------------------------ def set_tail(self, g, op1_mat, op2_mat): tail = g.tail raw_tail = self.ed.get_high_frequency_tail_coeff_component( op1_mat, op2_mat, self.xi(g.mesh), Norder=tail.order_max) for idx in xrange(tail.order_max): tail[idx + 1] = raw_tail[idx] # ------------------------------------------------------------------ def xi(self, mesh): if mesh.statistic == 'Fermion': return -1.0 elif mesh.statistic == 'Boson': return +1.0 else: raise NotImplementedError # ------------------------------------------------------------------ def set_g3_tau(self, g3_tau, op1, op2, op3): ops = [op1, op2, op3] ops_mat = np.array([self.rep.sparse_matrix(op) for op in ops]) for idxs, taus, perm, perm_sign in SquareTrianglesMesh(g3_tau): ops_perm_mat = ops_mat[perm + [2]] taus_perm = np.array(taus).T[perm] data = self.ed.get_timeordered_two_tau_greens_function( taus_perm, ops_perm_mat) for idx, d in zip(idxs, data): g3_tau[Idxs(idx)] = perm_sign * d # ------------------------------------------------------------------ def set_g40_tau(self, g40_tau, g_tau): assert (type(g_tau.mesh) == MeshImTime) assert (g_tau.target_shape == (1, 1, 1, 1)) for t1, t2, t3 in g40_tau.mesh: g40_tau[t1, t2, t3] = g_tau(t1 - t2) * g_tau(t3.value) - g_tau( t1.value) * g_tau(t3 - t2) # ------------------------------------------------------------------ def set_g40_tau_matrix(self, g40_tau, g_tau): assert (type(g_tau.mesh) == MeshImTime) assert (g_tau.target_shape == g40_tau.target_shape[:2]) assert (g_tau.target_shape == g40_tau.target_shape[2:]) for t1, t2, t3 in g40_tau.mesh: g40_tau[t1, t2, t3] *= 0. g40_tau[t1, t2, t3] += np.einsum('ba,dc->abcd', g_tau(t1 - t2), g_tau(t3.value)) g40_tau[t1, t2, t3] -= np.einsum('da,bc->abcd', g_tau(t1.value), g_tau(t3 - t2)) # ------------------------------------------------------------------ def set_g4_tau(self, g4_tau, op1, op2, op3, op4): ops = [op1, op2, op3, op4] ops_mat = np.array([self.rep.sparse_matrix(op) for op in ops]) for idxs, taus, perm, perm_sign in CubeTetrasMesh(g4_tau): ops_perm_mat = ops_mat[perm + [3]] taus_perm = np.array(taus).T[perm] data = self.ed.get_timeordered_three_tau_greens_function( taus_perm, ops_perm_mat) for idx, d in zip(idxs, data): g4_tau[Idxs(idx)] = perm_sign * d # ------------------------------------------------------------------ def set_g4_tau_matrix(self, g4_tau, op_list): assert (g4_tau.target_shape == tuple([len(op_list)] * 4)) for (i1, o1), (i2, o2), (i3, o3), (i4, o4) in \ mpi_op_comb(op_list, repeat=4): self.set_g4_tau(g4_tau[i1, i2, i3, i4], o1, dagger(o2), o3, dagger(o4)) g4_tau = mpi_all_reduce_g(g4_tau) return g4_tau
class TriqsExactDiagonalization(object): """ Exact diagonalization for Triqs operator expressions. """ # ------------------------------------------------------------------ def __init__(self, H, fundamental_operators, beta): self.beta = beta self.rep = SparseMatrixRepresentation(fundamental_operators) self.ed = SparseExactDiagonalization(self.rep.sparse_matrix(H), beta) # ------------------------------------------------------------------ def get_expectation_value(self, op): return self.ed.get_expectation_value(self.rep.sparse_matrix(op)) # ------------------------------------------------------------------ def get_free_energy(self): return self.ed.get_free_energy() def get_partition_function(self): return self.ed.get_partition_function() def get_density_matrix(self): return self.ed.get_density_matrix() def get_ground_state_energy(self): return self.ed.get_ground_state_energy() # ------------------------------------------------------------------ def set_g2_tau(self, g_tau, op1, op2): assert (type(g_tau.mesh) == MeshImTime) assert (self.beta == g_tau.mesh.beta) assert (g_tau.target_shape == (1, 1)) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) tau = np.array([tau for tau in g_tau.mesh]) g_tau.data[:, 0, 0] = \ self.ed.get_tau_greens_function_component( tau, op1_mat, op2_mat) self.set_tail(g_tau, op1_mat, op2_mat) # ------------------------------------------------------------------ def set_g2_iwn(self, g_iwn, op1, op2): assert (self.beta == g_iwn.mesh.beta) assert (g_iwn.target_shape == (1, 1)) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) iwn = np.array([iwn for iwn in g_iwn.mesh]) g_iwn.data[:, 0, 0] = \ self.ed.get_frequency_greens_function_component( iwn, op1_mat, op2_mat, self.xi(g_iwn.mesh)) self.set_tail(g_iwn, op1_mat, op2_mat) # ------------------------------------------------------------------ def set_tail(self, g, op1_mat, op2_mat): tail = g.tail raw_tail = self.ed.get_high_frequency_tail_coeff_component( op1_mat, op2_mat, self.xi(g.mesh), Norder=tail.order_max) for idx in xrange(tail.order_max): tail[idx + 1][:] = raw_tail[idx] # ------------------------------------------------------------------ def xi(self, mesh): if mesh.statistic == 'Fermion': return -1.0 elif mesh.statistic == 'Boson': return +1.0 else: raise NotImplementedError # ------------------------------------------------------------------ def set_g3_tau(self, g3_tau, op1, op2, op3): assert (g3_tau.target_shape == (1, 1, 1, 1)) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) op3_mat = self.rep.sparse_matrix(op3) ops_mat = np.array([op1_mat, op2_mat, op3_mat]) for idxs, taus, perm, perm_sign in SquareTrianglesMesh(g3_tau): ops_perm_mat = ops_mat[perm + [2]] taus_perm = np.array(taus).T[perm] data = self.ed.get_timeordered_two_tau_greens_function( taus_perm, ops_perm_mat) for idx, d in zip(idxs, data): g3_tau[list(idx)][:] = perm_sign * d # ------------------------------------------------------------------ def set_g40_tau(self, g40_tau, g_tau): assert (type(g_tau.mesh) == MeshImTime) #assert( g_tau.target_shape == g40_tau.target_shape ) for (i1, i2, i3), (t1, t2, t3) in enumerate_tau3(g40_tau): g40_tau[[i1, i2, i3]][:] = \ g_tau(t1-t2)*g_tau(t3) - g_tau(t1)*g_tau(t3-t2) # ------------------------------------------------------------------ def set_g4_tau(self, g4_tau, op1, op2, op3, op4): assert (g4_tau.target_shape == (1, 1, 1, 1)) op1_mat = self.rep.sparse_matrix(op1) op2_mat = self.rep.sparse_matrix(op2) op3_mat = self.rep.sparse_matrix(op3) op4_mat = self.rep.sparse_matrix(op4) ops_mat = np.array([op1_mat, op2_mat, op3_mat, op4_mat]) for idxs, taus, perm, perm_sign in CubeTetrasMesh(g4_tau): ops_perm_mat = ops_mat[perm + [3]] taus_perm = np.array(taus).T[perm] data = self.ed.get_timeordered_three_tau_greens_function( taus_perm, ops_perm_mat) for idx, d in zip(idxs, data): g4_tau[list(idx)][:] = perm_sign * d