pygsti.optimize.optimize

Optimization (minimization) functions

Module Contents

Functions

minimize(fn, x0, method='cg', callback=None, tol=1e-10, maxiter=1000000, maxfev=None, stopval=None, jac=None, verbosity=0, **addl_kwargs)

Minimizes the function fn starting at x0.

_fmin_supersimplex(fn, x0, abs_outer_tol, rel_outer_tol, inner_tol, max_outer_iter, min_inner_maxiter, max_inner_maxiter, callback, printer)

Minimize a function using repeated applications of the simplex algorithm.

_fmin_simplex(fn, x0, slide=1.0, tol=1e-08, maxiter=1000)

Minimizes a function using a custom simplex implmentation.

_fmin_particle_swarm(f, x0, err_crit, iter_max, printer, popsize=100, c1=2, c2=2)

A simple implementation of the Particle Swarm Optimization Algorithm.

_fmin_evolutionary(f, x0, num_generations, num_individuals, printer)

Minimize a function using an evolutionary algorithm.

create_objfn_printer(obj_func, start_time=None)

Create a callback function that prints the value of an objective function.

_fwd_diff_jacobian(f, x0, eps=1e-10)

check_jac(f, x0, jac_to_check, eps=1e-10, tol=1e-06, err_type='rel', verbosity=1)

Checks a jacobian function using finite differences.

pygsti.optimize.optimize.minimize(fn, x0, method='cg', callback=None, tol=1e-10, maxiter=1000000, maxfev=None, stopval=None, jac=None, verbosity=0, **addl_kwargs)

Minimizes the function fn starting at x0.

This is a gateway function to all other minimization routines within this module, providing a common interface to many different minimization methods (including and extending beyond those available from scipy.optimize).

Parameters
  • fn (function) – The function to minimize.

  • x0 (numpy array) – The starting point (argument to fn).

  • method (string, optional) – Which minimization method to use. Allowed values are: “simplex” : uses _fmin_simplex “supersimplex” : uses _fmin_supersimplex “customcg” : uses fmax_cg (custom CG method) “brute” : uses scipy.optimize.brute “basinhopping” : uses scipy.optimize.basinhopping with L-BFGS-B “swarm” : uses _fmin_particle_swarm “evolve” : uses _fmin_evolutionary (which uses DEAP) < methods available from scipy.optimize.minimize >

  • callback (function, optional) – A callback function to be called in order to track optimizer progress. Should have signature: myCallback(x, f=None, accepted=None). Note that create_objfn_printer(…) function can be used to create a callback.

  • tol (float, optional) – Tolerance value used for all types of tolerances available in a given method.

  • maxiter (int, optional) – Maximum iterations.

  • maxfev (int, optional) – Maximum function evaluations; used only when available, and defaults to maxiter.

  • stopval (float, optional) – For basinhopping method only. When f <= stopval then basinhopping outer loop will terminate. Useful when a bound on the minimum is known.

  • jac (function) – Jacobian function.

  • verbosity (int) – Level of detail to print to stdout.

  • addl_kwargs (dict) – Additional arguments for the specific optimizer being used.

Returns

scipy.optimize.Result object – Includes members ‘x’, ‘fun’, ‘success’, and ‘message’.

pygsti.optimize.optimize._fmin_supersimplex(fn, x0, abs_outer_tol, rel_outer_tol, inner_tol, max_outer_iter, min_inner_maxiter, max_inner_maxiter, callback, printer)

Minimize a function using repeated applications of the simplex algorithm.

By varying the maximum number of iterations and repeatedly calling scipy’s Nelder-Mead simplex optimization, this function performs as a robust (but slow) minimization.

Parameters
  • fn (function) – The function to minimize.

  • x0 (numpy array) – The starting point (argument to fn).

  • abs_outer_tol (float) – Absolute tolerance of outer loop

  • rel_outer_tol (float) – Relative tolerance of outer loop

  • inner_tol (float) – Tolerance fo inner loop

  • max_outer_iter (int) – Maximum number of outer-loop iterations

  • min_inner_maxiter (int) – Minimum number of inner-loop iterations

  • max_inner_maxiter (int) – Maxium number of outer-loop iterations

  • printer (VerbosityPrinter) – Printer for displaying output status messages.

