Esempio n. 1
0
    def run_optimization(self,
                         initial_control_amplitudes: np.ndarray,
                         verbose: bool = False):
        """See base class. """

        super().prepare_optimization(
            initial_optimization_parameters=initial_control_amplitudes)

        try:
            result = scipy.optimize.basinhopping(
                func=self.cost_fktn_wrapper,
                x0=initial_control_amplitudes.T.flatten(),
                niter=self.termination_conditions["max_iterations"],
                T=self.temperature,
                stepsize=self.step_size,
                take_step=self._take_step,
                callback=None,
                interval=self.interval,
                disp=verbose)

            if self.system_simulator.stats is not None:
                self.system_simulator.stats.end_t_opt = time.time()

            optim_result = optimization_data.OptimizationResult(
                final_cost=result.fun,
                indices=self.system_simulator.cost_indices,
                final_parameters=result.x.reshape(self.pulse_shape[::-1]).T,
                num_iter=result.nfev,
                termination_reason=result.message,
                status=result.status,
                optimizer=self,
                optim_summary=self.optim_iter_summary,
                optimization_stats=self.system_simulator.stats)

        except WallTimeExceeded:
            if self.system_simulator.stats is not None:
                self.system_simulator.stats.end_t_opt = time.time()

            optim_result = optimization_data.OptimizationResult(
                final_cost=self._min_costs,
                indices=self.system_simulator.cost_indices,
                final_parameters=self._min_costs_par,
                num_iter=self._n_cost_fkt_eval,
                termination_reason='Maximum Wall Time Exceeded',
                status=5,
                optimizer=self,
                optim_summary=self.optim_iter_summary,
                optimization_stats=self.system_simulator.stats)

        return optim_result
Esempio n. 2
0
    def write_state_to_result(self):
        """ Writes the current state into an instance of 'OptimizationResult'.

        Intended for saving progress when terminating the optimization in an
        unexpected way.

        Returns
        -------
        result: optimization_data.OptimizationResult
            The current result of the optimization.

        """
        if self.system_simulator.stats is not None:
            self.system_simulator.stats.end_t_opt = time.time()

        if self.use_jacobian_function:
            jac_norm = np.linalg.norm(
                self.cost_jacobian_wrapper(self._min_costs_par))
        else:
            jac_norm = 0

        optim_result = optimization_data.OptimizationResult(
            final_cost=self._min_costs,
            indices=self.system_simulator.cost_indices,
            final_parameters=self._min_costs_par,
            final_grad_norm=jac_norm,
            num_iter=self._n_cost_fkt_eval,
            termination_reason='Maximum Wall Time Exceeded',
            status=5,
            optimizer=self,
            optim_summary=self.optim_iter_summary,
            optimization_stats=self.system_simulator.stats)
        return optim_result
Esempio n. 3
0
    def run_optimization(
            self,
            initial_control_amplitudes: np.array,
            verbose: int = 0) -> optimization_data.OptimizationResult:
        """See base class. """
        super().prepare_optimization(
            initial_optimization_parameters=initial_control_amplitudes)

        if self.use_jacobian_function:
            jac = super().cost_jacobian_wrapper
        else:
            jac = '2-point'

        try:
            result = scipy.optimize.least_squares(
                fun=super().cost_fktn_wrapper,
                x0=initial_control_amplitudes.T.flatten(),
                jac=jac,
                bounds=self.bounds,
                method=self.method,
                ftol=self.termination_conditions["min_cost_gain"],
                xtol=self.termination_conditions["min_amplitude_change"],
                gtol=self.termination_conditions["min_gradient_norm"],
                max_nfev=self.termination_conditions["max_iterations"],
                verbose=verbose)

            if self.system_simulator.stats is not None:
                self.system_simulator.stats.end_t_opt = time.time()

            optim_result = optimization_data.OptimizationResult(
                final_cost=result.fun,
                indices=self.system_simulator.cost_indices,
                final_parameters=result.x.reshape(self.pulse_shape[::-1]).T,
                final_grad_norm=np.linalg.norm(result.grad),
                num_iter=result.nfev,
                termination_reason=result.message,
                status=result.status,
                optimizer=self,
                optim_summary=self.optim_iter_summary,
                optimization_stats=self.system_simulator.stats)
        except WallTimeExceeded:
            optim_result = self.write_state_to_result()

        return optim_result
