import numpy as np

def gcd(a, b):
    r = 1
    while r != 0:
        r = a%b
        if r == 0:
            break
        a = b
        b = r
    
    return b

print(gcd(630, 56))

N = 20
a = 7

r_vals = np.arange(2, N)
a_power_r = np.power(a, r_vals)
print(a_power_r)
a_r_mod_N = a_power_r%N

import matplotlib.pyplot as plt

plt.plot(r_vals, a_r_mod_N)

def modulo(a, r, N):
    
    mod = a
    for i in range(r - 1):
        mod = (mod*a)%N
        
    return mod 

print(modulo(7, 4, 15))

def period(a, N):
    
    mod = a
    for i in range(2, N):
        mod = (mod*a)%N
        if mod == 1:
            return i

print(period(7, 15))

print(period(11, 31))

from qiskit import *

n = 4
circ = QuantumCircuit(n, n)

def swap(circuit, i, j):
    circuit.cx(i, j)
    circuit.cx(j, i)
    circuit.cx(i, j)
    return circuit 

def unitary_7(circuit):
    for i in range(n):
        circuit.x(i)
    
    circuit = swap(circuit, 1, 2)
    circuit = swap(circuit, 2, 3)
    circuit = swap(circuit, 0, 3)
    circuit.barrier()
    
    return circuit


# circ.x(0)
# circ.x(1)
# circ.barrier()

r = 4
for j in range(r):
    circ = unitary_7(circ)

circ.draw()

backend = Aer.get_backend('statevector_simulator')
job = execute(circ, backend)
results = job.result()
psi = results.get_statevector(circ)

for i in range(len(psi)):
    print(f"{bin(i)} :" + str(abs(psi[i]**2)))