Returns

scipy.optimize.Result object – Includes members ‘x’, ‘fun’, ‘success’, and ‘message’.

pygsti.optimize.optimize._fmin_simplex(fn, x0, slide=1.0, tol=1e-08, maxiter=1000)

Minimizes a function using a custom simplex implmentation.

This was used primarily to check scipy’s Nelder-Mead method and runs much slower, so there’s not much reason for using this method.

Parameters
  • fn (function) – The function to minimize.

  • x0 (numpy array) – The starting point (argument to fn).

  • slide (float, optional) – Affects initial simplex point locations

  • tol (float, optional) – Relative tolerance as a convergence criterion.

  • maxiter (int, optional) – Maximum iterations.

Returns

scipy.optimize.Result object – Includes members ‘x’, ‘fun’, ‘success’, and ‘message’.

pygsti.optimize.optimize._fmin_particle_swarm(f, x0, err_crit, iter_max, printer, popsize=100, c1=2, c2=2)

A simple implementation of the Particle Swarm Optimization Algorithm.

(Pradeep Gowda 2009-03-16)

Parameters
  • f (function) – The function to minimize.

  • x0 (numpy array) – The starting point (argument to fn).

  • err_crit (float) – Critical error (i.e. tolerance). Stops when error < err_crit.

  • iter_max (int) – Maximum iterations.

  • popsize (int, optional) – Population size. Larger populations are better at finding the global optimum but make the algorithm take longer to run.

  • c1 (float, optional) – Coefficient describing a particle’s affinity for it’s (local) maximum.

  • c2 (float, optional) – Coefficient describing a particle’s affinity for the best maximum any particle has seen (the current global max).

Returns

scipy.optimize.Result object – Includes members ‘x’, ‘fun’, ‘success’, and ‘message’.

pygsti.optimize.optimize._fmin_evolutionary(f, x0, num_generations, num_individuals, printer)

Minimize a function using an evolutionary algorithm.

Uses python’s deap package to perform an evolutionary algorithm to find a function’s global minimum.

Parameters
  • f (function) – The function to minimize.

  • x0 (numpy array) – The starting point (argument to fn).

  • num_generations (int) – The number of generations to carry out. (similar to the number of iterations)

  • num_individuals (int) – The number of individuals in each generation. More individuals make finding the global optimum more likely, but take longer to run.

Returns

scipy.optimize.Result object – Includes members ‘x’, ‘fun’, ‘success’, and ‘message’.

pygsti.optimize.optimize.create_objfn_printer(obj_func, start_time=None)

Create a callback function that prints the value of an objective function.

Parameters
  • obj_func (function) – The objective function to print.

  • start_time (float , optional) – A reference starting time to use when printing elapsed times. If None, then the system time when this function is called is used (which is often what you want).

Returns

function – A callback function which prints obj_func.

pygsti.optimize.optimize._fwd_diff_jacobian(f, x0, eps=1e-10)
pygsti.optimize.optimize.check_jac(f, x0, jac_to_check, eps=1e-10, tol=1e-06, err_type='rel', verbosity=1)

Checks a jacobian function using finite differences.

Parameters
  • f (function) – The function to check.

  • x0 (numpy array) – The point at which to check the jacobian.

  • jac_to_check (function) – A function which should compute the jacobian of f at x0.

  • eps (float, optional) – Epsilon to use in finite difference calculations of jacobian.

  • tol (float, optional) – The allowd tolerance on the relative differene between the values of the finite difference and jac_to_check jacobians if err_type == ‘rel’ or the absolute difference if err_type == ‘abs’.

  • err_type ({'rel', 'abs'), optional) – How to interpret tol (see above).

  • verbosity (int, optional) – Controls how much detail is printed to stdout.

Returns

  • errSum (float) – The total error between the jacobians.

  • errs (list) – List of (row,col,err) tuples giving the error for each row and column.

  • ffd_jac (numpy array) – The computed forward-finite-difference jacobian.