```python
from sympy.physics.quantum.qubit import Qubit, IntQubit, IntQubitBra, matrix_to_qubit
from sympy.physics.quantum.dagger import Dagger
from sympy.physics.quantum.gate import Z
from sympy.physics.quantum.represent import represent
```

## Qubits 

### We could have also called 
Alternatively we could have also done
from sympy.physics.quantum.qubit import Qubit



```python
Qubit(0,0,0)
```




    |000>




```python
q = Qubit('0101')
q
```




    |0101>




```python
q.nqubits
```




    4




```python
len(q)
```




    4




```python
q.dimension
```




    4




```python
q.flip(1)
```




    |0111>




```python
q.qubit_values
```




    (0, 1, 0, 1)




```python
Dagger(q)
```




    <0101|




```python
type(Dagger(q))
```




    sympy.physics.quantum.qubit.QubitBra




```python
ip = Dagger(q)*q
ip
```




    <0101|0101>




```python
# Create a qubit for integer 5
q = IntQubit(Qubit('101'))
q
```




    |5>




```python
q.as_int()
```




    5




```python
q.nqubits
```




    3




```python
q.qubit_values
```




    (1, 0, 1)




```python
# Represent a state and then go back to its qubit form
q = Qubit('01')
matrix_to_qubit(represent(q))
```




    |01>




```python
# measure all, matrix to density
from sympy.physics.quantum.qubit import Qubit, measure_all, measure_partial, measure_all_oneshot, measure_partial_oneshot
from sympy.physics.quantum.gate import H, X, Y, Z
from sympy.physics.quantum.qapply import qapply
```


```python
c = H(0)*H(1)*Qubit('00')
c
```




    H(0)*H(1)*|00>




```python
q = qapply(c) 
measure_all(q)
```




    [(|00>, 1/4), (|01>, 1/4), (|10>, 1/4), (|11>, 1/4)]




```python
# measure partial
measure_partial(q, (0,))
```




    [(sqrt(2)*|00>/2 + sqrt(2)*|10>/2, 1/2),
     (sqrt(2)*|01>/2 + sqrt(2)*|11>/2, 1/2)]




```python
# measure partial oneshot
measure_partial_oneshot(q, (0,))
```




    sqrt(2)*|01>/2 + sqrt(2)*|11>/2




```python
# measure all one shot
measure_all_oneshot(q)
```




    |10>



### Applying Hadamard gate to a single Qubit system



## Shor's Algorithm 

