DiscreteVariable

class DiscreteVariable

Bases: VariableBase

A class for the variable of an arbitrary type with arbitrary discretization defined by users.

Methods

__init__

Constructor.

binary_to_idx

Convert a binary vector to a value index of the variable.

construct_discretize_table

Discretization information.

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 a non-binary variable.

generate_random_value

Generate a random value compatible with the variable.

idx_to_binary

Convert a value index to a binary vector.

idx_to_value

Converts a value index to a value of the variable.

issue_amplify_variable

Issue the Amplify SDK's variables (amplify.PolyArray) for the variable only when VariableBase.poly_array is None.

issue_amplify_variable_impl

Issue the Amplify SDK variables relevant to this variable.

nullify_poly_array

Make poly_array = None.

to_amplify_poly

Converts the variable to the Amplify's polynomial.

to_poly

Convert the variable to a polynomial with a unity coefficient.

value_to_idx

Converts a value of the variable to a value index.

Attributes

bounds

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

delta

Return the distance between discretization points.

discrete_domain

discretized_list

Discretization list.

incremental_list

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

The Amplify SDK's PolyArray that represents 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__(discretized_list: list[Any], method: str = 'domain_wall') None

Constructor.

Parameters:
  • discretized_list (list[Any]) – A list of discretized values.

  • method (str, optional) – Encoding method. ‘dw’: domain-wall. ‘one_hot’: one-hot. ‘amplify’ is not available

  • 'dw'. (as of now. Defaults to)

Raises:
  • ValueError – If ‘amplify’ is specified as the encoding method.

  • ValueError – If the length of the discretized_list is less than 2.

  • ValueError – If different types exists in the discretized_list.

  • ValueError – If the discretized_list has duplicate elements.

__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

__str__() str

Return 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]) int

Convert a binary vector to a value index of the variable.

Parameters:

binary (list[int]) – The binary vector to convert.

Raises:

RuntimeError – If this method is called with the variable encoding method being “amplify”.

Returns:

The resulting value index.

Return type:

int

construct_discretize_table() ndarray

Discretization information.

Returns:

Discretization table.

Return type:

np.ndarray

decode(amplify_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:

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

Returns:

The decoded value.

Return type:

Any

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 form of the Amplify SDK variables (i.e. binary variables).

Return type:

list[int | Any]

generate_amplify_constraint() ConstraintList

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

Raises:

RuntimeError – If the poly_array is not set.

Returns:

The resulting constraints.

Return type:

amplify.ConstraintList

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, optional) – 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

idx_to_binary(idx: int) list[int]

Convert a value index to a binary vector.

Parameters:

idx (int) – The value index to convert.

Raises:

RuntimeError – If this method is called with the variable encoding method being “amplify”.

Returns:

The resulting binary vector.

Return type:

list[int]

idx_to_value(idx: int) Any

Converts a value index to a value of the variable.

Parameters:

idx (int) – The value index.

Returns:

The resulting value of the variable.

Return type:

Any

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

Issue the Amplify SDK’s variables (amplify.PolyArray) for the variable only when VariableBase.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.

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

Issue the Amplify SDK variables relevant to this variable.

Parameters:
  • generator (amplify.VariableGenerator) – A variable generator instantiated by the Amplify SDK.

  • var_counter (dict[amplify.VariableType, int]) – Counters to count the number of variables of each variable type.

  • var_name (dict[amplify.VariableType, str]) – _description_

Raises:

RuntimeError – If the encoding ‘amplify’ is used for DiscreteVariable.

nullify_poly_array() None

Make poly_array = None.

to_amplify_poly() Poly

Converts the variable to the Amplify’s polynomial.

Raises:

RuntimeError – If poly_array is not set for the variable.

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

value_to_idx(value: Any) int

Converts a value of the variable to a value index.

Parameters:

value (Any) – The value to convert.

Raises:

ValueError – If the value is not found in the discretized_list.

Returns:

The resulting value index.

Return type:

int

__abstractmethods__ = frozenset({})
__dict__ = mappingproxy({'__module__': 'amplify_bbopt.variable', '__doc__': 'A class for the variable of an arbitrary type with arbitrary discretization defined by users.', '__init__': <function DiscreteVariable.__init__>, 'discretized_list': <property object>, 'incremental_list': <property object>, 'value_to_idx': <function DiscreteVariable.value_to_idx>, 'idx_to_value': <function DiscreteVariable.idx_to_value>, 'to_amplify_poly': <function DiscreteVariable.to_amplify_poly>, 'issue_amplify_variable_impl': <function DiscreteVariable.issue_amplify_variable_impl>, '__abstractmethods__': frozenset(), '_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 discretized_list: list

Discretization list.

Returns:

Discretization list.

Return type:

list

property incremental_list: list[Any]
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

The Amplify SDK’s PolyArray that represents this variable.

property type: type

Return the type of the variable.

Returns:

The variable type.

Return type:

type