from qiskit import QuantumCircuit
qc = QuantumCircuit(1,1)
qc.x(0)
qc.measure(0,0)
qc.draw('mpl')

from qiskit import Aer, execute
simulator = Aer.get_backend('qasm_simulator')
nshots = 2**13
job = execute(qc, backend=simulator,shots=nshots)
counts = job.result().get_counts()
from qiskit.tools.visualization import plot_histogram
plot_histogram(counts)

from qiskit import QuantumCircuit

def qc_df(): # divisor de feixes 50:50
    qc = QuantumCircuit(1, name='DF')
    qc.s(0)
    qc.h(0)
    qc.s(0)
    return qc
qcdf = qc_df(); qcdf.draw('mpl')

def qc_espelhos():
    qc = QuantumCircuit(1, name='E')
    qc.z(0)
    qc.y(0)
    return qc
qce = qc_espelhos(); qce.draw('mpl')

def qc_bomba():
    qc = QuantumCircuit(2, name='B')
    qc.cx(0,1)
    return qc
qcb = qc_bomba(); qcb.draw('mpl')

def qc_detector_de_bomba():
    qc = QuantumCircuit(2, name='DB')
    qcdf = qc_df(); qc.append(qcdf,[0])
    qce = qc_espelhos(); qc.append(qce,[0])
    qcb = qc_bomba(); qc.append(qcb,[0,1])
    qcdf = qc_df(); qc.append(qcdf,[0])
    return qc
qcdb = qc_detector_de_bomba(); qcdb.draw('mpl')

qc = QuantumCircuit(2,2)
qcdb = qc_detector_de_bomba(); qc.append(qcdb,[0,1])
qc.measure([0,1],[0,1])
qc.draw('mpl')

# Simulação clássica
from qiskit import Aer, execute
simulator = Aer.get_backend('qasm_simulator')
nshots = 2**13
job = execute(qc, backend=simulator,shots=nshots)
counts = job.result().get_counts()
from qiskit.tools.visualization import plot_histogram
plot_histogram(counts)

import qiskit
qiskit.IBMQ.save_account('18d1a392e10ffca595cb87ad064ca04fb8f407e702274610ba421f54ac39fbef8e14b664d350f3e48cc9762e4b63aad22ecb2135f16468825ae37b14a85c1762', overwrite = True)
qiskit.IBMQ.load_account()
#provider = qiskit.IBMQ.get_provider(hub='ibm-q', group='open', project='main')
provider = qiskit.IBMQ.get_provider(hub='ibm-q-research-2', group='federal-uni-sant-1', project='main')
device = provider.get_backend('ibm_nairobi')
nshots = 2**13

# Experimento (simulação quântica)
job = execute(qc, backend=device, shots=nshots)
print(job.job_id())

job = device.retrieve_job('cmpt4asb9x9g0088t2c0')
from qiskit.tools.visualization import plot_histogram
plot_histogram(job.result().get_counts())



