Exemplo n.º 1
0
    def get_spectrum_vs_paramvals(self, param_vals, update_hilbertspace, evals_count=10, get_eigenstates=False,
                                  param_name="external_parameter", num_cpus=settings.NUM_CPUS):
        """Return eigenvalues (and optionally eigenstates) of the full Hamiltonian as a function of a parameter.
        Parameter values are specified as a list or array in `param_vals`. The Hamiltonian `hamiltonian_func`
        must be a function of that particular parameter, and is expected to internally set subsystem parameters.
        If a `filename` string is provided, then eigenvalue data is written to that file.

        Parameters
        ----------
        param_vals: ndarray of floats
            array of parameter values
        update_hilbertspace: function
            update_hilbertspace(param_val) specifies how a change in the external parameter affects
            the Hilbert space components
        evals_count: int, optional
            number of desired energy levels (default value = 10)
        get_eigenstates: bool, optional
            set to true if eigenstates should be returned as well (default value = False)
        param_name: str, optional
            name for the parameter that is varied in `param_vals` (default value = "external_parameter")
        num_cpus: int, optional
            number of cores to be used for computation (default value: settings.NUM_CPUS)

        Returns
        -------
        SpectrumData object
        """
        target_map = cpu_switch.get_map_method(num_cpus)
        if get_eigenstates:
            func = functools.partial(self._esys_for_paramval, update_hilbertspace=update_hilbertspace,
                                     evals_count=evals_count)
            with utils.InfoBar("Parallel computation of eigenvalues [num_cpus={}]".format(num_cpus), num_cpus):
                eigensystem_mapdata = list(target_map(func, tqdm(param_vals, desc='Spectral data', leave=False,
                                                                 disable=(num_cpus > 1))))
            eigenvalue_table, eigenstate_table = spec_utils.recast_esys_mapdata(eigensystem_mapdata)
        else:
            func = functools.partial(self._evals_for_paramval,  update_hilbertspace=update_hilbertspace,
                                     evals_count=evals_count)
            with utils.InfoBar("Parallel computation of eigensystems [num_cpus={}]".format(num_cpus), num_cpus):
                eigenvalue_table = list(target_map(func, tqdm(param_vals, desc='Spectral data', leave=False,
                                                              disable=(num_cpus > 1))))
            eigenvalue_table = np.asarray(eigenvalue_table)
            eigenstate_table = None

        return storage.SpectrumData(eigenvalue_table, self.get_initdata(), param_name, param_vals,
                                    state_table=eigenstate_table)
Exemplo n.º 2
0
    def get_spectrum_vs_paramvals(
        self,
        param_name: str,
        param_vals: ndarray,
        evals_count: int = 6,
        subtract_ground: bool = False,
        get_eigenstates: bool = False,
        filename: str = None,
        num_cpus: int = settings.NUM_CPUS,
    ) -> SpectrumData:
        """Calculates eigenvalues/eigenstates for a varying system parameter,
        given an array of parameter values. Returns a `SpectrumData` object with
        `energy_data[n]` containing eigenvalues calculated for parameter value
        `param_vals[n]`.

        Parameters
        ----------
        param_name:
            name of parameter to be varied
        param_vals:
            parameter values to be plugged in
        evals_count:
            number of desired eigenvalues (sorted from smallest to largest)
            (default value = 6)
        subtract_ground:
            if True, eigenvalues are returned relative to the ground state eigenvalue
            (default value = False)
        get_eigenstates:
            return eigenstates along with eigenvalues (default value = False)
        filename:
            file name if direct output to disk is wanted
        num_cpus:
            number of cores to be used for computation
            (default value: settings.NUM_CPUS)
        """
        previous_paramval = getattr(self, param_name)
        tqdm_disable = num_cpus > 1 or settings.PROGRESSBAR_DISABLED

        target_map = get_map_method(num_cpus)
        if not get_eigenstates:
            func = functools.partial(self._evals_for_paramval,
                                     param_name=param_name,
                                     evals_count=evals_count)
            with InfoBar(
                    "Parallel computation of eigensystems [num_cpus={}]".
                    format(num_cpus),
                    num_cpus,
            ):
                eigenvalue_table = list(
                    target_map(
                        func,
                        tqdm(
                            param_vals,
                            desc="Spectral data",
                            leave=False,
                            disable=tqdm_disable,
                        ),
                    ))
            eigenvalue_table = np.asarray(eigenvalue_table)
            eigenstate_table = None
        else:
            func = functools.partial(self._esys_for_paramval,
                                     param_name=param_name,
                                     evals_count=evals_count)
            with InfoBar(
                    "Parallel computation of eigenvalues [num_cpus={}]".format(
                        num_cpus),
                    num_cpus,
            ):
                # Note that it is useful here that the outermost eigenstate object is
                # a list, as for certain applications the necessary hilbert space
                # dimension can vary with paramvals
                eigensystem_mapdata = list(
                    target_map(
                        func,
                        tqdm(
                            param_vals,
                            desc="Spectral data",
                            leave=False,
                            disable=tqdm_disable,
                        ),
                    ))
            eigenvalue_table, eigenstate_table = recast_esys_mapdata(
                eigensystem_mapdata)

        if subtract_ground:
            for param_index, _ in enumerate(param_vals):
                eigenvalue_table[param_index] -= eigenvalue_table[param_index][
                    0]

        setattr(self, param_name, previous_paramval)
        specdata = SpectrumData(
            eigenvalue_table,
            self.get_initdata(),
            param_name,
            param_vals,
            state_table=eigenstate_table,
        )
        if filename:
            specdata.filewrite(filename)

        return SpectrumData(
            eigenvalue_table,
            self.get_initdata(),
            param_name,
            param_vals,
            state_table=eigenstate_table,
        )
