From ffb4f088bd3cc6c751694a870f2cf7949ff68b3b Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 12 Nov 2024 13:19:26 -0500 Subject: [PATCH] Avoid overhead for synthesized nodes lookup (#13424) After #12550 a hash implementation was added to the implementation of DAGOpNode to be able to have identical instances of dag nodes used be usable in a set or dict. This is because after #12550 changed the DAGCircuit so the DAGOpNode instances were just a python view of the data contained in the nodes of a dag. While prior to #12550 the actual DAGOpNode objects were returned by reference from DAG methods. However, this hash implementation has additional overhead compared to the object identity based version used before. This has caused a regression in some cases for high level synthesis when it's checking for nodes it's already synthesized. This commit addresses this by changing the dict key to be the node id instead of the node object. The integer hashing is significantly faster than the object hashing. (cherry picked from commit 8c6ad024c51119d2ee0d3881466add3e4b50e2d3) --- qiskit/transpiler/passes/synthesis/high_level_synthesis.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py index 87d953e27f81..d7b5b0d535b8 100644 --- a/qiskit/transpiler/passes/synthesis/high_level_synthesis.py +++ b/qiskit/transpiler/passes/synthesis/high_level_synthesis.py @@ -382,7 +382,7 @@ def _run( # If the synthesis changed the operation (i.e. it is not None), store the result. if synthesized is not None: - synthesized_nodes[node] = (synthesized, synthesized_context) + synthesized_nodes[node._node_id] = (synthesized, synthesized_context) # If the synthesis did not change anything, just update the qubit tracker. elif not processed: @@ -407,8 +407,9 @@ def _run( outer_to_local = context.to_local_mapping() for node in dag.topological_op_nodes(): - if node in synthesized_nodes: - op, op_context = synthesized_nodes[node] + + if op_tuple := synthesized_nodes.get(node._node_id, None): + op, op_context = op_tuple if isinstance(op, Operation): out.apply_operation_back(op, node.qargs, node.cargs)