-
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
Add GatesInBasis analysis pass #6832
Conversation
This commit adds a new analysis pass, GatesInBasis, which is used to check if all the gates in the circuit are currently in the basis set or not. The intent for this pass is to use this to conditionally trigger the execution of basis translation (normally the BasisTranslator) in the main optimization loop of the preset pass managers, however this is blocked on Qiskit#6830. So for now this just adds the pass so we can use it in the future.
ad01ec3
to
195e4e1
Compare
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.
On the whole, this looks good. This is another place where the Gate
/Instruction
distinction can be hard to reason about (it took longer than I would've expected to try and think if there were any edge cases between Instruction
/Gate
, op_nodes
/gate_nodes
, and basis_gates
/supported_instructions
, but I do think what's here, combined with where it will be run in the optimization loop, is correct.)
I would've expected though the BasisTranslator
to effectively be a no-op if the circuit is already in the basis (and effectively have a version of this check implemented already). Is this not the case? Or does this pass perform that check more quickly?
This commit reworks the GatesInBasis pass to keep track of the instruction counts in the DAGCircuit object whenever we add or remove an op node from the DAG. Then the gates in basis pass can simply lookup against that counts dictionary whether or not all the gates are in the basis or not. This way we avoid a full dag traversal to determine the gates in the dag.
Co-authored-by: John Lapeyre <[email protected]>
LGTM. Thanks! |
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.
Thanks @mtreinish , this looks great! Can DAGCircuit.count_ops()
use this now? Also, can this be added as a check in test.python.test_dagcircuit.raise_if_dagcircuit_invalid
?
Yeah, we can replace the implementation of that with |
5a481d1
to
d9b9163
Compare
Done in: 215e008 |
def run(self, dag): | ||
"""Run the GatesInBasis pass on `dag`.""" | ||
gates_out_of_basis = False | ||
basic_instrs = {"measure", "reset", "barrier", "snapshot", "delay"} |
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.
What's the reason for having an extra set of "magic" instructions that always count as being in the basis? Are these ones absolutely guaranteed to always be removed by a later transpiler pass?
If you're looking to ignore directives, it might be better to keep track of the Instruction._directive
parameter, but that doesn't fit super nicely with the current set. Alternatively, we could make this an __init__
kwarg, and default to this set if we're not given a value?
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.
This is just mirroring what the basis_translator
does https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/transpiler/passes/basis/basis_translator.py#L85 it's a poor but prevalent assumption the transpiler makes everywhere that these instructions are universal for all backends. (It's something I'm trying to change in #5885 by making the instructions support explicit), but for the time being I couldn't think of any other way to do this. I can add a kwarg but I think it'll just be better to update this for backendv2 when things are ready
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.
Ok cool, if it's a baked-in assumption elsewhere in Terra, we may as well leave it as it is for now.
Co-authored-by: itoko <[email protected]>
I'd advocate that the only place we should work to maintain dictionary ordering is where we accept or return an |
Ok sure, done in: dda9f59 |
Summary
This commit adds a new analysis pass, GatesInBasis, which is used to
check if all the gates in the circuit are currently in the basis set or
not. The intent for this pass is to use this to conditionally trigger
the execution of basis translation (normally the BasisTranslator) in the
main optimization loop of the preset pass managers, however this is
blocked on #6830. So for now this just adds the pass so we can use it in
the future.
Details and comments