qudit_inst/numerical/
problem.rs

1//! This module defines the `Problem` trait and associated traits for defining optimization problems.
2//!
3//! Any object implementing the `Problem` trait and some associated `ProvidesX` traits defines how an
4//! `OptimizationStrategy` can build the associated methods necessary for optimization. Certain optimizers
5//! and strategies will require specific capabilities and will fail to compile if not provided.
6//! This design is for performance reasons. Some problems may be computationally expensive to build as well as evaluate.
7//! The separation of a `Problem` from the `Function` it generates allows performance optimizations in these cases.
8
9use crate::numerical::function::CostFunction;
10use crate::numerical::function::Gradient;
11use crate::numerical::function::Hessian;
12use crate::numerical::function::Jacobian;
13use crate::numerical::function::ResidualFunction;
14use qudit_core::RealScalar;
15
16/// A trait for defining optimization problems.
17pub trait Problem {
18    /// Returns the number of parameters in the problem.
19    fn num_params(&self) -> usize;
20}
21
22/// A trait for problems that provide a cost function.
23pub trait ProvidesCostFunction<R: RealScalar>: Problem {
24    /// The type of the cost function.
25    type CostFunction: CostFunction<R>;
26
27    /// Builds the cost function.
28    fn build_cost_function(&self) -> Self::CostFunction;
29}
30
31/// A trait for problems that provide a gradient.
32pub trait ProvidesGradient<R: RealScalar>: Problem {
33    /// The type of the cost function.
34    type Gradient: Gradient<R>;
35
36    /// Builds the gradient.
37    fn build_gradient(&self) -> Self::Gradient;
38}
39
40/// A trait for problems that provide a Hessian.
41pub trait ProvidesHessian<R: RealScalar>: Problem {
42    /// The type of the cost function.
43    type Hessian: Hessian<R>;
44
45    /// Builds the Hessian.
46    fn build_hessian(&self) -> Self::Hessian;
47}
48
49/// A trait for problems that provide a residual function.
50pub trait ProvidesResidualFunction<R: RealScalar>: Problem {
51    /// The type of the cost function.
52    type ResidualFunction: ResidualFunction<R>;
53
54    /// Builds the residual function.
55    fn build_residual_function(&self) -> Self::ResidualFunction;
56}
57
58/// A trait for problems that provide a Jacobian.
59pub trait ProvidesJacobian<R: RealScalar>: Problem {
60    /// The type of the cost function.
61    type Jacobian: Jacobian<R>;
62
63    /// Builds the Jacobian.
64    fn build_jacobian(&self) -> Self::Jacobian;
65}