__Shor’s Factorization Algorithm__:

- Shor’s Factorization Algorithm is proposed by Peter Shor.
- It suggests that quantum mechanics allows the factorization to be performed in polynomial time, rather than exponential time achieved after using classical algorithms.
- This could have a drastic impact on the field of data security, a concept based on the prime factorization of large numbers.
- Many polynomial-time algorithms for integer multiplication (e.g., Euclid’s Algorithm) do exist, but no polynomial-time algorithm for factorization exists.
- So, Shor came up with an algorithm i.e. Shor’s Factorization Algorithm, an algorithm for factorizing non-prime integers
**N**of**L**bits. **Quantum algorithms**are far much better than classical algorithms because they are based on Quantum Fourier Transform.- Run time on the classical computer is
**O[exp (L**but that on the quantum computer is^{1/3}(log L)^{2/3})]**O(L**.^{3}) - So, Shor’s Algorithm in principle, shows that a quantum computer is capable of factoring very large numbers in polynomial time.

__Shor’s Algorithm depends on__:

__The Algorithm stands as__:

Given an odd composite number **N**, find an integer **d**, strictly between **1** and **N**, that divides **N**.

Shor’s Algorithm consists of the following two parts:

- Conversion of the problem of factorizing to the problem of finding the period. This part can be implemented with classical means.
- Finding the period or Quantum period finding using the
**Quantum Fourier Transform**, and is responsible for quantum speedup, and utilizes quantum parallelism.

In Shor’s Algorithm, the **Input** is Non-prime number **N** and the **Output** is Non-trivial factor of **N**

INPUT (N)—>—>SHOR’S ALGORITHMOUTPUT (Non-trivial factor of N)

**Algorithm: **It contains a few steps, at only step 2 the use of quantum computers is required.

- Choose any random number let say
**r**, such that**r < N**so that they are co-primes of each other. - A quantum computer is used to determine the unknown period
**p**of the function**f**_{r, N}(x) = r^{x }**mod****N**. - If
**p**is an odd integer, then go back to**Step 1**. Else move to the next step. - Since
**p**is an even integer so,**(r**.^{p/2}– 1)(r^{p/2}+ 1) = r^{p}– 1 = 0 mod N - Now, if the value of
**r**, go back to^{p/2}+ 1 = 0 mod N**Step 1**. - If the value of
**r**, Else move to the next step.^{p/2}+ 1 != 0 mod N - Compute
**d = gcd(r**.^{p/2}-1, N) - The answer required is ‘
**d**’.

__Classical part (__The order finding problem):

This is the classical part of order finding problem. Given that **x** and **N**, such that **x<N** and **gcd(x, N) = 1**. The order of **x** is the least positive integer, y such that **x ^{y} = 1(mod N)**.

- A random number
**n**is picked, such that**n < N**. Compute**gcd(n, N)**. - This can be done using the Euclid Algorithm.
- If
**gcd(n, N) != 1**, then there is a non-trivial factor of N.If**(x+p) = n**.^{x + p }mod N = n^{x}mod N = f(x) - If r is odd, then go back to
**Step 1**. - If
**n**, then go back to^{p/2}= -1(mod N)**Step 1**. - The
**gcd(n**is a non-trivial factor of^{p/2 }+/- 1, N)**N**.

__Quantum part:__

This is the quantum order finding part. Choose a power of 2, then

Q = 2L such that N^{2 }<= Q <= 2N^{2}

And consider f = {0, 1, 2, …, Q-1}

Where, **f(y)=1/(Q) ^{1/2} ∑_{x=0}^{Q-1}I f(x) I w^{xy}** and

**w = exp(2π i/Q)**, i.e. Q

^{th}root of unity.

- Let’s perform
**Shor’s Algorithm**using an example: To factor an odd integer N (let N = 17). - Choose an integer Q such that
**N**( lets Q = 324).^{2 }<= Q ≤ 2 N^{2} - Then, randomly choose any integer n such that gcd(n, N)=1, (let us choose the integer be n=7).
- Then create two quantum registers (these registers should be entangled so that the collapse of the input registered corresponds to the collapse of the output register)
**Input Register:**must be containing enough qubits to represent numbers as large as Q-1.(i.e., up to 323, so we need 9 qubits).**Output Register:**must be containing enough qubits to represent numbers as large as**(N – 1)**. (i.e., up to 16, so we require 4 qubits).

**Code :**

## Python

`from` `qiskit ` `import` `IBMQ` `from` `qiskit.aqua ` `import` `QuantumInstance` `from` `qiskit.aqua.algorithms ` `import` `Shor` `# Enter your API token here` `IBMQ.enable_account(` `'ENTER API TOKEN HERE'` `) ` `provider ` `=` `IBMQ.get_provider(hub` `=` `'ibm-q'` `)` `# Specifies the quantum device` `backend ` `=` `provider.get_backend(` `'ibmq_qasm_simulator'` `)` `print` `(` `'\n Shors Algorithm'` `)` `print` `(` `'--------------------'` `)` `print` `(` `'\nExecuting...\n'` `)` `# Function to run Shor's algorithm` `# where 21 is the integer to be factored` `factors ` `=` `Shor(` `35` `)` `result_dict ` `=` `factors.run(QuantumInstance(` ` ` `backend, shots` `=` `1` `, skip_qobj_validation` `=` `False` `))` ` ` `# Get factors from results` `result ` `=` `result_dict[` `'factors'` `]` `print` `(result)` `print` `(` `'\nPress any key to close'` `)` `input` `()` |

**Output :**

Shors Algorithm - - - - - - - - - - - - - Executing... [[5,7]] Press any key to close

Output for the code above showing the factors 5 and 7 for N=35.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**