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

Oxidize PauliFeatureMap #13045

Merged
merged 17 commits into from
Sep 12, 2024
Merged

Oxidize PauliFeatureMap #13045

merged 17 commits into from
Sep 12, 2024

Conversation

Cryoris
Copy link
Contributor

@Cryoris Cryoris commented Aug 27, 2024

Summary

This PR oxidizes the PauliFeatureMap (and derivatives) and implements a first step in refactoring the circuit library, where structurally fixed circuits are moved to functions. As part of this, PauliFeatureMap & co. are pending deprecation.

Details and comments

Some benchmark data for reps=10 and different numbers of qubits n:

-- n = 10, speedup 8.07x
main: 0.008 +- 0.001
this: 0.001 +- 0.000

-- n = 20, speedup 5.38x
main: 0.028 +- 0.004
this: 0.005 +- 0.004

-- n = 50, speedup 6.28x
main: 0.174 +- 0.005
this: 0.028 +- 0.006

-- n = 100, speedup 6.69x
main: 0.743 +- 0.022
this: 0.111 +- 0.007

-- n = 200, speedup 7.14x
main: 3.365 +- 0.144
this: 0.471 +- 0.015
  • The most time is spent in parameter multiplication, which could be further improved by caching the expression across the repetitions (edit: tried, was slower), or (bigger scale) have the parameter logic in Rust.
  • If Fixed ZZFeatureMap not accepting a list of entanglement #12767 is merged, this will be handled in a follow up. Edit: Already included in this, as it was a minor update only.

@Cryoris Cryoris added Changelog: New Feature Include in the "Added" section of the changelog Rust This PR or issue is related to Rust code in the repository mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Aug 27, 2024
@Cryoris Cryoris added this to the 1.3.0 milestone Aug 27, 2024
@Cryoris Cryoris requested a review from a team as a code owner August 27, 2024 09:29
@qiskit-bot
Copy link
Collaborator

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

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

@coveralls
Copy link

coveralls commented Aug 27, 2024

Pull Request Test Coverage Report for Build 10813596822

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

  • 277 of 284 (97.54%) changed or added relevant lines in 8 files are covered.
  • 677 unchanged lines in 23 files lost coverage.
  • Overall coverage decreased (-0.3%) to 88.916%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/accelerate/src/circuit_library/entanglement.rs 26 27 96.3%
qiskit/circuit/library/data_preparation/pauli_feature_map.py 23 24 95.83%
crates/accelerate/src/circuit_library/pauli_feature_map.rs 208 210 99.05%
crates/circuit/src/operations.rs 12 15 80.0%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/mixins/multiply.py 1 93.75%
crates/accelerate/src/circuit_library/entanglement.rs 1 96.48%
qiskit/synthesis/one_qubit/one_qubit_decompose.py 1 70.67%
qiskit/quantum_info/operators/mixins/group.py 1 92.59%
qiskit/quantum_info/operators/channel/quantum_channel.py 1 72.14%
qiskit/quantum_info/operators/mixins/adjoint.py 1 90.0%
qiskit/quantum_info/operators/mixins/linear.py 1 88.0%
qiskit/visualization/circuit/_utils.py 2 94.25%
crates/qasm2/src/lex.rs 2 92.73%
qiskit/circuit/classicalfunction/types.py 3 0.0%
Totals Coverage Status
Change from base Build 10794993036: -0.3%
Covered Lines: 73388
Relevant Lines: 82536

💛 - Coveralls

/// insert_barriers: Whether to insert barriers in between the Hadamard and evolution layers.
/// data_map_func: An accumulation function that takes as input a vector of parameters the
/// current gate acts on and returns a scalar.
///
Copy link
Member

Choose a reason for hiding this comment

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

parameter_prefix appears in the python code but not here. parameters appear here and not in the python code.

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 a bit confusing that both the python and rust functions have the same name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the parameters are constructed Python-side because (1) we avoid calling Python once we're in Rust space and (2) I'm not even sure we can create ParameterVectors Rust side 🤔

And I gave them the same name because they do the same thing 🙂 I know we often have function_inner on the Rust side but IMO that makes it look like it's some internal-only function -- even though they are perfectly fine to be called from the Rust interface 😄 but we can discuss this, if you want to change it 👍🏻

Copy link
Member

@ShellyGarion ShellyGarion left a comment

Choose a reason for hiding this comment

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

Very nice PR! I only have a few questions.

Copy link
Contributor

@alexanderivrii alexanderivrii 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 the great work, the PR is in a very good shape. The only minor question that I had is why several python tests for the (older) PauliFeatureMap were not transferred to the (newer) pauli_feature_map.

The other comment is that the pauli_feature_map method in this PR essentially implements the very general circuit construction from NLocal (namely the _build method) in the special case of how it's called from PauliFeatureMap, and I am wondering if continuing this thread may lead to some code duplication in the future. However, I am completely fine with how this looks right now, since, were we to decide to do so, we could always implement the extended build_nlocal method as a function and let pauli_feature_map call this function with appropriate arguments. Personally I also find the new code easier to read because I do not need to carefully trace which arguments in NLocal's _build are set to which values and would be against this potential future generalization, but some other people may think otherwise. You do not need to do anything, but I am leaving this as a comment to see if anyone would strongly object.

Last but not least, I am awed by your Rust skills and have learned quite a few things by reviewing this PR. 🦀

- improve readability
- only create barrier if necessary
performance seems to be unaffected :)
Copy link
Contributor

@alexanderivrii alexanderivrii 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 the improvements, LGTM!

@alexanderivrii alexanderivrii added this pull request to the merge queue Sep 12, 2024
Merged via the queue into Qiskit:main with commit 1ff6361 Sep 12, 2024
15 checks passed
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Sep 18, 2024
* port PauliFeatureMap to Rust

* more docs, less error expecting

* cleanup, docstrings

* clippy & speedup

* base for dictionary entanglement

* rm unnecessary pub

* add reno

* refactor ``rmultiply_param``

* fix & test dict entanglement

* clippy

* Refactor Rust code a bit

- improve readability
- only create barrier if necessary

* RIP iterators (for proper error handling)

performance seems to be unaffected :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the 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.

6 participants