!pip install tabulate
%matplotlib inline
# Importing standard Qiskit libraries and configuring account
from qiskit import QuantumCircuit, execute, Aer, IBMQ
from qiskit.compiler import transpile, assemble
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from tabulate import tabulate
# Loading your IBM Q account(s)
provider = IBMQ.load_account()
#Get the library to check the answers
%pip install -I git+https://github.com/mnp-club/MnP_QC_Workshop.git
from mnp_qc_workshop_2020.discrete_logarithm import qft_dagger, oracle, check_answer

n_qubits = 3 #Number of qubits for the registers of |x1> and |x2>
#For figuring out you would just need 2*n_qubits classical bits
qc_disc_log = QuantumCircuit(4+n_qubits+n_qubits, n_qubits+n_qubits)
for i in range(2*n_qubits):
    qc_disc_log.h(i)
qc_disc_log.x(9)
qc_disc_log.append(oracle(), range(10))
qc_disc_log.append(qft_dagger(3), range(3))
qc_disc_log.append(qft_dagger(3) ,[3,4,5])
qc_disc_log.measure(range(2*n_qubits), range(2*n_qubits))
qc_disc_log.draw('text')

backend = Aer.get_backend('qasm_simulator')
results = execute(qc_disc_log, backend, shots=8192).result()
counts = results.get_counts()
plot_histogram(counts)

rows_x_1, eigenvalues_x_1 = [], []
for output in counts:
    decimal = int(output, 2)//8
    eigenvalue = decimal/(2**3)
    eigenvalues_x_1.append(eigenvalue)
    rows_x_1.append(["%s(bin) = %i(dec)" % (output[0:3], decimal), "%i/%i = %.2f" % (decimal, 2**3, eigenvalue)])
print(tabulate(rows_x_1, headers=["Register Output", "Phase"]))

rows_x_2, eigenvalues_x_2 = [], []
for output in counts:
    decimal = int(output, 2) - ((int(output, 2)//8)*8)
    eigenvalue = decimal/(2**3)
    eigenvalues_x_2.append(eigenvalue)
    rows_x_2.append(["%s(bin) = %i(dec)" % (output[3:6], decimal), "%i/%i = %.2f" % (decimal, 2**3, eigenvalue)])
print(tabulate(rows_x_2, headers=["Register Output", "Phase"]))

#Store your value for s here
s = 3

#Do analysis of oracle here
qc = QuantumCircuit(10, 4)
qc.x(0)
qc.x(9)
qc.append(oracle(), range(10))
qc.measure([6,7,8,9], range(4))
backend = Aer.get_backend('qasm_simulator')
results = execute(qc, backend, shots=8192).result()
counts = results.get_counts()
plot_histogram(counts)

# On x1=1, ans = 2 and on x2=1, ans =  8

#Store values of a and b here
a, b = 2, 8

check_answer(s,a,b)