from qiskit import *
nshots = 8192
qiskit.IBMQ.load_account()
provider = qiskit.IBMQ.get_provider(hub='ibm-q', group='open', project='main')
simulator = Aer.get_backend('qasm_simulator')
device = provider.get_backend('ibmq_bogota')
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_histogram

def qc_ent_dist():
    qc = QuantumCircuit(2, name = 'E dist')
    qc.h([1]); qc.cx(1, 0)
    return qc
qc_ent_dist_ = qc_ent_dist()
qc_ent_dist_.draw(output = 'mpl')

def qc_encoding(cbits):
    qc = QuantumCircuit(1, name = 'codificação')
    if cbits == '00':
        qc.id([0])
    elif cbits == '01':
        qc.z([0])
    elif  cbits == '10':
        qc.x([0])
    elif  cbits == '11':
        qc.x([0]); qc.z([0])
    return qc
qc_encoding_ = qc_encoding('11')
qc_encoding_.draw(output = 'mpl')

def qc_decoding():
    qc = QuantumCircuit(2, name = 'decodificação')
    qc.cx(1, 0); qc.h([1])
    return qc
qc_decoding_ = qc_decoding(); qc_decoding_.draw(output = 'mpl')

cbits = '00'
qc = QuantumCircuit(2, 2)
qc_ent_dist_ = qc_ent_dist(); qc.append(qc_ent_dist_, [0,1])
qc.barrier()
qc_encoding_ = qc_encoding(cbits); qc.append(qc_encoding_, [0])
qc.barrier()
qc_decoding_ = qc_decoding(); qc.append(qc_decoding_, [0,1])
qc.measure([0,1],[0,1])
#qc.draw(output = 'mpl')
qc.decompose().draw(output = 'mpl')

result = execute(qc, backend = simulator, shots = nshots).result()
plot_histogram(result.get_counts(qc))

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

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