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

Moving Target and InstructionProperties to rust #12292

Open
wants to merge 100 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 both the Target and InstructionProperties classes, allowing all 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 Target and InstructionProperties structs.
Here's a more detailed description of what has changed:

Target

  • Qargs was added as a representation of a tuple of qubits in rust. Allows for quick conversion of tuples from and to rust without compromising the information presented. Can be cast into a tuple when needed in Python.
  • PropsMap was added as a representation of a dict[Option[Qarg], Option[InstructionProperties]] in rust. Objects of this type are kept in the python heap which prevents PyO3 from performing expensive conversions.
  • GateMap was added as a representation of a dict[str, PropsMap] in rust. Created to avoid conversions to dict from PyO3.
  • qiskit.transpiler.target includes a Python extension of the Target class due to the following:
    • build_coupling_map(): This method was kept in Python due to the usage of rustworkx.PyDiGraph which doesn't have a proper representation in Rust.
      • The helper functions _build_coupling_graph(), _filter_coupling_graph() were also left in python.
    • The magic __str__ method in Target to preserve the way data was displayed.

Changes introduced:

  • qiskit.transpiler.target includes Python extensions of the Target and InstructionProperties classes which includes due to the following:
    • The magic __str__ method InstructionProperties to preserve the way data was displayed.
    • The capacity to extend InstructionProperties to include custom properties depends highly on overloading the __new__ method, which cannot be done by extending __init__. Therefore a wrapper of InstructionProperties was kept on the python side that correctly calls the __new__ method, and allows for extra attributes to be added when extended. However, since the Target class does not really use these extra properties, Rust is unable to see them.

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.

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.
- Use IndexSet as a base to preserve the insertion order.
- Other tweaks and fixes.
- Fix `GateMap.__iter__` to use an IndexKeys iterator.
- Other small tweaks and fixes.
- Make `__iter__` use the keys() method in `GateMap`.
…edule_map`

- Add `tupelize` function to create tuples from non-downcastable items.
- Fix creation of Parameters by iterating through members of tuple object and mapping them to parameters in `update_from_instruction_schedule_map`.
- Add missing logic for creating a Target with/without `qubit_properties`.
- Add tuple conversion of `Qargs` to store items in a dict in `BasisTranslator` and `UnitarySynthesis` passes.
- Cast `PropsMap` object to dict when comparing in `test_fake_backends.py`.
- Modify logic of helper functions that receive a bound object reference, a second `py` not required as an argument.
- Add set operation methods to `GateMapKeys`.
- Other tweaks and fixes.
- Fix repeated erroneous calls to `add_instruction` in `update_from_instruction_schedule_map`
- Add missing condition in `instruction_supported`
- Use `IndexSet` instead of `HashSet` for `QargsSet`.
- Other small tweaks and fixes.
- Create `QargSet` and `PropsMap` using the new macros.
- Return a `TargetOpNames` ordered set to python in `operation_names`.
- Remove the Python side `operation_names.`
- Fix faulty docstring in `target.py`.
- Other tweaks and fixes.
- Remove duplicate Iterator in GateMap.
- Other small tweaks and fixes.
@raynelfss raynelfss marked this pull request as ready for review May 15, 2024 17:42
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Eric-Arellano
  • @Qiskit/terra-core
  • @kevinhartman
  • @mtreinish
  • @nkanazawa1989

@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
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
3 participants