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

Improve performance of RB circuit generation #1263

Merged
merged 3 commits into from
Sep 6, 2023

Conversation

eendebakpt
Copy link
Contributor

Summary

The circuit generation for RB experiments is slow due to the use of indexing on sparse matrices (_CLIFFORD_COMPOSE_2Q from qiskit_experiments.library.randomized_benchmarking.clifford_utils). Since indexing is the only operation used on _CLIFFORD_COMPOSE_2Q we can improve performance by using the lil_matrix format.

Details and comments

Improve performance of RB circuit generation by changing the format of _CLIFFORD_COMPOSE_2Q to lil_matrix.

Micro benchmark:

import scipy.sparse
from qiskit_experiments.library.randomized_benchmarking.clifford_utils import _CLIFFORD_COMPOSE_2Q

_CLIFFORD_COMPOSE_2Q_lil =  scipy.sparse.lil_matrix(_CLIFFORD_COMPOSE_2Q)

%timeit _CLIFFORD_COMPOSE_2Q[2,3]
%timeit _CLIFFORD_COMPOSE_2Q_lil[2,3]

Result:

29.4 µs ± 4.01 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
2.64 µs ± 777 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

Application benchmark:

import time
import qiskit
from qiskit_experiments.library import InterleavedRB

gate=qiskit.circuit.library.CXGate()
rb=InterleavedRB(gate, physical_qubits= (0,1),lengths= [1,10,50,100,200], num_samples= 10)

t0=time.time()
c=rb.circuits()
dt=time.time()-t0

results in a speedup from 2.1419544219970703 to 1.154689073562622 seconds.

PR checklist (delete when all criteria are met)

  • I have read the contributing guide CONTRIBUTING.md.
  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have added a release note file using reno if this change needs to be documented in the release notes. (no release note required)

Copy link
Contributor

@itoko itoko left a comment

Choose a reason for hiding this comment

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

Thanks, @eendebakpt I think this is really a nice idea. Although it requires a matrix conversion once when loading the data, I've confirmed the conversion overhead is negligible (less than 0.1 seconds in my env).
I approve as the test failure seems irrelevant to the change in this PR.

@coruscating coruscating added this pull request to the merge queue Sep 6, 2023
Merged via the queue into qiskit-community:main with commit 60655bb Sep 6, 2023
nkanazawa1989 pushed a commit to nkanazawa1989/qiskit-experiments that referenced this pull request Jan 10, 2024
### Summary

The circuit generation for RB experiments is slow due to the use of
indexing on sparse matrices (`_CLIFFORD_COMPOSE_2Q` from
`qiskit_experiments.library.randomized_benchmarking.clifford_utils`).
Since indexing is the only operation used on `_CLIFFORD_COMPOSE_2Q` we
can improve performance by using the `lil_matrix` format.

### Details and comments

Improve performance of RB circuit generation by changing the format of
`_CLIFFORD_COMPOSE_2Q` to `lil_matrix`.

Micro benchmark:
```
import scipy.sparse
from qiskit_experiments.library.randomized_benchmarking.clifford_utils import _CLIFFORD_COMPOSE_2Q

_CLIFFORD_COMPOSE_2Q_lil =  scipy.sparse.lil_matrix(_CLIFFORD_COMPOSE_2Q)

%timeit _CLIFFORD_COMPOSE_2Q[2,3]
%timeit _CLIFFORD_COMPOSE_2Q_lil[2,3]
```
Result:
```
29.4 µs ± 4.01 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
2.64 µs ± 777 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
```

Application benchmark:
```
import time
import qiskit
from qiskit_experiments.library import InterleavedRB

gate=qiskit.circuit.library.CXGate()
rb=InterleavedRB(gate, physical_qubits= (0,1),lengths= [1,10,50,100,200], num_samples= 10)

t0=time.time()
c=rb.circuits()
dt=time.time()-t0
```
results in a speedup from 2.1419544219970703 to 1.154689073562622
seconds.



### PR checklist (delete when all criteria are met)

- [x] I have read the contributing guide `CONTRIBUTING.md`.
- [x] I have added the tests to cover my changes.
- [x] I have updated the documentation accordingly.
- [x] I have added a release note file using `reno` if this change needs
to be documented in the release notes. (no release note required)

---------

Co-authored-by: Helena Zhang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants