import sys

sys.path.append("../..")

from qiskit.quantum_info.random import random_unitary, random_clifford
from qiskit import QuantumCircuit
from clonk.utils.transpiler_passes.pass_manager_v2 import level_0_pass_manager
from clonk.backend_utils import FakeAllToAll
from clonk.utils.riswap_gates.riswap import RiSwapGate
from clonk.utils.transpiler_passes.weyl_decompose import RootiSwapWeylDecomposition
from qiskit.transpiler.passes import CountOps
from qiskit.transpiler import PassManager
from qiskit.circuit.library import CXGate

N = 2000
basis_gate = RiSwapGate(0.5)

pm0 = PassManager()
pm0.append(RootiSwapWeylDecomposition(basis_gate=basis_gate))
pm0.append(CountOps())

res = 0
for _ in range(N):
    qc = QuantumCircuit(2)
    qc.append(random_clifford(2), [0, 1])
    # random_unitary(dim=4)
    pm0.run(qc)
    res += pm0.property_set["count_ops"]["riswap"]
print(res / N)

N = 2000
basis_gate = CXGate()

pm0 = PassManager()
pm0.append(RootiSwapWeylDecomposition(basis_gate=basis_gate))
pm0.append(CountOps())

res = 0
for _ in range(N):
    qc = QuantumCircuit(2)
    qc.append(random_clifford(2), [0, 1])
    # random_unitary(dim=4)
    pm0.run(qc)
    res += (
        pm0.property_set["count_ops"]["cx"]
        if "cx" in pm0.property_set["count_ops"].keys()
        else 0
    )
print(res / N)

from qiskit.circuit.library import CPhaseGate
import numpy as np

basis_gate = CXGate()

pm0 = PassManager()
pm0.append(RootiSwapWeylDecomposition(basis_gate=basis_gate))
pm0.append(CountOps())

qc = QuantumCircuit(2)
qc.append(CPhaseGate(np.pi / 2), [0, 1])
# random_unitary(dim=4)
transp = pm0.run(qc)

transp.draw(output="mpl")

from qiskit import transpile

transp = transpile(qc, basis_gates=["rz", "sx", "cx"], optimization_level=3)
print(transp.draw(output="latex_source"))

from qiskit.circuit.library import CPhaseGate
import numpy as np

basis_gate = RiSwapGate(0.5)

pm0 = PassManager()
pm0.append(RootiSwapWeylDecomposition(basis_gate=basis_gate))
pm0.append(CountOps())

qc = QuantumCircuit(2)
qc.append(CPhaseGate(np.pi / 2), [0, 1])
# random_unitary(dim=4)
transp = pm0.run(qc)

print(transp.draw(output="latex"))