QAOptimizerBase¶
- class QAOptimizerBase¶
Bases:
OptimizerBase
,Generic
[T
,M
]Base class for black-box optimizater based on (quantum) annealing of a surrogate/acquisition model.
T
=TypeVar
(T
, bound=TrainerBase
)M
=TypeVar
(M
, bound=QUBOConvertibleBase
)
Methods
Initialize QA-based optimizer.
Return the optimization history.
A function to execute black-box optmization with FMQA.
Set the current best solution and corresponding objective function value.
Update the weight for the objective function.
Attributes
The current Amplify-SDK model.
Objective function value corresponding to the current best solution (optimaized input).
Current best solution (optimized input).
Custom objective function that may be ceated directly from amplify.PolyArray.
Current training dataset (both input and output of an individual objective function).
The number of current optimization cycle.
A number of the samples in initial data.
Objective function class instance.
The latest weight for the objective function.
The current surrogate model.
The instance of the trainer class.
- class SolutionFrequency¶
Bases:
object
Class to determine whether a given solution appears frequently or not.
- how_frequent(solution: FlatSolutionDict) int ¶
- is_frequent(solution: FlatSolutionDict) bool ¶
- __dict__ = mappingproxy({'__module__': 'amplify_bbopt.optimizer', '__doc__': 'Class to determine whether a given solution appears frequently or not.', '__init__': <function QAOptimizerBase.SolutionFrequency.__init__>, '_update_item': <function QAOptimizerBase.SolutionFrequency._update_item>, '_update_counter': <function QAOptimizerBase.SolutionFrequency._update_counter>, 'is_frequent': <function QAOptimizerBase.SolutionFrequency.is_frequent>, 'how_frequent': <function QAOptimizerBase.SolutionFrequency.how_frequent>, '__dict__': <attribute '__dict__' of 'SolutionFrequency' objects>, '__weakref__': <attribute '__weakref__' of 'SolutionFrequency' objects>, '__annotations__': {'_solution_counter': 'dict[tuple, int]'}})¶
- __weakref__¶
list of weak references to the object (if defined)
- classmethod __class_getitem__(params)¶
- __init__(
- data: DataList,
- objective: BlackBoxFuncBase,
- client: FixstarsClient | DWaveSamplerClient | LeapHybridSamplerClient | LeapHybridCQMSamplerClient | FujitsuDA4Client | ToshibaSQBM2Client | GurobiClient | NECVA2Client | None,
- trainer_class: type[T],
- seed: int = 0,
- objective_weight: float | Callable[[Any], float] = 1.0,
Initialize QA-based optimizer.
- Parameters:
data (DataList) – Initial training dataset.
objective (BlackBoxFuncBase) – A black-box function class instance created with
blackbox
decorator.client (amplify.FixstarsClient | amplify.DWaveSamplerClient | amplify.LeapHybridSamplerClient | amplify.LeapHybridCQMSamplerClient | amplify.FujitsuDA4Client | amplify.ToshibaSQBM2Client | amplify.GurobiClient | amplify.NECVA2Client) – A solver client available in the Amplify SDK.
trainer_class (Type[T]) – A trainer class for a surrogate/acquisition model.
seed (int, optional) – A random seed. Defaults to 0.
objective_weight (float | Callable[[Any], float], optional) – A weight imposed on the objective function in amplify.Model. If a Callable object is passed, a weight is the return value of the object with the used optimizer instance as an argument at each optimization cycle. Generally, weight is only useful in case of multi-objective optimization using
MultiObjectiveOptimizer
. Defaults to 1.0.
- classmethod __init_subclass__(*args, **kwargs)¶
This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
- __str__() str ¶
Some human-readable information relevant to the optimizer.
- Returns:
Description.
- Return type:
- fetch_history() History ¶
Return the optimization history.
- Returns:
The optimization history.
- Return type:
- optimize(
- num_cycles: int = 10,
- constraint_weight: float | None = None,
- num_solves: int = 1,
- search_max: int = 1000,
- show_annealing_history: bool = False,
- target_num_iterations: int | None = None,
A function to execute black-box optmization with FMQA.
- Parameters:
num_cycles (int, optional) – A number of optimization iterations. Defaults to 10.
constraint_weight (float | None, optional) – A weight for constraints. If set None, the weight is determined based on the latest training data as in 2 times of
DataList.abs_y_max
. If constraint weights are seperately set forConstraint.weight
, this constraint_weight is multiplied on top ofConstraint.weight
. Defaults to None.num_solves (int, optional) – A number of serial solver execution in the Amplify SDK’s serial solver execution (https://amplify.fixstars.com/en/docs/amplify/v1/serial.html). Defaults to 1.
search_max (int, optional) – If the found solution is found the training data, alternative solution close to the original solution is searched for at most search_max times. Defaults to 1000.
show_annealing_history (bool, optional) – True to show a annealing history with time stamp. Available when Amplify Annealing Engine (amplify.FixstarsClient) is specified as the client in the constructor. Defaults to False.
target_num_iterations (int | None, optional) – Annealing timeout is controlled based on the [previous timeout] * target_num_iterations / [previous num_iterations]. Available when Amplify Annealng Engine is used for optimization solver. Set None to use the given timeout constantly. Defaults to None.
- set_best(best_solution_dict: FlatSolutionDict | None = None) None ¶
Set the current best solution and corresponding objective function value.
If
QAOptimizerBase.custom_amplify_objective
is set, theQAOptimizerBase.best_objective
include the value of the custom objective as well. This inclusion of the custom objective is the difference fromOptimizerBase.set_best
.- Parameters:
best_solution_dict (FlatSolutionDict | None, optional) – A solution dictionary. This may contains solution values from other objective functions in case of multiple-objective optimization. In this case, ‘best’ means the variable value vector relevant to an individual objective function, that is a subset of the entire solution which achieve the best of multiple objectives combined. Therefore, the set best here may not be the same as the best in QAOptimizerBase.data. If None is specified, search the best from OptimizerBase.data. Defaults to None.
- update_objective_weight(optimizer: Any) None ¶
Update the weight for the objective function.
The weight is determined according to the weight function passed to
OptimizerBase.__init__
(OptimizerBase.objective_weight
is callable). In case ofOptimizerBase.objective_weight
being float, the weight is constant over optimization cycles. Expected to be executed at the beginning of each optmization cycle.- Parameters:
optimizer (Any) – An optimizer to be passed to the callable
OptimizerBase.objective_weight
.
- __abstractmethods__ = frozenset({})¶
- __dict__ = mappingproxy({'__module__': 'amplify_bbopt.optimizer', '__doc__': 'Base class for black-box optimizater based on (quantum) annealing of a surrogate/acquisition model.\n\n - :obj:`T` = :obj:`TypeVar` (:obj:`T`, bound= :obj:`TrainerBase`)\n - :obj:`M` = :obj:`TypeVar` (:obj:`M`, bound= :obj:`QUBOConvertibleBase`)\n\n ', '__init__': <function QAOptimizerBase.__init__>, 'SolutionFrequency': <class 'amplify_bbopt.optimizer.QAOptimizerBase.SolutionFrequency'>, 'set_best': <function QAOptimizerBase.set_best>, 'custom_amplify_objective': <property object>, '__str__': <function QAOptimizerBase.__str__>, 'surrogate_model': <property object>, 'amplify_model': <property object>, 'trainer': <property object>, '_evaluate_objective_func': <function QAOptimizerBase._evaluate_objective_func>, '_evaluate_custom_objective': <function QAOptimizerBase._evaluate_custom_objective>, 'optimize': <function QAOptimizerBase.optimize>, '_generate_amplify_model': <function QAOptimizerBase._generate_amplify_model>, '_anneal': <function QAOptimizerBase._anneal>, '_generate_alternative_solution': <function QAOptimizerBase._generate_alternative_solution>, '_ensure_uniqueness': <function QAOptimizerBase._ensure_uniqueness>, 'fetch_history': <function QAOptimizerBase.fetch_history>, '__orig_bases__': (<class 'amplify_bbopt.optimizer.OptimizerBase'>, typing.Generic[~T, ~M]), '__parameters__': (~T, ~M), '__abstractmethods__': frozenset(), '_abc_impl': <_abc._abc_data object>, '__annotations__': {'_surrogate_model': 'M | None', '_amplify_model': 'amplify.Model | None', '_custom_amplify_objective': 'amplify.Poly | None', '_duplicate_solution_counter': 'dict[tuple, int]', '_target_num_iterations': 'int | None', '_elapsed_time': 'list[float]', '_is_de_duplication': 'list[bool]', '_objective_weight': 'float | None', '_best_objective': 'int | float | None'}})¶
- __orig_bases__ = (<class 'amplify_bbopt.optimizer.OptimizerBase'>, typing.Generic[~T, ~M])¶
- __parameters__ = (~T, ~M)¶
- __slots__ = ()¶
- __weakref__¶
list of weak references to the object (if defined)
- property amplify_model: Model | None¶
The current Amplify-SDK model. If the model is not constructed yet, return None.
- Return type:
amplify.Model | None
- property best_objective: int | float¶
Objective function value corresponding to the current best solution (optimaized input).
- property best_solution: StructuredSolutionDict¶
Current best solution (optimized input).
- property custom_amplify_objective: Poly | None¶
Custom objective function that may be ceated directly from amplify.PolyArray.
Custom objective does NOT have to be converted from the surrogate model of the black-box function. Retieve amplify.PolyArray via
Variables.poly_array
orVariables.amplify_variables
, and construct an Amplify SDK’s objective function directly, which will be added to the objective constructed from a surrogate model while creating amplify.Model. Such a custom objective function must be created after the instantiation of an QA-based optimizer class, as this is when amplify.PolyArray’s are issued for each of the variables.- Returns:
Custom objective function.
- Return type:
amplify.Poly | None
- property data: DataList¶
Current training dataset (both input and output of an individual objective function).
- property objective: BlackBoxFuncBase¶
Objective function class instance.
- property surrogate_model: M | None¶
The current surrogate model. If not set return None.
- Return type:
M | None
- property trainer: T¶
The instance of the trainer class.