Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing CouplingMap.__eq__ #9766

Merged
merged 15 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think maybe having a comment here to say this will create a 5 node coupling map with 3 disconnected (it might not be obvious from a quick glance).

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