Пример #1
0
def recursive_solver(h, J, gam, tree, **kwargs):
    '''Recursive method for identifying energy bins and significant modes'''

    # pull parameters from kwargs
    cache_dir = kwargs['cache_dir']
    verbose = kwargs['verbose']

    # energy resolution
    e_res = np.max(np.abs(J))*E_RES

    if verbose:
        print('Detected problem size: {0}'.format(h.size))

    # try to read from cache
    if cache_dir is not None:
        # get or create hash table
        if 'hash_table' not in kwargs:
            kwargs['hash_table'] = generate_hash_table(direc=cache_dir)
        hval, K, hp, inds = hash_problem(h, J, gam=gam)
        hash_pars = {'hval':hval, 'K':K, 'hp':hp, 'inds':inds}
        # try to look up solution
        if hval in kwargs['hash_table']:
            try:
                Es, modes = from_cache(cache_dir, **hash_pars)
                return Es, modes
            except:
                print('Something went wrong reading from cache')

    # solution not cached, compute
    if not tree['children']:
        if verbose:
            print('Running exact solver...')
        e_vals, e_vecs = exact_solve(h, J, gamma=gam, k=10*len(h))
        Es, modes = proc_exact_solve(e_vals, e_vecs, e_res)
    else:
        if verbose:
            print('Running recursive partitioning...')
        h_p, J_p, C_p = partition(h, J, tree)

        # solve each partition recursively
        ES, MS = [], []
        for h_, J_, tree_ in zip(h_p, J_p, tree['children']):
            Es_, modes_ = recursive_solver(h_, J_, gam, tree_, **kwargs)
            ES.append(np.array(Es_))
            MS.append(modes_)

        # select modes to include
        modes = select_modes(ES, MS)

        # solve component system
        Es, modes = solve_comp(h_p, J_p, C_p, gam, modes, tree, e_res, **kwargs)

    # save to cache
    if 'hash_table' in kwargs:
        try:
            kwargs['hash_table'][hash_pars['hval']] = \
                to_cache(Es, modes, cache_dir, **hash_pars)
        except:
            print('Failed to cache solution...')
    return Es, modes
Пример #2
0
    def solve(self):

        # try to read solution from cache
        if self.solver.cache_dir is not None:
            hval, K, hp, inds = hash_problem(h, J, gam=gam)
            hash_pars = {'hval':hval, 'K':K, 'hp':hp, 'inds':inds}
            # try to look up solution
            if hval in self.solver.hash_table:
                try:
                    Es, modes = from_cache(self.solver.cache_dir, **hash_pars)
                except:
Пример #3
0
    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...')
Пример #4
0
def recursive_solver(h, J, gam, tree, **kwargs):
    '''Recursive method for identifying energy bins and significant modes'''

    # pull parameters from kwargs
    cache_dir = kwargs['cache_dir']
    verbose = kwargs['verbose']

    # energy resolution
    e_res = np.max(np.abs(J)) * E_RES

    if verbose:
        print('Detected problem size: {0}'.format(h.size))

    # try to read from cache
    if cache_dir is not None:
        # get or create hash table
        if 'hash_table' not in kwargs:
            kwargs['hash_table'] = generate_hash_table(direc=cache_dir)
        hval, K, hp, inds = hash_problem(h, J, gam=gam)
        hash_pars = {'hval': hval, 'K': K, 'hp': hp, 'inds': inds}
        # try to look up solution
        if hval in kwargs['hash_table']:
            try:
                Es, modes = from_cache(cache_dir, **hash_pars)
                return Es, modes
            except:
                print('Something went wrong reading from cache')

    # solution not cached, compute
    if not tree['children']:
        if verbose:
            print('Running exact solver...')
        e_vals, e_vecs = exact_solve(h, J, gamma=gam, k=10 * len(h))
        Es, modes = proc_exact_solve(e_vals, e_vecs, e_res)
    else:
        if verbose:
            print('Running recursive partitioning...')
        h_p, J_p, C_p = partition(h, J, tree)

        # solve each partition recursively
        ES, MS = [], []
        for h_, J_, tree_ in zip(h_p, J_p, tree['children']):
            Es_, modes_ = recursive_solver(h_, J_, gam, tree_, **kwargs)
            ES.append(np.array(Es_))
            MS.append(modes_)

        # select modes to include
        modes = select_modes(ES, MS)

        # solve component system
        Es, modes = solve_comp(h_p, J_p, C_p, gam, modes, tree, e_res,
                               **kwargs)

    # save to cache
    if 'hash_table' in kwargs:
        try:
            kwargs['hash_table'][hash_pars['hval']] = \
                to_cache(Es, modes, cache_dir, **hash_pars)
        except:
            print('Failed to cache solution...')
    return Es, modes
Пример #5
0
    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...')