Skip to main content

VariationalQuantumOptimizer

Struct VariationalQuantumOptimizer 

Source
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(&params, loss_fn).expect("step failed");
assert_eq!(next.len(), 3);

Implementations§

Source§

impl<A> VariationalQuantumOptimizer<A>
where A: Float + ScalarOperand + Debug + Send + Sync,

Source

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);
Source

pub fn new(learning_rate: A) -> Self

Create a new VQE-inspired SPSA optimizer with the given learning rate.

Source

pub fn with_perturbation(self, c: A) -> Self

Configure the SPSA perturbation magnitude c.

Source

pub fn with_gain_decay(self, alpha: A, gamma: A) -> Self

Configure the SPSA decay exponents α (gain) and γ (perturbation).

Source

pub fn with_stability(self, big_a: A) -> Self

Configure the SPSA stability offset A.

Source

pub fn with_seed(self, seed: u64) -> Self

Seed the optimizer’s RNG.

Source

pub fn alpha(&self) -> A

Returns the SPSA α exponent.

Source

pub fn gamma(&self) -> A

Returns the SPSA γ exponent.

Source

pub fn big_a(&self) -> A

Returns the SPSA stability offset A.

Source

pub fn c(&self) -> A

Returns the SPSA perturbation numerator c.

Source

pub fn step_count(&self) -> usize

Returns the current step counter k.

Source

pub fn last_loss(&self) -> Option<A>

Returns the most recently observed loss value, if any.

Source

pub fn seed(&self) -> u64

Returns the seed.

Source

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.

Source

pub fn set_lr(&mut self, learning_rate: A)

Set the learning rate. Inherent helper that mirrors the trait method.

Source

pub fn a_k(&self, k: usize) -> A

SPSA gain a_k.

Source

pub fn c_k(&self, k: usize) -> A

SPSA perturbation c_k.

Source

pub fn reset(&mut self)

Reset the step counter and re-seed the RNG.

Source

pub fn ansatz_factor(theta: A) -> A

Quantum-inspired ansatz factor cos²(θ_i / 2). Public for testing.

Source

pub fn spsa_gradient<F>( &mut self, params: &Array1<A>, loss_fn: F, k: usize, ) -> Result<(Array1<A>, A, Array1<A>)>
where F: Fn(&Array1<A>) -> A,

Compute an SPSA gradient estimate using the supplied loss function.

Returns (gradient, c_k, delta).

Source

pub fn step_from_loss<F>( &mut self, params: &Array1<A>, loss_fn: F, ) -> Result<Array1<A>>
where F: Fn(&Array1<A>) -> A,

Perform a loss-driven SPSA step.

This is the canonical VQE-style update that uses a closed-form loss rather than relying on user-provided gradients.

Trait Implementations§

Source§

impl<A: Debug + Float + ScalarOperand + Debug> Debug for VariationalQuantumOptimizer<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A, D> Optimizer<A, D> for VariationalQuantumOptimizer<A>
where A: Float + ScalarOperand + Debug + Send + Sync, D: Dimension,

Source§

fn step( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<Array<A, D>>

Updates parameters using the given gradients Read more
Source§

fn get_learning_rate(&self) -> A

Gets the current learning rate
Source§

fn set_learning_rate(&mut self, learning_rate: A)

Sets a new learning rate
Source§

fn step_list( &mut self, params_list: &[&Array<A, D>], gradients_list: &[&Array<A, D>], ) -> Result<Vec<Array<A, D>>>

Updates multiple parameter arrays at once Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V