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

Speed-up QubitMapper.mode_based_mapping #397

Merged
merged 6 commits into from
Nov 5, 2021

Conversation

t-imamichi
Copy link
Contributor

@t-imamichi t-imamichi commented Oct 18, 2021

Summary

This PR improves performance of JW transformation by

  • computing SparsePauliOp of "+", "-", "E", "N" beforehand
  • reducing the overhead of SparsePauliOp._add

If we introduce a specialized function to compute the sum of SparsePauliOp (e.g., SparsePauliOp.sum(list[SparsePauliOp]) -> SparsePauliOp, we can further improve the performance around 2x. But, we need discussion to introduce a new public API to Terra.

Update: SpasePauliOp.sum is merged Qiskit/qiskit#7202. So, I replaced the workaround of sum with SparsePauliOp.sum.

Details and comments

I also prepared separate PRs to improve the performance of JW as follows:

I notice that SparsePauliOp.simplify has a critical performance issue, which is addressed at Qiskit/qiskit#7122, so, this PR with terra main still takes a long time.

Microbenchmark

Nature branch Terra branch Time
main main 61.88646222 sec
this PR main 53.311054609 sec
main stable 12.960038270999998 sec
this PR stable 10.459337683 sec
main Qiskit/qiskit#7122 12.132281858 sec
this PR Qiskit/qiskit#7122 4.158444311 sec
this PR 7122 + 7126 + 7138 1.7728687539999999 sec

Update: the final version with SparsePauliOp.sum takes 1.63 sec.

import random
from timeit import timeit

from qiskit_nature.converters.second_quantization import QubitConverter
from qiskit_nature.mappers.second_quantization import JordanWignerMapper
from qiskit_nature.operators.second_quantization import FermionicOp

random.seed(123)
k = 10
n = 100

op = sum(
    (FermionicOp(''.join(random.choices('+-ENI', k=k)), display_format="dense") * random.random())
    for _ in range(n))
mapper = JordanWignerMapper()
qubit_conv = QubitConverter(mapper)
print(f"{timeit(lambda: qubit_conv.convert(op), number=1)} sec")

@t-imamichi t-imamichi added the Performance improve code perfomance (memory or speed) label Oct 20, 2021
mtreinish pushed a commit to Qiskit/qiskit-metapackage that referenced this pull request Oct 20, 2021
Add a benchmark of SparsePauliOp._add via SparsePauliOp.__add__.

This method is an important building block of various algorithms (I'm working on the qubit mapper of Qiskit nature).
I'm working on the performance improvement of this method. I hope the improvement will be visible.

Qiskit/qiskit#7138
qiskit-community/qiskit-nature#397

* Add a benchmark of SparsePauliOp.add

* use `__add__`

* tweak param list
mrossinek
mrossinek previously approved these changes Oct 22, 2021
Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

Thanks @t-imamichi!

pbark
pbark previously approved these changes Oct 26, 2021
Copy link
Contributor

@pbark pbark left a comment

Choose a reason for hiding this comment

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

This LGTM! Thanks @t-imamichi

@t-imamichi t-imamichi dismissed stale reviews from pbark and mrossinek via a462c4c November 3, 2021 09:04
@t-imamichi
Copy link
Contributor Author

t-imamichi commented Nov 3, 2021

Because Qiskit/qiskit#7202 is merged, I replace the workaround of sum with SpasePauliOp.sum. It gets intuitive and faster.

Copy link
Member

@mrossinek mrossinek left a comment

Choose a reason for hiding this comment

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

LGTM!

@t-imamichi we can merge this when you can enable Allow edit by maintainers so that we can keep this branch updated with main 👍

@t-imamichi
Copy link
Contributor Author

Thanks. I forgot to enable it. I did it now.

@mrossinek
Copy link
Member

@woodsp-ibm Offline you mentioned that you might have some more comments on this PR. Is that still the case or can this be merged?

@woodsp-ibm woodsp-ibm merged commit 3a665e3 into qiskit-community:main Nov 5, 2021
@t-imamichi t-imamichi deleted the fast-mapper3 branch April 26, 2022 09:21
Anthony-Gandon pushed a commit to Anthony-Gandon/qiskit-nature that referenced this pull request May 25, 2023
* Speedup QubitMapper.mode_based_mapping

* update comments and fix lint

* replace `_sum` with SparsePauliOp.sum

Co-authored-by: Max Rossmannek <oss@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
jakelishman pushed a commit to jakelishman/qiskit-terra that referenced this pull request Aug 1, 2023
…#1348)

Add a benchmark of SparsePauliOp._add via SparsePauliOp.__add__.

This method is an important building block of various algorithms (I'm working on the qubit mapper of Qiskit nature).
I'm working on the performance improvement of this method. I hope the improvement will be visible.

Qiskit#7138
qiskit-community/qiskit-nature#397

* Add a benchmark of SparsePauliOp.add

* use `__add__`

* tweak param list
jakelishman pushed a commit to jakelishman/qiskit-terra that referenced this pull request Aug 11, 2023
…#1348)

Add a benchmark of SparsePauliOp._add via SparsePauliOp.__add__.

This method is an important building block of various algorithms (I'm working on the qubit mapper of Qiskit nature).
I'm working on the performance improvement of this method. I hope the improvement will be visible.

Qiskit#7138
qiskit-community/qiskit-nature#397

* Add a benchmark of SparsePauliOp.add

* use `__add__`

* tweak param list
SamD-1998 pushed a commit to SamD-1998/qiskit-terra that referenced this pull request Sep 7, 2023
…#1348)

Add a benchmark of SparsePauliOp._add via SparsePauliOp.__add__.

This method is an important building block of various algorithms (I'm working on the qubit mapper of Qiskit nature).
I'm working on the performance improvement of this method. I hope the improvement will be visible.

Qiskit#7138
qiskit-community/qiskit-nature#397

* Add a benchmark of SparsePauliOp.add

* use `__add__`

* tweak param list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Performance improve code perfomance (memory or speed)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants