# Install the plugin
# !pip install -e .

from qiskit.transpiler import PassManager
from qiskit_transpiler_service.ai.routing import AIRouting
from qiskit.circuit.library import EfficientSU2

circuit = EfficientSU2(100, entanglement="circular", reps=1).decompose()
print(
    f"Original circuit -> Depth: {circuit.depth()}, Gates(2q): {circuit.num_nonlocal_gates()}"
)
circuit.draw(output="mpl", fold=-1, scale=0.2, style="iqp")

qiskit_ai_transpiler = PassManager(
    [AIRouting(backend_name="ibm_torino", optimization_level=1, layout_mode="optimize")]
)

ai_transpiled_circuit = qiskit_ai_transpiler.run(circuit)

print(
    f"Qiskit AI Transpiler -> Depth: {ai_transpiled_circuit.depth()}, Gates(2q): {ai_transpiled_circuit.num_nonlocal_gates()}"
)
ai_transpiled_circuit.draw(output="mpl", fold=-1, scale=0.2, style="iqp")

from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService

coupling_map = QiskitRuntimeService().backend("ibm_torino").coupling_map

qiskit_lvl3_transpiler = generate_preset_pass_manager(
    optimization_level=3, coupling_map=coupling_map
)

lvl3_transpiled_circuit = qiskit_lvl3_transpiler.run(circuit)

print(
    f"Qiskit lvl3 Transpiler -> Depth: {lvl3_transpiled_circuit.depth()}, Gates(2q): {lvl3_transpiled_circuit.num_nonlocal_gates()}"
)
lvl3_transpiled_circuit.draw(output="mpl", fold=-1, scale=0.2, style="iqp")

from qiskit_transpiler_service.transpiler_service import TranspilerService

qiskit_lvl3_transpiler_service = TranspilerService(
    backend_name="ibm_torino",
    ai="false",
    optimization_level=3,
)

lvl3_transpiled_circuit_v2 = qiskit_lvl3_transpiler_service.run(circuit)

print(
    f"Qiskit lvl3 Transpiler -> Depth: {lvl3_transpiled_circuit_v2.depth()}, Gates(2q): {lvl3_transpiled_circuit_v2.num_nonlocal_gates()}"
)
lvl3_transpiled_circuit_v2.draw(output="mpl", fold=-1, scale=0.2, style="iqp")

qiskit_lvl3_ai_transpiler_service = TranspilerService(
    backend_name="ibm_torino",
    ai="true",
    optimization_level=3,
)

lvl3_transpiled_ai_circuit_v2 = qiskit_lvl3_ai_transpiler_service.run(circuit)

print(
    f"Qiskit lvl3 AI Transpiler -> Depth: {lvl3_transpiled_ai_circuit_v2.depth()}, Gates(2q): {lvl3_transpiled_ai_circuit_v2.num_nonlocal_gates()}"
)
lvl3_transpiled_ai_circuit_v2.draw(output="mpl", fold=-1, scale=0.2, style="iqp")

