22
22
Iterator ,
23
23
Iterable ,
24
24
)
25
-
26
25
import networkx as nx
27
- from cirq import value
26
+ from cirq import _compat , value
28
27
from cirq .devices .grid_qubit import _BaseGridQid
29
28
from cirq .devices .line_qubit import _BaseLineQid
30
29
@@ -59,6 +58,10 @@ def qubit_set(self) -> Optional[AbstractSet['cirq.Qid']]:
59
58
# Default to the qubits being unknown.
60
59
return None
61
60
61
+ @_compat .deprecated (
62
+ deadline = 'v0.15' ,
63
+ fix = 'qubit coupling data can now be found in device.metadata if provided.' ,
64
+ )
62
65
def qid_pairs (self ) -> Optional [FrozenSet ['cirq.SymmetricalQidPair' ]]:
63
66
"""Returns a set of qubit edges on the device, if possible.
64
67
@@ -74,27 +77,28 @@ def qid_pairs(self) -> Optional[FrozenSet['cirq.SymmetricalQidPair']]:
74
77
`cirq.UnconstrainedDevice` has this property), then `None` is
75
78
returned.
76
79
"""
77
- qs = self .qubit_set ()
78
- if qs is None :
79
- return None
80
- if all (isinstance (q , _BaseGridQid ) for q in qs ):
81
- return frozenset (
82
- [
83
- SymmetricalQidPair (q , q2 )
84
- for q in [cast (_BaseGridQid , q ) for q in qs ]
85
- for q2 in [q + (0 , 1 ), q + (1 , 0 )]
86
- if q2 in qs
87
- ]
88
- )
89
- if all (isinstance (q , _BaseLineQid ) for q in qs ):
90
- return frozenset (
91
- [
92
- SymmetricalQidPair (q , q + 1 )
93
- for q in [cast (_BaseLineQid , q ) for q in qs ]
94
- if q + 1 in qs
95
- ]
96
- )
97
- return frozenset ([SymmetricalQidPair (q , q2 ) for q in qs for q2 in qs if q < q2 ])
80
+ with _compat .block_overlapping_deprecation ('device\\ .metadata' ):
81
+ qs = self .qubit_set ()
82
+ if qs is None :
83
+ return None
84
+ if all (isinstance (q , _BaseGridQid ) for q in qs ):
85
+ return frozenset (
86
+ [
87
+ SymmetricalQidPair (q , q2 )
88
+ for q in [cast (_BaseGridQid , q ) for q in qs ]
89
+ for q2 in [q + (0 , 1 ), q + (1 , 0 )]
90
+ if q2 in qs
91
+ ]
92
+ )
93
+ if all (isinstance (q , _BaseLineQid ) for q in qs ):
94
+ return frozenset (
95
+ [
96
+ SymmetricalQidPair (q , q + 1 )
97
+ for q in [cast (_BaseLineQid , q ) for q in qs ]
98
+ if q + 1 in qs
99
+ ]
100
+ )
101
+ return frozenset ([SymmetricalQidPair (q , q2 ) for q in qs for q2 in qs if q < q2 ])
98
102
99
103
def decompose_operation (self , operation : 'cirq.Operation' ) -> 'cirq.OP_TREE' :
100
104
"""Returns a device-valid decomposition for the given operation.
@@ -166,6 +170,10 @@ def can_add_operation_into_moment(
166
170
return not moment .operates_on (operation .qubits )
167
171
168
172
173
+ @_compat .deprecated_class (
174
+ deadline = 'v0.15' ,
175
+ fix = 'Qid coupling information can now be found in device.metadata if applicable.' ,
176
+ )
169
177
@value .value_equality
170
178
class SymmetricalQidPair :
171
179
def __init__ (self , qid1 : 'cirq.Qid' , qid2 : 'cirq.Qid' ):
@@ -204,8 +212,8 @@ class DeviceMetadata:
204
212
205
213
def __init__ (
206
214
self ,
207
- qubits : Optional [ Iterable ['cirq.Qid' ]] = None ,
208
- nx_graph : Optional [ 'nx.graph' ] = None ,
215
+ qubits : Iterable ['cirq.Qid' ],
216
+ nx_graph : 'nx.graph' ,
209
217
):
210
218
"""Construct a DeviceMetadata object.
211
219
@@ -216,16 +224,11 @@ def __init__(
216
224
directional coupling, undirected edges indicate bi-directional
217
225
coupling.
218
226
"""
219
- if qubits is not None :
220
- qubits = frozenset (qubits )
221
- self ._qubits_set : Optional [FrozenSet ['cirq.Qid' ]] = (
222
- None if qubits is None else frozenset (qubits )
223
- )
224
-
227
+ self ._qubits_set : FrozenSet ['cirq.Qid' ] = frozenset (qubits )
225
228
self ._nx_graph = nx_graph
226
229
227
230
@property
228
- def qubit_set (self ) -> Optional [ FrozenSet ['cirq.Qid' ] ]:
231
+ def qubit_set (self ) -> FrozenSet ['cirq.Qid' ]:
229
232
"""Returns a set of qubits on the device, if possible.
230
233
231
234
Returns:
@@ -234,7 +237,7 @@ def qubit_set(self) -> Optional[FrozenSet['cirq.Qid']]:
234
237
return self ._qubits_set
235
238
236
239
@property
237
- def nx_graph (self ) -> Optional [ 'nx.Graph' ] :
240
+ def nx_graph (self ) -> 'nx.Graph' :
238
241
"""Returns a nx.Graph where nodes are qubits and edges are couple-able qubits.
239
242
240
243
Returns:
@@ -243,31 +246,20 @@ def nx_graph(self) -> Optional['nx.Graph']:
243
246
return self ._nx_graph
244
247
245
248
def _value_equality_values_ (self ):
246
- graph_equality = None
247
- if self ._nx_graph is not None :
248
- graph_equality = (
249
- tuple (sorted (self ._nx_graph .nodes ())),
250
- tuple (sorted (self ._nx_graph .edges (data = 'directed' ))),
251
- )
249
+ graph_equality = (
250
+ tuple (sorted (self ._nx_graph .nodes ())),
251
+ tuple (sorted (self ._nx_graph .edges (data = 'directed' ))),
252
+ )
252
253
253
254
return self ._qubits_set , graph_equality
254
255
255
256
def _json_dict_ (self ):
256
- graph_payload = ''
257
- if self ._nx_graph is not None :
258
- graph_payload = nx .readwrite .json_graph .node_link_data (self ._nx_graph )
259
-
260
- qubits_payload = ''
261
- if self ._qubits_set is not None :
262
- qubits_payload = sorted (list (self ._qubits_set ))
257
+ graph_payload = nx .readwrite .json_graph .node_link_data (self ._nx_graph )
258
+ qubits_payload = sorted (list (self ._qubits_set ))
263
259
264
260
return {'qubits' : qubits_payload , 'nx_graph' : graph_payload }
265
261
266
262
@classmethod
267
263
def _from_json_dict_ (cls , qubits , nx_graph , ** kwargs ):
268
- if qubits == '' :
269
- qubits = None
270
- graph_obj = None
271
- if nx_graph != '' :
272
- graph_obj = nx .readwrite .json_graph .node_link_graph (nx_graph )
264
+ graph_obj = nx .readwrite .json_graph .node_link_graph (nx_graph )
273
265
return cls (qubits , graph_obj )
0 commit comments