Variable¶

class Variable¶

Bases: ABC

A base class for all variables.

Methods

__init__

Constructor for a decision variable.

decode

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

encode

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

generate_amplify_constraint

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

generate_random_value

Generate a random value compatible with the variable.

to_amplify_poly

Convert the variable to the Amplify SDK's polynomial.

to_poly

Convert the variable to a polynomial with a unity coefficient.

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 this variable.

type

Return the type of the variable.

__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

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

Constructor for a decision variable.

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.

  • 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 lower bound is greater than the upper bound.

  • ValueError – If the given encoding method is invalid.

__mul__(other: Any) Poly¶

Multiply the variable by a value.

If this is directly called for a variable list class, the sum of all the variables applied __mul__ is considered.

Parameters:

other – A value to multiply.

Returns:

The resulting polynomial of the variable with a coefficient being the value.

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¶

Multiply the variable by a value.

If this is directly called for a variable list class, the sum of all the variables applied __rmul__ is considered.

Parameters:

other – A value to multiply.

Returns:

The resulting polynomial of the variable with a coefficient being the value.

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

__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

abstract decode(encoded_value_vector: list[int | Any]) Any¶

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

Parameters:

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

Returns:

The decoded value.

Return type:

Any

abstract encode(value: Any) list[int | Any]¶

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

Parameters:

value (Any) – The value to encode.

Returns:

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

Return type:

list[int | Any]

abstract generate_amplify_constraint() ConstraintList¶

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

Returns:

The resulting constraints.

Return type:

amplify.ConstraintList

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

Generate a random value compatible with the variable.

If ref_value specified, ensure that the return value value != 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 (Any | None) – A reference value. Defaults to None.

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

Returns:

The resulting value of the variable.

Return type:

Any

abstract to_amplify_poly() Poly¶

Convert the variable to the Amplify SDK’s polynomial.

Returns:

The resulting polynomial.

Return type:

amplify.Poly

to_poly() Poly¶

Convert the variable to a polynomial with a unity coefficient.

If this is directly called for a variable list class instance, the sum of all the variables in the list is considered.

Returns:

The constructed polynomial.

Return type:

Poly

__abstractmethods__ = frozenset({'decode', 'encode', 'generate_amplify_constraint', 'generate_random_value', 'num_amplify_variables', 'to_amplify_poly'})¶
__dict__ = mappingproxy({'__module__': 'amplify_bbopt.variable', '__doc__': 'A base class for all variables.', '__init__': <function Variable.__init__>, 'discrete_domain': <property object>, 'to_poly': <function Variable.to_poly>, '__mul__': <function Variable.__mul__>, '__rmul__': <function Variable.__rmul__>, '__add__': <function Variable.__add__>, '__sub__': <function Variable.__sub__>, '__radd__': <function Variable.__radd__>, '__rsub__': <function Variable.__rsub__>, 'len': <property object>, 'name': <property object>, 'type': <property object>, 'bounds': <property object>, 'nbins': <property object>, 'delta': <property object>, 'method': <property object>, 'num_amplify_variables': <property object>, 'to_amplify_poly': <function Variable.to_amplify_poly>, 'encode': <function Variable.encode>, 'decode': <function Variable.decode>, 'generate_amplify_constraint': <function Variable.generate_amplify_constraint>, 'generate_random_value': <function Variable.generate_random_value>, '__dict__': <attribute '__dict__' of 'Variable' objects>, '__weakref__': <attribute '__weakref__' of 'Variable' objects>, '__abstractmethods__': frozenset({'num_amplify_variables', 'generate_random_value', 'encode', 'generate_amplify_constraint', 'to_amplify_poly', 'decode'}), '_abc_impl': <_abc._abc_data object>, '__annotations__': {'_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

abstract property num_amplify_variables: int¶

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

Returns:

The number of the Amplify SDK variables.

Return type:

int

property type: type¶

Return the type of the variable.

Returns:

The variable type.

Return type:

type