@@ -120,7 +120,7 @@ def rename_register(self, regname, newname):
120
120
self .output_map [(newname , i )] = self .output_map [(regname , i )]
121
121
self .output_map .pop ((regname , i ), None )
122
122
# n node d = data
123
- for _ , d in self .multi_graph .nodes_iter (data = True ):
123
+ for _ , d in self .multi_graph .nodes (data = True ):
124
124
if d ["type" ] == "in" or d ["type" ] == "out" :
125
125
if d ["name" ][0 ] == regname :
126
126
d ["name" ] = (newname , d ["name" ][1 ])
@@ -141,7 +141,7 @@ def rename_register(self, regname, newname):
141
141
if d ["condition" ][0 ] == regname :
142
142
d ["condition" ] = (newname , d ["condition" ][1 ])
143
143
# eX = edge, d= data
144
- for _ , _ , d in self .multi_graph .edges_iter (data = True ):
144
+ for _ , _ , d in self .multi_graph .edges (data = True ):
145
145
if d ["name" ][0 ] == regname :
146
146
d ["name" ] = (newname , d ["name" ][1 ])
147
147
@@ -195,7 +195,7 @@ def _add_wire(self, name, isClassical=False):
195
195
self .multi_graph .node [out_node ]["type" ] = "out"
196
196
self .multi_graph .node [in_node ]["name" ] = name
197
197
self .multi_graph .node [out_node ]["name" ] = name
198
- self .multi_graph .edge [in_node ][out_node ][0 ]["name" ] = name
198
+ self .multi_graph .adj [in_node ][out_node ][0 ]["name" ] = name
199
199
else :
200
200
raise DAGCircuitError ("duplicate wire %s" % name )
201
201
@@ -369,7 +369,7 @@ def apply_operation_back(self, name, qargs, cargs=None, params=None,
369
369
# and adding new edges from the operation node to each output node
370
370
al = [qargs , all_cbits ]
371
371
for q in itertools .chain (* al ):
372
- ie = self .multi_graph .predecessors (self .output_map [q ])
372
+ ie = list ( self .multi_graph .predecessors (self .output_map [q ]) )
373
373
assert len (ie ) == 1 , "output node has multiple in-edges"
374
374
self .multi_graph .add_edge (ie [0 ], self .node_counter , name = q )
375
375
self .multi_graph .remove_edge (ie [0 ], self .output_map [q ])
@@ -581,8 +581,7 @@ def compose_back(self, input_circuit, wire_map=None):
581
581
# Compose
582
582
self .basis = union_basis
583
583
self .gates = union_gates
584
- topological_sort = nx .topological_sort (input_circuit .multi_graph )
585
- for node in topological_sort :
584
+ for node in nx .topological_sort (input_circuit .multi_graph ):
586
585
nd = input_circuit .multi_graph .node [node ]
587
586
if nd ["type" ] == "in" :
588
587
# if in wire_map, get new name, else use existing name
@@ -640,8 +639,7 @@ def compose_front(self, input_circuit, wire_map=None):
640
639
# Compose
641
640
self .basis = union_basis
642
641
self .gates = union_gates
643
- ts = nx .topological_sort (input_circuit .multi_graph , reverse = True )
644
- for n in ts :
642
+ for n in reversed (list (nx .topological_sort (input_circuit .multi_graph ))):
645
643
nd = input_circuit .multi_graph .node [n ]
646
644
if nd ["type" ] == "out" :
647
645
# if in wire_map, get new name, else use existing name
@@ -769,8 +767,7 @@ def qasm(self, decls_only=False, add_swap=False,
769
767
out += "gate swap a,b { cx a,b; cx b,a; cx a,b; }\n "
770
768
# Write the instructions
771
769
if not decls_only :
772
- ts = nx .topological_sort (self .multi_graph )
773
- for n in ts :
770
+ for n in nx .topological_sort (self .multi_graph ):
774
771
nd = self .multi_graph .node [n ]
775
772
if nd ["type" ] == "op" :
776
773
if nd ["condition" ] is not None :
@@ -843,9 +840,9 @@ def _make_pred_succ_maps(self, n):
843
840
nodes for the operation node n in self.multi_graph.
844
841
"""
845
842
pred_map = {e [2 ]['name' ]: e [0 ] for e in
846
- self .multi_graph .in_edges_iter (nbunch = n , data = True )}
843
+ self .multi_graph .in_edges (nbunch = n , data = True )}
847
844
succ_map = {e [2 ]['name' ]: e [1 ] for e in
848
- self .multi_graph .out_edges_iter (nbunch = n , data = True )}
845
+ self .multi_graph .out_edges (nbunch = n , data = True )}
849
846
return pred_map , succ_map
850
847
851
848
def _full_pred_succ_maps (self , pred_map , succ_map , input_circuit ,
@@ -874,7 +871,7 @@ def _full_pred_succ_maps(self, pred_map, succ_map, input_circuit,
874
871
full_succ_map [w ] = self .output_map [w ]
875
872
full_pred_map [w ] = self .multi_graph .predecessors (
876
873
self .output_map [w ])[0 ]
877
- assert len (self .multi_graph .predecessors (self .output_map [w ])) == 1 ,\
874
+ assert len (list ( self .multi_graph .predecessors (self .output_map [w ]) )) == 1 ,\
878
875
"too many predecessors for (%s,%d) output node" % (
879
876
w [0 ], w [1 ])
880
877
return full_pred_map , full_succ_map
@@ -913,8 +910,7 @@ def substitute_circuit_all(self, name, input_circuit, wires=None):
913
910
# that we add from the input_circuit.
914
911
self .basis = union_basis
915
912
self .gates = union_gates
916
- ts = nx .topological_sort (self .multi_graph )
917
- for n in ts :
913
+ for n in nx .topological_sort (self .multi_graph ):
918
914
nd = self .multi_graph .node [n ]
919
915
if nd ["type" ] == "op" and nd ["name" ] == name :
920
916
if nd ["condition" ] is None :
@@ -930,8 +926,7 @@ def substitute_circuit_all(self, name, input_circuit, wires=None):
930
926
# Now that we know the connections, delete node
931
927
self .multi_graph .remove_node (n )
932
928
# Iterate over nodes of input_circuit
933
- tsin = nx .topological_sort (input_circuit .multi_graph )
934
- for m in tsin :
929
+ for m in nx .topological_sort (input_circuit .multi_graph ):
935
930
md = input_circuit .multi_graph .node [m ]
936
931
if md ["type" ] == "op" :
937
932
# Insert a new node
@@ -955,8 +950,8 @@ def substitute_circuit_all(self, name, input_circuit, wires=None):
955
950
for w in full_pred_map :
956
951
self .multi_graph .add_edge (full_pred_map [w ], full_succ_map [w ],
957
952
name = w )
958
- o_pred = self .multi_graph .predecessors (
959
- self .output_map [w ])
953
+ o_pred = list ( self .multi_graph .predecessors (
954
+ self .output_map [w ]))
960
955
if len (o_pred ) > 1 :
961
956
assert len (o_pred ) == 2 , \
962
957
"expected 2 predecessors here"
@@ -1023,8 +1018,7 @@ def substitute_circuit_one(self, node, input_circuit, wires=None):
1023
1018
# Now that we know the connections, delete node
1024
1019
self .multi_graph .remove_node (node )
1025
1020
# Iterate over nodes of input_circuit
1026
- tsin = nx .topological_sort (input_circuit .multi_graph )
1027
- for m in tsin :
1021
+ for m in nx .topological_sort (input_circuit .multi_graph ):
1028
1022
md = input_circuit .multi_graph .node [m ]
1029
1023
if md ["type" ] == "op" :
1030
1024
# Insert a new node
@@ -1049,7 +1043,7 @@ def substitute_circuit_one(self, node, input_circuit, wires=None):
1049
1043
for w in full_pred_map :
1050
1044
self .multi_graph .add_edge (
1051
1045
full_pred_map [w ], full_succ_map [w ], name = w )
1052
- o_pred = self .multi_graph .predecessors (self .output_map [w ])
1046
+ o_pred = list ( self .multi_graph .predecessors (self .output_map [w ]) )
1053
1047
if len (o_pred ) > 1 :
1054
1048
assert len (o_pred ) == 2 , "expected 2 predecessors here"
1055
1049
p = [x for x in o_pred if x != full_pred_map [w ]]
@@ -1064,8 +1058,7 @@ def get_named_nodes(self, name):
1064
1058
% name )
1065
1059
1066
1060
# Iterate through the nodes of self in topological order
1067
- ts = nx .topological_sort (self .multi_graph )
1068
- for n in ts :
1061
+ for n in nx .topological_sort (self .multi_graph ):
1069
1062
nd = self .multi_graph .node [n ]
1070
1063
if nd ["type" ] == "op" and nd ["name" ] == name :
1071
1064
nlist .append (n )
@@ -1213,8 +1206,7 @@ def serial_layers(self):
1213
1206
same structure as in layers().
1214
1207
"""
1215
1208
layers_list = []
1216
- ts = nx .topological_sort (self .multi_graph )
1217
- for n in ts :
1209
+ for n in nx .topological_sort (self .multi_graph ):
1218
1210
nxt_nd = self .multi_graph .node [n ]
1219
1211
if nxt_nd ["type" ] == "op" :
1220
1212
new_layer = DAGCircuit ()
@@ -1260,21 +1252,21 @@ def collect_runs(self, namelist):
1260
1252
# Iterate through the nodes of self in topological order
1261
1253
# and form tuples containing sequences of gates
1262
1254
# on the same qubit(s).
1263
- ts = nx .topological_sort (self .multi_graph )
1255
+ ts = list ( nx .topological_sort (self .multi_graph ) )
1264
1256
nodes_seen = dict (zip (ts , [False ] * len (ts )))
1265
1257
for node in ts :
1266
1258
nd = self .multi_graph .node [node ]
1267
1259
if nd ["type" ] == "op" and nd ["name" ] in namelist \
1268
1260
and not nodes_seen [node ]:
1269
1261
group = [node ]
1270
1262
nodes_seen [node ] = True
1271
- s = self .multi_graph .successors (node )
1263
+ s = list ( self .multi_graph .successors (node ) )
1272
1264
while len (s ) == 1 and \
1273
1265
self .multi_graph .node [s [0 ]]["type" ] == "op" and \
1274
1266
self .multi_graph .node [s [0 ]]["name" ] in namelist :
1275
1267
group .append (s [0 ])
1276
1268
nodes_seen [s [0 ]] = True
1277
- s = self .multi_graph .successors (s [0 ])
1269
+ s = list ( self .multi_graph .successors (s [0 ]) )
1278
1270
if len (group ) > 1 :
1279
1271
group_list .append (tuple (group ))
1280
1272
return set (group_list )
@@ -1285,8 +1277,7 @@ def count_ops(self):
1285
1277
Returns a dictionary of counts keyed on the operation name.
1286
1278
"""
1287
1279
op_dict = {}
1288
- ts = nx .topological_sort (self .multi_graph )
1289
- for node in ts :
1280
+ for node in nx .topological_sort (self .multi_graph ):
1290
1281
nd = self .multi_graph .node [node ]
1291
1282
name = nd ["name" ]
1292
1283
if nd ["type" ] == "op" :
0 commit comments