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

Rewrite singleton handling including SingletonInstruction #11014

Merged
merged 5 commits into from
Oct 13, 2023

Commits on Oct 13, 2023

  1. Rewrite singleton handling including SingletonInstruction

    This is a large rewrite of the singleton gate handling, building off the
    work done across the library to make the initial implementation work.
    
    There are two main purposes to this commit:
    
    * Make `SingletonInstruction` available in addition to `SingletonGate`,
      and have these be easy to maintain in conjunction, not need to
      duplicate overrides between them, and not require inheritors to do any
      nasty multiple inheritance or the like.
    
    * Fix regressions in the construction time of `SingletonGate` instances.
    
    In the end, this turned into a fairly complete rewrite that completely
    switches out the method of defining the classes; it transpires that the
    previous way of having the "immutable overrides" on _all_ instances of
    the classes was the main source of slowdown, with `__setattr__` being a
    large problem.  The previous method was far easier from an
    implementation perspective, but the runtime costs ended up being too
    high.
    
    This new form takes a vastly different strategy, explained in detail in
    `qiskit/circuit/singleton.py`.  The gist is that we instead make the
    singleton instance a _subclass_ of the class object we're creating, and
    only it contains the overrides to make itself immutable.  We get around
    the instantiation problem (`__init__` isn't special and doesn't
    skip the forbidden `__setattr__`) by constructing an instance of the
    _base_ class, and then dynamically switching in the overrides
    afterwards.  Since the overrides and the dynamic singleton type of the
    instance are designed to be co-operative (including in `__slots__`
    layout), this can be done safely.
    jakelishman committed Oct 13, 2023
    Configuration menu
    Copy the full SHA
    4cf52f2 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    145decf View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    1274fe7 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    e1cde2f View commit details
    Browse the repository at this point in the history
  5. Fix typos

    Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
    jakelishman and mtreinish committed Oct 13, 2023
    Configuration menu
    Copy the full SHA
    f3c6a43 View commit details
    Browse the repository at this point in the history