Skip to content

quast_decisiontree.algorithms.classical.optimizer_builder

quast_decisiontree.algorithms.classical.optimizer_builder

OptimizerBuilder instances for configuring classical optimizers.

Uses scipy-native ScipyOptimizer by default. Qiskit-based optimizers are available via optional import for backward compatibility.

maxiter module-attribute

maxiter = HyperParam(
    name="maxiter",
    hparam_type=int,
    description="Maximum number of iterations to perform",
    default=128,
    test=lambda x: x > 0,
)

maxfev module-attribute

maxfev = HyperParam(
    name="maxfev",
    hparam_type=int,
    description="Maximum number of function evaluations to perform",
    default=1024,
    test=lambda x: x > 0,
)

tol module-attribute

tol = HyperParam(
    name="tol",
    hparam_type=float,
    description="The tolerance for convergence",
    default=1e-06,
    test=lambda x: x > 0,
)

delta_beta module-attribute

delta_beta = HyperParam(
    name="delta_beta",
    hparam_type=float,
    description="Delta of beta parameter for Linear Ramp initialization",
    default=0.5,
    test=lambda x: x > 0,
)

delta_gamma module-attribute

delta_gamma = HyperParam(
    name="delta_gamma",
    hparam_type=float,
    description="Delta of gamma parameter for Linear Ramp initialization",
    default=0.5,
    test=lambda x: x > 0,
)

COBYLABuilder module-attribute

COBYLABuilder = OptimizerBuilder(
    superclass=CobylaScipy,
    name="Cobyla",
    hyperparams=[maxiter],
    description="Constrained Optimization by Linear Approximation",
)

PowellBuilder module-attribute

PowellBuilder = OptimizerBuilder(
    superclass=PowellScipy,
    name="Powell",
    hyperparams=[maxfev],
    description="Powell optimizer",
)

NMBuilder module-attribute

NMBuilder = OptimizerBuilder(
    superclass=NelderMeadScipy,
    name="Nelder-Mead",
    hyperparams=[maxfev],
    description="Nelder Mead (simplex) method, a gradient-free optimizer",
)

LBFGSBBuilder module-attribute

LBFGSBBuilder = OptimizerBuilder(
    superclass=LBFGSBScipy,
    name="L-BFGS-B",
    hyperparams=[maxiter],
    description="L-BFGS-B gradient-based optimizer with bounds support",
)

LinearRampBuilder module-attribute

LinearRampBuilder = OptimizerBuilder(
    superclass=LinearRamp,
    name="LinearRamp",
    hyperparams=[delta_beta, delta_gamma],
    description="Linear Ramp initializer",
)

OptimizerBuilder

Bases: AutoClassBuilder

Class allowing to build optimizers automatically by providing information about the hyperparameters one needs to provide (or is able to provide) to customize their behavior.

Source code in src/quast_decisiontree/algorithms/classical/optimizer_builder.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class OptimizerBuilder(AutoClassBuilder):
    """Class allowing to build optimizers automatically by providing information about the
    hyperparameters one needs to provide (or is able to provide) to customize their behavior.
    """

    instances = []

    def __init__(
        self,
        superclass,
        hyperparams: list,
        name: str | None = None,
        description: str | None = None,
    ) -> None:
        """Creates an optimizer builder.

        Args:
            superclass: The underlying optimizer class that will be used to construct
                the optimizer.
            hyperparams (list): The hyperparameters matching the superclass constructor.
            name (Optional[str], optional): An optional name to be shown to the user.
            description (Optional[str], optional): An optional description to be shown
                to the user when selecting an optimizer.
        """
        super().__init__(superclass, hyperparams, name, description)
        OptimizerBuilder.instances.append(self)

instances class-attribute instance-attribute

instances = []

__init__

__init__(
    superclass, hyperparams, name=None, description=None
)

Creates an optimizer builder.

Parameters:

Name Type Description Default
superclass

The underlying optimizer class that will be used to construct the optimizer.

required
hyperparams list

The hyperparameters matching the superclass constructor.

required
name Optional[str]

An optional name to be shown to the user.

None
description Optional[str]

An optional description to be shown to the user when selecting an optimizer.

None
Source code in src/quast_decisiontree/algorithms/classical/optimizer_builder.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(
    self,
    superclass,
    hyperparams: list,
    name: str | None = None,
    description: str | None = None,
) -> None:
    """Creates an optimizer builder.

    Args:
        superclass: The underlying optimizer class that will be used to construct
            the optimizer.
        hyperparams (list): The hyperparameters matching the superclass constructor.
        name (Optional[str], optional): An optional name to be shown to the user.
        description (Optional[str], optional): An optional description to be shown
            to the user when selecting an optimizer.
    """
    super().__init__(superclass, hyperparams, name, description)
    OptimizerBuilder.instances.append(self)

LinearRamp

Linear Ramp initializer for QAOA parameters.

Source code in src/quast_decisiontree/algorithms/classical/optimizer_builder.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
class LinearRamp:
    """Linear Ramp initializer for QAOA parameters."""

    def __init__(self, delta_beta: float, delta_gamma: float) -> None:
        self.delta_beta = delta_beta
        self.delta_gamma = delta_gamma

    def minimize(self, fun, x0, bounds=None):
        from scipy.optimize import OptimizeResult

        layers = int(x0.shape[0] / 2)
        gammas = np.arange(1, layers + 1) * self.delta_gamma / layers
        betas = np.arange(1, layers + 1)[::-1] * self.delta_beta / layers
        x = np.concatenate((gammas, -2 * betas))
        fval = fun(x)
        return OptimizeResult(x=x, fun=fval, nfev=1, success=True)

delta_beta instance-attribute

delta_beta = delta_beta

delta_gamma instance-attribute

delta_gamma = delta_gamma

__init__

__init__(delta_beta, delta_gamma)
Source code in src/quast_decisiontree/algorithms/classical/optimizer_builder.py
134
135
136
def __init__(self, delta_beta: float, delta_gamma: float) -> None:
    self.delta_beta = delta_beta
    self.delta_gamma = delta_gamma

minimize

minimize(fun, x0, bounds=None)
Source code in src/quast_decisiontree/algorithms/classical/optimizer_builder.py
138
139
140
141
142
143
144
145
146
def minimize(self, fun, x0, bounds=None):
    from scipy.optimize import OptimizeResult

    layers = int(x0.shape[0] / 2)
    gammas = np.arange(1, layers + 1) * self.delta_gamma / layers
    betas = np.arange(1, layers + 1)[::-1] * self.delta_beta / layers
    x = np.concatenate((gammas, -2 * betas))
    fval = fun(x)
    return OptimizeResult(x=x, fun=fval, nfev=1, success=True)