Variable¶
- class Variable¶
Bases:
ABC
A base class for all variables.
Methods
Constructor for a decision variable.
Decode values of the the Amplify SDK variables (i.e. binary variables) to a value of this variable.
Encode the value to values of the Amplify SDK variables (i.e. binary variables) if necessary.
Generate Amplify SDK's constraints related to the conversion of non-binary variable.
Generate a random value compatible with the variable.
Convert the variable to the Amplify SDK's polynomial.
Convert the variable to a polynomial with a unity coefficient.
Attributes
Return the lower and upper bounds that the variable can take.
Return the distance between discretization points.
Return the number of the variables.
Return the encoding method.
Return the name of the variable.
Return the number of discretization bins.
Return the number of the Amplify SDK variables encoded from this variable.
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:
- __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:
- __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:
- __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:
- __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:
- __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:
- 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.
- abstract encode(value: Any) list[int | Any] ¶
Encode the value to values of the Amplify SDK variables (i.e. binary variables) if necessary.
- 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( ) 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:
- __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 len: int¶
Return the number of the variables. This is significant for variable list classes.
- Returns:
The number of the variables.
- Return type:
- 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:
- property nbins: int¶
Return the number of discretization bins.
- Returns:
The number of discretization bins.
- Return type: