Skip to content
Merged
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
15 changes: 10 additions & 5 deletions qiskit/transpiler/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, couplinglist=None, description=None):
self._dist_matrix = None
# a sorted list of physical qubits (integers) in this coupling map
self._qubit_list = None
# number of qubits in the graph
self._size = None
# a sorted list of physical qubits (integers) in this coupling map
self._is_symmetric = None

Expand All @@ -58,7 +60,9 @@ def __init__(self, couplinglist=None, description=None):

def size(self):
"""Return the number of physical qubits in this graph."""
return len(self.graph)
if self._size is None:
self._size = len(self.graph)
return self._size

def get_edges(self):
"""
Expand All @@ -85,6 +89,7 @@ def add_physical_qubit(self, physical_qubit):
self.graph.add_node(physical_qubit)
self._dist_matrix = None # invalidate
self._qubit_list = None # invalidate
self._size = None # invalidate

def add_edge(self, src, dst):
"""
Expand Down Expand Up @@ -169,10 +174,10 @@ def distance(self, physical_qubit1, physical_qubit2):
Raises:
CouplingError: if the qubits do not exist in the CouplingMap
"""
if physical_qubit1 not in self.physical_qubits:
raise CouplingError("%s not in coupling graph" % (physical_qubit1,))
if physical_qubit2 not in self.physical_qubits:
raise CouplingError("%s not in coupling graph" % (physical_qubit2,))
if physical_qubit1 >= self.size():
raise CouplingError("%s not in coupling graph" % physical_qubit1)
if physical_qubit2 >= self.size():
raise CouplingError("%s not in coupling graph" % physical_qubit2)
Comment on lines +177 to +180
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it make sense to likewise check if0 > physical_qubit1 here?

if self._dist_matrix is None:
self._compute_distance_matrix()
return int(self._dist_matrix[physical_qubit1, physical_qubit2])
Expand Down