Ejemplo n.º 1
0
    def __init__(self, N=15, a=2):
        """
        Constructor.

        Args:
            N (int): The integer to be factored.
            a (int): A random integer a that satisfies a < N and gcd(a, N) = 1
        """
        self.validate(locals())
        super().__init__()

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise AquaError(
                'The input needs to be an odd integer greater than 1.')

        self._N = N

        if a >= N or math.gcd(a, self._N) != 1:
            raise AquaError(
                'The integer a needs to satisfy a < N and gcd(a, N) = 1.')

        self._a = a

        self._ret = {'factors': []}

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.info('The input integer is a power: {}={}^{}.'.format(
                N, b, p))
            self._ret['factors'].append(b)
Ejemplo n.º 2
0
    def __init__(self, N=15, a=2):
        """
        Constructor.

        Args:
            N (int): The integer to be factored.
            a (int): A random integer a that satisfies a < N and gcd(a, N) = 1
         Raises:
            AquaError: invalid input
        """
        validate(locals(), self._INPUT_SCHEMA)
        super().__init__()
        self._n = None
        self._up_qreg = None
        self._down_qreg = None
        self._aux_qreg = None

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise AquaError('The input needs to be an odd integer greater than 1.')

        self._N = N

        if a >= N or math.gcd(a, self._N) != 1:
            raise AquaError('The integer a needs to satisfy a < N and gcd(a, N) = 1.')

        self._a = a

        self._ret = {'factors': []}

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.info('The input integer is a power: %s=%s^%s.', N, b, p)
            self._ret['factors'].append(b)
Ejemplo n.º 3
0
    def __init__(
        self,
        N: int = 15,
        a: int = 2,
        quantum_instance: Optional[Union[QuantumInstance, BaseBackend,
                                         Backend]] = None
    ) -> None:
        """
        Args:
            N: The integer to be factored, has a min. value of 3.
            a: Any integer that satisfies 1 < a < N and gcd(a, N) = 1.
            quantum_instance: Quantum Instance or Backend

         Raises:
            ValueError: Invalid input
        """
        warn_package('aqua.algorithms.factorizers',
                     'qiskit.algorithms.factorizers', 'qiskit-terra')
        validate_min('N', N, 3)
        validate_min('a', a, 2)
        super().__init__(quantum_instance)
        self._n = None  # type: Optional[int]
        self._up_qreg = None
        self._down_qreg = None  # type: Optional[QuantumRegister]
        self._aux_qreg = None  # type: Optional[QuantumRegister]

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise ValueError(
                'The input needs to be an odd integer greater than 1.')

        self._N = N

        if a >= N or math.gcd(a, self._N) != 1:
            raise ValueError(
                'The integer a needs to satisfy a < N and gcd(a, N) = 1.')

        self._a = a

        self._ret = AlgorithmResult({
            "factors": [],
            "total_counts": 0,
            "successful_counts": 0
        })

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.info('The input integer is a power: %s=%s^%s.', N, b, p)
            self._ret['factors'].append(b)

        self._qft = QFT(do_swaps=False).to_instruction()
        self._iqft = self._qft.inverse()

        self._phi_add_N = None  # type: Optional[Gate]
        self._iphi_add_N = None
Ejemplo n.º 4
0
    def __init__(self,
                 N: int = 15,
                 a: int = 2,
                 quantum_instance: Optional[Union[QuantumInstance,
                                                  BaseBackend]] = None,
                 job_id=None) -> None:
        """
        Args:
            N: The integer to be factored, has a min. value of 3.
            a: A random integer that satisfies a < N and gcd(a, N) = 1, has a min. value of 2.
            quantum_instance: Quantum Instance or Backend

         Raises:
            ValueError: Invalid input
        """
        validate_min('N', N, 3)
        validate_min('a', a, 2)
        super().__init__(quantum_instance)
        self.job_id = job_id
        self._n = None
        self._up_qreg = None
        self._down_qreg = None
        self._aux_qreg = None

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise ValueError(
                'The input needs to be an odd integer greater than 1.')

        self._N = N

        if a >= N or math.gcd(a, self._N) != 1:
            raise ValueError(
                'The integer a needs to satisfy a < N and gcd(a, N) = 1.')

        self._a = a

        self._ret = {'factors': []}

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.info('The input integer is a power: %s=%s^%s.', N, b, p)
            self._ret['factors'].append(b)

        self._qft = QFT(do_swaps=False)
        self._iqft = self._qft.inverse()
Ejemplo n.º 5
0
    def __init__(self, N: int = 15, a: int = 2) -> None:
        """
        Constructor.

        Args:
            N: The integer to be factored, has a min. value of 3.
            a: A random integer a that satisfies a < N and gcd(a, N) = 1,
                has a min. value of 2.
         Raises:
            ValueError: invalid input
        """
        validate_min('N', N, 3)
        validate_min('a', a, 2)
        super().__init__()
        self._n = None
        self._up_qreg = None
        self._down_qreg = None
        self._aux_qreg = None

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise ValueError(
                'The input needs to be an odd integer greater than 1.')

        self._N = N

        if a >= N or math.gcd(a, self._N) != 1:
            raise ValueError(
                'The integer a needs to satisfy a < N and gcd(a, N) = 1.')

        self._a = a

        self._ret = {'factors': []}

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.info('The input integer is a power: %s=%s^%s.', N, b, p)
            self._ret['factors'].append(b)
Ejemplo n.º 6
0
    def __init__(self, N=15):
        """
        Constructor.

        Args:
            N (int): The integer to be factored.
        """
        self.validate(locals())
        super().__init__()

        # check the input integer
        if N < 1 or N % 2 == 0:
            raise AquaError(
                'The input needs to be an odd integer greater than 1.')

        self._N = N
        self._ret = {'factors': []}

        # check if the input integer is a power
        tf, b, p = is_power(N, return_decomposition=True)
        if tf:
            logger.warning(f'The input integer is a power: {N}={b}^{p}.')
            self._ret['factors'].append(b)