Adding filter_fn argument to Collect1qRuns and Collect2qBlocks#15618
Adding filter_fn argument to Collect1qRuns and Collect2qBlocks#15618alexanderivrii wants to merge 3 commits intoQiskit:mainfrom
filter_fn argument to Collect1qRuns and Collect2qBlocks#15618Conversation
|
One or more of the following people are relevant to this code:
|
Pull Request Test Coverage Report for Build 21468458880Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
jakelishman
left a comment
There was a problem hiding this comment.
Taken at face value, the code in this PR is fine. On an API question, though: is this is better on Collect1qRuns, or should it be logic that's in the subsequent consumer of it? Having it here doesn't affect the actual collection of a run at all, so it can be done afterwards without a performance penalty. Or is there an API reason that it the logic about whether to use a collected run or not should be a function of the producer, not the consumer?
What does the full pipeline look like where you'd use this?
| from __future__ import annotations | ||
|
|
||
| import typing | ||
|
|
||
| from collections.abc import Callable | ||
|
|
||
| from qiskit.transpiler.basepasses import AnalysisPass | ||
|
|
||
| if typing.TYPE_CHECKING: | ||
| from qiskit.dagcircuit import DAGCircuit, DAGOpNode | ||
|
|
There was a problem hiding this comment.
Now we're on Python 3.10, I don't think you need the __future__ import, and you can just import DAGCircuit, DAGOpNode without the guard because pylint shouldn't complain any more (but not 100% certain).
There was a problem hiding this comment.
Thanks Jake, trying out your suggestion in 24bd9aa. But it does already work locally for me (with python 3.13).
There was a problem hiding this comment.
Hmm, this seems to have worked for all of our platforms/python versions.
|
Jake: I agree that I could have equally well added the logic to Personally I am slightly leaning towards having this logic on the producer side, because internally the rust functions for collecting runs/blocks also depend on |
|
Superseded by #15625. |
Summary
This PR adds a new argument
filter_fnto python-space single-qubit/two-qubit collection passesCollect1qRunsandCollect2qBlocks. This argument can be used to filter collected subcircuits based on custom criteria, such as "only return 2-qubit blocks with at least 3 CX-gates", or "only return 1-qubit runs with no Hadamard gates".Note that this is different from the filtering mechanisms used within
DAGCircuit.collect_1q_runsandDAGCircuit.collect_2q_blocks, which specify which nodes should be included in runs/blocks in the first place. The new additional filtering mechanism filters entire runs/blocks.I personally need this for improving the Clifford+T transpiler pipeline, where we want to create a
PassManagerthat collects 1-qubit runs, but avoids resynthesizing runs that already consist entirely of Clifford+T gates. Note that this is not a part of this PR.Details and comments
Even though the immediate use-cases filter the runs just based on the number or the types of gates they contain, the filtering function also takes the
DAGCircuitas an argument, as I think this could be useful for more complex filtering strategies. But I don't feel too strongly about this, and can removeDAGCircuitto keep the signatures simpler.This is currently implemented purely in python space because I don't think we will get much of a performance gain from passing a python callback to Rust. Additionally, it would be better to wait with the Rust implementation until #15301 is merged.