# optimization_variable¶

Graph.optimization_variable(count, lower_bound, upper_bound, is_lower_unbounded=False, is_upper_unbounded=False, initial_values=None, *, name=None)

Creates optimization variables, which can be bounded, semi-bounded, or unbounded.

Use this function to create a sequence of variables that can be tuned by the optimizer (within specified bounds) in order to minimize the cost function.

Parameters
• count (int) – The number $$N$$ of individual real-valued variables to create.

• lower_bound (float) – The lower bound $$v_\mathrm{min}$$ for generating an initial value for the variables. This will also be used as lower bound if the variables are lower bounded. The same lower bound applies to all count individual variables.

• upper_bound (float) – The upper bound $$v_\mathrm{max}$$ for generating an initial value for the variables. This will also be used as upper bound if the variables are upper bounded. The same upper bound applies to all count individual variables.

• is_lower_unbounded (bool, optional) – Defaults to False. Set this flag to True to define a semi-bounded variable with lower bound $$-\infty$$; in this case, the lower_bound parameter is used only for generating an initial value.

• is_upper_unbounded (bool, optional) – Defaults to False. Set this flag to True to define a semi-bounded variable with upper bound $$+\infty$$; in this case, the upper_bound parameter is used only for generating an initial value.

• initial_values (np.ndarray or List[np.ndarray], optional) – Initial values for optimization variable. Defaults to None, meaning the optimizer initializes these variables with random values. You can either provide a single initial value, or a list of them. Note that all optimization variables in a graph with non-default initial values must have the same length. That is, you must set them either as a single array or a list of arrays of the same length.

• name (str, optional) – The name of the node.

Returns

The sequence $$\{v_n\}$$ of $$N$$ optimization variables. If both is_lower_unbounded and is_upper_unbounded are False, these variables are bounded such that $$v_\mathrm{min}\leq v_n\leq v_\mathrm{max}$$. If one of the flags is True (for example is_lower_unbounded=True), these variables are semi-bounded (for example $$-\infty \leq v_n \leq v_\mathrm{max}$$). If both of them are True, then these variables are unbounded and satisfy that $$-\infty \leq v_n \leq +\infty$$.

Return type

Tensor

anchored_difference_bounded_variables()

Create anchored optimization variables with a difference bound.

calculate_optimization()

Function to find the minimum of a generic function.

Examples

>>> variables = graph.optimization_variable(
...     2, lower_bound=0, upper_bound=1, name="variables"
... )
>>> x = variables[0]
>>> y = variables[1]
>>> cost = (x - 0.1) ** 2 + graph.sin(y) ** 2
>>> cost.name = "cost"
>>> result = qctrl.functions.calculate_optimization(
...     graph=graph, cost_node_name="cost", output_node_names=["variables"]
... )
>>> result.cost
0.0
>>> result.output["variables"]["value"]
array([0.1, 0.])


See examples about optimal control of quantum systems in the How to optimize controls in arbitrary quantum systems using graphs user guide.