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

Compatibility classes for converting BackendV1 to BackendV2 to facilitate migration #8611

Closed
chriseclectic opened this issue Aug 24, 2022 · 2 comments · Fixed by #8759
Closed
Labels
type: feature request New feature or request

Comments

@chriseclectic
Copy link
Member

What should we add?

For package developers trying to transition to new backends it would be very convenient if terra had wrapper backend classes/functions that can make a backend V1 into a backend V2 and vice versa.

Something to this effect was recently added to qiskit-experiments to help facilitate handling both types of backends in tests, however it falls a bit short of general use because it uses duck-typing of methods for both backends but wont actually be an instance of a backend so can't be used as a drop-in replacement.

It would be convenient having either a backend class or generator function that can take one backend and return an equivalent instance of the other.

Eg we could have classes that can be initialized with another backend like:

class WrappedBackendV1(BackendV1):
    def __init__(self, backend: Union[BackendV1, BackendV2]):

class WrappedBackendV2(BackendV2):
    def __init__(self, backend: Union[BackendV1, BackendV2]):

An alternative would be a function to generate a backend either V1->V2 or V2->V1 (or trivially V1->V1, V2->V2)

# function interface
def wrapped_backend(
    backend: Union[BackendV1, BackendV2], target_cls=BackendV2
) -> Union[BackendV1, BackendV2]
@chriseclectic chriseclectic added the type: feature request New feature or request label Aug 24, 2022
@jakelishman
Copy link
Member

jakelishman commented Aug 24, 2022

From a typing perspective I don't like your proposed interfaces haha (target_cls implies wrapped_backend lives in a dependent type system, and are the unions in WrappedBackend meant to keep expanding with every new spec, so the classes just get more and more ifs?), but the conversion functionality seems like a sensible addition to help migration. I'm not convinced that we can make a sensible BackendV2 -> BackendV1 converter, because BackendV2 contains much more precise information about various Target properties that can't be represented in a BackendV1, but we don't want people to keep using BackendV1 anyway, and we wouldn't provide "backwards migration" for any other new feature.

Since I complained about your proposed interface, my suggestion would be to add a single class method BackendV2.from_v1(cls, base: BackendV1) -> BackendV2. It's got stricter/safer typing, and it's read as an associated constructor of the BackendV2 class. Internally it'd probably return an instance of some private _ProxyBackendV1AsV2 type.

@mtreinish
Copy link
Member

As for the specific details of building this most of the code already exists in the fake providers already. The majority of such a compatibility interface would be converting BackendConfiguration, BackendProperties, and PulseDefaults into a Target and assorted other instance attributes. The fake providers (plus qiskit-ibm-provider and qiskit-ibm-runtime) took some draft code I wrote for them to do that conversion several months ago to build the same converter. So having I think both potentially promoting https://github.com/Qiskit/qiskit-terra/blob/main/qiskit/providers/fake_provider/utils/backend_converter.py as more public (for external consumption in other *ibm* providers) and using that to build the interface shouldn't be very difficult.

mtreinish added a commit to mtreinish/qiskit-core that referenced this issue Sep 14, 2022
This commit adds a new class BackendV2Converter which is a BackendV2
implementation that converts an input BackendV1 object into a BackendV2
implementation. This is useful for users that are supporting working
with arbitrary providers so that they can standardize on using the
newest access patterns even if a provider is still using BackendV1.
Similarly, for qiskit's internal usage, this gives us a path to move all
of the transpiler internals to use Target and avoid carrying around
duplicate information in the PassManagerConfig for passes that haven't
been updated. This will enable us to convert input BackendV1 instances
to a target once on initial calling and have the transpiler only ever
see a target.

Fixes Qiskit#8611
@mergify mergify bot closed this as completed in #8759 Sep 28, 2022
mergify bot pushed a commit that referenced this issue Sep 28, 2022
* Add BackendV2Convert class for treating BackendV1 as BackendV2