Exemplo n.º 3
0
    def get_spectrum_vs_paramvals(self,
                                  param_name,
                                  param_vals,
                                  evals_count=6,
                                  subtract_ground=False,
                                  get_eigenstates=False,
                                  filename=None,
                                  num_cpus=settings.NUM_CPUS):
        """Calculates eigenvalues/eigenstates for a varying system parameter, given an array of parameter values.
        Returns a `SpectrumData` object with `energy_data[n]` containing eigenvalues calculated for
        parameter value `param_vals[n]`.

        Parameters
        ----------
        param_name: str
            name of parameter to be varied
        param_vals: ndarray
            parameter values to be plugged in
        evals_count: int, optional
            number of desired eigenvalues (sorted from smallest to largest) (default value = 6)
        subtract_ground: bool, optional
            if True, eigenvalues are returned relative to the ground state eigenvalue (default value = False)
        get_eigenstates: bool, optional
            return eigenstates along with eigenvalues (default value = False)
        filename: str, optional
            file name if direct output to disk is wanted
        num_cpus: int, optional
            number of cores to be used for computation (default value: settings.NUM_CPUS)

        Returns
        -------
        SpectrumData object
        """
        previous_paramval = getattr(self, param_name)

        target_map = get_map_method(num_cpus)
        if get_eigenstates:
            func = functools.partial(self._esys_for_paramval,
                                     param_name=param_name,
                                     evals_count=evals_count)
            with InfoBar(
                    "Parallel computation of eigenvalues [num_cpus={}]".format(
                        num_cpus), num_cpus):
                eigensystem_mapdata = list(
                    target_map(
                        func,
                        tqdm(param_vals,
                             desc='Spectral data',
                             leave=False,
                             disable=(num_cpus > 1))))
            eigenvalue_table, eigenstate_table = recast_esys_mapdata(
                eigensystem_mapdata)
        else:
            func = functools.partial(self._evals_for_paramval,
                                     param_name=param_name,
                                     evals_count=evals_count)
            with InfoBar(
                    "Parallel computation of eigensystems [num_cpus={}]".
                    format(num_cpus), num_cpus):
                eigenvalue_table = list(
                    target_map(
                        func,
                        tqdm(param_vals,
                             desc='Spectral data',
                             leave=False,
                             disable=(num_cpus > 1))))
            eigenvalue_table = np.asarray(eigenvalue_table)
            eigenstate_table = None

        if subtract_ground:
            for param_index, _ in enumerate(param_vals):
                eigenvalue_table[param_index] -= eigenvalue_table[param_index,
                                                                  0]

        setattr(self, param_name, previous_paramval)
        specdata = SpectrumData(eigenvalue_table,
                                self._get_metadata_dict(),
                                param_name,
                                param_vals,
                                state_table=eigenstate_table)
        if filename:
            specdata.filewrite(filename)

        return SpectrumData(eigenvalue_table,
                            self._get_metadata_dict(),
                            param_name,
                            param_vals,
                            state_table=eigenstate_table)