pub struct VariationalQuantumOptimizer<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
Variational Quantum Optimizer.
VariationalQuantumOptimizer implements a SPSA optimizer with a
quantum-inspired ansatz update rule. SPSA approximates the gradient with
g_i(k) ≈ (L(θ + c_k * Δ) - L(θ - c_k * Δ)) / (2 * c_k * Δ_i)where Δ ∈ {-1, +1}^d is sampled uniformly at every iteration. The gain
sequences follow the canonical Spall (1998) recipe:
a_k = a / (k + 1 + A)^α
c_k = c / (k + 1)^γThe “quantum ansatz” applies a rotation-gate-inspired factor cos²(θ_i / 2)
to the SPSA update, smoothing updates near θ_i = 0 (mimicking how a
rotation gate has unit effect near identity) and vanishing near θ_i = π.
§Examples
use optirs_core::quantum_inspired::VariationalQuantumOptimizer;
use scirs2_core::ndarray::Array1;
let mut optimizer: VariationalQuantumOptimizer<f64> =
VariationalQuantumOptimizer::new(0.1)
.with_perturbation(0.05)
.with_seed(7);
let params = Array1::from_vec(vec![0.5, -0.3, 1.2]);
let loss_fn = |theta: &Array1<f64>| theta.iter().map(|x| x * x).sum::<f64>();
let next = optimizer.step_from_loss(¶ms, loss_fn).expect("step failed");
assert_eq!(next.len(), 3);Implementations§
Source§impl<A> VariationalQuantumOptimizer<A>
impl<A> VariationalQuantumOptimizer<A>
Sourcepub fn with_default_gain() -> Self
pub fn with_default_gain() -> Self
Create a VQE-inspired SPSA optimizer with the canonical SPSA gain
a = 0.1, matching the defaults already used for c, α, γ and
A.
§Examples
use optirs_core::quantum_inspired::VariationalQuantumOptimizer;
let optimizer = VariationalQuantumOptimizer::<f64>::with_default_gain();
assert!((optimizer.learning_rate() - 0.1).abs() < 1e-12);Sourcepub fn new(learning_rate: A) -> Self
pub fn new(learning_rate: A) -> Self
Create a new VQE-inspired SPSA optimizer with the given learning rate.
Sourcepub fn with_perturbation(self, c: A) -> Self
pub fn with_perturbation(self, c: A) -> Self
Configure the SPSA perturbation magnitude c.
Sourcepub fn with_gain_decay(self, alpha: A, gamma: A) -> Self
pub fn with_gain_decay(self, alpha: A, gamma: A) -> Self
Configure the SPSA decay exponents α (gain) and γ (perturbation).
Sourcepub fn with_stability(self, big_a: A) -> Self
pub fn with_stability(self, big_a: A) -> Self
Configure the SPSA stability offset A.
Sourcepub fn step_count(&self) -> usize
pub fn step_count(&self) -> usize
Returns the current step counter k.
Sourcepub fn learning_rate(&self) -> A
pub fn learning_rate(&self) -> A
Returns the learning rate. Inherent helper that mirrors the trait
method Optimizer::get_learning_rate so callers do not need to
disambiguate the dimension type.
Sourcepub fn set_lr(&mut self, learning_rate: A)
pub fn set_lr(&mut self, learning_rate: A)
Set the learning rate. Inherent helper that mirrors the trait method.
Sourcepub fn ansatz_factor(theta: A) -> A
pub fn ansatz_factor(theta: A) -> A
Quantum-inspired ansatz factor cos²(θ_i / 2). Public for testing.
Trait Implementations§
Source§impl<A: Debug + Float + ScalarOperand + Debug> Debug for VariationalQuantumOptimizer<A>
impl<A: Debug + Float + ScalarOperand + Debug> Debug for VariationalQuantumOptimizer<A>
Source§impl<A, D> Optimizer<A, D> for VariationalQuantumOptimizer<A>
impl<A, D> Optimizer<A, D> for VariationalQuantumOptimizer<A>
Source§fn step(
&mut self,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<Array<A, D>>
fn step( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<Array<A, D>>
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Auto Trait Implementations§
impl<A> Freeze for VariationalQuantumOptimizer<A>where
A: Freeze,
impl<A> RefUnwindSafe for VariationalQuantumOptimizer<A>where
A: RefUnwindSafe,
impl<A> Send for VariationalQuantumOptimizer<A>where
A: Send,
impl<A> Sync for VariationalQuantumOptimizer<A>where
A: Sync,
impl<A> Unpin for VariationalQuantumOptimizer<A>where
A: Unpin,
impl<A> UnsafeUnpin for VariationalQuantumOptimizer<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for VariationalQuantumOptimizer<A>where
A: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.