import numpy as np
from qiskit import *
from qiskit.visualization import plot_histogram, plot_gate_map
from qiskit.tools.monitor import job_monitor
from qiskit.test.mock import FakeProvider
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline

backends = list(filter(lambda x:
not (x.configuration().simulator or
    x.configuration().n_qubits <= 5),
                       FakeProvider().backends()))

ghz = QuantumCircuit(5, 5)
ghz.h(0)
for idx in range(1, 5):
    ghz.cx(0, idx)
ghz.barrier()
ghz.measure(range(5), range(5))
ghz.draw(output='mpl')

ghz_basis = ghz.decompose()
ghz_basis.draw(output='mpl')

import inspect
inspect.signature(transpile)

qc = QuantumCircuit(3, 2)
# Preparation using superposition
qc.h(0)
qc.h(1)
qc.x(2)
qc.h(2)
qc.barrier()

# Function Uw
qc.x(0)
qc.ccx(0, 1, 2)
qc.x(0)
qc.barrier()

# Function Us
qc.h(0)
qc.h(1)
qc.x(0)
qc.x(1)
qc.ccx(0, 1, 2)
qc.x(0)
qc.x(1)
qc.h(0)
qc.h(1)
qc.barrier()

# Measure
qc.measure([0, 1], [0, 1])
qc.draw(output='mpl')

qc_basis = qc.decompose()
qc_basis.draw(output='mpl')

print(qc.depth(), ', ', qc_basis.depth())

# Without barrier

qc = QuantumCircuit(3, 2)
# Preparation using superposition
qc.h(0)
qc.h(1)
qc.x(2)
qc.h(2)

# Function Uw
qc.x(0)
qc.ccx(0, 1, 2)
qc.x(0)

# Function Us
qc.h(0)
qc.h(1)
qc.x(0)
qc.x(1)
qc.ccx(0, 1, 2)
qc.x(0)
qc.x(1)
qc.h(0)
qc.h(1)

# Measure
qc.measure([0, 1], [0, 1])
qc.draw(output='mpl')

qc_basis = qc.decompose()
qc_basis.draw(output='mpl')

print(qc.depth(), ', ', qc_basis.depth())