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

Add rust friendly assign parameters methods #12913

Merged
merged 11 commits into from
Aug 30, 2024

Conversation

raynelfss
Copy link
Contributor

@raynelfss raynelfss commented Aug 6, 2024

Summary

Follow up on #12794.
These commits expose some rust-friendly assign_parameters methods in CircuitData allowing us to perform parameter assignments without having call to python directly.

Details and comments

The following commits add the methods assign_paramters_from_slice and assign_parameters_from_mapping which enable the assignment of parameters in rust in a friendly way instead of using hard-Python structures (Py<_> or Bound<'_, _>. Here are their use cases:

  • assign_paramters_from_slice: Use whenever a collection of Param needs to be assigned. The amount of parameters sent needs to be of the same size as the the number of parameters in the CircuitData instance.
  • assign_parameters_from_mapping: Use whenever of mapping between ParameterUuid instances from the CircuitData and Param instances coming from Gates or Operations. This can be used for partial assignment, and the number of parameters can be smaller or equal to the amount of parameters in the CircuitData instance.

Known issues

  • The mapping of Params : Params may not be optimal as we wouldn't know if the types of parameters we're binding are valid, Param::Float does not posses a uuid and therefore is not a valid key for the mapping. Will do a mapping between ParameterUuid and Param to assume that all keys are valid instances with valid uuids.
  • The mapping of assign_parameters_from_mapping does not accept instances of Map<Param, Param> due to param not implementing the hash property in Rust. Now it accepts any mapping of ParameterUuid : Param as long as it can be turned into an iterator of type (ParameterUuid, Param).

Blockers

Other comments

  • Feel free to point out any issues in the comments and I will quickly address.

@raynelfss raynelfss requested a review from a team as a code owner August 6, 2024 18:09
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core
  • @kevinhartman
  • @mtreinish

@raynelfss raynelfss added the Rust This PR or issue is related to Rust code in the repository label Aug 6, 2024
@raynelfss raynelfss assigned raynelfss and unassigned raynelfss Aug 6, 2024
@raynelfss raynelfss added this to the 1.3 beta milestone Aug 6, 2024
@coveralls
Copy link

coveralls commented Aug 6, 2024

Pull Request Test Coverage Report for Build 10632745058

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 44 of 64 (68.75%) changed or added relevant lines in 2 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.02%) to 89.179%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_data.rs 35 55 63.64%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/two_qubit_decompose.rs 1 90.82%
crates/qasm2/src/lex.rs 4 92.23%
Totals Coverage Status
Change from base Build 10598013395: -0.02%
Covered Lines: 71660
Relevant Lines: 80355

💛 - Coveralls

@raynelfss raynelfss added the on hold Can not fix yet label Aug 8, 2024
@raynelfss raynelfss changed the title Add rust friendly assign parameters methods Add rust friendly assign parameters methods Aug 8, 2024
@raynelfss raynelfss removed the on hold Can not fix yet label Aug 10, 2024
@jakelishman jakelishman self-assigned this Aug 29, 2024
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks for this Ray. My biggest concern here is in the first review comment - the new interface gives the Python-space forms additional allocations that they didn't need before. I think we ought to either rework the Python forms (which I suspect might be possible) or at least rework the inner interface a little such that those allocations aren't necessary; they logically aren't, since they weren't there before.

crates/circuit/src/circuit_data.rs Outdated Show resolved Hide resolved
crates/circuit/src/circuit_data.rs Outdated Show resolved Hide resolved
crates/circuit/src/circuit_data.rs Outdated Show resolved Hide resolved
Comment on lines +1153 to +1155
/// Assigns parameters to circuit data based on a mapping of `ParameterUuid` : `Param`.
/// This mapping assumes that the provided `ParameterUuid` keys are instances
/// of `ParameterExpression`.
Copy link
Member

Choose a reason for hiding this comment

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

It's conventional in Rust documentation to put the assumptions that cause panics in a section called # Panics to really make them stand out - idiomatic Rust is far stricter about errors than Python, where the conventions are more like "just raise an exception". Panics are serious business.

(But also see the other comment - imo it'd be cleaner just to handle the error properly, since it won't cost us anything.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

- Remove initial allocation of `Param` instances within a vec in `assign_parameters_iterable`.
- Revert changes in `assign_parameters_mapping`.
- Fix error message in `assign_parameter_from_slice`.
- Return an error if a `uuid` is not found in `assign_parameters from mapping. Also, return error if a valid uuid is not found to avoid panicking.
- Add `clone_ref(py)` method to `Param` to ensure that we use the most efficient cloning methods (either `clone_ref` with gil or copying).
- Implement trait `AsRef<Param>` for `Param` to be able to send both owned and non-owned instances to `assign_parameters_inner`.
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Thanks Ray, this looks much cleaner to me now, and it's good to see no new allocations added in the existing paths. One tiny comment about a comment is all, but code-wise this is good to merge.

crates/circuit/src/operations.rs Show resolved Hide resolved
Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

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

Ace, thanks!

@jakelishman jakelishman added Changelog: None Do not include in changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Aug 30, 2024
@jakelishman jakelishman added this pull request to the merge queue Aug 30, 2024
Merged via the queue into Qiskit:main with commit edb249e Aug 30, 2024
15 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: circuit Related to the core of the `QuantumCircuit` class or the circuit library 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.

4 participants