polypus 0.8.0

Rust-core quantum variational optimization framework with a Python (PyO3) interface: circuit simulation, VQC/QML training and population-based optimizers (DE, PSO, QNG, CMA-ES). Python users: `pip install polypus-quantum` (the module imports as `polypus`).
Documentation
use pyo3::prelude::*;

/// Rotosolve optimizer configuration: Ostaszewski, Grant and Benedetti's
/// closed-form coordinate update, with the value reuse of Nakanishi, Fujii and
/// Todo.
///
/// It needs a circuit in which every trainable parameter is the angle of
/// exactly one rotation generated by a single Pauli operator (`rx`, `ry`,
/// `rz`, `rxx`, `rzz`, `p`, `u1`, any angle of `u`, `u2` or `u3`, and for
/// Qiskit circuits also `ryy`, `rzx` and the angle θ of `r`), as `±θ + c`,
/// and a fitness that averages a cost per bitstring over
/// the shots. Then the fitness is a sinusoid of each parameter, and one
/// parameter at a time moves to its maximum, computed from the current point
/// and the two points `±π/2` along the parameter. `train` and `qml.train`
/// check the circuit before anything runs and raise `ValueError` naming the
/// first gate or parameter that does not qualify; `qml.train` also rejects a
/// `polypus.SampleCost`, which is not such an average.
///
/// Each step is one batch of two circuits; the step reuses the value the
/// previous one predicted for the current point, and measures the point
/// again (a third circuit) on the first step and every `reevaluate_every`
/// steps, so shot noise does not build up. A sweep visits every parameter once,
/// in order. After the last step, the point it reached is measured too, one
/// more circuit. `best_params` is the best of the points measured. The run stops
/// after `patience` consecutive sweeps that improve the best measured fitness
/// by less than `tolerance`, or after `sweeps` sweeps. The settings are
/// validated when `train`/`qml.train` starts, before any circuit runs: an
/// invalid one raises `ValueError` (contract C-5).
#[pyclass(module = "polypus")]
pub struct Rotosolve {
    /// Sweeps at most, each one step per parameter. At least 1.
    #[pyo3(get, set)]
    pub sweeps: u32,
    /// Steps between measurements of the current point, at least 1. Defaults
    /// to 5: the first step and every fifth after it send three circuits
    /// instead of two. `1` measures the point on every step, as plain
    /// Rotosolve does.
    #[pyo3(get, set)]
    pub reevaluate_every: u32,
    /// The least improvement of the best measured fitness over a sweep that
    /// counts as progress, in the objective's units, finite and `>= 0`.
    /// Defaults to `0.01`; `0` never stops early.
    #[pyo3(get, set)]
    pub tolerance: f64,
    /// Consecutive sweeps without progress that stop the run, at least 1.
    /// Defaults to 2.
    #[pyo3(get, set)]
    pub patience: u32,
    /// Optional RNG seed pinned on the optimizer object. Consumed by
    /// `train`/`qml.train` per the precedence rule (contract C-7): the explicit
    /// `seed` kwarg passed to the call wins; this field is the fallback; a fresh
    /// OS-entropy value is used when neither is set. It only draws the
    /// starting point. `None` by default.
    #[pyo3(get, set)]
    pub seed: Option<u64>,
}

#[pymethods]
impl Rotosolve {
    #[new]
    #[pyo3(signature = (sweeps = 10, reevaluate_every = 5, tolerance = 0.01, patience = 2, seed = None))]
    pub fn new(
        sweeps: u32,
        reevaluate_every: u32,
        tolerance: f64,
        patience: u32,
        seed: Option<u64>,
    ) -> Self {
        Rotosolve {
            sweeps,
            reevaluate_every,
            tolerance,
            patience,
            seed,
        }
    }
}