



from qiskit import *
import numpy as np
import math
import qiskit
nshots = 8192
IBMQ.load_account()
provider = qiskit.IBMQ.get_provider(hub='ibm-q', group='open', project='main')
device = provider.get_backend('ibmq_manila')
simulator = Aer.get_backend('qasm_simulator')
from qiskit.tools.monitor import job_monitor
#from qiskit.ignis.verification.tomography import state_tomography_circuits, StateTomographyFitter
#from qiskit.ignis.mitigation.measurement import complete_meas_cal, CompleteMeasFitter
from qiskit.visualization import plot_histogram

def qc_bb84():
    qr = QuantumRegister(4)
    cr = ClassicalRegister(4)
    qc = QuantumCircuit(qr,cr)
    qc.h([1,2])
    qc.measure([1,2],[1,2])
    qc.x(0).c_if(cr[1],1)
    qc.h(0).c_if(cr[2],1)
    qc.barrier()
    qc.barrier()
    qc.h(3)
    qc.measure(qr[3],cr[3])
    qc.h(0).c_if(cr[3],1)
    qc.measure(0,0)
    return qc
qc_bb84_ = qc_bb84(); qc_bb84_.draw('mpl')

nshots = 1
N = 100
counts = []
for j in range(0,N):
    job_sim = execute(qc, backend=simulator, shots=nshots)
    counts_sim = job_sim.result().get_counts(qc)
    counts.append(counts_sim)
counts[0:5]

counts_keys = [j for j in counts]
counts_keys

k=0;l=1;m=0;n=0
s= str(k)+str(l)+str(m)+str(n)
s

eo = [] # observavel escolhido por Alice e Bob (mesmo = 0, diferente=1)
for j in range(0,N):
    for k in range(0,2):
        for l in range(0,2):
            for m in range(0,2):
                for n in range(0,2):
                    s = str(n) + str(m) + str(l) + str(k)
                    if counts[j][s] in counts and  counts[j][s] == 1:
                        if l==0 and m==0:
                            eo.append(0)
                        else:
                            eo.append(1)
eo
                        

qr = QuantumRegister(4)
