From 5ce80ab45bfdec0f300d4f2095d4fc8dfe3eaae6 Mon Sep 17 00:00:00 2001 From: Daniel Puzzuoli Date: Fri, 10 Mar 2023 11:49:56 -0800 Subject: [PATCH] Implementing CouplingMap.__eq__ (#9766) * implementing Coupling.__eq__ * Update qiskit/transpiler/coupling.py Co-authored-by: Matthew Treinish * adding comments to test, and adding release note * black formatting * updating CouplingMap.__eq__ to check edge list equality instead of isomorphism * updating release notes * updating test doc string * Update test/python/transpiler/test_coupling.py Co-authored-by: Matthew Treinish * Update test/python/transpiler/test_coupling.py Co-authored-by: Matthew Treinish * Update test/python/transpiler/test_coupling.py Co-authored-by: Matthew Treinish * Update test/python/transpiler/test_coupling.py Co-authored-by: Matthew Treinish --------- Co-authored-by: Matthew Treinish --- qiskit/transpiler/coupling.py | 16 ++++++++++++++++ .../coupling-map-eq-b0507b703d62a5f3.yaml | 8 ++++++++ test/python/transpiler/test_coupling.py | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 releasenotes/notes/coupling-map-eq-b0507b703d62a5f3.yaml diff --git a/qiskit/transpiler/coupling.py b/qiskit/transpiler/coupling.py index 7d8dc6e5467b..26d4052aa02b 100644 --- a/qiskit/transpiler/coupling.py +++ b/qiskit/transpiler/coupling.py @@ -412,6 +412,22 @@ def __str__(self): string += "]" return string + def __eq__(self, other): + """Check if the graph in ``other`` has the same node labels and edges as the graph in + ``self``. + + This function assumes that the graphs in :class:`.CouplingMap` instances are connected. + + Args: + other (CouplingMap): The other coupling map. + + Returns: + bool: Whether or not other is isomorphic to self. + """ + if not isinstance(other, CouplingMap): + return False + return set(self.graph.edge_list()) == set(other.graph.edge_list()) + def draw(self): """Draws the coupling map. diff --git a/releasenotes/notes/coupling-map-eq-b0507b703d62a5f3.yaml b/releasenotes/notes/coupling-map-eq-b0507b703d62a5f3.yaml new file mode 100644 index 000000000000..72f6fecd974b --- /dev/null +++ b/releasenotes/notes/coupling-map-eq-b0507b703d62a5f3.yaml @@ -0,0 +1,8 @@ +--- +upgrade: + - | + The :meth:`.CouplingMap.__eq__`` method has been updated to check that the edge lists of the + underlying graphs contain the same elements. Under the assumption that the underlying graphs are + connected, this check additionally ensures that the graphs have the same number of nodes with + the same labels. Any code using ``CouplingMap() == CouplingMap()`` to check object equality + should be updated to ``CouplingMap() is CouplingMap()``. \ No newline at end of file diff --git a/test/python/transpiler/test_coupling.py b/test/python/transpiler/test_coupling.py index cffc22c59844..b15c389353e5 100644 --- a/test/python/transpiler/test_coupling.py +++ b/test/python/transpiler/test_coupling.py @@ -448,6 +448,25 @@ def test_implements_iter(self): expected = [(0, 1), (1, 0), (1, 2), (2, 1)] self.assertEqual(sorted(coupling), expected) + def test_equality(self): + """Test that equality checks that the graphs have the same nodes, node labels, and edges.""" + + # two coupling maps with 4 nodes and the same edges + coupling0 = CouplingMap([(0, 1), (0, 2), (2, 3)]) + coupling1 = CouplingMap([(0, 1), (0, 2), (2, 3)]) + self.assertEqual(coupling0, coupling1) + + # coupling map with 5 nodes not equal to the previous 2 + coupling2 = CouplingMap([(0, 1), (0, 2), (2, 4)]) + self.assertNotEqual(coupling0, coupling2) + + # coupling map isomorphic to coupling0, but with cyclically shifted labels + coupling3 = CouplingMap([(1, 2), (1, 3), (3, 0)]) + self.assertNotEqual(coupling0, coupling3) + + # additional test for comparison to a non-CouplingMap object + self.assertNotEqual(coupling0, 1) + class CouplingVisualizationTest(QiskitVisualizationTestCase): @unittest.skipUnless(optionals.HAS_GRAPHVIZ, "Graphviz not installed")