# Exercice 3 : le transpiler peut-il sauver l'arbre ?
from qiskit.transpiler import generate_preset_pass_manager
from qiskit_ibm_runtime.fake_provider import FakeFez

from common import ghz_chain, ghz_tree

fake = FakeFez()
print("niveau  circuit  CZ   prof. 2q   durée")
for level in [0, 1, 2, 3]:
    pm = generate_preset_pass_manager(
        optimization_level=level, backend=fake, seed_transpiler=7, scheduling_method="alap"
    )
    for name, build in [("chaîne", ghz_chain), ("arbre ", ghz_tree)]:
        qc = build(8)
        qc.measure_all()
        isa = pm.run(qc)
        two_qubit_depth = isa.depth(lambda ins: ins.operation.num_qubits == 2)
        duration = isa.estimate_duration(fake.target, unit="u")
        print(f"  {level}     {name}   {isa.count_ops()['cz']:>3}   {two_qubit_depth:>5}     {duration:.2f} µs")
