-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Use singletons for standard library unparameterized, non-controlled gates #10314
Commits on Jun 20, 2023
-
Use singletons for standard library unparameterized, non-controlled g…
…ates This commit adds a new class SingletonGate which is a Gate subclass that reuses a single instance by default for all instances of a particular class. This greatly reduces the memory overhead and significant improves the construction speed for making multiple instances of the same gate. The tradeoff is in the flexibility of use because it precludes having any potentially mutable state in the shared instance. This is a large change to the data model of qiskit because it previously could be assumed that any gate instance was unique and there weren't any unintended side effects from modifying it in isolation (for example doing XGate().label = 'foo' wouldn't potentially break other code). To limit the impact around this instances of SingletonGate do not allow mutation of an existing instance. This can (and likely will) cause unexpected issues as usage of the class is released. Specifically what used to be valid will now raise an exception because it is a shared instance. This is evident from the code modifications necessary to most of the Qiskit code base to enable working with instances of SingletonGates. The knock on effects of this downstream are likely significant and managing how we roll this feature out is going to be equally if not more important than the feature itself. This is why I'm not personally convinced we want to do all this commit includes in a single release. I've opened this as a pull request primarily to start the conversation on how we want to do the roll out to try and minimize and notify downstream users of the potential breakage to avoid issues. The primary issue I have is this doesn't really follow the Qiskit deprecation policy as there is no user facing notification or documentation of this pending change and code that worked in the previously release will not work in the release with this feature. For some aspects of this change (primarily the setters on gate attributes) this can easily be handled by deprecating it in planned singleton standard library gates and waiting the necessary amount of time. But the more fundamental data model changes are hard to announce ahead of time. We can have a release note about it coming in the future but it will end up being very abstract and users will not necessarily be able to act on it ahead of time without concrete examples to test with. This was an issue for me in developing this feature as I couldn't anticipate where API breakages would occur until I switched over all the standard library gates, and there still might be others. Due to the large performance gains this offers and also in the interest of testing the API implications of using singleton gates the unparameterized and non-controlled gates available in qiskit.circuit.library.standard_gates are all updated to be subclasses of singleton gates. In aggregate this is causing construction to be roughly 6x faster and building circuits comprised solely of these gates consume 1/4th the memory as before. But it also exposed a large number of internal changes we needed to make to the wider circuit, QPY, qasm2, dagcircuit, transpiler, converters, and test modules to support working with singleton gates. Besides this there are a couple seemingly unrelated API changes in this PR and it is caused by inconsistencies in the Instruction/Gate API that were preventing this from working. The first which is the ECRGate class was missing a label kwarg in the parent. Similarly all Gate classes and subclasses were missing duration and unit kwargs on their constructors. These are necessary to be able to use these fields on singletons because we need an interface to construct an instance that has the state set so we avoid the use of the global shared instance. In the release notes I labeled these as bugfixes, because in all the cases the parent clases were exposing these interfaces and it primarily is an oversight that they were missing in these places. But personally this does seem more like something we'd normally document as a feature rather than a bugfix. A follow up PR will add a SingletonControlledGate class which will be similar to SingletonGate but will offer two singleton instance based on the value of ctrl_state (and also handle nested labels and other nested mutable state in the base gate). We can then update the standard library gates like CXGate, and CHGate to also be singletons. The ctrl state attribute is primarily why these gates were not included in this commit.
Configuration menu - View commit details
-
Copy full SHA for 7087462 - Browse repository at this point
Copy the full SHA 7087462View commit details -
There are some differences in how the inspect stdlib module behaves between python 3.8 and newer versions of python. This was causing divergence in the test and qpy behavior where inspect was used to determine different aspects of a gate (either whether label was supported as an arg or find the number of free parameters). This commit fixes this by adjusting the code to handle both newer versions of inspect as well as older ones.
Configuration menu - View commit details
-
Copy full SHA for 330beb8 - Browse repository at this point
Copy the full SHA 330beb8View commit details -
Configuration menu - View commit details
-
Copy full SHA for c653304 - Browse repository at this point
Copy the full SHA c653304View commit details -
Configuration menu - View commit details
-
Copy full SHA for 7d54237 - Browse repository at this point
Copy the full SHA 7d54237View commit details -
Configuration menu - View commit details
-
Copy full SHA for 1743262 - Browse repository at this point
Copy the full SHA 1743262View commit details -
Configuration menu - View commit details
-
Copy full SHA for ce6cf24 - Browse repository at this point
Copy the full SHA ce6cf24View commit details -
Configuration menu - View commit details
-
Copy full SHA for aadd8cb - Browse repository at this point
Copy the full SHA aadd8cbView commit details
Commits on Jun 21, 2023
-
Configuration menu - View commit details
-
Copy full SHA for ffa051c - Browse repository at this point
Copy the full SHA ffa051cView commit details -
Add methods to deal with mutability of singleton gates
This commit adds two methods to the SingletonGate class, mutable and to_mutable. The mutable() method is a property method that returns whether a particular instance is mutable or a shared singleton instance. The second method to_mutable() returns a mutable copy of the gate.
Configuration menu - View commit details
-
Copy full SHA for 09dbcd7 - Browse repository at this point
Copy the full SHA 09dbcd7View commit details -
Configuration menu - View commit details
-
Copy full SHA for d1435cd - Browse repository at this point
Copy the full SHA d1435cdView commit details
Commits on Jul 5, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 7d91466 - Browse repository at this point
Copy the full SHA 7d91466View commit details -
Disallow custom attribute on singleton instances
This commit adds a __setattr__ method to the singleton gate class to ensure that custom attributes are not settable for a shared singleton instance. It prevents addign a custom attribute if the instance is in singleton mode and will raise a NotImplementedError to avoid silently sharing state in the single shared instance.
Configuration menu - View commit details
-
Copy full SHA for c29887a - Browse repository at this point
Copy the full SHA c29887aView commit details
Commits on Jul 17, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 4d17c9f - Browse repository at this point
Copy the full SHA 4d17c9fView commit details -
Configuration menu - View commit details
-
Copy full SHA for a788da4 - Browse repository at this point
Copy the full SHA a788da4View commit details
Commits on Jul 26, 2023
-
Configuration menu - View commit details
-
Copy full SHA for a03cce5 - Browse repository at this point
Copy the full SHA a03cce5View commit details
Commits on Aug 3, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 9b49dc3 - Browse repository at this point
Copy the full SHA 9b49dc3View commit details -
Configuration menu - View commit details
-
Copy full SHA for 43f41d3 - Browse repository at this point
Copy the full SHA 43f41d3View commit details
Commits on Aug 23, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 4e9911a - Browse repository at this point
Copy the full SHA 4e9911aView commit details
Commits on Sep 7, 2023
-
Configuration menu - View commit details
-
Copy full SHA for 95ecb6d - Browse repository at this point
Copy the full SHA 95ecb6dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 96cf467 - Browse repository at this point
Copy the full SHA 96cf467View commit details -
Add .mutable and .to_mutable() to Instruction
To unify the access patterns between SingletonGates and other instructions this commit adds a common property mutable and method to_mutable() to check if any Instruction (not just singletons) are mutable or not and to get a mutable copy. For things that don't inherit from SingletonGate these are hard coded to `True` and to return a copy as by default every Instruction is mutable, only `SingletonGate` objects are different in this regard.
Configuration menu - View commit details
-
Copy full SHA for e1a0ef4 - Browse repository at this point
Copy the full SHA e1a0ef4View commit details -
Unify handling of gates in scheduling passes
Previously we were explicitly handling the SingletonGate class in the scheduling passes. But with the introduction of mutable and to_mutable() on Instruction we don't need to condition on gates being singleton or not and we can just handle them in the same manner as other instructions. This commit implements this change.
Configuration menu - View commit details
-
Copy full SHA for 3784da4 - Browse repository at this point
Copy the full SHA 3784da4View commit details -
Configuration menu - View commit details
-
Copy full SHA for b90eebb - Browse repository at this point
Copy the full SHA b90eebbView commit details -
Fix logic for SingletonGate.copy
Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
Configuration menu - View commit details
-
Copy full SHA for 4b242e1 - Browse repository at this point
Copy the full SHA 4b242e1View commit details -
Configuration menu - View commit details
-
Copy full SHA for d9ff18a - Browse repository at this point
Copy the full SHA d9ff18aView commit details -
Configuration menu - View commit details
-
Copy full SHA for 86ea2a0 - Browse repository at this point
Copy the full SHA 86ea2a0View commit details -
Configuration menu - View commit details
-
Copy full SHA for 32d5395 - Browse repository at this point
Copy the full SHA 32d5395View commit details -
Configuration menu - View commit details
-
Copy full SHA for 306fcc8 - Browse repository at this point
Copy the full SHA 306fcc8View commit details
Commits on Sep 18, 2023
-
Co-authored-by: Jake Lishman <jake@binhbar.com>
Configuration menu - View commit details
-
Copy full SHA for 75eb6e6 - Browse repository at this point
Copy the full SHA 75eb6e6View commit details -
Configuration menu - View commit details
-
Copy full SHA for efc7a12 - Browse repository at this point
Copy the full SHA efc7a12View commit details -
Configuration menu - View commit details
-
Copy full SHA for b95ac96 - Browse repository at this point
Copy the full SHA b95ac96View commit details -
Configuration menu - View commit details
-
Copy full SHA for 8deb2cb - Browse repository at this point
Copy the full SHA 8deb2cbView commit details -
Configuration menu - View commit details
-
Copy full SHA for 2479b12 - Browse repository at this point
Copy the full SHA 2479b12View commit details -
Configuration menu - View commit details
-
Copy full SHA for 9c9bc09 - Browse repository at this point
Copy the full SHA 9c9bc09View commit details -
Configuration menu - View commit details
-
Copy full SHA for 37a30cb - Browse repository at this point
Copy the full SHA 37a30cbView commit details -
Configuration menu - View commit details
-
Copy full SHA for ba04a8e - Browse repository at this point
Copy the full SHA ba04a8eView commit details
Commits on Sep 19, 2023
-
Handle positional initialization arguments in SingletonGate
If there are any positional arguments set when initializing a new SingletonGate subclass those were not being handled correctly before. If there is a positional argument being set that would indicate at least a label is being set and indicates a mutable instance should be returned instead of the immutable singleton. This commit adds the missing check to the __new__ logic and also adds a test to verify the behavior is correct.
Configuration menu - View commit details
-
Copy full SHA for 7f99a09 - Browse repository at this point
Copy the full SHA 7f99a09View commit details