use rand_core::Rng;
use crate::rng::below;
use crate::{Oracle, PhaseOracle, StateVector};
use super::{amplify, ratio};
const LAMBDA: f64 = 6.0 / 5.0;
pub fn search_unknown<O, F, C, R>(
state: &mut StateVector,
oracle: &mut PhaseOracle<O, F, C>,
max_queries: usize,
rng: &mut R,
) -> Option<usize>
where
O: Oracle<C>,
F: Fn(usize) -> Option<C>,
R: Rng + ?Sized,
{
let ceiling = libm::sqrt(ratio(state.states(), 1));
let start = oracle.queries();
let mut bound = 1.0;
while oracle.queries() - start < max_queries {
let remaining = max_queries - (oracle.queries() - start);
let iterations = pick_iterations(bound, rng).min(remaining - 1);
amplify(state, oracle, iterations);
let index = state.sample(rng);
if oracle.check(index) {
return Some(index);
}
bound = (bound * LAMBDA).min(ceiling);
}
None
}
fn pick_iterations<R: Rng + ?Sized>(bound: f64, rng: &mut R) -> usize {
#[expect(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "1 ≤ bound ≤ √N ≤ 2^12, so the ceiling is a small positive integer"
)]
let choices = libm::ceil(bound) as usize;
below(rng, choices)
}