%run init.ipynb

from qiskit import *
nshots = 8192
IBMQ.load_account()
provider = IBMQ.get_provider(hub = 'ibm-q-research-2', group = 'federal-uni-sant-1', project = 'main')
simulator = Aer.get_backend('qasm_simulator')
device = provider.get_backend('ibmq_jakarta')
from qiskit.tools.visualization import plot_histogram
from qiskit.tools.monitor import job_monitor, backend_overview, backend_monitor

def bernstein_vazirani(s):
    ls = len(s)
    qc = QuantumCircuit(ls+1,ls)
    qc.h(range(ls))
    qc.x(ls)
    qc.h(ls)
    qc.barrier();
    for ii, yesno in enumerate(reversed(s)): # the black box
        if yesno == '1':
            qc.cx(ii, ls);
    qc.barrier();
    qc.h(range(ls));
    qc.barrier();
    qc.measure(range(ls),range(ls))
    return qc

s = '011' # secretnumber
qc = bernstein_vazirani(s)
qc.draw(output='mpl')

simulator = Aer.get_backend('qasm_simulator')
result = execute(qc, backend = simulator, shots = 1).result()
counts = result.get_counts()
print(counts)

jobE = execute(qc, backend = device, shots = nshots)
job_monitor(jobE)

jobS = execute(qc, backend = simulator, shots = nshots)

plot_histogram([jobS.result().get_counts(0), jobE.result().get_counts(0)], 
               bar_labels = False, legend = ['sim', 'exp'])

