static FunctionNamespace.calculate_gradient_free_optimization(*, graph, cost_node_name, output_node_names, iteration_count=100, target_cost=None, optimization_count=4, cost_history_scope='NONE', seed=None, **kwargs)

Perform gradient-free optimization of generic real-valued functions.

Use this function to determine a choice of variables that minimizes the value of a scalar real-valued cost function of those variables. You express that cost function as a graph describing how the input variables are transformed into the cost value.

This function provides an alternative to the gradient based optimizer, and is useful when the gradient is either very costly to compute or inaccessible (for example if the graph includes a node that does not allow gradients).

  • graph (qctrl.graphs.Graph) – The graph describing the cost \(C(\mathbf v)\) and outputs \(\{F_j(\mathbf v)\}\) as functions of the optimizable input variables \(\mathbf v\). The graph must contain nodes with names \(s\) (giving the cost function) and \(\{s_j\}\) (giving the output functions).

  • cost_node_name (str) – The name \(s\) of the real-valued scalar graph node that defines the cost function \(C(\mathbf v)\) to be minimized.

  • output_node_names (List[str]) – The names \(\{s_j\}\) of the graph nodes that define the output functions \(\{F_j(\mathbf v)\}\). The function evaluates these using the optimized variables and returns them in the output.

  • iteration_count (int, optional) – The number of iterations the optimizer performs until it halts. Defaults to 100.

  • target_cost (float, optional) – A target value of the cost that you can set as an early stop condition for the optimizer. If the cost becomes equal or smaller than this value, the optimization halts. Defaults to None, which means that the optimizer runs for iteration_count iterations.

  • optimization_count (int, optional) – The number \(N\) of independent randomly seeded optimizations to perform. The function returns the results from the best optimization (the one with the lowest cost). Defaults to 4. Depending on the landscape of the optimization problem, a larger value will help in finding lower costs, at the expense of prolonging computation time.

  • cost_history_scope (qctrl.dynamic.types.HistoryScope, optional) – Configuration for the scope of the returned cost history data. Use this to select if you want the history data to be returned. Defaults to no cost history data returned.

  • seed (int, optional) – Seed for the random number generator used in the optimizer. If set, must be non-negative. Use this option to generate deterministic results from the optimizer. Note that if your graph contains nodes generating random samples, you need to also set seeds for those nodes to ensure a reproducible result.


The result of an optimization. It includes the minimized cost \(C(\mathbf v_\mathrm{optimized})\), and the outputs \(\{s_j: F_j(\mathbf v_\mathrm{optimized})\}\) corresponding to the variables that achieve that minimum cost.

Return type:


See also


Perform gradient-based deterministic optimization of generic real-valued functions.


Perform gradient-based stochastic optimization of generic real-valued functions.


Given a cost function \(C(\mathbf v)\) of variables \(\mathbf v\), this function computes an estimate \(\mathbf v_\mathrm{optimized}\) of \(\mathrm{argmin}_{\mathbf v} C(\mathbf v)\), namely the choice of variables \(\mathbf v\) that minimizes \(C(\mathbf v)\). The function then calculates the values of arbitrary output functions \(\{F_j(\mathbf v_\mathrm{optimized})\}\) with that choice of variables.

This function represents the cost and output functions as nodes of a graph. This graph defines the input optimization variables \(\mathbf v\), and how these variables are transformed into the corresponding cost and output quantities. You build the graph from primitive nodes defined in the graphs of the Q-CTRL Python package. Each such node, which can be identified by a name, represents a function of the previous nodes in the graph (and thus, transitively, a function of the input variables). You can use any named scalar real-valued node \(s\) as the cost function, and any named nodes \(\{s_j\}\) as outputs.

After you provide a cost function \(C(\mathbf v)\) (via a graph), this function runs an optimization process \(N\) times, each with random initial variables, to identify \(N\) local minima of the cost function, and then takes the variables corresponding to the best such minimum as \(\mathbf v_\mathrm{optimized}\).

The optimizer will carry out the gradient-free optimization for a number of iterations set by iteration_count. If a target_cost is passed and the cost becomes less than or equal to this value then the optimizer will terminate early.

The gradient-free optimizer used here is the covariance matrix adaptation evolution strategy (CMA-ES) based optimizer. For more detail on CMA-ES see CMA-ES on Wikipedia.


See the How to optimize controls using gradient-free optimization user guide.