-
Notifications
You must be signed in to change notification settings - Fork 982
/
named_topologies_test.py
118 lines (95 loc) · 3.72 KB
/
named_topologies_test.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
# Copyright 2021 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from unittest.mock import MagicMock
import cirq
import networkx as nx
import pytest
from cirq import (
draw_gridlike,
LineTopology,
TiltedSquareLattice,
get_placements,
draw_placements,
)
@pytest.mark.parametrize('width, height', list(itertools.product([1, 2, 3, 24], repeat=2)))
def test_tilted_square_lattice(width, height):
topo = TiltedSquareLattice(width, height)
assert topo.graph.number_of_edges() == width * height
assert all(1 <= topo.graph.degree[node] <= 4 for node in topo.graph.nodes)
assert topo.name == f'tilted-square-lattice-{width}-{height}'
assert topo.n_nodes == topo.graph.number_of_nodes()
assert nx.is_connected(topo.graph)
assert nx.algorithms.planarity.check_planarity(topo.graph)
def test_bad_tilted_square_lattice():
with pytest.raises(ValueError):
_ = TiltedSquareLattice(0, 3)
with pytest.raises(ValueError):
_ = TiltedSquareLattice(3, 0)
def test_tilted_square_methods():
topo = TiltedSquareLattice(5, 5)
ax = MagicMock()
topo.draw(ax=ax)
ax.scatter.assert_called()
qubits = topo.nodes_as_gridqubits()
assert all(isinstance(q, cirq.GridQubit) for q in qubits)
def test_tilted_square_lattice_n_nodes():
for width, height in itertools.product(list(range(1, 4 + 1)), repeat=2):
topo = TiltedSquareLattice(width, height)
assert topo.n_nodes == topo.graph.number_of_nodes()
def test_line_topology():
n = 10
topo = LineTopology(n)
assert topo.n_nodes == n
assert topo.n_nodes == topo.graph.number_of_nodes()
assert all(1 <= topo.graph.degree[node] <= 2 for node in topo.graph.nodes)
assert topo.name == 'line-10'
ax = MagicMock()
topo.draw(ax=ax)
ax.scatter.assert_called()
with pytest.raises(ValueError, match='greater than 1.*'):
_ = LineTopology(1)
assert LineTopology(2).n_nodes == 2
assert LineTopology(2).graph.number_of_nodes() == 2
def test_line_topology_nodes_as_qubits():
for n in range(2, 10, 2):
assert LineTopology(n).nodes_as_linequbits() == cirq.LineQubit.range(n)
@pytest.mark.parametrize('tilted', [True, False])
def test_draw_gridlike(tilted):
graph = nx.grid_2d_graph(3, 3)
ax = MagicMock()
pos = draw_gridlike(graph, tilted=tilted, ax=ax)
ax.scatter.assert_called()
for (row, column), _ in pos.items():
assert 0 <= row < 3
assert 0 <= column < 3
@pytest.mark.parametrize('tilted', [True, False])
def test_draw_gridlike_qubits(tilted):
graph = nx.grid_2d_graph(3, 3)
graph = nx.relabel_nodes(graph, {(r, c): cirq.GridQubit(r, c) for r, c in sorted(graph.nodes)})
ax = MagicMock()
pos = draw_gridlike(graph, tilted=tilted, ax=ax)
ax.scatter.assert_called()
for q, _ in pos.items():
assert 0 <= q.row < 3
assert 0 <= q.col < 3
def test_get_placements():
topo = TiltedSquareLattice(4, 2)
syc23 = TiltedSquareLattice(8, 4).graph
placements = get_placements(syc23, topo.graph)
assert len(placements) == 12
axes = [MagicMock() for _ in range(4)]
draw_placements(syc23, topo.graph, placements[::3], axes=axes)
for ax in axes:
ax.scatter.assert_called()