import numpy
from qiskit.circuit.library import GraphState
from qiskit.test.mock import FakeMumbai
from qiskit.transpiler import CouplingMap
from qiskit.transpiler.passes import VF2Layout, CSPLayout
from qiskit.visualization import plot_gate_map, plot_circuit_layout

backend = FakeMumbai()
config = backend.configuration()
cm = config.coupling_map
size = config.n_qubits
display(plot_gate_map(backend))

rows = [x[0] for x in cm]
cols = [x[1] for x in cm]

A = numpy.zeros((size, size))
A[rows, cols] = 1

from random import shuffle
random_map = list(range(size))
shuffle(random_map)
B = numpy.zeros((size, size))
for i in range(size):
    for j in range(size):
        B[i][j] = A[random_map[i]][random_map[j]]
A = B
    
circuit = GraphState(A).decompose()
circuit.draw(fold=-1)

vf2_pass = VF2Layout(CouplingMap(cm))
csp_pass = CSPLayout(CouplingMap(cm), time_limit=None, call_limit=None)

csp_transpiled = csp_pass(circuit)

vf2_transpiled = vf2_pass(circuit)

plot_circuit_layout(vf2_transpiled, backend)

plot_circuit_layout(csp_transpiled, backend)

from qiskit import QuantumCircuit

# T-shape interaction map
circuit = QuantumCircuit(5)
circuit.cx(0,1)
circuit.cx(0,2)
circuit.cx(0,3)
circuit.cx(3,4)

# Multiple possible answers. This allows for noise-awareness (ish).
vf2_transpiled = vf2_pass(circuit)
vf2_transpiled

