qleisli 0.2.2

Experimental quantum language with linear ownership, exact contracts and independent finite IR verification
Documentation
//! Experimental fixed-width arithmetic; leftmost bits have the lowest weight.
//!
//! All definitions are ordinary checked .qli and return every input wire.
//! Full-space contracts, including overflow and value 15, are documented in
//! docs/arithmetic-order-finding.md and docs/stdlib-contracts.md, A001-A003.
//! These are fixed finite routines, not generalized integer arithmetic.
use std::quantum::x;
use std::quantum::cnot;
use std::quantum::toffoli;
use std::quantum::split;
use std::quantum::join;

/// Map |y> to |(y + 1) mod 4> with amplitude +1 for every two-bit input.
/// The first bit has weight 1 and the second weight 2; overflow wraps.
/// Consume and return both owners with no auxiliary or measurement.
pub unitary fn increment2(q: Q<(Bit, Bit)>) -> Q<(Bit, Bit)> {
    let (low, high) = split(q);
    let (low, high) = cnot(low, high);
    join(x(low), high)
}

/// Map |x,y> to |x,(y + x) mod 4> with amplitude +1 for all x,y in 0..4.
/// The first pair stores x and the second y, each least-significant bit first.
/// Return all four owners in those positions; no auxiliary or input promise.
pub unitary fn add2(q: Q<((Bit, Bit), (Bit, Bit))>) -> Q<((Bit, Bit), (Bit, Bit))> {
    let (a, b) = split(q);
    let (a0, a1) = split(a);
    let (b0, b1) = split(b);
    // Carry must use the original low bits, before b0 is overwritten.
    let ((a0, b0), b1) = toffoli(a0, b0, b1);
    let (a1, b1) = cnot(a1, b1);
    let (a0, b0) = cnot(a0, b0);
    join(join(a0, a1), join(b0, b1))
}

/// Map |y> to |2*y mod 15> for 0 <= y < 15, and leave |15> fixed.
/// Every basis image has amplitude +1; no residue-range promise is needed.
/// Input bits (a,b,c,d) have weights (1,2,4,8); return owners as (d,a,b,c).
/// This whole-space permutation uses no auxiliary and performs no measurement.
pub unitary fn mul2_mod15(q: Q<((Bit, Bit), (Bit, Bit))>) -> Q<((Bit, Bit), (Bit, Bit))> {
    let (ab, cd) = split(q);
    let (a, b) = split(ab);
    let (c, d) = split(cd);
    // Rotation implements 2*y mod 15 for y<15, and fixes 15.
    join(join(d, a), join(b, c))
}