# Install the plugin
# !pip install -e .

from qiskit import QuantumCircuit
from qiskit.transpiler import PassManager
from qiskit_transpiler_service.ai.collection import CollectPermutations
from qiskit_transpiler_service.ai.synthesis import AIPermutationSynthesis
from qiskit.circuit.library import Permutation

num_qubits = 27
circuit = QuantumCircuit(num_qubits)
circuit.append(
    Permutation(
        num_qubits=num_qubits, pattern=[(i + 1) % num_qubits for i in range(num_qubits)]
    ),
    qargs=range(num_qubits),
)
circuit = circuit.decompose(reps=2)

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

ai_optimize_perms = PassManager(
    [
        CollectPermutations(do_commutative_analysis=True),
        AIPermutationSynthesis(backend_name="ibm_cairo"),
    ]
)

# AI Synthesis passes respect the coupling map and should run after transpiling
ai_optimized_circuit = ai_optimize_perms.run(circuit)

print(
    f"AI-Optimized circuit -> Depth: {ai_optimized_circuit.decompose(reps=3).depth()}, Gates(2q): {ai_optimized_circuit.decompose(reps=3).num_nonlocal_gates()}"
)
ai_optimized_circuit.draw(output="mpl", fold=-1, scale=0.25, style="iqp")



