import sys

sys.path.append("..")
# from benchmark_suite.backend_suite import backends

# from clonk.backend_utils.backend_analysis_pass import TopologyAnalysis
# from qiskit.transpiler.passmanager import PassManager
# from qiskit import QuantumCircuit
# qc = QuantumCircuit(1)
# for backend_label, backend_obj in backends.items():
#     print(backend_label)
#     pm1 = PassManager(TopologyAnalysis(backend_obj.backend))
#     pm1.run(qc)
#     #FIXME, just save the property_set dictionary directly
#     for key, value in pm1.property_set.items():
#         backend_obj.save_data(parameter=key, data=value)
#     backend_obj.save_json()

# hypercube analsis
from qiskit import QuantumCircuit
from qiskit.transpiler import PassManager
from clonk.backend_utils.backend_analysis_pass import TopologyAnalysis

qc = QuantumCircuit(1)
import sys

sys.path.append("..")
from clonk.backend_utils.mock_backends import FakeHyperCubeV2, FakeHyperCubeSnail

backend_list = [
    FakeHyperCubeSnail(corral_skip_pattern=(0, 0)),
    FakeHyperCubeSnail(corral_skip_pattern=(0, 1)),
]

max_connectivity = 0
min_distance = 100
best = None

for i in range(6):
    for j in range(6):
        backend = FakeHyperCubeSnail(corral_skip_pattern=(i, j))
        print(backend.name[:-3])
        try:
            pm1 = PassManager(TopologyAnalysis(backend))
            pm1.run(qc)
        except Exception:
            continue
            # coupling error
        # print(f"Num qubits {backend.num_qubits}")

        connectivity = pm1.property_set["Avg_Connectivity"]
        distance = pm1.property_set["Avg_Distance"]
        if distance <= min_distance:
            if distance < min_distance or connectivity > max_connectivity:
                best = backend.name[:-3]
                min_distance = distance
                max_connectivity = connectivity
                degree = pm1.property_set["Degree"]
                diameter = pm1.property_set["Diameter"]
        # if connectivity >= max_connectivity:
        #     if connectivity > max_connectivity or distance < min_distance:
        #         best = backend.name[:-3]
        #         min_distance = distance
        #         max_connectivity = connectivity

print(f"best: {best}")
print(min_distance)
print(max_connectivity)
print(degree)
print(diameter)

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 mock_backends.ibm import *
# from mock_backends import *
# backend_list = [PenguinV1(), PenguinV2(), PenguinV3(), PenguinV4(), FakeHeavyHex()] #FalconR4()]
from benchmark_suite.backend_suite_v2 import (
    small_motivation_backends,
    hpca_modular_backends,
    hpca_corral_backends,
)

backend_list = hpca_corral_backends

for backend in backend_list:
    print(backend.label)
    pm1 = PassManager(TopologyAnalysis(backend.backend))
    pm1.run(qc)
    print(f"Num qubits {backend.backend.num_qubits}")
    # just save the property_set dictionary directly
    backend.data.update(dict(pm1.property_set))
    backend.save_json()
    # then print it
    for key, value in pm1.property_set.items():
        print(key, str(round(value, 2)))
    print()

from clonk.backend_utils.mock_backends import FakeModular

backend_list = [FakeModular(module_size=4, children=4, total_levels=3)]
for backend in backend_list:
    print(backend.name)
    pm1 = PassManager(TopologyAnalysis(backend))
    pm1.run(qc)
    print(f"Num qubits {backend.num_qubits}")
    # then print it
    for key, value in pm1.property_set.items():
        print(key, str(round(value, 2)))
    print()