From [Wikipedia](https://en.wikipedia.org/wiki/Shor%27s_algorithm), [**Also** read Peter Shor's arxiv research paper](https://arxiv.org/abs/quant-ph/9508027)

Shor's algorithm, named after mathematician Peter Shor, is a quantum algorithm (an algorithm that runs on a quantum computer) for integer factorization formulated in 1994. Informally it solves the following problem: given an integer N, find its prime factors.

On a quantum computer, to factor an integer N, Shor's algorithm runs in polynomial time (the time taken is polynomial in log N, which is the size of the input).[1] Specifically it takes quantum gates of order O((log N)2(log log N)(log log log N)) using fast multiplication,[2] demonstrating that the integer factorization problem can be efficiently solved on a quantum computer and is thus in the complexity class BQP. This is substantially faster than the most efficient known classical factoring algorithm, the general number field sieve, which works in sub-exponential time – about O(e1.9 (log N)1/3 (log log N)2/3).[3] The efficiency of Shor's algorithm is due to the efficiency of the quantum Fourier transform, and modular exponentiation by repeated squarings.

If a quantum computer with a sufficient number of qubits could operate without succumbing to noise and other quantum decoherence phenomena, Shor's algorithm could be used to break public-key cryptography schemes such as the widely used RSA scheme. RSA is based on the assumption that factoring large numbers is computationally intractable. So far as is known, this assumption is valid for classical (non-quantum) computers; no classical algorithm is known that can factor in polynomial time. However, Shor's algorithm shows that factoring is efficient on an ideal quantum computer, so it may be feasible to defeat RSA by constructing a large quantum computer. It was also a powerful motivator for the design and construction of quantum computers and for the study of new quantum computer algorithms. It has also facilitated research on new cryptosystems that are secure from quantum computers, collectively called post-quantum cryptography.

In 2001, Shor's algorithm was demonstrated by a group at IBM, who factored 15 into 3 × 5, using an NMR implementation of a quantum computer with 7 qubits.[4] After IBM's implementation, two independent groups implemented Shor's algorithm using photonic qubits, emphasizing that multi-qubit entanglement was observed when running the Shor's algorithm circuits.[5][6] In 2012, the factorization of 15 was performed with solid-state qubits.[7] Also in 2012, the factorization of 21 was achieved, setting the record for the largest number factored with Shor's algorithm.[8] In April 2012, the factorization of 143 was achieved, although this used adiabatic quantum computation rather than Shor's algorithm.[9] In November 2014, it was discovered that this 2012 adiabatic quantum computation had also factored larger numbers, the largest being 56153.[10][11]

### Plain english?

Let's say you have two prime numers. $p = 17$ and $q = 19$, then multiply these as follows as $pq = 323$. 
Now lets subtract these two by 1 and multiply that as in:

$$ (p-1)*(q-1) = 16*18 = 288$$.


```python
p = 17
q = 19
A = p*q
B = (p-1)*(q-1)
print("Product of p and q is :",A)
print("Product of p-1 and q-1 is :", B)
```

    Product of p and q is : 323
    Product of p-1 and q-1 is : 288


#### What next?
Now, in a range of 1 and B(288), pick any number such that its LCD (lowest common denimonator) with A(323) is 1.
Let's say we pick 9?

What you'll realize is that you have to go a whole lot before you can find that number which is divisible by 9.


```python
num = 0
for x in range(1, 289):
    if (num % x) ==0:
        print ("Fact :")
```


```python
! python shor.py
```
	projectq
	--------
	Implementation of Shor's algorithm.
	Number to factor: 15

	Factoring N = 15: 00000001

	Bad luck: Found 15 and 1
Allocate : 109
CCR(0.19634954084936207) : 16
CCR(0.39269908169872414) : 32
CCR(0.7853981633974483) : 64
CCR(1.1780972450961724) : 8
CCR(1.3744467859455345) : 16
CCR(1.5707963267948966) : 104
CCR(1.9634954084936207) : 8
CCR(2.1598449493429825) : 16
CCR(2.356194490192345) : 24
CCR(2.552544031041707) : 16
CCR(2.748893571891069) : 32
CCR(3.141592653589793) : 192
CCR(3.5342917352885173) : 16
CCR(3.730641276137879) : 8
CCR(3.9269908169872414) : 24
CCR(4.123340357836604) : 8
CCR(4.319689898685965) : 16
CCR(4.71238898038469) : 88
CCR(4.908738521234052) : 8
CCR(5.105088062083414) : 16
CCR(5.497787143782138) : 56
CCR(5.890486225480862) : 16
CCR(6.086835766330224) : 8
CR(0.19634954084936207) : 144
CR(0.39269908169872414) : 288
CR(0.7853981633974483) : 432
CR(1.5707963267948966) : 568
CR(10.995574287564276) : 568
CR(11.780972450961723) : 432
CR(12.173671532660448) : 288
CR(12.370021073509811) : 144
CR(2.945243112740431) : 64
CR(3.141592653589793) : 64
CR(4.71238898038469) : 64
CR(5.497787143782138) : 64
CR(5.890486225480862) : 64
CSwap : 32
CX : 128
Deallocate : 109
H : 1408
Measure : 9
R(0.39269908169872414) : 64
R(0.7853981633974483) : 64
R(1.5707963267948966) : 64
R(3.141592653589793) : 64
R(3.3379421944391554) : 64
X : 130

Max. width (number of qubits) : 11.	projectq
	--------
	Implementation of Shor's algorithm.
	Number to factor: 21

	Factoring N = 21: 0000000000

	Factors found :-) : 3 * 7 = 21
