from IPython.display import IFrame; IFrame("https://www.ibm.com/quantum", 900,500)

import csv
with open('ibm_lagos_calibrations_2023-09-28T17_22_25Z.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row))

import qiskit
qiskit.__qiskit_version__

# Copie se API Token no IBMQ e cole aqui
qiskit.IBMQ.save_account('ee8631db565e87e97b066d08e02cafb9bfa745be07e573095b666daa608a37ce58ae6ef5c174aff56a935dadfe1fa9c82edfe37aec6b24774bc94613658cc7d8', 
                  overwrite = True)

# Execute esse comando uma vez só
qiskit.IBMQ.load_account()

provider = qiskit.IBMQ.get_provider(hub='ibm-q', group='open', project='main')
provider.backends()

from qiskit import *

qr = QuantumRegister(2)
cr = ClassicalRegister(2)
qc = QuantumCircuit(qr,cr)
qc.h(qr[0])
qc.cx(qr[0],qr[1])
qc.measure(qr[0],cr[0])
qc.measure(qr[1],cr[1])
qc.draw('mpl')

simulator = Aer.get_backend('qasm_simulator') 

nshots = 8192 # número de "prepara-e-mede"

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

from qiskit.tools.visualization import plot_histogram
plot_histogram(job.result().get_counts(qc))

# Acessando as contagens com numpy

counts = job.result().get_counts(qc)
counts

counts['00']

# probabilidade
p00 = 0; p01 = 0; p10 = 0; p11 = 0
for j in (0,2):
    for k in (0,2):
        if '00' in counts:
            p00 = counts['00']/nshots
        if '01' in counts:
            p01 = counts['01']/nshots
        if '10' in counts:
            p10 = counts['10']/nshots
        if '11' in counts:
            p11 = counts['11']/nshots
p00, p01, p10, p11

device = provider.get_backend('ibm_lagos')
from qiskit.tools.monitor import job_monitor

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

plot_histogram(job.result().get_counts(qc))

counts = job.result().get_counts(qc)
counts

counts['00']

# probabilidade
p00 = 0; p01 = 0; p10 = 0; p11 = 0
for j in (0,2):
    for k in (0,2):
        if '00' in counts:
            p00 = counts['00']/nshots
        if '01' in counts:
            p01 = counts['01']/nshots
        if '10' in counts:
            p10 = counts['10']/nshots
        if '11' in counts:
            p11 = counts['11']/nshots
p00, p01, p10, p11

