Skip to content

Commit

Permalink
dag.two_qubit_ops bites again
Browse files Browse the repository at this point in the history
  • Loading branch information
1ucian0 committed Nov 2, 2021
1 parent f8b0041 commit b7eb52c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
14 changes: 10 additions & 4 deletions qiskit/transpiler/passes/layout/vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from retworkx import PyGraph, PyDiGraph, vf2_mapping
from qiskit.transpiler.layout import Layout
from qiskit.transpiler.basepasses import AnalysisPass
from qiskit.transpiler.exceptions import TranspilerError


class VF2Layout(AnalysisPass):
Expand Down Expand Up @@ -50,10 +51,15 @@ def run(self, dag):
"""run the layout method"""
qubits = dag.qubits
qubit_indices = {qubit: index for index, qubit in enumerate(qubits)}
interactions = [
(qubit_indices[gate.qargs[0]], qubit_indices[gate.qargs[1]])
for gate in dag.two_qubit_ops()
]

interactions = []
for node in dag.op_nodes(include_directives=False):
len_args = len(node.qargs)
if len_args == 2:
interactions.append((qubit_indices[node.qargs[0]], qubit_indices[node.qargs[1]]))
if len_args >= 3:
raise TranspilerError("VF2Layout only can handle 2-qubit gates or less. Node "
f"{node.name} ({node}) is {len_args}-qubit")

if self.strict_direction:
cm_graph = self.coupling_map.graph
Expand Down
21 changes: 18 additions & 3 deletions test/python/transpiler/test_vf2_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import retworkx

from qiskit import QuantumRegister, QuantumCircuit
from qiskit.transpiler import CouplingMap
from qiskit.transpiler import CouplingMap, TranspilerError
from qiskit.transpiler.passes import VF2Layout
from qiskit.converters import circuit_to_dag
from qiskit.test import QiskitTestCase
Expand Down Expand Up @@ -112,7 +112,7 @@ class TestVF2LayoutLattice(LayoutTestCase):
def graph_state_from_pygraph(self, graph):
"""Creates a GraphState circuit from a PyGraph"""
adjacency_matrix = retworkx.adjacency_matrix(graph)
return GraphState(adjacency_matrix)
return GraphState(adjacency_matrix).decompose()

def test_hexagonal_lattice_graph_20_in_25(self):
"""A 20x20 interaction map in 25x25 coupling map"""
Expand Down Expand Up @@ -282,7 +282,7 @@ def test_perfect_fit_Manhattan(self):
adj_matrix = numpy.zeros((65, 65))
adj_matrix[rows, cols] = 1

circuit = GraphState(adj_matrix)
circuit = GraphState(adj_matrix).decompose()
circuit.measure_all()

dag = circuit_to_dag(circuit)
Expand Down Expand Up @@ -320,6 +320,21 @@ def test_seed(self):

self.assertNotEqual(layout_1, layout_2)

def test_3_q_gate(self):
"""The pass does not handle gates with more than 2 qubits"""
seed_1 = 42

cmap5 = FakeTenerife().configuration().coupling_map

qr = QuantumRegister(3, "qr")
circuit = QuantumCircuit(qr)
circuit.ccx(qr[1], qr[0], qr[2])
dag = circuit_to_dag(circuit)

pass_1 = VF2Layout(CouplingMap(cmap5), seed=seed_1)
with self.assertRaises(TranspilerError):
pass_1.run(dag)


if __name__ == "__main__":
unittest.main()

0 comments on commit b7eb52c

Please sign in to comment.