%matplotlib inline
from qiskit import QuantumCircuit
from qiskit.compiler import transpile
from qiskit.transpiler import PassManager
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, IBMQ
from qiskit.tools.monitor import backend_overview, backend_monitor
from qiskit.compiler import transpile, assemble
from qiskit.visualization import *
from qiskit.transpiler import *
from qiskit.converters import circuit_to_dag
from qiskit.tools.visualization import dag_drawer
import pprint

provider = IBMQ.load_account()

real_backend = provider.get_backend('ibmq_16_melbourne')

plot_error_map(real_backend)

qr = QuantumRegister(3)
cr = ClassicalRegister(3)
circuit = QuantumCircuit(qr, cr)

circuit.h(qr[0])
circuit.cx(qr[0], qr[2])
circuit.measure(qr, cr)

circuit.draw(output='mpl')

compiled_circuit = transpile(circuit, real_backend, optimization_level=0)
compiled_circuit.draw(output='mpl', idle_wires=False)

compiled_circuit = transpile(circuit, real_backend, optimization_level=3)
compiled_circuit.draw(output='mpl', idle_wires=False)

qr2 = QuantumRegister(14)
cr2 = ClassicalRegister(14)
circuit2 = QuantumCircuit(qr2, cr2)

circuit2.h(qr2[0])
circuit2.cx(qr2[0], qr2[6])
circuit2.cx(qr2[6], qr2[13])
circuit2.cx(qr2[13], qr2[7])
circuit2.cx(qr2[7], qr2[0])
circuit2.measure(qr2, cr2)

for level in range(4):
    compiled_circuit2 = transpile(circuit2, real_backend, optimization_level=level)
    print('---------- Level = ' + str(level) + '----------')
    print('gates = ', compiled_circuit2.count_ops())
    print('depth = ', compiled_circuit2.depth())