Skip to content
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

Merged
merged 37 commits into from
Sep 19, 2023

Commits on Jun 20, 2023

  1. 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.
    mtreinish committed Jun 20, 2023
    Configuration menu
    Copy the full SHA
    7087462 View commit details
    Browse the repository at this point in the history
  2. Fix Python 3.8 compatibility

    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.
    mtreinish committed Jun 20, 2023
    Configuration menu
    Copy the full SHA
    330beb8 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    c653304 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    7d54237 View commit details
    Browse the repository at this point in the history
  5. Fix lint

    mtreinish committed Jun 20, 2023
    Configuration menu
    Copy the full SHA
    1743262 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    ce6cf24 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    aadd8cb View commit details
    Browse the repository at this point in the history

Commits on Jun 21, 2023

  1. Fix doc build

    mtreinish committed Jun 21, 2023
    Configuration menu
    Copy the full SHA
    ffa051c View commit details
    Browse the repository at this point in the history
  2. 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.
    mtreinish committed Jun 21, 2023
    Configuration menu
    Copy the full SHA
    09dbcd7 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    d1435cd View commit details
    Browse the repository at this point in the history

Commits on Jul 5, 2023

  1. Configuration menu
    Copy the full SHA
    7d91466 View commit details
    Browse the repository at this point in the history
  2. 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.
    mtreinish committed Jul 5, 2023
    Configuration menu
    Copy the full SHA
    c29887a View commit details
    Browse the repository at this point in the history

Commits on Jul 17, 2023

  1. Configuration menu
    Copy the full SHA
    4d17c9f View commit details
    Browse the repository at this point in the history
  2. Fix rebase error

    mtreinish committed Jul 17, 2023
    Configuration menu
    Copy the full SHA
    a788da4 View commit details
    Browse the repository at this point in the history

Commits on Jul 26, 2023

  1. Configuration menu
    Copy the full SHA
    a03cce5 View commit details
    Browse the repository at this point in the history

Commits on Aug 3, 2023

  1. Fix rebase issues

    mtreinish committed Aug 3, 2023
    Configuration menu
    Copy the full SHA
    9b49dc3 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    43f41d3 View commit details
    Browse the repository at this point in the history

Commits on Aug 23, 2023

  1. Configuration menu
    Copy the full SHA
    4e9911a View commit details
    Browse the repository at this point in the history

Commits on Sep 7, 2023

  1. Configuration menu
    Copy the full SHA
    95ecb6d View commit details
    Browse the repository at this point in the history
  2. Fix module docstring

    mtreinish committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    96cf467 View commit details
    Browse the repository at this point in the history
  3. 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.
    mtreinish committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    e1a0ef4 View commit details
    Browse the repository at this point in the history
  4. 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.
    mtreinish committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    3784da4 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    b90eebb View commit details
    Browse the repository at this point in the history
  6. Fix logic for SingletonGate.copy

    Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
    mtreinish and jakelishman committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    4b242e1 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    d9ff18a View commit details
    Browse the repository at this point in the history
  8. Remove unused imports

    mtreinish committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    86ea2a0 View commit details
    Browse the repository at this point in the history
  9. Update release notes

    mtreinish committed Sep 7, 2023
    Configuration menu
    Copy the full SHA
    32d5395 View commit details
    Browse the repository at this point in the history
  10. Configuration menu
    Copy the full SHA
    306fcc8 View commit details
    Browse the repository at this point in the history

Commits on Sep 18, 2023

  1. Fix release note typos

    Co-authored-by: Jake Lishman <jake@binhbar.com>
    mtreinish and jakelishman committed Sep 18, 2023
    Configuration menu
    Copy the full SHA
    75eb6e6 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    efc7a12 View commit details
    Browse the repository at this point in the history
  3. Improve setattr performance

    mtreinish committed Sep 18, 2023
    Configuration menu
    Copy the full SHA
    b95ac96 View commit details
    Browse the repository at this point in the history
  4. Fix deepcopy logic

    mtreinish committed Sep 18, 2023
    Configuration menu
    Copy the full SHA
    8deb2cb View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    2479b12 View commit details
    Browse the repository at this point in the history
  6. Fix docs typos

    mtreinish committed Sep 18, 2023
    Configuration menu
    Copy the full SHA
    9c9bc09 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    37a30cb View commit details
    Browse the repository at this point in the history
  8. Fix lint

    mtreinish committed Sep 18, 2023
    Configuration menu
    Copy the full SHA
    ba04a8e View commit details
    Browse the repository at this point in the history

Commits on Sep 19, 2023

  1. 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.
    mtreinish committed Sep 19, 2023
    Configuration menu
    Copy the full SHA
    7f99a09 View commit details
    Browse the repository at this point in the history