Optimization Module#
The qmlhc.optim package provides both a general Optimizer API
(qmlhc.optim.api) and a NumPy-based optimizer registry
(qmlhc.optim.registry_numpy) that wire multiple optimization
strategies to a unified project interface.
It enables interchangeable use of gradient-free, gradient-based, and trust-region algorithms for training or adaptive control within the hypercausal learning flow.
Architecture Overview#
+--------------------------+
| qmlhc.optim |
+-------------+------------+
|
+--------v--------+ +------------------------------+
| api | ---> | registry_numpy |
| (Optimizer API) | | (factory for NumPy backends) |
+-----------------+ +------------------------------+
Unified Optimizer Interface#
All optimizers returned by the NumPy registry expose a unified interface:
initialize(params) -> statestep_params(model, params, context) -> (new_params, state)
Where:
params- possibly nested dictionary of numeric parameters.model- callable that acceptsparamsand returns a scalar loss.context- optional dictionary with auxiliary signals (gradients, KL, rollouts, etc.).
Registry Keys (NumPy)#
Use create_optimizer_numpy(name, **kwargs) with:
"finite-diff"- finite-difference gradient estimation"spsa"- simultaneous perturbation stochastic approximation (SPSA)"adam"- Adam optimizer (using provided or estimated gradients)"natural-grad"- natural gradient optimizer with statistical preconditioning"trust-kl"- trust-region optimizer with KL constraint (wrapper)"dual-ascent"- dual-ascent optimizer with Lagrange multiplier (wrapper)"mpc"- short-horizon model predictive control optimizer (MPC)"kfac"- Kronecker-factored approximation (blockwise preconditioner)
Core Contracts#
Optimizer |
Summary |
|---|---|
|
Estimates gradients via finite differences on |
|
SPSA with simultaneous perturbations (optional antithetic mode). Constant evaluation cost (~2 function calls) regardless of parameter dimensionality. |
|
Adam optimizer applied to either estimated gradients (e.g., FD/SPSA) or provided ones through |
|
Natural Gradient optimizer using statistical preconditioning (e.g., covariance of branch states).
Consumes |
|
Trust-region wrapper enforcing a KL divergence constraint on state transitions.
Wraps a base optimizer and applies backtracking line-search until |
|
Dual-ascent wrapper that updates primal parameters using a base optimizer and a Lagrange multiplier for constraint enforcement (e.g., KL). Requires a constraint function in the execution context. |
|
Short-horizon MPC optimizer performing multi-step rollouts using |
|
K-FAC preconditioner performing Kronecker-factored blockwise updates to accelerate second-order optimization, using statistics from structured parameter sets when available. |
Required Context (by Optimizer)#
Optimizer |
Required |
|---|---|
|
(Optional) |
|
(Optional) |
|
Requires either |
|
Requires |
|
Requires |
|
Requires |
|
Requires |
|
Optionally uses blockwise statistics in |
Implementation Notes#
Wrapper optimizers (
trust-kl,dual-ascent) do not perform updates directly; they delegate the actual parameter step to their ``base_opt`` and apply additional logic (KL bound, constraint enforcement, etc.) around it.If neither gradients nor a gradient estimator are provided to
adamornatural-grad, the optimizer cannot proceed. Provide either agrad_estimatorin the constructor orcontext['grads']at runtime.
Minimal Implementation Example#
The following snippet illustrates how to integrate an optimizer within the QML-HCS pipeline using the unified NumPy registry. It shows the initialization and parameter-update logic conceptually, without executing a full training loop.
from qmlhc.optim.registry_numpy import create_optimizer_numpy
# Define a derivative-free SPSA optimizer
opt = create_optimizer_numpy("spsa", lr0=0.05, eps0=0.10, antithetic=True)
# Initialize optimizer state
params = {"alpha": 1.0}
opt_state = opt.initialize(params)
# Example single update (model function provided externally)
new_params, opt_state = opt.step_params(
model=lambda p: (p["alpha"] - 1.0) ** 2, # illustrative cost
params=params,
context={"step": 0}
)
Note
This example illustrates only how optimizers are conceptually integrated into the hypercausal learning flow. For a fully runnable integration combining SPSA, KL trust-region control, and adaptive drift resilience under PennyLane backends, see Full Hypercausal System Demo. The physical variant extending drift into hardware-level perturbations is documented under Alternative Drift Mode (Hardware-Style).
Invariants and Notes#
All optimizers expose the same interface (initialize, step_params).
Registry keys are case-insensitive (e.g., “SPSA” == “spsa”).
Wrappers like
trust-klordual-ascentaccept abase_optargument.NumPy serves as the canonical backend for deterministic optimization.
Improper names trigger
KeyErrorlisting all available optimizers.