Variables¶

class Variables¶

Bases: object

A class for variables with different types.

Methods

__init__

Constructor.

convert_to_amplify_solution_dict

Convert from Amplify-BBOpt's solution dict to Amplify SDK's solution dict.

decode

Decodes an encoded value vector to an input value vector.

encode

Encode an input value vector to an the Amplify SDK's variable value vector.

generate_amplify_constraint

Constructs one_hot/domain_wall constraints required for non-binary decision variables.

generate_random_value

Generates random value vector compatible with self.

issue_amplify_variable

Issue the Amplify SDK's variables (amplify.PolyArray) for all elemental variables.

nullify_poly_array

Make poly_array = None for all variables involved.

set_elemental_poly_array

Set amplify.PolyArray (issued in Variables.issue_amplify_variable) of all elemental variables to Variables.poly_array.

unify_variables

Unify duplicate variables in different Variables class instances associated with this BlackBoxFuncList class.

Attributes

amplify_variables

The Amplify SDK's variables (amplify.PolyArray) in dictionary form that represents the variables in this Variables.

flat_names

Return names of the variables.

names

Return names of the variables.

num_amplify_variables

A number of encoded variables for [num_elemental_variables] variables.

num_elemental_variables

A number of instances of VariableBase and VariableBase in the VariableListBase instances.

poly_array

The Amplify SDK's variables (amplify.PolyArray) that represents the variables in this Variables.

variable_generator

The Amplify SDK's variable generator.

__getattr__(name: str) VariableBase | VariableListBase¶

Get a variable in var_dict.

Parameters:

name (str) – The variable name.

Raises:

ValueError – If variable with a given name is not found.

Returns:

A variable or variable list.

Return type:

VariableBase | VariableListBase

__getitem__(i: int) VariableBase | VariableListBase¶
__init__(**kwargs: VariableBase | VariableListBase) None¶

Constructor.

__len__() int¶
__str__() str¶

Return a human-readable information of the variables.

Returns:

The human-readable information of the variables.

Return type:

str

convert_to_amplify_solution_dict(
solution_dict: StructuredSolutionDict,
) dict[Poly, int | float]¶

Convert from Amplify-BBOpt’s solution dict to Amplify SDK’s solution dict.

Parameters:

solution_dict (StructuredSolutionDict) – Amplify-BBOpt’s solution dict. This can be entire solution in case of multiple-objective optimization (keys other than this Variables’ keys can exist in the solution dict).

Returns:

The converted Amplify SDK’;’s solution dict.

Return type:

dict[amplify.Poly, int | float]

decode(encoded_values: list[int | float]) StructuredSolution¶

Decodes an encoded value vector to an input value vector.

Parameters:

encoded_values (list[int | float]) – A primitive value vector (as in the Amplify SDK’s variables).

Raises:

RuntimeError – The number of encoded variables does not match with the length of the encoded value vector.

Returns:

The resulting input value vector.

Return type:

StructuredSolution

encode(x: StructuredSolution) list[int | float]¶

Encode an input value vector to an the Amplify SDK’s variable value vector.

Parameters:

x (StructuredSolution) – The input value vector.

Raises:

RuntimeError – If the length of the input value vector does not match with the number of variables.

Returns:

A list of the resulting encoded value vector.

Return type:

list[int | float]

generate_amplify_constraint() ConstraintList¶

Constructs one_hot/domain_wall constraints required for non-binary decision variables.

Returns:

The resulting constraints.

Return type:

amplify.ConstraintList

generate_random_value(
rng: Generator,
ref_value: StructuredSolution | None = None,
find_neighbour: bool = False,
) StructuredSolution¶

Generates random value vector compatible with self. If ref_value is specified, only one element in ref_value will be randomly modified so that the return value vector yields close values to the ref_value. Note that ref_value will not be modified.

Parameters:
  • rng (np.random.Generator) – NumPy’s random generator.

  • ref_value (StructuredSolution | None, optional) – A reference input value vector. Defaults to None.

  • find_neighbour (bool, optional) – True to generate a random value neighbour to the reference value. Defaults to False.

Returns:

The resulting input value vector of the variables.

Return type:

StructuredSolution

issue_amplify_variable() None¶

Issue the Amplify SDK’s variables (amplify.PolyArray) for all elemental variables.

nullify_poly_array(vars_count: int = 0) None¶

Make poly_array = None for all variables involved.

set_elemental_poly_array() None¶

Set amplify.PolyArray (issued in Variables.issue_amplify_variable) of all elemental variables to Variables.poly_array.

Raises:

RuntimeError – If poly_array is not set for the variable.

unify_variables(
var_dict_universe: dict[str, VariableBase | VariableListBase],
variable_generator: VariableGenerator,
) None¶

Unify duplicate variables in different Variables class instances associated with this BlackBoxFuncList class.

Parameters:
  • var_dict_universe (dict[str, VariableBase | VariableListBase]) – A variable dictionary containing all the variables with their names in the multiple Variables class instances.

  • variable_generator (amplify.VariableGenerator) – A variable generator instantiated in the Amplify SDK.

Raises:

RuntimeError – If unknown variable name is found.

__dict__ = mappingproxy({'__module__': 'amplify_bbopt.variables', '__doc__': 'A class for variables with different types.', '__init__': <function Variables.__init__>, 'variable_generator': <property object>, 'unify_variables': <function Variables.unify_variables>, 'issue_amplify_variable': <function Variables.issue_amplify_variable>, 'set_elemental_poly_array': <function Variables.set_elemental_poly_array>, 'num_elemental_variables': <property object>, 'num_amplify_variables': <property object>, '__len__': <function Variables.__len__>, '__getitem__': <function Variables.__getitem__>, '__getattr__': <function Variables.__getattr__>, '_set_variable': <function Variables._set_variable>, '_del_variable': <function Variables._del_variable>, 'nullify_poly_array': <function Variables.nullify_poly_array>, 'poly_array': <property object>, 'amplify_variables': <property object>, 'convert_to_amplify_solution_dict': <function Variables.convert_to_amplify_solution_dict>, 'names': <property object>, 'flat_names': <property object>, '__str__': <function Variables.__str__>, 'encode': <function Variables.encode>, 'decode': <function Variables.decode>, 'generate_random_value': <function Variables.generate_random_value>, 'generate_amplify_constraint': <function Variables.generate_amplify_constraint>, '__dict__': <attribute '__dict__' of 'Variables' objects>, '__weakref__': <attribute '__weakref__' of 'Variables' objects>, '__annotations__': {'var_dict': 'dict[str, VariableBase | VariableListBase]', '_poly_array': 'amplify.PolyArray | None'}})¶
__weakref__¶

list of weak references to the object (if defined)

property amplify_variables: dict[str, Poly | PolyArray]¶

The Amplify SDK’s variables (amplify.PolyArray) in dictionary form that represents the variables in this Variables.

property flat_names: list[str]¶

Return names of the variables. The variable names for a variable list is expanded.

(the names of elemental variables in a variable list are returned instead of the name of the variable list).

Raises:

RuntimeError – If the variable name is not set.

Returns:

A list of the variable names in a ‘flat’ manner.

Return type:

list

property names: list¶

Return names of the variables.

Returns:

A list of the variable names.

Return type:

list

property num_amplify_variables: int¶

A number of encoded variables for [num_elemental_variables] variables.

property num_elemental_variables: int¶

A number of instances of VariableBase and VariableBase in the VariableListBase instances.

property poly_array: PolyArray¶

The Amplify SDK’s variables (amplify.PolyArray) that represents the variables in this Variables.

property variable_generator: VariableGenerator¶

The Amplify SDK’s variable generator.