Skip to content

Commit a860498

Browse files
committed
Fix node order in layers for consistency accross Python versions
1 parent b459480 commit a860498

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

projectq/cengines/_gate_manager.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _coffman_graham_ranking(dag):
9999
levels[node] = level
100100

101101
for layer in layers:
102-
layer.reverse()
102+
layer.sort(key=lambda node: node.node_id)
103103
return layers
104104

105105

@@ -346,7 +346,8 @@ def step(self):
346346

347347
class _DAGNodeBase(object):
348348
# pylint: disable=too-few-public-methods
349-
def __init__(self, cmd, *args):
349+
def __init__(self, node_id, cmd, *args):
350+
self.node_id = node_id
350351
self.logical_ids = frozenset(args)
351352
self.cmd = cmd
352353
self.compatible_successor_cmds = []
@@ -368,8 +369,8 @@ class _DAGNodeSingle(_DAGNodeBase):
368369
"""
369370

370371
# pylint: disable=too-few-public-methods
371-
def __init__(self, cmd, logical_id):
372-
super(_DAGNodeSingle, self).__init__(cmd, logical_id)
372+
def __init__(self, node_id, cmd, logical_id):
373+
super(_DAGNodeSingle, self).__init__(node_id, cmd, logical_id)
373374
self.logical_id = logical_id
374375

375376

@@ -380,8 +381,9 @@ class _DAGNodeDouble(_DAGNodeBase):
380381
"""
381382

382383
# pylint: disable=too-few-public-methods
383-
def __init__(self, cmd, logical_id0, logical_id1):
384-
super(_DAGNodeDouble, self).__init__(cmd, logical_id0, logical_id1)
384+
def __init__(self, node_id, cmd, logical_id0, logical_id1):
385+
super(_DAGNodeDouble, self).__init__(node_id, cmd, logical_id0,
386+
logical_id1)
385387
self.logical_id0 = logical_id0
386388
self.logical_id1 = logical_id1
387389

@@ -393,6 +395,7 @@ class CommandDAG(object):
393395
"""
394396
def __init__(self):
395397
self._dag = nx.DiGraph()
398+
self._node_id = 0
396399
self._logical_ids_in_diag = set()
397400
self.near_term_layer = []
398401

@@ -430,6 +433,7 @@ def clear(self):
430433
Remove all nodes from the DAG and all layers.
431434
"""
432435
self._dag.clear()
436+
self._node_id = 0
433437
self._logical_ids_in_diag = set()
434438
self.near_term_layer = []
435439

@@ -466,7 +470,8 @@ def add_command(self, cmd):
466470
self._back_layer[logical_ids[1]].append_compatible_cmd(cmd)
467471
return
468472

469-
new_node = _DAGNodeDouble(cmd, *logical_ids)
473+
new_node = _DAGNodeDouble(self._node_id, cmd, *logical_ids)
474+
self._node_id += 1
470475
self._dag.add_node(new_node)
471476

472477
if logical_id0_in_dag:
@@ -490,7 +495,8 @@ def add_command(self, cmd):
490495
logical_id_in_dag = logical_id in self._logical_ids_in_diag
491496

492497
if isinstance(cmd.gate, (AllocateQubitGate, DeallocateQubitGate)):
493-
new_node = _DAGNodeSingle(cmd, logical_id)
498+
new_node = _DAGNodeSingle(self._node_id, cmd, logical_id)
499+
self._node_id += 1
494500
self._dag.add_node(new_node)
495501

496502
if logical_id_in_dag:
@@ -502,7 +508,8 @@ def add_command(self, cmd):
502508
self._layers_up_to_date = False
503509
else:
504510
if not logical_id_in_dag:
505-
new_node = _DAGNodeSingle(cmd, logical_id)
511+
new_node = _DAGNodeSingle(self._node_id, cmd, logical_id)
512+
self._node_id += 1
506513
self._dag.add_node(new_node)
507514
self._logical_ids_in_diag.add(logical_id)
508515

@@ -582,7 +589,7 @@ def calculate_qubit_interaction_subgraphs(self, max_order=2):
582589
break
583590
else:
584591
continue # only executed if the inner loop did NOT break
585-
break # only executed if the inner loop DID break
592+
break # only executed if the inner loop DID break
586593

587594
return [
588595
sorted(graph.subgraph(g),

projectq/cengines/_gate_manager_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def generate_grid_graph(nrows, ncols):
7878
return graph
7979

8080

81-
def gen_cmd(*logical_ids, gate=X):
81+
def gen_cmd(*logical_ids, **kwargs):
82+
gate = kwargs.get('gate', X)
8283
if len(logical_ids) == 1:
8384
qb0 = WeakQubitRef(engine=None, idx=logical_ids[0])
8485
return Command(None, gate, qubits=([qb0], ))
85-
elif len(logical_ids) == 2:
86+
if len(logical_ids) == 2:
8687
qb0 = WeakQubitRef(engine=None, idx=logical_ids[0])
8788
qb1 = WeakQubitRef(engine=None, idx=logical_ids[1])
8889
return Command(None, gate, qubits=([qb0], ), controls=[qb1])
89-
else:
90-
raise RuntimeError('Unsupported')
90+
raise RuntimeError('Unsupported')
9191

9292

9393
def search_cmd(command_dag, cmd):
@@ -897,8 +897,8 @@ def test_qubit_manager_generate_qubit_interaction_graph(qubit_manager):
897897
assert all([n in subgraphs[0] for n in [0, 1, 2, 3]])
898898
assert subgraphs[0][0] == 0
899899
assert subgraphs[0][-2:] in ([1, 3], [3, 1])
900-
assert len(subgraphs[1]) == 3
901-
assert all([n in subgraphs[1] for n in [4, 5, 6]])
900+
assert len(subgraphs[1]) == 4
901+
assert all([n in subgraphs[1] for n in [4, 5, 6, 7]])
902902

903903

904904
def test_qubit_manager_generate_swaps_change_mapping(qubit_manager):

projectq/cengines/_graphmapper_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,8 @@ def test_qubit_placement_double_two_qubit_gate(grid33_graph_mapper):
349349
])
350350
mapping = mapper.current_mapping
351351

352-
# Make sure that the qb[2] was allocated at backend_id 0
353352
assert backend.received_commands[0].gate == Allocate
354-
assert backend.received_commands[0].qubits[0][0].id == 0
353+
assert backend.received_commands[0].qubits[0][0].id in [0, 2, 6, 8]
355354
assert backend.received_commands[0].tags == [LogicalQubitIDTag(2)]
356355

357356

@@ -574,7 +573,7 @@ def test_send_two_qubit_gate_before_swap(simple_mapper):
574573
}
575574
else:
576575
# qb[2] moved, all_cmds[5] not possible
577-
assert mapper._stored_commands == [all_cmds[5]] + all_cmds[-4:]
576+
assert backend._stored_commands == [all_cmds[5]] + all_cmds[-4:]
578577
assert mapper.current_mapping == {
579578
0: 0,
580579
1: 2,

0 commit comments

Comments
 (0)