from qiskit import QuantumCircuit
from qiskit.transpiler import PassManager
import sys

sys.path.append("..")
from clonk.backend_utils.backend_analysis_pass import TopologyAnalysis

qc = QuantumCircuit(1)
from clonk.backend_utils.mock_backends.ibm import *

backend_list = [PenguinV1(), PenguinV2(), PenguinV3(), PenguinV4(), FalconR4()]

for backend in backend_list:
    print(backend.name)
    pm1 = PassManager(TopologyAnalysis(backend))
    pm1.run(qc)
    # FIXME, just save the property_set dictionary directly
    for key, value in pm1.property_set.items():
        print(key, value)
    print()

# from clonk.utils.transpiler_passes import level_0_pass_manager
# pm_lattice = level_0_pass_manager(PenguinV1(), basis_gate="CR")
# qc = QuantumCircuit(2)
# qc.cx(0,1)
# transp = pm_lattice.run(qc)
# transp.draw(output='mpl')

import sys

sys.path.append("..")
from clonk.backend_utils.mock_backends import FakeHeavyHex

from circuit_suite import circuits

circuit_lambdas = [v.circuit_lambda(10) for k, v in circuits.items()]
circuit_labels = [v.label for k, v in circuits.items()]
backend_list = [
    PenguinV1(),
    PenguinV2(),
    PenguinV3(),
    PenguinV4(),
    FakeHeavyHex(),
]  # FalconR4()]

import matplotlib.pyplot as plt
import numpy as np

labels = [backend.name for backend in backend_list]

circuit_depth_list = []
for circuit in circuit_lambdas:
    circuit_depth_temp = []
    for backend in backend_list:
        from clonk.utils.transpiler_passes import level_0_pass_manager

        pm = level_0_pass_manager(backend, basis_gate="CR")
        pm.run(circuit)
        circuit_depth_temp.append(pm.property_set["depth"])
    circuit_depth_list.append(circuit_depth_temp)

x = np.arange(len(labels))  # the label locations
width = 0.5 / (len(labels) - 1)  # the width of the bars

fig, ax = plt.subplots()
count = 0
for circuit_depth, label in zip(circuit_depth_list, circuit_labels):
    rects1 = ax.bar(x + width * count, circuit_depth, width, label=label)
    ax.bar_label(rects1, padding=3)
    count += 1

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel("Depth")
ax.set_title("IBM Topologies on Circuit Benchmark")
ax.set_xticks(x, labels)
ax.legend()

fig.tight_layout()

plt.show()

from qiskit import QuantumCircuit
import sys

sys.path.append("..")
qc = QuantumCircuit(2)
qc.swap(0, 1)
from clonk.utils.transpiler_passes import level_0_pass_manager
from clonk.backend_utils.mock_backends import *

pm = level_0_pass_manager(PenguinV1(), "CR", decompose_1q=False)
transp = pm.run(qc)
transp.draw(output="mpl")
# from qiskit.quantum_info import Operator
# Operator(transp).equiv(Operator(qc))