def solve_comp(h_p, J_p, C_p, gam, modes, tree, e_res, **kwargs): '''Formulate and solve the component Hamiltonian at the current tree node''' verbose = kwargs['verbose'] t = time() if verbose: print('\nRunning components solver...') Hs = build_comp_H(h_p, J_p, C_p, gam, modes) # print(Hs.diagonal()) t1 = time() # run sparse matric solver if verbose: print('H matrix size {0}'.format(str(Hs.shape))) print('Running sparse solver...'), e_vals, e_vecs = solve_sparse(Hs, more=False) if verbose: print('solver time: {0:.4f} sec'.format(time() - t1)) Es, modes = proc_comp_solve(e_vals, e_vecs, modes, tree, e_res) if verbose: print('Component solver time: {0:.4f}'.format(time() - t)) try: if kwargs['full_output']: return Es, modes, e_vals, e_vecs raise KeyError except KeyError: return Es, modes
def out_handler(h, J, gam, prod_states): '''Make an estimation of low energy spectrum using the determined applicable subset of product states and the problem parameters''' # create a single list of the product states prod_states = [list(ps) for ps in prod_states] pstates = sorted(set(reduce(lambda x, y: x + y, prod_states))) # find the energy associated with each product state modes = np.matrix(pstates) H = build_comp_H([h], [J], [], gam, [modes]) Eps = H.diagonal() # sort product states by energy ps_order = sorted(list(enumerate(Eps)), key=lambda x: x[1]) ps_order, Eps = zip(*ps_order) # find the eigenstates in the reduced mode space e_vals, e_vecs = solve_sparse(H) E = e_vals states = e_vecs states = [states[ps_order, i] for i in xrange(len(E))] # get cell polarizations for each eigenstate state_pols = [rp_state_to_pol(state, pstates) for state in states] return E, states, pstates
def out_handler(h, J, gam, prod_states): '''Make an estimation of low energy spectrum using the determined applicable subset of product states and the problem parameters''' # create a single list of the product states prod_states = [list(ps) for ps in prod_states] pstates = sorted(set(reduce(lambda x, y: x+y, prod_states))) # find the energy associated with each product state modes = np.matrix(pstates) Hs = build_comp_H([h], [J], [], gam, [modes]) Eps = Hs.diagonal() # sort product states by energy ps_order = sorted(list(enumerate(Eps)), key=lambda x: x[1]) ps_order, Eps = zip(*ps_order) # find the eigenstates in the reduced mode space e_vals, e_vecs = solve_sparse(Hs, more=True) E = e_vals states = e_vecs states = [states[ps_order, i] for i in xrange(len(E))] # get cell polarizations for each eigenstate state_pols = [rp_state_to_pol(state, pstates) for state in states] return E, states, Eps, pstates, state_pols
def solve_comp(h_p, J_p, C_p, gam, modes, tree, e_res, **kwargs): '''Formulate and solve the component Hamiltonian at the current tree node''' verbose = kwargs['verbose'] t = time() if verbose: print('\nRunning components solver...') Hs = build_comp_H(h_p, J_p, C_p, gam, modes) # print(Hs.diagonal()) t1 = time() # run sparse matric solver if verbose: print('H matrix size {0}'.format(str(Hs.shape))) print('Running sparse solver...'), e_vals, e_vecs = solve_sparse(Hs, more=False) if verbose: print('solver time: {0:.4f} sec'.format(time()-t1)) Es, modes = proc_comp_solve(e_vals, e_vecs, modes, tree, e_res) if verbose: print('Component solver time: {0:.4f}'.format(time()-t)) try: if kwargs['full_output']: return Es, modes, e_vals, e_vecs raise KeyError except KeyError: return Es, modes
def evd(self): ''' ''' if self.e_vals is None or self.e_vecs is None: Hs = self.Hx+sp.diags(self.Hz) e_vals, e_vecs = solve_sparse(Hs, more=False) self.e_vals = e_vals self.e_vecs = e_vecs return self.e_vals, self.e_vecs
def evd(self): ''' ''' if self.e_vals is None or self.e_vecs is None: Hs = self.Hx + sp.diags(self.Hz) e_vals, e_vecs = solve_sparse(Hs, more=False) self.e_vals = e_vals self.e_vecs = e_vecs return self.e_vals, self.e_vecs
def mode_solve(self, modes): '''Solve the node in the basis of known modes''' e_res = np.max(np.abs(self.J))*E_RES f = lambda x: np.round(x, 5) _Hz = np.array(self.Hz) _Hx = sp.coo_matrix(self.Hx) self.modes = modes Hx, Hz = self.direct_construction() self.Hx = Hx self.Hz = Hz if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) e_vals, e_vecs = solve_sparse(Hs, more=False) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res) # reduce Hamiltonian if self.Hx is not None: self.Hx = self.Hx[inds,:][:,inds] self.Hz = self.Hz[inds] # store data self.Es = np.array(Es) self.minds = minds self.modes = np.matrix(modes) self.e_vals = e_vals self.e_vecs = e_vecs[inds,:]
def mode_solve(self, modes): '''Solve the node in the basis of known modes''' e_res = np.max(np.abs(self.J)) * E_RES f = lambda x: np.round(x, 5) _Hz = np.array(self.Hz) _Hx = sp.coo_matrix(self.Hx) self.modes = modes Hx, Hz = self.direct_construction() self.Hx = Hx self.Hz = Hz if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) e_vals, e_vecs = solve_sparse(Hs, more=False) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res) # reduce Hamiltonian if self.Hx is not None: self.Hx = self.Hx[inds, :][:, inds] self.Hz = self.Hz[inds] # store data self.Es = np.array(Es) self.minds = minds self.modes = np.matrix(modes) self.e_vals = e_vals self.e_vecs = e_vecs[inds, :]
def solve(self): '''Solve the problem node, recursively solves all children''' self.vprint('Problem size: {0}...'.format(len(self.h))) # check cache for solution if CACHING and self.cache['dir']: # compute hash_parameters hval, K, hp, inds = core.hash_problem(self.h, self.J, gam=self.gam, nx=self.nx, nz=self.nz) self.hash_pars = {'hval': hval, 'K': K, 'hp': hp, 'inds': inds} # look in hash table if hval in self.cache['table']: try: self.from_cache() Hx, Hz = self.direct_construction() self.Hx = Hx self.Hz = Hz return except Exception as e: print(e.message) print('Something went wrong load from cache') # solution not cached, compute e_res = np.max(np.abs(self.J))*E_RES # proportional energy resolution # if small enough, solve exactly. Otherwise solve recursively. if not self.tree['children']: self.vprint('Running exact solver...') t = time() # construct matrix elements Hx, Hz = self.exact_formulation() if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) # store local Hamiltonian components self.Hx = Hx self.Hz = Hz # solve e_vals, e_vecs = solve_sparse(Hs, more=True) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res) else: self.vprint('Running recursive solver') # solve each child for child in self.children: child.solve() # select mode indices to keep from each child cinds = self.select_modes() # reduce the Hilbert space of the children Hamiltonians for inds, child in zip(cinds, self.children): # reduce operators if child.Hx is not None: child.Hx = child.Hx[inds,:][:,inds] child.Hz = child.Hz[inds] # reduce number of modes child.modes = child.modes[inds] # forget old pauli matrices child.px = {} child.pz = {} # formulate local Hamiltonian Hx, Hz = self.comp_formulation() if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) # store local Hamiltonian components self.Hx = Hx self.Hz = Hz # solve e_vals, e_vecs = solve_sparse(Hs, more=False) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res, comp=True) # reduce local Hamiltonians if self.Hx is not None: self.Hx = self.Hx[inds,:][:, inds] self.Hz = self.Hz[inds] # formatting and storage self.Es = np.array(Es) self.minds = minds self.modes = np.matrix(modes) self.e_vals = e_vals self.e_vecs = e_vecs[inds,:] # delete references to children to free memory self.children = None if CACHING and self.cache['dir'] and self.hash_pars['hval'] not in self.cache['table']: try: self.cache['table'][self.hash_pars['hval']] = self.to_cache() except Exception as e: print(e.message) print('Failed to cache solution...')
def solve(self): '''Solve the problem node, recursively solves all children''' self.vprint('Problem size: {0}...'.format(len(self.h))) # check cache for solution if CACHING and self.cache['dir']: # compute hash_parameters hval, K, hp, inds = core.hash_problem(self.h, self.J, gam=self.gam, nx=self.nx, nz=self.nz) self.hash_pars = {'hval': hval, 'K': K, 'hp': hp, 'inds': inds} # look in hash table if hval in self.cache['table']: try: self.from_cache() Hx, Hz = self.direct_construction() self.Hx = Hx self.Hz = Hz return except Exception as e: print(e.message) print('Something went wrong load from cache') # solution not cached, compute e_res = np.max(np.abs( self.J)) * E_RES # proportional energy resolution # if small enough, solve exactly. Otherwise solve recursively. if not self.tree['children']: self.vprint('Running exact solver...') t = time() # construct matrix elements Hx, Hz = self.exact_formulation() if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) # store local Hamiltonian components self.Hx = Hx self.Hz = Hz # solve e_vals, e_vecs = solve_sparse(Hs, more=True) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res) else: self.vprint('Running recursive solver') # solve each child for child in self.children: child.solve() # select mode indices to keep from each child cinds = self.select_modes() # reduce the Hilbert space of the children Hamiltonians for inds, child in zip(cinds, self.children): # reduce operators if child.Hx is not None: child.Hx = child.Hx[inds, :][:, inds] child.Hz = child.Hz[inds] # reduce number of modes child.modes = child.modes[inds] # forget old pauli matrices child.px = {} child.pz = {} # formulate local Hamiltonian Hx, Hz = self.comp_formulation() if Hx is None: Hs = sp.diags(Hz) else: Hs = Hx + sp.diags(Hz) # store local Hamiltonian components self.Hx = Hx self.Hz = Hz # solve e_vals, e_vecs = solve_sparse(Hs, more=False) Es, minds, modes, inds = self.proc_solve(e_vals, e_vecs, e_res, comp=True) # reduce local Hamiltonians if self.Hx is not None: self.Hx = self.Hx[inds, :][:, inds] self.Hz = self.Hz[inds] # formatting and storage self.Es = np.array(Es) self.minds = minds self.modes = np.matrix(modes) self.e_vals = e_vals self.e_vecs = e_vecs[inds, :] # delete references to children to free memory self.children = None if CACHING and self.cache['dir'] and self.hash_pars[ 'hval'] not in self.cache['table']: try: self.cache['table'][self.hash_pars['hval']] = self.to_cache() except Exception as e: print(e.message) print('Failed to cache solution...')