We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
String formating bug in Coupling
The descriptions of qubits are tuple (e.g. `('q',0)`) that are not automatically converted to strings when `"%s not in coupling graph" % q1`, a `TypeError: not all arguments converted during string formatting` raises.

### Informations

- **Qiskit Terra version**: 0.6.0
- **Python version**: 3.6.1
- **Operating system**: MacOS

### What is the current behavior?
```
In [1]: from qiskit.mapper._coupling import Coupling
In [2]: graph = Coupling({0: [1, 2], 1: [2]})
In [3]: graph.distance(('q0',0),('q1',1))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-88a8c8c0c516> in <module>()
----> 1 graph.distance(('q0',0),('q1',1))

/Users/luciano.bello/repos/qiskit-terra/qiskit/mapper/_coupling.py in distance(self, q1, q2)
    168             raise CouplingError("distance has not been computed")
    169         if q1 not in self.qubits:
--> 170             raise CouplingError("%s not in coupling graph" % q1)
    171         if q2 not in self.qubits:
    172             raise CouplingError("%s not in coupling graph" % q2)

TypeError: not all arguments converted during string formatting
```

### What is the expected behavior?
```
In [1]: from qiskit.mapper._coupling import Coupling
In [2]: graph = Coupling({0: [1, 2], 1: [2]})
In [3]: graph.distance(('q0',0),('q1',1))
---------------------------------------------------------------------------
CouplingError                             Traceback (most recent call last)
<ipython-input-3-88a8c8c0c516> in <module>()
----> 1 graph.distance(('q0',0),('q1',1))

/Users/luciano.bello/repos/qiskit-terra/qiskit/mapper/_coupling.py in distance(self, q1, q2)
    169             raise CouplingError("distance has not been computed")
    170         if q1 not in self.qubits:
--> 171             raise CouplingError("%s not in coupling graph" % (q1,))
    172         if q2 not in self.qubits:
    173             raise CouplingError("%s not in coupling graph" % (q2,))

CouplingError: "('q0', 0) not in coupling graph"
```

### Suggested solutions

