//! Fixed two- and three-bit quantum Fourier transforms with positive phase.
//!
//! Ordinary checked .qli definitions; no general-size or approximate QFT API.
//! Basis labels are little-endian and each circuit includes output-bit reversal.
//! Contracts and finite regression status: docs/stdlib-contracts.md, F001-F002.
use std::quantum::h;
use std::quantum::t;
use std::quantum::split;
use std::quantum::join;
/// Return the input owner unchanged with phase +1 on both basis states.
unitary fn identity(q: Q<Bit>) -> Q<Bit> { q }
/// Apply diag(1,i) using two T gates, preserving the phase needed under control.
unitary fn phase_quarter(q: Q<Bit>) -> Q<Bit> { repeat_static(2, t, q) }
/// Apply F4: |x> maps to sum_y exp(2*pi*i*x*y/4)|y>/2 for every input.
/// The leftmost bit has weight 1; output reversal is part of the exact unitary.
/// Consume and return both owners without measurement or auxiliary allocation.
pub unitary fn qft2(q: Q<(Bit, Bit)>) -> Q<(Bit, Bit)> {
let (a, b) = split(q);
let b = h(b);
let (a, b) = qif(a, b) { 0 => identity, 1 => phase_quarter };
let a = h(a);
join(b, a)
}
/// Apply F8: |x> maps to sum_y exp(2*pi*i*x*y/8)|y>/sqrt(8) for every input.
/// For input ((a,b),c), x = a + 2*b + 4*c; output reversal is included.
/// Consume and return all three owners with no measurement or auxiliary.
pub unitary fn qft3(q: Q<((Bit, Bit), Bit)>) -> Q<((Bit, Bit), Bit)> {
let (ab, c) = split(q);
let (a, b) = split(ab);
let c = h(c);
let (b, c) = qif(b, c) { 0 => identity, 1 => phase_quarter };
let (a, c) = qif(a, c) { 0 => identity, 1 => t };
let b = h(b);
let (a, b) = qif(a, b) { 0 => identity, 1 => phase_quarter };
let a = h(a);
join(join(c, b), a)
}