Source code for PySparQ.pysparq.algorithms.shor

"""
Shor's Quantum Factorization Algorithm Implementation
"""

import pysparq as ps


[docs] class ShorExecutionFailed(Exception): """Exception raised when Shor's algorithm fails to find factors.""" ...
[docs] def general_expmod(a: int, x: int, N: int) -> int: """Compute a^x mod N efficiently using square-and-multiply.""" ...
[docs] def find_best_fraction(y: int, Q: int, N: int) -> tuple[int, int]: """Find the best fraction c/r approximating y/Q using Farey sequence.""" ...
[docs] def compute_period(meas_result: int, size: int, N: int) -> int: """Compute the period from measurement result.""" ...
[docs] def check_period(period: int, a: int, N: int) -> None: """Check if period is valid for factoring.""" ...
[docs] def shor_postprocess(meas: int, size: int, a: int, N: int) -> tuple[int, int]: """Classical post-processing for Shor's algorithm.""" ...
[docs] class ModMul: """Controlled modular multiplication operation."""
[docs] reg: str
[docs] a: int
[docs] x: int
[docs] N: int
[docs] opnum: int
_condition_bits: list[tuple[str | int, int]] _condition_regs: list[str | int] def __init__(self, reg: str, a: int, x: int, N: int) -> None: ...
[docs] def conditioned_by_all_ones(self, cond: str) -> "ModMul": ...
[docs] def conditioned_by_nonzeros(self, cond: str | int) -> "ModMul": ...
[docs] def clear_conditions(self) -> None: ...
[docs] def dag(self, state: ps.SparseState) -> None: ...
def __call__(self, state: ps.SparseState) -> None: ...
[docs] class SemiClassicalShor: """Semi-classical implementation of Shor's algorithm."""
[docs] a: int
[docs] N: int
[docs] n: int
[docs] size: int
[docs] meas_result: int
[docs] period: int
[docs] p: int
[docs] q: int
def __init__(self, a: int, N: int) -> None: ...
[docs] def run(self) -> tuple[int, int]: ...
[docs] class ExpMod: """Modular exponentiation operation."""
[docs] input_reg: str
[docs] output_reg: str
[docs] a: int
[docs] N: int
[docs] period: int
[docs] axmodn: list[int]
def __init__( self, input_reg: str, output_reg: str, a: int, N: int, period: int ) -> None: ... def __call__(self, state: ps.SparseState) -> None: ...
[docs] def dag(self, state: ps.SparseState) -> None: ...
[docs] class Shor: """Full quantum Shor's algorithm."""
[docs] work_reg: str
[docs] ancilla_reg: str
[docs] expmod: ExpMod
def __init__( self, work_reg: str, ancilla_reg: str, a: int, N: int, period: int, ) -> None: ... def __call__(self, state: ps.SparseState) -> None: ...
[docs] def factor(N: int, a: int | None = ...) -> tuple[int, int]: """Factor N using Shor's algorithm.""" ...
[docs] def factor_full_quantum(N: int, a: int | None = ...) -> tuple[int, int]: """Factor N using full quantum Shor's algorithm.""" ...
[docs] def create_shor_demo() -> str: """Generate a demo script for Shor's algorithm.""" ...