Skip to content

Commit

Permalink
Implementing CouplingMap.__eq__ (#9766)
Browse files Browse the repository at this point in the history
* implementing Coupling.__eq__

* Update qiskit/transpiler/coupling.py

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* 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 <mtreinish@kortar.org>

* Update test/python/transpiler/test_coupling.py

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update test/python/transpiler/test_coupling.py

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

* Update test/python/transpiler/test_coupling.py

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
  • Loading branch information
DanPuzzuoli and mtreinish committed Mar 10, 2023
1 parent 332f4b2 commit 5ce80ab
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions releasenotes/notes/coupling-map-eq-b0507b703d62a5f3.yaml
Original file line number Diff line number Diff line change
@@ -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()``.
19 changes: 19 additions & 0 deletions test/python/transpiler/test_coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5ce80ab

Please sign in to comment.