VariableListBase

class VariableListBase

Bases: Variable

A base class for all variable lists.

Methods

__init__

Constructor.

binary_to_idx

Convert a Amplify SDK's variable value vector (i.e. binary variable) to value indices of the variable list.

construct_discretize_table

Discretization information.

decode

Decodes values of the Amplify SDK variables (i.e. binary variables) to a list of values of this variable list.

encode

Encode the values to values of the Amplify SDK variables (i.e. binary variables) if necessary.

generate_amplify_constraint

Generate the Amplify SDK's constraints related to the conversion of non-binary variable.

generate_random_value

Generate a random value compatible with the variable.

idx_to_binary

Convert value indices of the variable array to a Amplify SDK's variable value vector.

idx_to_value

Convert value indices to values of the variable list.

issue_amplify_variable

Issue the Amplify SDK variables (amplify.PolyArray) relevant to the variables contained in this variable list, only when VariableListBase.poly_array is None.

nullify_poly_array

Make poly_array = None for the elemental variables in the variable list.

sum

to_amplify_poly

Convert the variable to a Amplify SDK's polynomial.

to_poly

Return the polynomial that represents the sum of all the variables in the variable list class.

value_to_idx

Convert values of the variable list to value indices.

Attributes

bounds

Return the lower and upper bounds that the variable can take.

delta

Return the distance between discretization points.

discrete_domain

len

Return the number of the variables.

method

Return the encoding method.

name

Return the name of the variable.

nbins

Return the number of discretization bins.

num_amplify_variables

Return the number of the Amplify SDK variables encoded from the variable.

poly_array

Return the Amplify SDK's PolyArray that represents all elemental variables existing in this variable list.

type

Return the type of the variable.

variable_list

Return the list of elemental variables in the variable list.

__add__(other: Any) Poly

Add the variable to another variable or a polynomial.

Parameters:

other – A variable or a polynomial to add.

Raises:

ValueError – If other is not a variable or a polynomial.

Returns:

The resulting polynomial.

Return type:

Poly

__getitem__(i: int) VariableBase
__init__(var_type: type, bounds: tuple, delta: float, nbins: int, length: int, method: str | None = 'domain_wall') None

Constructor.

Parameters:
  • var_type (type) – A type of the variable.

  • bounds (tuple) – A lower and upper bounds that the variable can take.

  • delta (float) – A discretization step width.

  • nbins (int) – A number of dicsretization bins.

  • length (int) – A number of the variables in the list.

  • method (str | None, optional) – Encoding method. ‘dw’: domain-wall. ‘one_hot’: one-hot. ‘amplify’: Amplify SDK’s encoder. Defaults to ‘dw’.

Raises:

ValueError – If the length is equal to or less than 1.

__iter__() Iterator[VariableBase]
__len__() int

Return the number of the variables.

Returns:

The number of the variables.

Return type:

int

__mul__(other: Any) Poly

Return the polynomial that represents the sum of all the variables multiplied by a value.

Parameters:

other – A value to multiply.

Returns:

The resulting polynomial.

Return type:

Poly

__radd__(other: Any) Poly

Add the variable to another variable or a polynomial.

Parameters:

other – A variable or a polynomial to add.

Raises:

ValueError – If other is not a variable or a polynomial.

Returns:

The resulting polynomial.

Return type:

Poly

__rmul__(other: Any) Poly

Return the polynomial that represents the sum of all the variables multiplied by a value.

Parameters:

other – A value to multiply.

Returns:

The resulting polynomial.

Return type:

Poly

__rsub__(other: Any) Poly

Subtract another variable or a polynomial from the variable.

Parameters:

other – A variable or a polynomial to subtract.

Raises:

ValueError – If other is not a variable or a polynomial.

Returns:

The resulting polynomial.

Return type:

Poly

__str__() str

Returns a human-readable information of the variables.

Returns:

The human-readable information of the variables.

Return type:

str

__sub__(other: Any) Poly

Subtract another variable or a polynomial from the variable.

Parameters:

other – A variable or a polynomial to subtract.

Raises:

ValueError – If other is not a variable or a polynomial.

Returns:

The resulting polynomial.

Return type:

Poly

binary_to_idx(binary: list[int]) list[int]

Convert a Amplify SDK’s variable value vector (i.e. binary variable) to value indices of the variable list.

Parameters:

binary (list[int]) – The Amplify SDK’s variable value vector.

Raises:

RuntimeError – If “amplify” is specified as the encoding method.

Returns:

A list of the resulting value indices of the variable list.

Return type:

list[int]

construct_discretize_table() ndarray

Discretization information.

Returns:

The variable list.

Return type:

np.ndarray

decode(amplify_value_vector: list[int | Any]) list[Any]

Decodes values of the Amplify SDK variables (i.e. binary variables) to a list of values of this variable list.

Parameters:

amplify_value_vector (list[int | Any]) – A list of the Amplify SDK variable values to decode.

Returns:

A list of the decoded values.

Return type:

list[Any]

encode(value_list: list[Any]) list[int | Any]

Encode the values to values of the Amplify SDK variables (i.e. binary variables) if necessary.

Parameters:

value_list (list[Any]) – The values to encode.

Returns:

The encoded value in the form of the Amplify SDK variables (i.e. binary variables).

Return type:

list[int | Any]

generate_amplify_constraint() ConstraintList

Generate the Amplify SDK’s constraints related to the conversion of non-binary variable.

Returns:

The resulting constraints.

Return type:

amplify.ConstraintList