Allocate : 166
CCR(0.09817477042468103) : 20
CCR(0.19634954084936207) : 40
CCR(0.39269908169872414) : 76
CCR(0.4908738521234052) : 20
CCR(0.7853981633974483) : 96
CCR(0.9817477042468103) : 22
CCR(1.0799224746714913) : 2
CCR(1.1780972450961724) : 30
CCR(1.2762720155208536) : 20
CCR(1.5707963267948966) : 189
CCR(1.6689710972195777) : 18
CCR(1.8653206380689396) : 20
CCR(1.9634954084936207) : 43
CCR(2.1598449493429825) : 2
CCR(2.356194490192345) : 53
CCR(2.552544031041707) : 30
CCR(2.945243112740431) : 9
CCR(3.141592653589793) : 300
CCR(3.3379421944391554) : 18
CCR(3.730641276137879) : 30
CCR(3.9269908169872414) : 73
CCR(4.123340357836604) : 1
CCR(4.319689898685965) : 23
CCR(4.417864669110646) : 10
CCR(4.614214209960008) : 9
CCR(4.71238898038469) : 111
CCR(5.006913291658733) : 10
CCR(5.105088062083414) : 30
CCR(5.203262832508095) : 1
CCR(5.3014376029327765) : 11
CCR(5.497787143782138) : 48
CCR(5.792311455056181) : 10
CCR(5.890486225480862) : 38
CCR(6.086835766330224) : 20
CCR(6.185010536754905) : 10
CR(0.09817477042468103) : 220
CR(0.19634954084936207) : 440
CR(0.39269908169872414) : 660
CR(0.7853981633974483) : 880
CR(1.5707963267948966) : 1191
CR(1.9634954084936207) : 100
CR(10.995574287564276) : 1091
CR(11.780972450961723) : 880
CR(12.173671532660448) : 660
CR(12.370021073509811) : 440
CR(12.468195843934492) : 220
CR(2.061670178918302) : 100
CR(3.141592653589793) : 100
CR(3.9269908169872414) : 100
CR(4.123340357836604) : 100
CSwap : 50
CX : 200
Deallocate : 166
H : 2602
Measure : 11
R(2.1598449493429825) : 100
R(2.356194490192345) : 100
R(3.141592653589793) : 100
R(4.221515128261284) : 100
R(4.319689898685965) : 100
R(4.71238898038469) : 100
X : 201

Max. width (number of qubits) : 13.

```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

def Hadamard(n):
    def Hn(H=np.array([[1, 1], [1, -1]], dtype=np.complex64), n=n):
        if n > 1:
            return Hn(H=np.kron(np.array([[1, 1], [1, -1]], dtype=np.complex64), H), n=n-1)
        return H

    return Hn(n=n)

def QFT(t):
    Q = np.zeros(shape=(2 ** t, 2 ** t), dtype=np.complex64)
    N = 2 ** t
    for i in range(N):
        for j in range(N):
            Q[i][j] = np.exp(np.pi * 2j * ((i * j) % N) / N)

    return Q

N = 21
t = 9
H = Hadamard(t)

reg1 = np.zeros(shape=(2 ** t), dtype=np.complex64)
reg2 = np.ones(shape=(2 ** t), dtype=np.complex64)
reg1[0] = 1
reg1 = H.dot(reg1)

for i in range(2 ** t):
    reg2[i] = 2 ** i % N

r = reg2[0]

for i in range(2 ** t):
    if reg2[i] != r:
        reg1[i] = 0

Q = QFT(9)
reg1 = np.linalg.inv(Q).dot(reg1)
print (abs(reg1))
print (abs(reg1[0]))
print (abs(reg1[85]))
print (abs(reg1[86]))

fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot(abs(reg1))
fig.savefig('plot.png')
plt.show()
```


```python

```

ipynb
c8aa0ed86976df5301a637e46f0e6bacca4b9fe3
Jupyter Notebook
2022-03-31T12:03:24.000Z
2017-08-06T17:40:59.000Z
6017441f2d476f9c6c568dd886da43c6c0fd89bd
Apache-2.0
nuhaltinsoy/Artificial-Intelligence-Deep-Learning-Machine-Learning-Tutorials
sympy/Quantum Computing.ipynb
6017441f2d476f9c6c568dd886da43c6c0fd89bd
2022-03-11T23:21:35.000Z
2017-08-28T14:59:36.000Z
Apache-2.0
nuhaltinsoy/Artificial-Intelligence-Deep-Learning-Machine-Learning-Tutorials
sympy/Quantum Computing.ipynb
7adab3877fc1d3f1d5f57e6c1743dae8f76f72c5
Apache-2.0
DanielMabadeje/Artificial-Intelligence-Deep-Learning-Machine-Learning-Tutorials
sympy/Quantum Computing.ipynb
2022-03-30T07:34:24.000Z
2017-08-06T16:51:46.000Z