# Number of iteration 
import math
import numpy as np
def num_iter(n):
    res = ((np.pi/4) * (math.sqrt(n))) - (1/2)
    iter_list = ['N:'+str(n), 'Number of iteration:'+ str(res)]
    return iter_list

for i in range(16):
    n = 2**i
    print(num_iter(n))

# importing Qiskit
from qiskit import Aer
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, execute
from qiskit.providers.aer import QasmSimulator

backend = Aer.get_backend('qasm_simulator')
prob_of_ans = []

for x in range(15):
    database = QuantumRegister(7)
    oracle = QuantumRegister(1)
    ancilla = QuantumRegister(5) 
    cr = ClassicalRegister(7)
    qc = QuantumCircuit(database, oracle, ancilla, cr)
    qc.h(database[:])
    qc.x(oracle[0])
    qc.h(oracle[0])


    for j in range(x):
    # oracle_7q
        # search 47: 0101111
        qc.x(database[0])
        qc.x(database[2])
        qc.mct(database[:], oracle[0], ancilla[:], mode='basic') 
        qc.x(database[0])
        qc.x(database[2])

    # diffusion_７q
        qc.h(database[:])
        qc.x(database[:])
        qc.h(database[6])
        qc.mct(database[0:6], database[6], ancilla[:], mode='basic')
        qc.h(database[6])
        qc.x(database[:])
        qc.h(database[:])


    qc.h(oracle[0])
    qc.x(oracle[0])
    qc.measure(database,cr)
    # Change the endian 
    qc = qc.reverse_bits() 

    job = execute(qc, backend=backend, shots=1000, backend_options={"fusion_enable":True})
    result = job.result()
    count = result.get_counts()
    answer = count['0101111']
    prob_of_ans.append(answer)

import matplotlib.pyplot as plt
iteration = [i for i in range(15)]
plt.bar(iteration, prob_of_ans)
plt.xlabel('# of iteration')
plt.ylabel('# of times the solution was obtained')