22
22
class TestDevice (cirq .Device ):
23
23
def __init__ (self ):
24
24
self .qubits = cirq .GridQubit .rect (2 , 8 )
25
+ neighbors = [(a , b ) for a in self .qubits for b in self .qubits if a .is_adjacent (b )]
26
+ self ._metadata = cirq .GridDeviceMetadata (neighbors , cirq .Gateset (cirq .H ))
27
+
28
+ @property
29
+ def metadata (self ):
30
+ return self ._metadata
25
31
26
32
27
33
def test_naive_qubit_placer ():
@@ -31,7 +37,7 @@ def test_naive_qubit_placer():
31
37
qubits , depth = 8 , two_qubit_op_factory = lambda a , b , _ : cirq .SQRT_ISWAP (a , b )
32
38
)
33
39
34
- assert all (q in cg .Sycamore23 .qubit_set () for q in circuit .all_qubits ())
40
+ assert all (q in cg .Sycamore23 .metadata . qubit_set for q in circuit .all_qubits ())
35
41
36
42
qp = cg .NaiveQubitPlacer ()
37
43
circuit2 , mapping = qp .place_circuit (
@@ -42,7 +48,7 @@ def test_naive_qubit_placer():
42
48
)
43
49
assert circuit is not circuit2
44
50
assert circuit == circuit2
45
- assert all (q in cg .Sycamore23 .qubit_set () for q in circuit2 .all_qubits ())
51
+ assert all (q in cg .Sycamore23 .metadata . qubit_set for q in circuit2 .all_qubits ())
46
52
for k , v in mapping .items ():
47
53
assert k == v
48
54
@@ -53,7 +59,7 @@ def test_random_device_placer_tilted_square_lattice():
53
59
circuit = cirq .experiments .random_rotations_between_grid_interaction_layers_circuit (
54
60
qubits , depth = 8 , two_qubit_op_factory = lambda a , b , _ : cirq .SQRT_ISWAP (a , b )
55
61
)
56
- assert not all (q in cg .Sycamore23 .qubit_set () for q in circuit .all_qubits ())
62
+ assert not all (q in cg .Sycamore23 .metadata . qubit_set for q in circuit .all_qubits ())
57
63
58
64
qp = cg .RandomDevicePlacer ()
59
65
circuit2 , mapping = qp .place_circuit (
@@ -64,7 +70,7 @@ def test_random_device_placer_tilted_square_lattice():
64
70
)
65
71
assert circuit is not circuit2
66
72
assert circuit != circuit2
67
- assert all (q in cg .Sycamore23 .qubit_set () for q in circuit2 .all_qubits ())
73
+ assert all (q in cg .Sycamore23 .metadata . qubit_set for q in circuit2 .all_qubits ())
68
74
for k , v in mapping .items ():
69
75
assert k != v
70
76
@@ -83,7 +89,7 @@ def test_random_device_placer_line():
83
89
)
84
90
assert circuit is not circuit2
85
91
assert circuit != circuit2
86
- assert all (q in cg .Sycamore23 .qubit_set () for q in circuit2 .all_qubits ())
92
+ assert all (q in cg .Sycamore23 .metadata . qubit_set for q in circuit2 .all_qubits ())
87
93
for k , v in mapping .items ():
88
94
assert k != v
89
95
@@ -120,3 +126,22 @@ def test_random_device_placer_small_device():
120
126
shared_rt_info = cg .SharedRuntimeInfo (run_id = '1' , device = TestDevice ()),
121
127
rs = np .random .RandomState (1 ),
122
128
)
129
+
130
+
131
+ def test_device_missing_metadata ():
132
+ class BadDevice (cirq .Device ):
133
+ pass
134
+
135
+ topo = cirq .TiltedSquareLattice (3 , 3 )
136
+ qubits = sorted (topo .nodes_to_gridqubits ().values ())
137
+ circuit = cirq .experiments .random_rotations_between_grid_interaction_layers_circuit (
138
+ qubits , depth = 8 , two_qubit_op_factory = lambda a , b , _ : cirq .SQRT_ISWAP (a , b )
139
+ )
140
+ qp = cg .RandomDevicePlacer ()
141
+ with pytest .raises (ValueError ):
142
+ qp .place_circuit (
143
+ circuit ,
144
+ problem_topology = topo ,
145
+ shared_rt_info = cg .SharedRuntimeInfo (run_id = '1' , device = BadDevice ()),
146
+ rs = np .random .RandomState (1 ),
147
+ )
0 commit comments