#738 



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `qiskit/mapper/_coupling.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 
3 # Copyright 2017, IBM.
4 #
5 # This source code is licensed under the Apache License, Version 2.0 found in
6 # the LICENSE.txt file in the root directory of this source tree.
7 
8 """
9 Directed graph object for representing coupling between qubits.
10 
11 The nodes of the graph correspond to named qubits and the directed edges
12 indicate which qubits are coupled and the permitted direction of CNOT gates.
13 The object has a distance function that can be used to map quantum circuits
14 onto a device with this coupling.
15 """
16 from collections import OrderedDict
17 import networkx as nx
18 from ._couplingerror import CouplingError
19 
20 
21 def coupling_dict2list(couplingdict):
22     """Convert coupling map dictionary into list.
23 
24     Example dictionary format: {0: [1, 2], 1: [2]}
25     Example list format: [[0, 1], [0, 2], [1, 2]]
26 
27     We do not do any checking of the input.
28 
29     Return coupling map in list format.
30     """
31     if not couplingdict:
32         return None
33     couplinglist = []
34     for ctl, tgtlist in couplingdict.items():
35         for tgt in tgtlist:
36             couplinglist.append([ctl, tgt])
37     return couplinglist
38 
39 
40 def coupling_list2dict(couplinglist):
41     """Convert coupling map list into dictionary.
42 
43     Example list format: [[0, 1], [0, 2], [1, 2]]
44     Example dictionary format: {0: [1, 2], 1: [2]}
45 
46     We do not do any checking of the input.
47 
48     Return coupling map in dict format.
49     """
50     if not couplinglist:
51         return None
52     couplingdict = {}
53     for pair in couplinglist:
54         if pair[0] in couplingdict:
55             couplingdict[pair[0]].append(pair[1])
56         else:
57             couplingdict[pair[0]] = [pair[1]]
58     return couplingdict
59 
60 
61 class Coupling:
62     """
63     Directed graph specifying fixed coupling.
64 
65     Nodes correspond to qubits and directed edges correspond to permitted
66     CNOT gates
67     """
68     # pylint: disable=invalid-name
69 
70     def __init__(self, couplingdict=None):
71         """
72         Create coupling graph.
73 
74         By default, the coupling graph has no nodes. The optional couplingdict
75         specifies the graph as an adjacency list. For example,
76         couplingdict = {0: [1, 2], 1: [2]}.
77         """
78         # self.qubits is dict from qubit (regname,idx) tuples to node indices
79         self.qubits = OrderedDict()
80         # self.index_to_qubit is a dict from node indices to qubits
81         self.index_to_qubit = {}
82         # self.node_counter is integer counter for labeling nodes
83         self.node_counter = 0
84         # self.G is the coupling digraph
85         self.G = nx.DiGraph()
86         # self.dist is a dict of dicts from node pairs to distances
87         # it must be computed, it is the distance on the digraph
88         self.dist = None
89         # Add edges to the graph if the couplingdict is present
90         if couplingdict is not None:
91             for v0, alist in couplingdict.items():
92                 for v1 in alist:
93                     regname = "q"
94                     self.add_edge((regname, v0), (regname, v1))
95             self.compute_distance()
96 
97     def size(self):
98         """Return the number of qubits in this graph."""
99         return len(self.qubits)
100 
101     def get_qubits(self):
102         """Return the qubits in this graph as a sorted (qreg, index) tuples."""
103         return sorted(list(self.qubits.keys()))
104 
105     def get_edges(self):
106         """Return a list of edges in the coupling graph.
107 
108         Each edge is a pair of qubits and each qubit is a tuple (qreg, index).
109         """
110         return list(map(lambda x: (self.index_to_qubit[x[0]],
111                                    self.index_to_qubit[x[1]]), self.G.edges()))
112 
113     def add_qubit(self, name):
114         """
115         Add a qubit to the coupling graph.
116 
117         name = tuple (regname, idx) for qubit
118         """
119         if name in self.qubits:
120             raise CouplingError("%s already in coupling graph" % name)
121         if not isinstance(name, tuple):
122             raise CouplingError("name %s is not a tuple")
123         if not (isinstance(name[0], str) and isinstance(name[1], int)):
124             raise CouplingError("name %s is not of the right form, it must"
125                                 " be: (regname, idx)")
126 
127         self.node_counter += 1
128         self.G.add_node(self.node_counter)
129         self.G.node[self.node_counter]["name"] = name
130         self.qubits[name] = self.node_counter
131         self.index_to_qubit[self.node_counter] = name
132 
133     def add_edge(self, s_name, d_name):
134         """
135         Add directed edge to coupling graph.
136 
137         s_name = source qubit tuple
138         d_name = destination qubit tuple
139         """
140         if s_name not in self.qubits:
141             self.add_qubit(s_name)
142         if d_name not in self.qubits:
143             self.add_qubit(d_name)
144         self.G.add_edge(self.qubits[s_name], self.qubits[d_name])
145 
146     def connected(self):
147         """
148         Test if the graph is connected.
149 
150         Return True if connected, False otherwise
151         """
152         try:
153             return nx.is_weakly_connected(self.G)
154         except nx.exception.NetworkXException:
155             return False
156 
157     def compute_distance(self):
158         """
159         Compute the undirected distance function on pairs of nodes.
160 
161         The distance map self.dist is computed from the graph using
162         all_pairs_shortest_path_length.
163         """
164         if not self.connected():
165             raise CouplingError("coupling graph not connected")
166         lengths = dict(nx.all_pairs_shortest_path_length(self.G.to_undirected()))
167         self.dist = {}
168         for i in self.qubits.keys():
169             self.dist[i] = {}
170             for j in self.qubits.keys():
171                 self.dist[i][j] = lengths[self.qubits[i]][self.qubits[j]]
172 
173     def distance(self, q1, q2):
174         """Return the undirected distance between qubit q1 to qubit q2."""
175         if self.dist is None:
176             raise CouplingError("distance has not been computed")
177         if q1 not in self.qubits:
178             raise CouplingError("%s not in coupling graph" % q1)
179         if q2 not in self.qubits:
180             raise CouplingError("%s not in coupling graph" % q2)
181         return self.dist[q1][q2]
182 
183     def __str__(self):
184         """Return a string representation of the coupling graph."""
185         s = ""
186         if self.qubits:
187             s += "qubits: "
188             s += ", ".join(["%s[%d] @ %d" % (k[0], k[1], v)
189                             for k, v in self.qubits.items()])
190         if self.get_edges():
191             s += "\nedges: "
192             s += ", ".join(
193                 ["%s[%d]-%s[%d]" % (e[0][0], e[0][1], e[1][0], e[1][1])
194                  for e in self.get_edges()])
195         return s
196
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/qiskit/mapper/_coupling.py b/qiskit/mapper/_coupling.py
--- a/qiskit/mapper/_coupling.py
+++ b/qiskit/mapper/_coupling.py
@@ -65,6 +65,7 @@
     Nodes correspond to qubits and directed edges correspond to permitted
     CNOT gates
     """
+
     # pylint: disable=invalid-name
 
     def __init__(self, couplingdict=None):
@@ -175,9 +176,9 @@
         if self.dist is None:
             raise CouplingError("distance has not been computed")
         if q1 not in self.qubits:
-            raise CouplingError("%s not in coupling graph" % q1)
+            raise CouplingError("%s not in coupling graph" % (q1,))
         if q2 not in self.qubits:
-            raise CouplingError("%s not in coupling graph" % q2)
+            raise CouplingError("%s not in coupling graph" % (q2,))
         return self.dist[q1][q2]
 
     def __str__(self):

{"golden_diff": "diff --git a/qiskit/mapper/_coupling.py b/qiskit/mapper/_coupling.py\n--- a/qiskit/mapper/_coupling.py\n+++ b/qiskit/mapper/_coupling.py\n@@ -65,6 +65,7 @@\n     Nodes correspond to qubits and directed edges correspond to permitted\n     CNOT gates\n     \"\"\"\n+\n     # pylint: disable=invalid-name\n \n     def __init__(self, couplingdict=None):\n@@ -175,9 +176,9 @@\n         if self.dist is None:\n             raise CouplingError(\"distance has not been computed\")\n         if q1 not in self.qubits:\n-            raise CouplingError(\"%s not in coupling graph\" % q1)\n+            raise CouplingError(\"%s not in coupling graph\" % (q1,))\n         if q2 not in self.qubits:\n-            raise CouplingError(\"%s not in coupling graph\" % q2)\n+            raise CouplingError(\"%s not in coupling graph\" % (q2,))\n         return self.dist[q1][q2]\n \n     def __str__(self):\n", "issue": "String formating bug in Coupling\nThe descriptions of qubits are tuple (e.g. `('q',0)`) that are not automatically converted to strings when `\"%s not in coupling graph\" % q1`, a `TypeError: not all arguments converted during string formatting` raises.\r\n\r\n### Informations\r\n\r\n- **Qiskit Terra version**: 0.6.0\r\n- **Python version**: 3.6.1\r\n- **Operating system**: MacOS\r\n\r\n### What is the current behavior?\r\n```\r\nIn [1]: from qiskit.mapper._coupling import Coupling\r\nIn [2]: graph = Coupling({0: [1, 2], 1: [2]})\r\nIn [3]: graph.distance(('q0',0),('q1',1))\r\n---------------------------------------------------------------------------\r\nTypeError                                 Traceback (most recent call last)\r\n<ipython-input-3-88a8c8c0c516> in <module>()\r\n----> 1 graph.distance(('q0',0),('q1',1))\r\n\r\n/Users/luciano.bello/repos/qiskit-terra/qiskit/mapper/_coupling.py in distance(self, q1, q2)\r\n    168             raise CouplingError(\"distance has not been computed\")\r\n    169         if q1 not in self.qubits:\r\n--> 170             raise CouplingError(\"%s not in coupling graph\" % q1)\r\n    171         if q2 not in self.qubits:\r\n    172             raise CouplingError(\"%s not in coupling graph\" % q2)\r\n\r\nTypeError: not all arguments converted during string formatting\r\n```\r\n\r\n### What is the expected behavior?\r\n```\r\nIn [1]: from qiskit.mapper._coupling import Coupling\r\nIn [2]: graph = Coupling({0: [1, 2], 1: [2]})\r\nIn [3]: graph.distance(('q0',0),('q1',1))\r\n---------------------------------------------------------------------------\r\nCouplingError                             Traceback (most recent call last)\r\n<ipython-input-3-88a8c8c0c516> in <module>()\r\n----> 1 graph.distance(('q0',0),('q1',1))\r\n\r\n/Users/luciano.bello/repos/qiskit-terra/qiskit/mapper/_coupling.py in distance(self, q1, q2)\r\n    169             raise CouplingError(\"distance has not been computed\")\r\n    170         if q1 not in self.qubits:\r\n--> 171             raise CouplingError(\"%s not in coupling graph\" % (q1,))\r\n    172         if q2 not in self.qubits:\r\n    173             raise CouplingError(\"%s not in coupling graph\" % (q2,))\r\n\r\nCouplingError: \"('q0', 0) not in coupling graph\"\r\n```\r\n\r\n### Suggested solutions\r\n\r\n#738 \r\n\r\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Copyright 2017, IBM.\n#\n# This source code is licensed under the Apache License, Version 2.0 found in\n# the LICENSE.txt file in the root directory of this source tree.\n\n\"\"\"\nDirected graph object for representing coupling between qubits.\n\nThe nodes of the graph correspond to named qubits and the directed edges\nindicate which qubits are coupled and the permitted direction of CNOT gates.\nThe object has a distance function that can be used to map quantum circuits\nonto a device with this coupling.\n\"\"\"\nfrom collections import OrderedDict\nimport networkx as nx\nfrom ._couplingerror import CouplingError\n\n\ndef coupling_dict2list(couplingdict):\n    \"\"\"Convert coupling map dictionary into list.\n\n    Example dictionary format: {0: [1, 2], 1: [2]}\n    Example list format: [[0, 1], [0, 2], [1, 2]]\n\n    We do not do any checking of the input.\n\n    Return coupling map in list format.\n    \"\"\"\n    if not couplingdict:\n        return None\n    couplinglist = []\n    for ctl, tgtlist in couplingdict.items():\n        for tgt in tgtlist:\n            couplinglist.append([ctl, tgt])\n    return couplinglist\n\n\ndef coupling_list2dict(couplinglist):\n    \"\"\"Convert coupling map list into dictionary.\n\n    Example list format: [[0, 1], [0, 2], [1, 2]]\n    Example dictionary format: {0: [1, 2], 1: [2]}\n\n    We do not do any checking of the input.\n\n    Return coupling map in dict format.\n    \"\"\"\n    if not couplinglist:\n        return None\n    couplingdict = {}\n    for pair in couplinglist:\n        if pair[0] in couplingdict:\n            couplingdict[pair[0]].append(pair[1])\n        else:\n            couplingdict[pair[0]] = [pair[1]]\n    return couplingdict\n\n\nclass Coupling:\n    \"\"\"\n    Directed graph specifying fixed coupling.\n\n    Nodes correspond to qubits and directed edges correspond to permitted\n    CNOT gates\n    \"\"\"\n    # pylint: disable=invalid-name\n\n    def __init__(self, couplingdict=None):\n        \"\"\"\n        Create coupling graph.\n\n        By default, the coupling graph has no nodes. The optional couplingdict\n        specifies the graph as an adjacency list. For example,\n        couplingdict = {0: [1, 2], 1: [2]}.\n        \"\"\"\n        # self.qubits is dict from qubit (regname,idx) tuples to node indices\n        self.qubits = OrderedDict()\n        # self.index_to_qubit is a dict from node indices to qubits\n        self.index_to_qubit = {}\n        # self.node_counter is integer counter for labeling nodes\n        self.node_counter = 0\n        # self.G is the coupling digraph\n        self.G = nx.DiGraph()\n        # self.dist is a dict of dicts from node pairs to distances\n        # it must be computed, it is the distance on the digraph\n        self.dist = None\n        # Add edges to the graph if the couplingdict is present\n        if couplingdict is not None:\n            for v0, alist in couplingdict.items():\n                for v1 in alist:\n                    regname = \"q\"\n                    self.add_edge((regname, v0), (regname, v1))\n            self.compute_distance()\n\n    def size(self):\n        \"\"\"Return the number of qubits in this graph.\"\"\"\n        return len(self.qubits)\n\n    def get_qubits(self):\n        \"\"\"Return the qubits in this graph as a sorted (qreg, index) tuples.\"\"\"\n        return sorted(list(self.qubits.keys()))\n\n    def get_edges(self):\n        \"\"\"Return a list of edges in the coupling graph.\n\n        Each edge is a pair of qubits and each qubit is a tuple (qreg, index).\n        \"\"\"\n        return list(map(lambda x: (self.index_to_qubit[x[0]],\n                                   self.index_to_qubit[x[1]]), self.G.edges()))\n\n    def add_qubit(self, name):\n        \"\"\"\n        Add a qubit to the coupling graph.\n\n        name = tuple (regname, idx) for qubit\n        \"\"\"\n        if name in self.qubits:\n            raise CouplingError(\"%s already in coupling graph\" % name)\n        if not isinstance(name, tuple):\n            raise CouplingError(\"name %s is not a tuple\")\n        if not (isinstance(name[0], str) and isinstance(name[1], int)):\n            raise CouplingError(\"name %s is not of the right form, it must\"\n                                \" be: (regname, idx)\")\n\n        self.node_counter += 1\n        self.G.add_node(self.node_counter)\n        self.G.node[self.node_counter][\"name\"] = name\n        self.qubits[name] = self.node_counter\n        self.index_to_qubit[self.node_counter] = name\n\n    def add_edge(self, s_name, d_name):\n        \"\"\"\n        Add directed edge to coupling graph.\n\n        s_name = source qubit tuple\n        d_name = destination qubit tuple\n        \"\"\"\n        if s_name not in self.qubits:\n            self.add_qubit(s_name)\n        if d_name not in self.qubits:\n            self.add_qubit(d_name)\n        self.G.add_edge(self.qubits[s_name], self.qubits[d_name])\n\n    def connected(self):\n        \"\"\"\n        Test if the graph is connected.\n\n        Return True if connected, False otherwise\n        \"\"\"\n        try:\n            return nx.is_weakly_connected(self.G)\n        except nx.exception.NetworkXException:\n            return False\n\n    def compute_distance(self):\n        \"\"\"\n        Compute the undirected distance function on pairs of nodes.\n\n        The distance map self.dist is computed from the graph using\n        all_pairs_shortest_path_length.\n        \"\"\"\n        if not self.connected():\n            raise CouplingError(\"coupling graph not connected\")\n        lengths = dict(nx.all_pairs_shortest_path_length(self.G.to_undirected()))\n        self.dist = {}\n        for i in self.qubits.keys():\n            self.dist[i] = {}\n            for j in self.qubits.keys():\n                self.dist[i][j] = lengths[self.qubits[i]][self.qubits[j]]\n\n    def distance(self, q1, q2):\n        \"\"\"Return the undirected distance between qubit q1 to qubit q2.\"\"\"\n        if self.dist is None:\n            raise CouplingError(\"distance has not been computed\")\n        if q1 not in self.qubits:\n            raise CouplingError(\"%s not in coupling graph\" % q1)\n        if q2 not in self.qubits:\n            raise CouplingError(\"%s not in coupling graph\" % q2)\n        return self.dist[q1][q2]\n\n    def __str__(self):\n        \"\"\"Return a string representation of the coupling graph.\"\"\"\n        s = \"\"\n        if self.qubits:\n            s += \"qubits: \"\n            s += \", \".join([\"%s[%d] @ %d\" % (k[0], k[1], v)\n                            for k, v in self.qubits.items()])\n        if self.get_edges():\n            s += \"\\nedges: \"\n            s += \", \".join(\n                [\"%s[%d]-%s[%d]\" % (e[0][0], e[0][1], e[1][0], e[1][1])\n                 for e in self.get_edges()])\n        return s\n", "path": "qiskit/mapper/_coupling.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n\n# Copyright 2017, IBM.\n#\n# This source code is licensed under the Apache License, Version 2.0 found in\n# the LICENSE.txt file in the root directory of this source tree.\n\n\"\"\"\nDirected graph object for representing coupling between qubits.\n\nThe nodes of the graph correspond to named qubits and the directed edges\nindicate which qubits are coupled and the permitted direction of CNOT gates.\nThe object has a distance function that can be used to map quantum circuits\nonto a device with this coupling.\n\"\"\"\nfrom collections import OrderedDict\nimport networkx as nx\nfrom ._couplingerror import CouplingError\n\n\ndef coupling_dict2list(couplingdict):\n    \"\"\"Convert coupling map dictionary into list.\n\n    Example dictionary format: {0: [1, 2], 1: [2]}\n    Example list format: [[0, 1], [0, 2], [1, 2]]\n\n    We do not do any checking of the input.\n\n    Return coupling map in list format.\n    \"\"\"\n    if not couplingdict:\n        return None\n    couplinglist = []\n    for ctl, tgtlist in couplingdict.items():\n        for tgt in tgtlist:\n            couplinglist.append([ctl, tgt])\n    return couplinglist\n\n\ndef coupling_list2dict(couplinglist):\n    \"\"\"Convert coupling map list into dictionary.\n\n    Example list format: [[0, 1], [0, 2], [1, 2]]\n    Example dictionary format: {0: [1, 2], 1: [2]}\n\n    We do not do any checking of the input.\n\n    Return coupling map in dict format.\n    \"\"\"\n    if not couplinglist:\n        return None\n    couplingdict = {}\n    for pair in couplinglist:\n        if pair[0] in couplingdict:\n            couplingdict[pair[0]].append(pair[1])\n        else:\n            couplingdict[pair[0]] = [pair[1]]\n    return couplingdict\n\n\nclass Coupling:\n    \"\"\"\n    Directed graph specifying fixed coupling.\n\n    Nodes correspond to qubits and directed edges correspond to permitted\n    CNOT gates\n    \"\"\"\n\n    # pylint: disable=invalid-name\n\n    def __init__(self, couplingdict=None):\n        \"\"\"\n        Create coupling graph.\n\n        By default, the coupling graph has no nodes. The optional couplingdict\n        specifies the graph as an adjacency list. For example,\n        couplingdict = {0: [1, 2], 1: [2]}.\n        \"\"\"\n        # self.qubits is dict from qubit (regname,idx) tuples to node indices\n        self.qubits = OrderedDict()\n        # self.index_to_qubit is a dict from node indices to qubits\n        self.index_to_qubit = {}\n        # self.node_counter is integer counter for labeling nodes\n        self.node_counter = 0\n        # self.G is the coupling digraph\n        self.G = nx.DiGraph()\n        # self.dist is a dict of dicts from node pairs to distances\n        # it must be computed, it is the distance on the digraph\n        self.dist = None\n        # Add edges to the graph if the couplingdict is present\n        if couplingdict is not None:\n            for v0, alist in couplingdict.items():\n                for v1 in alist:\n                    regname = \"q\"\n                    self.add_edge((regname, v0), (regname, v1))\n            self.compute_distance()\n\n    def size(self):\n        \"\"\"Return the number of qubits in this graph.\"\"\"\n        return len(self.qubits)\n\n    def get_qubits(self):\n        \"\"\"Return the qubits in this graph as a sorted (qreg, index) tuples.\"\"\"\n        return sorted(list(self.qubits.keys()))\n\n    def get_edges(self):\n        \"\"\"Return a list of edges in the coupling graph.\n\n        Each edge is a pair of qubits and each qubit is a tuple (qreg, index).\n        \"\"\"\n        return list(map(lambda x: (self.index_to_qubit[x[0]],\n                                   self.index_to_qubit[x[1]]), self.G.edges()))\n\n    def add_qubit(self, name):\n        \"\"\"\n        Add a qubit to the coupling graph.\n\n        name = tuple (regname, idx) for qubit\n        \"\"\"\n        if name in self.qubits:\n            raise CouplingError(\"%s already in coupling graph\" % name)\n        if not isinstance(name, tuple):\n            raise CouplingError(\"name %s is not a tuple\")\n        if not (isinstance(name[0], str) and isinstance(name[1], int)):\n            raise CouplingError(\"name %s is not of the right form, it must\"\n                                \" be: (regname, idx)\")\n\n        self.node_counter += 1\n        self.G.add_node(self.node_counter)\n        self.G.node[self.node_counter][\"name\"] = name\n        self.qubits[name] = self.node_counter\n        self.index_to_qubit[self.node_counter] = name\n\n    def add_edge(self, s_name, d_name):\n        \"\"\"\n        Add directed edge to coupling graph.\n\n        s_name = source qubit tuple\n        d_name = destination qubit tuple\n        \"\"\"\n        if s_name not in self.qubits:\n            self.add_qubit(s_name)\n        if d_name not in self.qubits:\n            self.add_qubit(d_name)\n        self.G.add_edge(self.qubits[s_name], self.qubits[d_name])\n\n    def connected(self):\n        \"\"\"\n        Test if the graph is connected.\n\n        Return True if connected, False otherwise\n        \"\"\"\n        try:\n            return nx.is_weakly_connected(self.G)\n        except nx.exception.NetworkXException:\n            return False\n\n    def compute_distance(self):\n        \"\"\"\n        Compute the undirected distance function on pairs of nodes.\n\n        The distance map self.dist is computed from the graph using\n        all_pairs_shortest_path_length.\n        \"\"\"\n        if not self.connected():\n            raise CouplingError(\"coupling graph not connected\")\n        lengths = dict(nx.all_pairs_shortest_path_length(self.G.to_undirected()))\n        self.dist = {}\n        for i in self.qubits.keys():\n            self.dist[i] = {}\n            for j in self.qubits.keys():\n                self.dist[i][j] = lengths[self.qubits[i]][self.qubits[j]]\n\n    def distance(self, q1, q2):\n        \"\"\"Return the undirected distance between qubit q1 to qubit q2.\"\"\"\n        if self.dist is None:\n            raise CouplingError(\"distance has not been computed\")\n        if q1 not in self.qubits:\n            raise CouplingError(\"%s not in coupling graph\" % (q1,))\n        if q2 not in self.qubits:\n            raise CouplingError(\"%s not in coupling graph\" % (q2,))\n        return self.dist[q1][q2]\n\n    def __str__(self):\n        \"\"\"Return a string representation of the coupling graph.\"\"\"\n        s = \"\"\n        if self.qubits:\n            s += \"qubits: \"\n            s += \", \".join([\"%s[%d] @ %d\" % (k[0], k[1], v)\n                            for k, v in self.qubits.items()])\n        if self.get_edges():\n            s += \"\\nedges: \"\n            s += \", \".join(\n                [\"%s[%d]-%s[%d]\" % (e[0][0], e[0][1], e[1][0], e[1][1])\n                 for e in self.get_edges()])\n        return s\n", "path": "qiskit/mapper/_coupling.py"}]}