def init_state(self): r"""Form the initial vector according to shape list:: n_0| | |n_p-1 C_0 ... C_p-1 \ | / m_0 \|/ m_p-1 A Returns ------- init : (self.size,) ndarray Formally, init = np.concatenate([C_0, ..., C_p-1, A], axis=None), where C_i is a (n_i * m_i,) ndarray, i <- {0, ..., p-1}, A is a (M,) ndarray, and M = m_0 * ... * m_p-1, m_i < n_i. """ dvr_list = self.dvr_list c_list = [] for i, (_, m_i) in enumerate(self.shape_list[:-1]): _, v_i = dvr_list[i].solve(n_state=m_i) v_i = np.transpose(v_i) c_list.append(np.reshape(v_i, -1)) vec_a = np.zeros(self.size_list[-1]) vec_a[0] = 1.0 vec_list = c_list + [vec_a] init = np.concatenate(vec_list, axis=None) self.vec = init self.update_mod_terms() return init
def vectorize(self, use_aux=False, shape_dict=None): """Return a vector from the arrays in the tensors in the network of self. Parameters ---------- use_aux : bool `True` to vectorize the arrays in .aux, else to use arrays in .array. shape_dict: dict Where to save the shape of each tensor. Return ------ vec : (n,) ndarray """ vec_list = [] for t in self.visitor(leaf=False): array = t.aux if use_aux else t.array vec = np.reshape(array, -1) vec_list.append(vec) if shape_dict is not None: shape_dict[t] = t.shape ans = np.concatenate(vec_list, axis=None) return ans
def get_ext_wfns(n_states, wfns, op, search_method='krylov'): wfns = np.array(wfns) n_states = np.shape(wfns)[1] space = np.transpose(np.array(wfns)) vecs = np.transpose(np.array(wfns)) assert np.shape(space)[1] <= n_states if search_method == 'krylov': while True: space = linalg.orth(vecs) if np.shape(space)[0] >= n_states: break vecs = list(op @ vecs) np.concatenate((space, vecs), axis=1) psi = space[:, :n_states] return np.transpose(psi) else: raise NotImplementedError
def _normalizer(vec): ans = [] for i in range(self.rank + 1): vec_i = self.get_sub_vec(i, vec) vec_i = np.reshape(vec_i, -1) if i == self.rank: std_norm = sqrt(1.0) else: std_norm = sqrt(self.shape_list[i][1]) norm = (linalg.norm(vec_i) / std_norm) logging.debug(__('norm {}: {}', i, norm)) ans.append(vec_i / norm) ans = np.concatenate(ans, axis=None) return ans
def term_hamiltonian(self, r, vec): """ Parameters ---------- r : int vec : (self.size,) ndarray """ logging.debug(__('Hamiltonian term {}...', r)) h_term = self.h_terms[r] mod_term = self.mod_terms[r] steps = len(self.shape_list) ans = [] for i in range(steps): v_i = self.get_sub_vec(i, vec) if i < steps - 1: h_list = [h for j, h in h_term if j == i] v_i = self._sp_op(i, v_i, h_list, mod_term) else: v_i = self._coeff_op(v_i, mod_term) v_i = np.reshape(v_i, -1) ans.append(v_i) ans = np.concatenate(ans, axis=None) return ans