-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improved fast-path detection in HLS #13070
Changes from 1 commit
5b2bf81
d29d827
b2ea63d
20a355b
0962be4
0c29c0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -280,6 +280,21 @@ def run(self, dag: DAGCircuit) -> DAGCircuit: | |
return self._run(dag, tracker) | ||
|
||
def _run(self, dag: DAGCircuit, tracker: QubitTracker) -> DAGCircuit: | ||
# Check if HighLevelSynthesis can be skipped. | ||
all_skipped = True | ||
for node in dag.topological_op_nodes(): | ||
qubits = tuple(dag.find_bit(q).index for q in node.qargs) | ||
if not ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to cache this result so we can just look it up in the second iteration over the DAG? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean hashing whether a particular node can be skipped? I briefly tried this, but in the main use-case, where only a single pass is required, this extra hashing makes the runtime a bit slower. |
||
dag.has_calibration_for(node) | ||
or len(node.qargs) < self._min_qubits | ||
or node.is_directive() | ||
or self._definitely_skip_node(node, qubits) | ||
): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This set of checks is the exact same set that we do during the loop below - should they be folded into Looking quickly, it seems like the internal logic of HLS has changed slightly since I wrote that fast path within the loop, so the logic around having a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, done in 0962be4. |
||
all_skipped = False | ||
break | ||
if all_skipped: | ||
return dag | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't have to do this, but in case you're interested, Python has a very uncommon control-flow construction that handles this precise case without an auxiliary variable: for node in dag.topological_op_nodes():
qubits = ...
if not self._should_skip(dag, node, qubits):
break
else:
return dag (The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neat, I like this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 0962be4. |
||
|
||
# Start by analyzing the nodes in the DAG. This for-loop is a first version of a potentially | ||
# more elaborate approach to find good operation/ancilla allocations. It greedily iterates | ||
# over the nodes, checking whether we can synthesize them, while keeping track of the | ||
|
@@ -312,7 +327,7 @@ def _run(self, dag: DAGCircuit, tracker: QubitTracker) -> DAGCircuit: | |
# now we are free to synthesize | ||
else: | ||
# this returns the synthesized operation and the qubits it acts on -- note that this | ||
# may be different than the original qubits, since we may use auxiliary qubits | ||
# may be different from the original qubits, since we may use auxiliary qubits | ||
synthesized, used_qubits = self._synthesize_operation(node.op, qubits, tracker) | ||
|
||
# if the synthesis changed the operation (i.e. it is not None), store the result | ||
|
@@ -335,7 +350,7 @@ def _run(self, dag: DAGCircuit, tracker: QubitTracker) -> DAGCircuit: | |
if len(synthesized_nodes) == 0: | ||
return dag | ||
|
||
# Otherwise we will rebuild with the new operations. Note that we could also | ||
# Otherwise, we will rebuild with the new operations. Note that we could also | ||
# check if no operation changed in size and substitute in-place, but rebuilding is | ||
# generally as fast or faster, unless very few operations are changed. | ||
out = dag.copy_empty_like() | ||
|
@@ -642,8 +657,9 @@ def _definitely_skip_node(self, node: DAGOpNode, qubits: tuple[int] | None) -> b | |
# The fast path is just for Rust-space standard gates (which excludes | ||
# `AnnotatedOperation`). | ||
node.is_standard_gate() | ||
# If it's a controlled gate, we might choose to do funny things to it. | ||
and not node.is_controlled_gate() | ||
# At the moment we don't consider fast-path handling for controlled gates over 3 or | ||
# more controlled qubits. However, we should not abort early for CX/CZ/etc. | ||
and not (node.is_controlled_gate() and node.num_qubits >= 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think maybe this comment has too many negatives in it? I think we do abort early for CX/CZ, right? Or maybe the comment is referring to "abort There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have reworded the comment. I have indeed meant that the function |
||
# If there are plugins to try, they need to be tried. | ||
and not self._methods_to_try(node.name) | ||
# If all the above constraints hold, and it's already supported or the basis translator | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We technically don't need to waste time getting a topological order for this function, we can just look at every op node in arbitrary order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, done in 0962be4.