generate_random_value(
rng: Generator,
ref_value: list[Any] | None = None,
find_neighbour: bool = False,
) Any

Generate a random value compatible with the variable.

If ref_value specified, only one element from ref_value is randomly modified so that values != ref_value. User-defined constraints (if there’s any) are not considered in this value.

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

  • ref_value (list[Any] | None, optional) – A list of reference values of the variable array. Defaults to None.

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

Returns:

A list of the resulting values of the variable array.

Return type:

Any

idx_to_binary(idx_list: list[int]) list[int]

Convert value indices of the variable array to a Amplify SDK’s variable value vector.

Parameters:

idx_list (list[int]) – A list of the value indices.

Raises:

RuntimeError – If “amplify” is specified as the encoding method.

Returns:

The resulting Amplify SDK’s variable value vector.

Return type:

list[int]

idx_to_value(idx_list: list[int]) list[Any]

Convert value indices to values of the variable list.

Parameters:

idx_list (list[int]) – A list of the value indices.

Raises:

RuntimeError – If “amplify” is specified as the encoding method.

Returns:

A list of the resulting values of the variable list.

Return type:

list[Any]

issue_amplify_variable(
generator: VariableGenerator,
var_counter: dict[VariableType, int],
var_name: dict[VariableType, str],
) None

Issue the Amplify SDK variables (amplify.PolyArray) relevant to the variables contained in this variable list, only when VariableListBase.poly_array is None.

Parameters:
  • generator (amplify.VariableGenerator) – A variable generator.

  • var_counter (dict[amplify.VariableType, int]) – Counter counts how many amplify variables of each amplify.VariableType are issued.

  • var_name (dict[amplify.VariableType, str]) – Name prefix of amplify variables of each amplify.VariableType.

nullify_poly_array() None

Make poly_array = None for the elemental variables in the variable list.

sum() Poly
to_amplify_poly() Poly

Convert the variable to a Amplify SDK’s polynomial. The sum of all variables in the list is considered.

Returns:

The resulting polynomials.

Return type:

amplify.Poly

to_poly() Poly

Return the polynomial that represents the sum of all the variables in the variable list class.

Returns:

The polynomial.

Return type:

Poly

value_to_idx(value_list: list[Any]) list[int]

Convert values of the variable list to value indices.

Parameters:

value_list (list[Any]) – A list of the values to convert.

Raises:

RuntimeError – If “amplify” is specified as the encoding method.

Returns:

The resulting value index list.

Return type:

list[int]

__abstractmethods__ = frozenset({})
__dict__ = mappingproxy({'__module__': 'amplify_bbopt.variable', '__doc__': 'A base class for all variable lists.', '__init__': <function VariableListBase.__init__>, 'nullify_poly_array': <function VariableListBase.nullify_poly_array>, 'poly_array': <property object>, '__len__': <function VariableListBase.__len__>, '__getitem__': <function VariableListBase.__getitem__>, '__iter__': <function VariableListBase.__iter__>, 'to_poly': <function VariableListBase.to_poly>, '__mul__': <function VariableListBase.__mul__>, '__rmul__': <function VariableListBase.__rmul__>, 'sum': <function VariableListBase.sum>, 'num_amplify_variables': <property object>, 'variable_list': <property object>, 'encode': <function VariableListBase.encode>, 'decode': <function VariableListBase.decode>, 'value_to_idx': <function VariableListBase.value_to_idx>, 'idx_to_value': <function VariableListBase.idx_to_value>, 'to_amplify_poly': <function VariableListBase.to_amplify_poly>, 'issue_amplify_variable': <function VariableListBase.issue_amplify_variable>, 'binary_to_idx': <function VariableListBase.binary_to_idx>, 'idx_to_binary': <function VariableListBase.idx_to_binary>, 'generate_amplify_constraint': <function VariableListBase.generate_amplify_constraint>, 'generate_random_value': <function VariableListBase.generate_random_value>, 'construct_discretize_table': <function VariableListBase.construct_discretize_table>, '__str__': <function VariableListBase.__str__>, '__abstractmethods__': frozenset(), '_abc_impl': <_abc._abc_data object>, '__annotations__': {'_variable_list': 'list[VariableBase]', '_name': 'str | None', '_poly_array': 'amplify.PolyArray | None'}})
__slots__ = ()
__weakref__

list of weak references to the object (if defined)

property bounds: tuple

Return the lower and upper bounds that the variable can take.

Returns:

The lower and upper bounds that the variable can take.

property delta: float

Return the distance between discretization points.

Returns:

The distance between discretization points.

property discrete_domain: list | None
property len: int

Return the number of the variables. This is significant for variable list classes.

Returns:

The number of the variables.

Return type:

int

property method: str | None

Return the encoding method.

Returns:

The encoding method. None is no encoding is necessary.

Return type:

str | None

property name: str | None

Return the name of the variable.

Returns:

The variable name.

Return type:

str

property nbins: int

Return the number of discretization bins.

Returns:

The number of discretization bins.

Return type:

int

property num_amplify_variables: int

Return the number of the Amplify SDK variables encoded from the variable.

Returns:

The number of the Amplify SDK variables.

Return type:

int

property poly_array: PolyArray | None

Return the Amplify SDK’s PolyArray that represents all elemental variables existing in this variable list.

Returns:

The Amplify SDK’s PolyArray.

Return type:

amplify.PolyArray

property type: type

Return the type of the variable.

Returns:

The variable type.

Return type:

type

property variable_list: list[VariableBase]

Return the list of elemental variables in the variable list.

Returns:

The list of the variables.

Return type:

list[VariableBase]