def vec_sparse_2_dense(key_list, vec_dist, dim): """Convertes a sparse vector to a dense one.""" import key_ops dense_vec = [0] * dim for key in vec_dist: index = key_ops.key_2_index(key_list, key) dense_vec[index] = vec_dist[key] return dense_vec
def dead_prob(h, key): """Gets the probability rate of death.""" import key_ops index = key_ops.key_2_index(h.key_list, key) vec_excited_list = h.matrix[index] try: diag_entry = vec_excited_list[key] except KeyError: diag_entry = 0 prob_rate = diag_entry - h.ref_energy return prob_rate
def disp_mat(self): """Displays the Hamiltonian as a dense matrix.""" import key_ops for vec in self.matrix: array = [0] * self.para_list[4] for key in vec: index = key_ops.key_2_index(self.key_list, key) array[index] = vec[key] for i in array: print '{0:6.3f}'.format(i), print ''
def excited_key_and_prob(h, key_gnd): """From a ground state, gets a possible excited state, and the probability rate.""" import key_ops import random index = key_ops.key_2_index(h.key_list, key_gnd) vec_excited_list = h.matrix[index] choice_num = len(vec_excited_list) - 1 if choice_num > 0: # Connected to others. key_exc = key_gnd while key_exc == key_gnd: key_exc = random.sample(vec_excited_list, 1)[0] entry = vec_excited_list[key_exc] prob_rate = choice_num * entry return key_exc, prob_rate else: return None, 0
def __init__(self, file_name): """Initializes a $Hamiltonian$ object.""" import key_ops import read_h import vec_ops self.para_list = [] self.eig_vecs = [] self.eig_vals = [] self.key_list = [] # Loads data from a file. read_h.read_outof_file(file_name, self.para_list, self.eig_vecs, self.eig_vals) # Makes an ordered list of keys. key_ops.create_keys(self.key_list, self.para_list) # Computes H=sum(Ei*|vi><vi|) self.matrix = [{}] * self.para_list[4] for i in range(self.para_list[4]): vec = self.eig_vecs[i] for key in vec: index = key_ops.key_2_index(self.key_list, key) column = vec_ops.vec_scalar_prod(vec, vec[key]*self.eig_vals[i] , False) self.matrix[index] = vec_ops.vec_add(self.matrix[index], column, False) # Reference determinant and reference energy. # Selects the lowest-energy determinant. self.ref_index = 0 self.ref_key = self.key_list[0] self.ref_energy = self.matrix[0][self.ref_key] for i in range(1, self.para_list[4]): try: temp_ref_energy = self.matrix[i][self.key_list[i]] except KeyError: temp_ref_energy = 0 if temp_ref_energy < self.ref_energy: self.ref_index = i self.ref_key = self.key_list[i] self.ref_energy = temp_ref_energy