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

Adding Target and InstructionProperties representations to rust #12292

Open
wants to merge 111 commits into
base: main
Choose a base branch
from

Conversation

raynelfss
Copy link
Contributor

@raynelfss raynelfss commented Apr 23, 2024

Summary

Fixes #12052

As we continue moving more parts of the transpiler to Rust, it is important that some of the important data about the devices we're compiling for is easily accessible from the Rust side of things. These commits aim to implement representations of both the Target and InstructionProperties classes, allowing most of their data to live inside of Rust but still be usable in Python.

Details and comments

qiskit._accelerate now has a target module which includes both the BaseTarget and BaseInstructionProperties structs.
Here's a more detailed description of what has changed:

Rust

  • BaseTarget will include the main data that the Target has in python. This will include all of the publicly available attributes of the existing Target class, except for the following:

    • _coupling_graph: of type rustworkx.PyDiGraph
    • _instruction_durations: of type InstructionDurations
    • _instruction_schedule_map: of type InstructionScheduleMap

    Due to these properties are not usable through Rust without using python api's, they were left in python.

    • This representation is purely additive through Python and non mutable through Rust.
  • BaseInstructionProperties represents the main data of `InstructionProperties, that being:

    • Error: f64,
    • Duratlon: f64

Python:

  • Target: this is a subclass of BaseTarget and has the missing attributes + functions from the previous version.
    • In addition it stores a copy of the _gate_map attribute from the rust side for easy access in python without conversions from rust.
    • We ensure that the data from Target._gate_map is identical to the data in super().gate_map by calling the super()'s equivalent function when adding or updating an instruction.
  • InstructionProperties: is a subclass of BaseInstructionProperties
    • The attribute of calibration to store the instruction's calibration object is present in this representation along with it's custom setter and getter.

Possible breaking changes:

- InstructionProperties is now PyO3 class which means it cannot be subclassed by overloading __init__() alone. The new architecture would require users to extend both __new__ and __init__ to add extra properties. These wouldn't necessarily be visible in rust. Fixed!

Broken tests

A procedure performed in Aer that attempts to erase the Target's internal data is no longer possible due to the these attributes being hidden from python. Solved by Qiskit/qiskit-aer#2142

Open to review!

Any suggestions, comments, feedback, or any request for changes are encouraged and welcome!

- Add `Target` class to test mobility between Rust and Python.
- Add `add_instruction` method to test compatibility with instructions.
- Property check caused most cases to panic.
- Will be commented out and restored at a later time.
- Instructions property returns all added to the target.
- Similar behavior to source.
- Add comments to instruction property.
- Use new_bound for new PyDicts.
- Remove redundant transformation of PyObject to PyTuple.
- Remove debugging print statement.
- Add `InstructionProperties` class to process properties in rust.
- Add `is_instance` and `is_class` to identify certain Python objects.
- Modify logic of `add_instruction` to use class check.
- Other tweaks and fixes.
- Partial addition from Target.py\
- Introduction of hashable qarg data structure.
- Other tweaks and fixes.
- Complete missing procedures in function.
- Rename `Qargs` to `HashableVec`.
- Make `HashableVec` generic.
- Separate `import_from_module_call` into call0 and call1.
- Other tweaks and fixes.
- Remove stray print statements.
- Other tweaks and fixes.
- Update `update_from_instruction_schedule_map to use PyResult and '?' operator.
- Use Bound Python objects whenever possible.
- Other tweaks and fixes.
- Add temporary _target module for testing.
- Remove update_from_instruction_schedule_map function back to python.
- Add python properties for all public attributes in rust
- Other tweaks and fixes.
- Add identical method `qargs` to obtain the qargs of a target.
- Other tweaks and fixes.
- Add function with identical behavior to the original in Target.
- Other tweaks and fixes.
- Add target module to qiskit init file.
- Remove is_instance method.
- Modify set_calibration method in InstructionProperty to leave typechecking to Python.
- Change rust Target alias to Target2.
- Other tweaks and fixes,
- Fix wrong setters/getters for calibration in InstructionProperty object in rust.
- Add FromPyObject trait to Hashable vec to receive Tuples and transform them directly into this type.
- Add operations_for_qargs for Target class in Rust side and Python.
- Fix return dict keys for `qargs_for_operation_name`.
- Add `timing_constrains` and `operation_from_name` to Python side.
- Other tweaks and fixes.
- Fix wrong name for function operation_for_qargs.
- Fix missing return value in the python side.
- Other tweaks and fixes.
- Make `InstructionProperties` "_calibration" attribute visible.
- Removed attribute "calibration", treat as class property.
- Other tweaks and fixes
- Port class method to rust and connect to Python wrapper.
- Other tweaks and fixes.
- These changes break current functionality of other functions, butemulate intended behavior better.
- Fixes coming soon.
@raynelfss raynelfss changed the title [WIP] Moving Target and InstructionProperties to rust Moving Target and InstructionProperties to rust May 15, 2024
@mtreinish mtreinish removed the on hold Can not fix yet label May 16, 2024
mtreinish added a commit to mtreinish/qiskit-aer that referenced this pull request May 17, 2024
This commit removes the qiskit aer translation plugin. This was added as
a workaround for a Qiskit/qiskit#11351. This has been fixed in Qiskit
since 0.45.2 and is no longer necessary. The mechanism by which the
workaround worked was unsound in practice as it was mutating the target
and also explicitly using private attributes of the Target. This is
causing real issues now as it only worked by assuming the target wasn't
shared between passmanagers which is never guaranteed. Similarly the
reliance on internal private attributes of the Target class will cause
issues in the future when the target internals change (see
Qiskit/qiskit#12292). This commit opts to remove the plugin in its
entirity as it's no longer necessary and actively causing issues with
Qiskit 1.1 and transpiling targeting aer backends with >1 circuit. As
it's private internal detail there isn't a release note.

Fixes Qiskit/qiskit#12425
Fixes Qiskit#2141
mtreinish added a commit to mtreinish/qiskit-aer that referenced this pull request May 17, 2024
This commit removes the qiskit aer translation plugin. This was added as
a workaround for a Qiskit/qiskit#11351. This has been fixed in Qiskit
since 0.45.2 and is no longer necessary. The mechanism by which the
workaround worked was unsound in practice as it was mutating the target
and also explicitly using private attributes of the Target. This is
causing real issues now as it only worked by assuming the target wasn't
shared between passmanagers which is never guaranteed. Similarly the
reliance on internal private attributes of the Target class will cause
issues in the future when the target internals change (see
Qiskit/qiskit#12292). This commit opts to remove the plugin in its
entirity as it's no longer necessary and actively causing issues with
Qiskit 1.1 and transpiling targeting aer backends with >1 circuit. As
it's private internal detail there isn't a release note.

Fixes Qiskit/qiskit#12425
Fixes Qiskit#2141
- Use `GILOneCell` to import python modules only once at initialization.
- Remove the custom data structure `Qargs` to avoid conversion overhead.
- `Qargs` does not use `PhysicalQubits`, `u32` is used instead.
- Fix `__setstate__ `and `__getstate__` methods for `PropsMap`, `GateMap`, and `key_like_set_iterator` macro_rule.
- Update code to use the new structures.
- TODO: Fix broken tests.
doichanj pushed a commit to Qiskit/qiskit-aer that referenced this pull request May 20, 2024
This commit removes the qiskit aer translation plugin. This was added as
a workaround for a Qiskit/qiskit#11351. This has been fixed in Qiskit
since 0.45.2 and is no longer necessary. The mechanism by which the
workaround worked was unsound in practice as it was mutating the target
and also explicitly using private attributes of the Target. This is
causing real issues now as it only worked by assuming the target wasn't
shared between passmanagers which is never guaranteed. Similarly the
reliance on internal private attributes of the Target class will cause
issues in the future when the target internals change (see
Qiskit/qiskit#12292). This commit opts to remove the plugin in its
entirity as it's no longer necessary and actively causing issues with
Qiskit 1.1 and transpiling targeting aer backends with >1 circuit. As
it's private internal detail there isn't a release note.

Fixes Qiskit/qiskit#12425
Fixes #2141
- Use tupleize to cast `Qargs` to `Tuple` in `instructions`.
- Use downcast to extract string in `add_instruction`.
- Other tweaks and fixes.
doichanj pushed a commit to doichanj/qiskit-aer that referenced this pull request May 23, 2024
This commit removes the qiskit aer translation plugin. This was added as
a workaround for a Qiskit/qiskit#11351. This has been fixed in Qiskit
since 0.45.2 and is no longer necessary. The mechanism by which the
workaround worked was unsound in practice as it was mutating the target
and also explicitly using private attributes of the Target. This is
causing real issues now as it only worked by assuming the target wasn't
shared between passmanagers which is never guaranteed. Similarly the
reliance on internal private attributes of the Target class will cause
issues in the future when the target internals change (see
Qiskit/qiskit#12292). This commit opts to remove the plugin in its
entirity as it's no longer necessary and actively causing issues with
Qiskit 1.1 and transpiling targeting aer backends with >1 circuit. As
it's private internal detail there isn't a release note.

Fixes Qiskit/qiskit#12425
Fixes Qiskit#2141
@raynelfss
Copy link
Contributor Author

I wonder if I should wait for #11461 for this to continue, since it introduces a change to the way Target.from_configuration() works.

@raynelfss raynelfss added the wont fix This will not be worked on label May 24, 2024
@raynelfss raynelfss marked this pull request as draft May 24, 2024 13:31
@raynelfss raynelfss removed the wont fix This will not be worked on label May 28, 2024
@raynelfss raynelfss changed the title Moving Target and InstructionProperties to rust Adding Target and InstructionProperties representations to rust May 28, 2024
- Rename `InstructionProperties` as `BaseInstructionProperties`.
   - Remove `Calibration` from the rust space.
- Restore `gate_map`, `coupling_map`, `instruction_schedule_map`, and `instruction_durations` to rust.
- Remove all unnecessary data structures from rust space.
- Other tweaks and fixes.
- Added rust native functionality to target such that a `py` would not be needed to use one.
- Add Index trait to make `Target` subscriptable.
- Other small tweaks and fixes.
- Remove uage of `inspect.isclass`.
- Rename `Target` to `BaseTarget` in the rust side.
- Rename `err.rs` to `errors.rs`.
- Remove rust-native `add_inst` and `update_inst` as Target should not be modified from Rust.
- Made `add_instruction` and `update_instruction_properties` private in `BaseTarget`.
- Add missing `get` method in `Target`.
- Other tweaks and fixes
@raynelfss raynelfss marked this pull request as ready for review May 29, 2024 17:13
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

doichanj added a commit to Qiskit/qiskit-aer that referenced this pull request May 31, 2024
* release 0.14.2

* Disable test shot_branching on MacOS (#2143)

* remove test_shot_branching

* disable sho_branching tests on MacOS

* fix test

* fix test

* format

* fix test_shot_branching

* fix test_runtime_parameterization

* fix tests for the latest Qiskit (#2138)

* Remove qiskit aer translation stage (#2142)

This commit removes the qiskit aer translation plugin. This was added as
a workaround for a Qiskit/qiskit#11351. This has been fixed in Qiskit
since 0.45.2 and is no longer necessary. The mechanism by which the
workaround worked was unsound in practice as it was mutating the target
and also explicitly using private attributes of the Target. This is
causing real issues now as it only worked by assuming the target wasn't
shared between passmanagers which is never guaranteed. Similarly the
reliance on internal private attributes of the Target class will cause
issues in the future when the target internals change (see
Qiskit/qiskit#12292). This commit opts to remove the plugin in its
entirity as it's no longer necessary and actively causing issues with
Qiskit 1.1 and transpiling targeting aer backends with >1 circuit. As
it's private internal detail there isn't a release note.

Fixes Qiskit/qiskit#12425
Fixes #2141

* Fix issue 2084 again (#2119)

* Fix issue 2084 again

* format

* fix test

* fix test

* Always hook omp functions in Mac (#2128)

* always hook omp functions in Mac

* fix recent test failures with the latest qiskit

---------

Co-authored-by: Jun Doi <doichan@jp.ibm.com>

* Add simulator_metadata in metadata of SamplerV2 (#2109)

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>

* Fix cuStateVec_enable option (#2146)

* Fix cuStateVec_enable option

* fix function name

* Add support for rotation gates (#2147)

* add rotation gates

* add cr* gates to operations.hpp

* fix test for stabilkize/extended-stabilizer

* fix test again

* Fix deterministic measure of stabilizer (#2132)

* Fix deterministic measure of stabilizer

* fix accumulation in loop

* format

---------

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>

* Fix init of EstimatorV2 and SamplerV2 (#2120)

* fix init of EstimatorV2 and SamplerV2 to handle method

* add release note

* fix from_backend and example in README

* remove setting density_matrix

* Fix deploy.yml (#2110)

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>

* Add support for ECR gate to MPS (#2137)

* Add support for ECR gate

* replace is not to !=

* fix release note

* Fix ecr implementation for stabilizer/extended-stabilizer

* Handle gates with ctrl_stete=0 (#2148)

* handle cx gate with ctrl_stete=0

* add handling *_o0, add test for cx_o0

* format

* fix test

* fix test

* fix test cx

* fix to handle multiple control states

* fix ctrl_state

* format

* resolve conflict again

* fix aer_compiler

* fix random seed (#2151)

Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>

* Move delay gate to custom instructions (#2153)

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Hiroshi Horii <hhorii@users.noreply.github.com>
Co-authored-by: Luciano Bello <bel@zurich.ibm.com>
Co-authored-by: Takashi Imamichi <31178928+t-imamichi@users.noreply.github.com>
@coveralls
Copy link

Pull Request Test Coverage Report for Build 9305626176

Details

  • 548 of 679 (80.71%) changed or added relevant lines in 6 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.08%) to 89.511%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/target_transpiler/errors.rs 3 6 50.0%
qiskit/transpiler/target.py 48 54 88.89%
crates/accelerate/src/target_transpiler/instruction_properties.rs 13 31 41.94%
crates/accelerate/src/target_transpiler/mod.rs 482 586 82.25%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.37%
Totals Coverage Status
Change from base Build 9291000954: -0.08%
Covered Lines: 62699
Relevant Lines: 70046

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API affects user-facing API change enhancement mod: transpiler Issues and PRs related to Transpiler priority: high Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Target representation to rust
4 participants