Esempio n. 4
0
    def run_optimization(self, initial_control_amplitudes: np.ndarray):
        """See base class. """

        self.prepare_optimization(
            initial_optimization_parameters=initial_control_amplitudes)

        pulse, costs = self.annealer.anneal()

        if self.system_simulator.stats is not None:
            self.system_simulator.stats.end_t_opt = time.time()

        optim_result = optimization_data.OptimizationResult(
            final_cost=costs,
            indices=self.system_simulator.cost_indices,
            final_parameters=pulse,
            optimizer=self,
            optim_summary=self.optim_iter_summary,
            optimization_stats=self.system_simulator.stats)

        return optim_result
Esempio n. 5
0
    def run_optimization(
            self,
            initial_control_amplitudes: np.array,
            verbose: bool = False) -> optimization_data.OptimizationResult:
        super().prepare_optimization(
            initial_optimization_parameters=initial_control_amplitudes)

        if self.use_jacobian_function:
            jac = self.cost_jacobian_wrapper
        else:
            jac = None

        if self.method == 'L-BFGS-B':
            try:
                result = scipy.optimize.minimize(
                    fun=self.cost_fktn_wrapper,
                    x0=initial_control_amplitudes.T.flatten(),
                    jac=jac,
                    bounds=self.bounds,
                    method=self.method,
                    options={
                        'ftol': self.termination_conditions["min_cost_gain"],
                        'gtol':
                        self.termination_conditions["min_gradient_norm"],
                        'maxiter':
                        self.termination_conditions["max_iterations"],
                        'disp': verbose
                    })

                optim_result = optimization_data.OptimizationResult(
                    final_cost=result.fun,
                    indices=self.system_simulator.cost_indices,
                    final_parameters=result.x.reshape(
                        self.pulse_shape[::-1]).T,
                    final_grad_norm=np.linalg.norm(result.jac),
                    num_iter=result.nfev,
                    termination_reason=result.message,
                    status=result.status,
                    optimizer=self,
                    optim_summary=self.optim_iter_summary,
                    optimization_stats=self.system_simulator.stats)
            except WallTimeExceeded:
                optim_result = self.write_state_to_result()

        elif self.method == 'Nelder-Mead':
            try:
                result = scipy.optimize.minimize(
                    fun=self.cost_fktn_wrapper,
                    x0=initial_control_amplitudes.T.flatten(),
                    bounds=self.bounds,
                    method=self.method,
                    options={
                        'maxiter':
                        self.termination_conditions["max_iterations"]
                    },
                )

                optim_result = optimization_data.OptimizationResult(
                    final_cost=result.fun,
                    indices=self.system_simulator.cost_indices,
                    final_parameters=result.x.reshape(
                        self.pulse_shape[::-1]).T,
                    num_iter=result.nfev,
                    termination_reason=result.message,
                    status=result.status,
                    optimizer=self,
                    optim_summary=self.optim_iter_summary,
                    optimization_stats=self.system_simulator.stats)
            except WallTimeExceeded:
                optim_result = self.write_state_to_result()

        else:
            try:
                result = scipy.optimize.minimize(
                    fun=self.cost_fktn_wrapper,
                    x0=initial_control_amplitudes.T.flatten(),
                    bounds=self.bounds,
                    method=self.method)

                optim_result = optimization_data.OptimizationResult(
                    final_cost=result.fun,
                    indices=self.system_simulator.cost_indices,
                    final_parameters=result.x.reshape(
                        self.pulse_shape[::-1]).T,
                    num_iter=result.nfev,
                    termination_reason=result.message,
                    status=result.status,
                    optimizer=self,
                    optim_summary=self.optim_iter_summary,
                    optimization_stats=self.system_simulator.stats)
            except WallTimeExceeded:
                optim_result = self.write_state_to_result()

        if self.system_simulator.stats is not None:
            self.system_simulator.stats.end_t_opt = time.time()

        return optim_result