-
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
Singleton parameterless controlled gates #10898
Singleton parameterless controlled gates #10898
Conversation
This commit adds a new class SingletonControlledGate which is very similar to the SingletonGate class, but instead can be used in place of the ControlledGate class as a parent class. This enables a controlled gate class to work as a singleton if there is no custom control state specified. This new class has a large amount of duplication with the SingletonGate class, but because of how the inheritance tree is built up here there is no avoiding this as we need to specialize for adding singleton support and SingletonGate is at the same level as ControlledGate. With the introduction of this new class all the standard library ControlledGate instances are updated so they inherit from SingletonControlledGate instead of ControlledGate. This should significantly reduce the memory footprint of repeated instances of these gates in circuits.
One or more of the the following people are requested to review this:
|
Ok, this should be ready for review now. I fixed the last failing test cases (all of which were caused by extra state needed for controlled gate reconstruction getting lost through |
I ran a subset of ASV just as a sanity check locally and it didn't show any statistically significant change:
|
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.
Looks good so far. Just a few minor comments.
Co-authored-by: Kevin Hartman <[email protected]>
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.
Overall LGTM!
My only suggestion would be that the release notes seem focused more heavily on users who are extending Qiskit with additional instructions and gates rather than on the user impact for building circuits. Specifically, I think we might want to communicate more concisely that gates in our circuit library not customized during construction no longer support mutation out of the box.
|
||
def __init__( | ||
self, | ||
num_ctrl_qubits: int, | ||
dirty_ancillas: bool = False, | ||
label: Optional[str] = None, | ||
ctrl_state: Optional[Union[str, int]] = None, | ||
_base_label=None, |
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.
Does this need to have unit
and duration
as well?
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 one gets tricky based on the how MCXGate
works. For some gates it calls __init__
from __new__
of MCXGate()
and tracking the args gets confusing because of the layer of indirection. I'll double check these really quickly to see if they're missing something.
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.
Yeah, I just double checked for singleton gates in this PR I think we're ok. If the number of controls results in a discrete gate class like CXGate
or CCXGate
we already have the handling on __new__
which internally calls __init__
for the gate object in MCXGate.__new__
(which is the parent of this). If it's not then it's not a singleton because the gates are parameterized over the number of controls. There is a second PR #11013 which will cover this more broadly, so I'd say lets just save this for that PR.
:class:`~.ControlledGate` and its direct subclasses, but mutation of these attributes | ||
on :class:`~.SingletonControlledGate` and its subclasses is disallowed, and will raise | ||
and exception. These attributes can be customized but only at creation time (i.e. via |
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 quite right anymore since now SingletonControlledGate
is a direct subclass of ControlledGate
:3
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 updated this in: 3a108ab let me know if there is more to update there.
There was an existing section on the user upgrade impact of singleton gates from #10314 that I expanded in this PR. I added a bit more detail to it in 3a108ab. But I think we can save deeper tweaks for the final release notes PR, especially since we'll likely be putting singletons in the prelude. |
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.
LGTM! Thanks for the doc fixes.
Summary
This commit adds a new class SingletonControlledGate which is very similar to the SingletonGate class, but instead can be used in place of the ControlledGate class as a parent class. This enables a controlled gate class to work as a singleton if there is no custom control state specified. This new class has a large amount of duplication with the SingletonGate class, but because of how the inheritance tree is built up here there is no avoiding this as we need to specialize for adding singleton support and SingletonGate is at the same level as ControlledGate.
With the introduction of this new class all the standard library ControlledGate instances are updated so they inherit from SingletonControlledGate instead of ControlledGate. This should significantly reduce the memory footprint of repeated instances of these gates in circuits.
Details and comments
TODO:
SingletonControlledGate
test cases