This commit adds a new class BackendV2Converter which is a BackendV2
implementation that converts an input BackendV1 object into a BackendV2
implementation. This is useful for users that are supporting working
with arbitrary providers so that they can standardize on using the
newest access patterns even if a provider is still using BackendV1.
Similarly, for qiskit's internal usage, this gives us a path to move all
of the transpiler internals to use Target and avoid carrying around
duplicate information in the PassManagerConfig for passes that haven't
been updated. This will enable us to convert input BackendV1 instances
to a target once on initial calling and have the transpiler only ever
see a target.

Fixes #8611

* Update docstring

* Return empty options for _default_options

* Remove leftover pylint disable

* Expand standard gate conversions and handle missing basis_gates

* Fix copy paste error qubit_props_list_from_props() docstring

* Add name mapping argument to allow setting custom name-> gate mappings for conversion

* Add missing gamma parameter

* Fix handling of global ops in configuration

* Raise exception on custom gates without mapping

* Move name mapping to standard gates module

* Fix lint and docs

* Use gate object name attribute to build name mapping

Co-authored-by: Jake Lishman <[email protected]>

* Fix lint

* Fix pylint cyclic-import error

* Update qiskit/providers/backend_compat.py

Co-authored-by: Naoki Kanazawa <[email protected]>

* Remove supported_instructions and add option for adding delay

The supported instructions field is underdocumented and it's not clear
that the instructions it contains are something that we want to support
directly. Since the only reason we added support for it in the target
generation was to handle the delay for ibm backends (mostly in tests as
the IBM provider will be using BackendV2 natively soon) this commit
removes its usage and adds a new flag for explicitly adding delay to the
backend's target.

* Add mention of converter class to API changes section

* Add missing flag usage to delay test

* Run black

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: Naoki Kanazawa <[email protected]>
ElePT pushed a commit to ElePT/qiskit-ibm-provider that referenced this issue Oct 9, 2023
…kit/qiskit#8759)

* Add BackendV2Convert class for treating BackendV1 as BackendV2

This commit adds a new class BackendV2Converter which is a BackendV2
implementation that converts an input BackendV1 object into a BackendV2
implementation. This is useful for users that are supporting working
with arbitrary providers so that they can standardize on using the
newest access patterns even if a provider is still using BackendV1.
Similarly, for qiskit's internal usage, this gives us a path to move all
of the transpiler internals to use Target and avoid carrying around
duplicate information in the PassManagerConfig for passes that haven't
been updated. This will enable us to convert input BackendV1 instances
to a target once on initial calling and have the transpiler only ever
see a target.

Fixes Qiskit/qiskit#8611

* Update docstring

* Return empty options for _default_options

* Remove leftover pylint disable

* Expand standard gate conversions and handle missing basis_gates

* Fix copy paste error qubit_props_list_from_props() docstring

* Add name mapping argument to allow setting custom name-> gate mappings for conversion

* Add missing gamma parameter

* Fix handling of global ops in configuration

* Raise exception on custom gates without mapping

* Move name mapping to standard gates module

* Fix lint and docs

* Use gate object name attribute to build name mapping

Co-authored-by: Jake Lishman <[email protected]>

* Fix lint

* Fix pylint cyclic-import error

* Update qiskit/providers/backend_compat.py

Co-authored-by: Naoki Kanazawa <[email protected]>

* Remove supported_instructions and add option for adding delay

The supported instructions field is underdocumented and it's not clear
that the instructions it contains are something that we want to support
directly. Since the only reason we added support for it in the target
generation was to handle the delay for ibm backends (mostly in tests as
the IBM provider will be using BackendV2 natively soon) this commit
removes its usage and adds a new flag for explicitly adding delay to the
backend's target.

* Add mention of converter class to API changes section

* Add missing flag usage to delay test

* Run black

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: Naoki Kanazawa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants