Skip to content

Conversation

doichanj
Copy link
Collaborator

@doichanj doichanj commented Apr 15, 2025

Summary

This PR is follow up PR to #13278
This PR implements ParameterExpression class in Rust that has Python interface for Parameter, ParameterExpression, ParameterVectorElement Python classes

Details and comments

Rust's ParameterExpression is implemented as base class for Python's Parameter, ParameterExpression, ParameterVectorElement classes, so that we can use Rust's ParameterExpression data directly in other Rust codes.

Python's ParameterExpression class is now a wrapper interface to Rust's implementation and that has set of Parameter symbols to keep symbol objects to support comparing 2 Parameters by is operation in some test cases.

Now ParameterExpression does not have qpy_replay, but it can be built on the fly as needed by traversing SymbolExpr. So we do not use SUBS and GRAD operations in qpy_replay.

@doichanj doichanj requested a review from a team as a code owner April 15, 2025 09:28
@qiskit-bot
Copy link
Collaborator

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

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia
  • @mtreinish
  • @nkanazawa1989

@eliarbel eliarbel added this to the 2.1.0 milestone Apr 16, 2025
@eliarbel eliarbel added C API Related to the C API Rust This PR or issue is related to Rust code in the repository labels Apr 16, 2025
@doichanj
Copy link
Collaborator Author

doichanj commented Apr 17, 2025

There are some issues I'm feeling difficulty in solving with ParameterExpression implemented in Rust space.

  • Do we have to accept ndarray for right hand side parameter for operators?
  • Do we have to test with is operator (in AssertIs) on Python? I think checking the equality with symbol name and uuid is enough.
  • I implemented with a single structure ParameterExpression, so we can not identify the instance as Parameter, ParameterExpression or ParameterVectorElement. Can we change test cases by comparing with __repr__ to identify parameter types?

@doichanj doichanj changed the title [WIP] Move Parameter classes from Python to Rust Move Parameter classes from Python to Rust Apr 21, 2025
@mtreinish
Copy link
Member

There are some issues I'm feeling difficulty in solving with ParameterExpression implemented in Rust space.

* Do we have to accept `ndarray` for right hand side parameter for operators?

I'm not sure I understand what your asking about here. Do you mean something like Paramter("b") + np.array([1.0, 2.0, 3.5])? I wouldn't expect that to be supported tbh. But I could also see how something in SparsePauliOp's symbolic mode needing that.

* Do we have to test with `is` operator (in  `AssertIs`) on Python? I think checking the equality with symbol name and `uuid` is enough.

I think this is probably fine, we have this limitation already for many things that get returned from circuits already. There might be a backwards compatibility concern here depending on exactly where this comes up. But if you write an upgrade release note documenting exactly what and how this is changing we can discuss the specifics during the full review. But in a lot of places tests that were doing self.assertIs were just poorly written making undocumented assumptions about how things are stored under the covers).

* I implemented with a single structure `ParameterExpression`, so we can not identify the instance as `Parameter`, `ParameterExpression` or `ParameterVectorElement`. Can we change test cases by comparing with `__repr__` to identify parameter types?

This isn't sufficient. The exact typing here is part of our API contract and doing __repr__ comparisons is not sufficient (or even a common pattern). There are a lot of places we do something like isinstance(param, Parameter) and this needs to still work the same way.

@raynelfss raynelfss linked an issue Jul 7, 2025 that may be closed by this pull request
@raynelfss raynelfss removed this from the 2.2.0 milestone Jul 31, 2025
@mtreinish
Copy link
Member

I'm going to close this as #14757 just merged. That PR took this as a starting point and continued it so this has effectively merged. Thanks for all the work on this.

@mtreinish mtreinish closed this Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C API Related to the C API 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.

Oxidize ParameterExpression
8 participants