Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions qiskit/dagcircuit/collect_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self, dag: DAGCircuit | DAGDependency):

self.dag = dag
self._pending_nodes: list[DAGOpNode | DAGDepNode] | None = None
self._in_degree: dict[DAGOpNode | DAGDepNode, int] | None = None
self._in_degree: dict[int, int] | None = None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic, using the node ids is only safe for a single dag. If we want to switch this to be keyed on node ids we can not cache it. We could get away with the cache with the DAGNode storage because the hash and eq implementations are unique; either pre-rust dagcircuit where it was object id based or post-rust dag where this is an actual implementation. But node ids are not unique between dags and the indices are reused between dags and will refer to different nodes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the point, but a single BlockCollector class only makes sense for a single DAG, in which case we should be fine using node ids, correct?

self._collect_from_back = False

if isinstance(dag, DAGCircuit):
Expand All @@ -80,7 +80,7 @@ def _setup_in_degrees(self):
self._in_degree = {}
for node in self._op_nodes():
deg = len(self._direct_preds(node))
self._in_degree[node] = deg
self._in_degree[self._get_node_id(node)] = deg
if deg == 0:
self._pending_nodes.append(node)

Expand All @@ -91,6 +91,13 @@ def _op_nodes(self) -> Iterable[DAGOpNode | DAGDepNode]:
else:
return self.dag.get_nodes()

def _get_node_id(self, node):
"""Returns the id of the given node."""
if not self.is_dag_dependency:
return node._node_id
else:
return node.node_id

def _direct_preds(self, node):
"""Returns direct predecessors of a node. This function takes into account the
direction of collecting blocks, that is node's predecessors when collecting
Expand Down Expand Up @@ -165,8 +172,8 @@ def collect_matching_block(self, filter_fn: Callable) -> list[DAGOpNode | DAGDep

# update the _in_degree of node's successors
for suc in self._direct_succs(node):
self._in_degree[suc] -= 1
if self._in_degree[suc] == 0:
self._in_degree[self._get_node_id(suc)] -= 1
if self._in_degree[self._get_node_id(suc)] == 0:
new_pending_nodes.append(suc)
else:
self._pending_nodes.append(node)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed a problem in :class:`~BlockCollector`, by hashing the nodes using their
integer ids.