Skip to content

Conversation

raynelfss
Copy link
Contributor

@raynelfss raynelfss commented Apr 21, 2025

Summary

The following commits expose rust native methods add_instruction and update_instruction_properties, opposite to their Python counterparts.

Details and comments

Now that we're able to build instructions efficiently from Rust, we should be able to add instructions to the Target (or the python's BaseTarget). The following commits add the following Rust-native methods:


add_instruction

Adds an instance of PackedOperation to the Target.

An addition to the Target from Python typically involves using an instance of TargetOperation, which has two variants representing variadics and regular instructions. However, since we don't yet support Variadics in Rust, this method is only able to work with NormalOperation and therefore accepts the Rust native types PackedOperation and a slice of Param (at least until #13267 is resolved).

update_instruction_properties

Modifies the InstructionProperties assigned of a collection of qargs for a certain instruction in the Target. This should be the only way of modifying instruction properties, at least until we can figure out a way of synchronizing the Python and Rust gate maps (see #14173).


These additions rely on the additions described below:

Pre-requisites

@coveralls
Copy link

coveralls commented Apr 21, 2025

Pull Request Test Coverage Report for Build 14835963529

Details

  • 87 of 116 (75.0%) changed or added relevant lines in 1 file are covered.
  • 4 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 87.901%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/target_transpiler/mod.rs 87 116 75.0%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/unitary_synthesis.rs 1 94.78%
crates/qasm2/src/lex.rs 3 92.73%
Totals Coverage Status
Change from base Build 14801671421: -0.02%
Covered Lines: 74566
Relevant Lines: 84830

💛 - Coveralls

@eliarbel eliarbel added this to the 2.1.0 milestone Apr 24, 2025
@eliarbel eliarbel linked an issue Apr 24, 2025 that may be closed by this pull request
5 tasks
- Add rust native way of adding an instruction to the `Target` without any need for Python.
- Additions are guaranteed to be reflected in Rust and Python.
- Remove `PyResult` from the output of the rust native functions.
- Add quotation marks to the output of error messages for gate names.
- Add rust native unit testing for the new methods to make sure they're working properly.
@raynelfss raynelfss added priority: high Changelog: None Do not include in changelog mod: transpiler Issues and PRs related to Transpiler Rust This PR or issue is related to Rust code in the repository labels Apr 24, 2025
@raynelfss raynelfss changed the title [WIP] Enable rust native Target creation. Enable rust native Target creation. Apr 24, 2025
@raynelfss raynelfss marked this pull request as ready for review April 24, 2025 18:05
@raynelfss raynelfss requested a review from a team as a code owner April 24, 2025 18:05
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a quick first pass through this it's looking good, just some initial comments inline.

@mtreinish mtreinish self-assigned this Apr 24, 2025
- Use rustdoc format for docstrings
- Removce generic type for property map in `add_instruction`.
- Add `QargsRef::as_owned_qargs` to return an owned version of Qargs.
@raynelfss raynelfss requested a review from mtreinish April 28, 2025 18:33
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, thanks for making the updates. I just have a few more small comments inline.

raynelfss and others added 2 commits May 1, 2025 13:27
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
- Remove `as_owned_qargs`.
- Use `String` instead of owned `Qargs` for `TargetError`.
@raynelfss raynelfss requested a review from mtreinish May 1, 2025 18:22
&mut self,
instruction: &'a str,
qargs: T,
properties: Option<InstructionProperties>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the Option here? Like I realize the data model supports None to indicate an ideal instruction with no properties, but I wonder if in practice this is something we should be supporting on an update method. The python space typing indicated None wasn't an allowed type only InstructionProperties.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. Getting rid of the option seems logical. However, what if the user were to update the properties of a global/variadic instruction temporarily, and then back to its original value None? What could the user do in this case and is this supported behavior?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine leaving it. I don't actually think that's a thing people will do in practice. The handling of globals is ill defined especially with properties set and it's not commonly used at all. That was not part of the original design intent of the target, and the fact it was possible was an implementation mistake that leaves us with undefined behavior. This is why I asked you to open #14268 so we can fix this hole in the API for 3.0 (regardless of what we do it will probably be backwards incompatible).

raynelfss and others added 2 commits May 2, 2025 09:38
Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for doing this.

@mtreinish mtreinish enabled auto-merge May 5, 2025 16:36
@mtreinish mtreinish added this pull request to the merge queue May 5, 2025
Merged via the queue into Qiskit:main with commit 03e13ea May 5, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog 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.

Create a more rust-centric Target
5 participants