number package provides arithmetic helpers used throughout the q simulator — particularly in Shor’s factoring algorithm, where period finding, modular exponentiation, and GCD computations are core steps.
Import path
Integer arithmetic
GCD
a and b using Euclid’s algorithm.
Pow
a**r using exponentiation by squaring. Returns 0 if a is zero, 1 if r is zero.
BaseExp
a and b such that a**b == N. If N is not a perfect power, returns (0, 0, false).
Log2
N (i.e. the bit length minus one).
IsPowOf2
true if N is a positive power of two.
IsOdd / IsEven
IsPrime
true if N is prime. Uses trial division up to √N.
IsTrivial
true if none of the supplied factors is a non-trivial factor of N — i.e. every factor is either 1, N, or does not divide N evenly. Used in Shor’s algorithm to discard attempts that only yield trivial factors.
Modular arithmetic
ModExp
a**r mod N using exponentiation by squaring. Returns 0 when a == 0 or N == 1.
ModExp2
a**(2**j) mod N. Equivalent to squaring a mod N exactly j times. Used when building the controlled-U² unitaries in Shor’s circuit.
Period finding
These three functions work together. After measuring the quantum register in Shor’s algorithm you get a rational approximationm to s/r. You convert it to a continued fraction, then step through its convergents until you find a denominator r that satisfies a**r ≡ 1 (mod N).
ContinuedFraction
real as a slice of integer coefficients. Expansion stops when the fractional part is within tolerance of zero.
Convergent
p, denominator q, and floating-point value p/q of the convergent for the given prefix of a continued-fraction expansion.
FindOrder
r of a mod N from the measured phase m. Internally it steps through the convergents of ContinuedFraction(m) and returns the first s/r such that ModExp(a, r, N) == 1.
Returns (s, r, d, true) on success, or the last convergent and false if no valid r is found.
Ldexp
a * 2**b as a float64. In Shor’s algorithm this converts the integer measurement result into the phase φ = m / 2^n:
Helpers
Sum
p. Useful for verifying that a probability distribution sums to 1.
MustParseInt
Must
v if err is nil, otherwise panics. A generic helper for wrapping functions that return (T, error).
Shor’s factoring algorithm
The functions above combine to implement the classical post-processing step of Shor’s algorithm. The full circuit and classical steps are shown below.Why continued fractions?
Why continued fractions?
The quantum phase estimation step outputs an integer
m such that m/2^n ≈ s/r for some integers s and r. The continued-fraction algorithm efficiently finds the best rational approximation to a real number with a small denominator, recovering r without an exhaustive search.ContinuedFraction expands the phase into coefficients [a₀; a₁, a₂, ...], and Convergent evaluates successive truncations of that expansion. The first truncation whose denominator satisfies a^r ≡ 1 (mod N) gives the period.Choosing a co-prime
Choosing a co-prime
Pick
a such that 1 < a < N and GCD(a, N) == 1. If GCD(a, N) > 1 you have already found a factor without needing the quantum step.