forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coupling.py
535 lines (483 loc) · 16.1 KB
/
test_coupling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
# pylint: disable=missing-docstring
import unittest
import numpy as np
import rustworkx as rx
from qiskit.transpiler import CouplingMap
from qiskit.transpiler.exceptions import CouplingError
from qiskit.providers.fake_provider import FakeRueschlikon
from qiskit.test import QiskitTestCase
from qiskit.utils import optionals
from ..visualization.visualization import QiskitVisualizationTestCase, path_to_diagram_reference
class CouplingTest(QiskitTestCase):
def test_empty_coupling_class(self):
coupling = CouplingMap()
self.assertEqual(0, coupling.size())
self.assertEqual([], coupling.physical_qubits)
self.assertEqual([], coupling.get_edges())
self.assertFalse(coupling.is_connected())
self.assertEqual("", str(coupling))
def test_coupling_str(self):
coupling_list = [[0, 1], [0, 2], [1, 2]]
coupling = CouplingMap(coupling_list)
expected = "[[0, 1], [0, 2], [1, 2]]"
self.assertEqual(expected, str(coupling))
def test_coupling_distance(self):
coupling_list = [(0, 1), (0, 2), (1, 2)]
coupling = CouplingMap(coupling_list)
self.assertTrue(coupling.is_connected())
physical_qubits = coupling.physical_qubits
result = coupling.distance(physical_qubits[0], physical_qubits[1])
self.assertIsInstance(result, int)
self.assertEqual(1, result)
def test_add_physical_qubits(self):
coupling = CouplingMap()
self.assertEqual("", str(coupling))
coupling.add_physical_qubit(0)
self.assertEqual([0], coupling.physical_qubits)
self.assertEqual("", str(coupling))
def test_add_physical_qubits_not_int(self):
coupling = CouplingMap()
self.assertRaises(CouplingError, coupling.add_physical_qubit, "q")
def test_add_edge(self):
coupling = CouplingMap()
self.assertEqual("", str(coupling))
coupling.add_edge(0, 1)
expected = "[[0, 1]]"
self.assertEqual(expected, str(coupling))
def test_neighbors(self):
"""Test neighboring qubits are found correctly."""
coupling = CouplingMap([[0, 1], [0, 2], [1, 0]])
physical_qubits = coupling.physical_qubits
self.assertEqual(set(coupling.neighbors(physical_qubits[0])), {1, 2})
self.assertEqual(set(coupling.neighbors(physical_qubits[1])), {0})
self.assertEqual(set(coupling.neighbors(physical_qubits[2])), set())
def test_distance_error(self):
"""Test distance between unconnected physical_qubits."""
graph = CouplingMap()
graph.add_physical_qubit(0)
graph.add_physical_qubit(1)
self.assertRaises(CouplingError, graph.distance, 0, 1)
def test_distance_self_loop(self):
"""Test distance between the same physical qubit."""
graph = CouplingMap()
graph.add_physical_qubit(0)
graph.add_physical_qubit(1)
self.assertEqual(0.0, graph.distance(0, 0))
def test_init_with_couplinglist(self):
coupling_list = [[0, 1], [1, 2]]
coupling = CouplingMap(coupling_list)
qubits_expected = [0, 1, 2]
edges_expected = [(0, 1), (1, 2)]
self.assertEqual(coupling.physical_qubits, qubits_expected)
self.assertEqual(coupling.get_edges(), edges_expected)
self.assertEqual(2, coupling.distance(0, 2))
def test_successful_reduced_map(self):
"""Generate a reduced map"""
fake = FakeRueschlikon()
cmap = fake.configuration().coupling_map
coupling_map = CouplingMap(cmap)
out = coupling_map.reduce([12, 11, 10, 9]).get_edges()
ans = [(1, 2), (3, 2), (0, 1)]
self.assertEqual(set(out), set(ans))
def test_bad_reduced_map(self):
"""Generate disconnected reduced map"""
fake = FakeRueschlikon()
cmap = fake.configuration().coupling_map
coupling_map = CouplingMap(cmap)
with self.assertRaises(CouplingError):
coupling_map.reduce([12, 11, 10, 3])
def test_disconnected_reduced_map_allowed(self):
"""Generate disconnected reduced map but do not error"""
fake = FakeRueschlikon()
cmap = fake.configuration().coupling_map
coupling_map = CouplingMap(cmap)
reduced_map = coupling_map.reduce([12, 11, 10, 3], check_if_connected=False)
reduced_edges = reduced_map.get_edges()
qubits_expected = [0, 1, 2, 3]
edges_expected = [(0, 1), (1, 2)]
self.assertEqual(qubits_expected, reduced_map.physical_qubits)
self.assertEqual(set(reduced_edges), set(edges_expected))
def test_symmetric_small_true(self):
coupling_list = [[0, 1], [1, 0]]
coupling = CouplingMap(coupling_list)
self.assertTrue(coupling.is_symmetric)
def test_symmetric_big_false(self):
coupling_list = [
[1, 0],
[1, 2],
[2, 3],
[4, 3],
[4, 10],
[5, 4],
[5, 6],
[5, 9],
[6, 8],
[9, 8],
[9, 10],
[7, 8],
[11, 3],
[11, 10],
[11, 12],
[12, 2],
[13, 1],
[13, 12],
]
coupling = CouplingMap(coupling_list)
self.assertFalse(coupling.is_symmetric)
def test_make_symmetric(self):
coupling_list = [[0, 1], [0, 2]]
coupling = CouplingMap(coupling_list)
coupling.make_symmetric()
edges = coupling.get_edges()
self.assertEqual(set(edges), {(0, 1), (0, 2), (2, 0), (1, 0)})
def test_full_factory(self):
coupling = CouplingMap.from_full(4)
edges = coupling.get_edges()
expected = [
(0, 1),
(0, 2),
(0, 3),
(1, 0),
(1, 2),
(1, 3),
(2, 0),
(2, 1),
(2, 3),
(3, 0),
(3, 1),
(3, 2),
]
self.assertEqual(set(edges), set(expected))
def test_line_factory(self):
coupling = CouplingMap.from_line(4)
edges = coupling.get_edges()
expected = [(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2)]
self.assertEqual(set(edges), set(expected))
def test_grid_factory(self):
coupling = CouplingMap.from_grid(2, 3)
edges = coupling.get_edges()
expected = [
(0, 3),
(0, 1),
(3, 0),
(3, 4),
(1, 0),
(1, 4),
(1, 2),
(4, 1),
(4, 3),
(4, 5),
(2, 1),
(2, 5),
(5, 2),
(5, 4),
]
self.assertEqual(set(edges), set(expected))
def test_grid_factory_unidirectional(self):
coupling = CouplingMap.from_grid(2, 3, bidirectional=False)
edges = coupling.get_edges()
expected = [(0, 3), (0, 1), (3, 4), (1, 4), (1, 2), (4, 5), (2, 5)]
self.assertEqual(set(edges), set(expected))
def test_heavy_hex_factory(self):
coupling = CouplingMap.from_heavy_hex(3, bidirectional=False)
edges = coupling.get_edges()
expected = [
(0, 9),
(0, 13),
(1, 13),
(1, 14),
(2, 14),
(3, 9),
(3, 15),
(4, 15),
(4, 16),
(5, 12),
(5, 16),
(6, 17),
(7, 17),
(7, 18),
(8, 12),
(8, 18),
(10, 14),
(10, 16),
(11, 15),
(11, 17),
]
self.assertEqual(set(edges), set(expected))
def test_heavy_hex_factory_bidirectional(self):
coupling = CouplingMap.from_heavy_hex(3, bidirectional=True)
edges = coupling.get_edges()
expected = [
(0, 9),
(0, 13),
(1, 13),
(1, 14),
(2, 14),
(3, 9),
(3, 15),
(4, 15),
(4, 16),
(5, 12),
(5, 16),
(6, 17),
(7, 17),
(7, 18),
(8, 12),
(8, 18),
(9, 0),
(9, 3),
(10, 14),
(10, 16),
(11, 15),
(11, 17),
(12, 5),
(12, 8),
(13, 0),
(13, 1),
(14, 1),
(14, 2),
(14, 10),
(15, 3),
(15, 4),
(15, 11),
(16, 4),
(16, 5),
(16, 10),
(17, 6),
(17, 7),
(17, 11),
(18, 7),
(18, 8),
]
self.assertEqual(set(edges), set(expected))
def test_heavy_square_factory(self):
coupling = CouplingMap.from_heavy_square(3, bidirectional=False)
edges = coupling.get_edges()
expected = [
(0, 15),
(1, 16),
(2, 11),
(3, 12),
(3, 17),
(4, 18),
(5, 11),
(6, 12),
(6, 19),
(7, 20),
(9, 15),
(9, 17),
(10, 16),
(10, 18),
(13, 17),
(13, 19),
(14, 18),
(14, 20),
(15, 1),
(16, 2),
(17, 4),
(18, 5),
(19, 7),
(20, 8),
]
self.assertEqual(set(edges), set(expected))
def test_heavy_square_factory_bidirectional(self):
coupling = CouplingMap.from_heavy_square(3, bidirectional=True)
edges = coupling.get_edges()
expected = [
(0, 15),
(1, 15),
(1, 16),
(2, 11),
(2, 16),
(3, 12),
(3, 17),
(4, 17),
(4, 18),
(5, 11),
(5, 18),
(6, 12),
(6, 19),
(7, 19),
(7, 20),
(8, 20),
(9, 15),
(9, 17),
(10, 16),
(10, 18),
(11, 2),
(11, 5),
(12, 3),
(12, 6),
(13, 17),
(13, 19),
(14, 18),
(14, 20),
(15, 0),
(15, 1),
(15, 9),
(16, 1),
(16, 2),
(16, 10),
(17, 3),
(17, 4),
(17, 9),
(17, 13),
(18, 4),
(18, 5),
(18, 10),
(18, 14),
(19, 6),
(19, 7),
(19, 13),
(20, 7),
(20, 8),
(20, 14),
]
self.assertEqual(set(edges), set(expected))
def test_hexagonal_lattice_2_2_factory(self):
coupling = CouplingMap.from_hexagonal_lattice(2, 2, bidirectional=False)
edges = coupling.get_edges()
expected = [
(0, 1),
(1, 2),
(2, 3),
(3, 4),
(5, 6),
(6, 7),
(7, 8),
(8, 9),
(9, 10),
(11, 12),
(12, 13),
(13, 14),
(14, 15),
(0, 5),
(2, 7),
(4, 9),
(6, 11),
(8, 13),
(10, 15),
]
self.assertEqual(set(edges), set(expected))
def test_hexagonal_lattice_2_2_factory_bidirectional(self):
coupling = CouplingMap.from_hexagonal_lattice(2, 2, bidirectional=True)
edges = coupling.get_edges()
expected = [
(0, 1),
(1, 0),
(1, 2),
(2, 1),
(2, 3),
(3, 2),
(3, 4),
(4, 3),
(5, 6),
(6, 5),
(6, 7),
(7, 6),
(7, 8),
(8, 7),
(8, 9),
(9, 8),
(9, 10),
(10, 9),
(11, 12),
(12, 11),
(12, 13),
(13, 12),
(13, 14),
(14, 13),
(14, 15),
(15, 14),
(0, 5),
(5, 0),
(2, 7),
(7, 2),
(4, 9),
(9, 4),
(6, 11),
(11, 6),
(8, 13),
(13, 8),
(10, 15),
(15, 10),
]
self.assertEqual(set(edges), set(expected))
def test_implements_iter(self):
"""Test that the object is implicitly iterable."""
coupling = CouplingMap.from_line(3)
expected = [(0, 1), (1, 0), (1, 2), (2, 1)]
self.assertEqual(sorted(coupling), expected)
def test_disjoint_coupling_map(self):
cmap = CouplingMap([[0, 1], [1, 0], [2, 3], [3, 2]])
self.assertFalse(cmap.is_connected())
distance_matrix = cmap.distance_matrix
expected = np.array(
[
[0, 1, np.inf, np.inf],
[1, 0, np.inf, np.inf],
[np.inf, np.inf, 0, 1],
[np.inf, np.inf, 1, 0],
]
)
np.testing.assert_array_equal(expected, distance_matrix)
def test_disjoint_coupling_map_distance_no_path_qubits(self):
cmap = CouplingMap([[0, 1], [1, 0], [2, 3], [3, 2]])
self.assertFalse(cmap.is_connected())
with self.assertRaises(CouplingError):
cmap.distance(0, 3)
def test_component_mapping(self):
cmap = CouplingMap([[0, 1], [1, 0], [2, 3], [3, 2]])
components = cmap.connected_components()
self.assertEqual(components[1].graph[0], 2)
self.assertEqual(components[1].graph[1], 3)
self.assertEqual(components[0].graph[0], 0)
self.assertEqual(components[0].graph[1], 1)
def test_components_connected_graph(self):
cmap = CouplingMap.from_line(5)
self.assertTrue(cmap.is_connected())
subgraphs = cmap.connected_components()
self.assertEqual(len(subgraphs), 1)
self.assertTrue(rx.is_isomorphic(cmap.graph, subgraphs[0].graph))
def test_components_disconnected_graph(self):
cmap = CouplingMap([[0, 1], [1, 2], [3, 4], [4, 5]])
self.assertFalse(cmap.is_connected())
subgraphs = cmap.connected_components()
self.assertEqual(len(subgraphs), 2)
expected_subgraph = CouplingMap([[0, 1], [1, 2]])
self.assertTrue(rx.is_isomorphic(expected_subgraph.graph, subgraphs[0].graph))
self.assertTrue(rx.is_isomorphic(expected_subgraph.graph, subgraphs[1].graph))
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")
@unittest.skipUnless(optionals.HAS_PIL, "Pillow not installed")
def test_coupling_draw(self):
"""Test that the coupling map drawing with respect to the reference file is correct."""
cmap = CouplingMap([[0, 1], [1, 2], [2, 3], [2, 4], [2, 5], [2, 6]])
image_ref = path_to_diagram_reference("coupling_map.png")
image = cmap.draw()
self.assertImagesAreEqual(image, image_ref, diff_tolerance=0.01)