from qiskit import *
from qiskit.visualization import plot_histogram
import numpy as np

IBMQ.load_account()

IBMQ.providers()

provider = IBMQ.get_provider('ibm-q')
provider.backends()

import qiskit.tools.jupyter
backend_ex = provider.get_backend('ibmq_16_melbourne')
backend_ex

backends = provider.backends(filters = 
    lambda x:x.configuration().n_qubits >= 2 
    and not x.configuration().simulator
    and x.status().operational==True
    )
backends

from qiskit.providers.ibmq import least_busy
backend = least_busy(provider.backends(filters = 
    lambda x:x.configuration().n_qubits >= 2 
    and not x.configuration().simulator
    and x.status().operational==True
    ))
backends

backend = provider.get_backend('ibmqx2')

qc_and = QuantumCircuit(3)
qc_and.ccx(0,1,2)
qc_and.draw()

qc_and.decompose().draw()

from qiskit.tools.monitor import job_monitor

def AND(inp1, inp2, backend, layout):
    
    qc = QuantumCircuit(3, 1) 
    qc.reset(range(3))
    
    if inp1=='1':
        qc.x(0)
    if inp2=='1':
        qc.x(1)
        
    qc.barrier()
    qc.ccx(0, 1, 2) 
    qc.barrier()
    qc.measure(2, 0) 
  
    qc_trans = transpile(qc, backend, initial_layout=layout, optimization_level=3)
    job = execute(qc_trans, backend, shots=8192)
    print(job.job_id())
    job_monitor(job)
    
    output = job.result().get_counts()
    
    return qc_trans, output

qc_trans, output = AND('0', '0', backend, [0, 2, 4])

qc_trans

output

from qiskit.visualization import plot_gate_map, plot_error_map
plot_gate_map(backend)

plot_error_map(backend)

