Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Support control flow in ConsolidateBlocks #10355
Support control flow in ConsolidateBlocks #10355
Changes from 11 commits
413aa02
c882c6a
d269db5
c32d69c
d4bc8bd
d210512
eabaee5
c567d0d
7a34941
3da4990
e4a39fe
248479d
dc69654
60e27b4
8bea1e7
87b979e
bf0a1f4
d3d2925
a5b57c1
c24f63e
2ae8558
c077e49
7cef53b
d4460a5
54bd958
be2a65f
8eae8c3
b36a949
5dc5f85
196beb7
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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 can likely make this
PassManager
just once up in__init__
and retrieve it fromself
each time, even during the recursion.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 idea. The cost should be negligible.
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.
After trying, I don't see an easy way to make this work. A
ConsolidateBlocks
instance is constructed before the top-levelRunningPassManager
injects the necessary information. The callPassManager
on my machine takes < 250ns. Constructing it and checking adict
and appending passes takes 13us or 26us.We could pass the required information when constructing
ConsolidateBlocks
. That would allow us to do this more cleanly in the future. Maybe populaterequires
based boolean-valued arguments. We'd support the status quo for a while.In fact, doing this would be less fragile and better signal intent than the status quo. This:
https://github.com/Qiskit/qiskit-terra/blob/fb9d5d8bb41f1e289b5ee895ea087cc92e74a921/qiskit/transpiler/preset_passmanagers/level3.py#L174-L179
is not as transparent as
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.
Passing the required information to
ConsolidateBlocks
would be my general choice so that the top level of the recursion and the recursive calls can all look more similar. At this stage in the 0.25 cycle, it might be better to leave what you've got and fix it in 0.45 instead so we've got more time.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.
The problem with this test is not inherently the builder interface, it's because the test compares the exact matrix representation of an inner-scope gate with an outer-scope gate without resolving the bit binding of the inner scope. That's a vital part of resolving control-flow scopes.
It would be good to have tests that explicitly use the builder interface to ensure that there is no problem with bit binding throughout, because that's how users will build control-flow circuits.
Canonicalizing the control-flow structure after the pass has run (which is the only place the test canonicalisation ought to be done, because the tested functionality should work whether or not the form is canonicalised) would not fix this test failure, because the problem is fundamentally that the test was requiring that
A(0, 1)
had the same matrix form asA(1, 0)
. Canonicalisation after the pass has run wouldn't change that the operator had been computed on (1, 0) (and suitably transposed) rather than (0, 1). Instead, the test could check the qubit binding, and swap the 2q operator if necessary:Fwiw, the pass is returning a correct answer in the flipped case from what I saw. It's just the test that's wrong.
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.
Is the builder interface the only interface? I mean, is constructing the object directly considered an internal detail? Is bit binding documented anywhere?
If there is a simple interface that is part of the API, it makes sense to me to use this when testing if possible. Unless it is much more difficult than the builder interface.... Also I recall that the builder interface was supposed to be more provisional than the lower-level design.
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 isn't really about the builder interface, it's about bit-binding that can also be done with the low-level interface. It's a really important part of the Terra data model that things that interact with scopes (which includes
Instruction.definition
fields and matrices) need to know how to bind theCircuitInstruction.qubits
to their counterparts in the inner scope. This existed before the control-flow instructions as well, it's just that it most frequently comes up with them.The builder interface for control-flow isn't any more provisional than any part of Terra.
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 is "bit binding"? I don't find anything like that phrase in terra. I really need some documentation or reference to code.
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.
I looked a bit. Looks as if upon construction some objects, such as control flow ops, don't do any mapping of qubits, nor do they make any assumptions about mapping.
QuantumCircuit.to_gate
may do some kind of mapping, but I think not. AndConsolidateBlocks
implements mapping qubits between the parent circuit and the blocks at a low level, throughout the code. By low level, I mean qiskit has no tools for mapping qubits between objects.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.
By bit binding I mean the relationship between the ordering of
CircuitInstruction.qubits
and how theoperation
is applied. If you accessoperation.definition
, or in any way get a matrix representation or inner circuit from theoperation
, you must ensure that you interpret the qubit ordering according toCircuitInstruction.qubits
. Your test is not doing that - you're taking a 2q matrix without regard to the ordering of the qubits, and consequently it's sometimes got a flipped basis if the block happened to be defined on1, 0
instead of0, 1
.This isn't specific to control flow.
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.
And Qiskit does this everywhere - this isn't something new, it's been a fundamental part of the data since the very first versions.