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 CircuitData::__getitem__ #11842

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Feb 20, 2024

Summary

The order of the variants in SliceOrInt determines which is tried first by the FromPyObject derivation. Int is more common in like 99.9+% of cases, so wants to be first.

Details and comments

For the microbenchmark:

import qiskit
qc = qiskit.QuantumCircuit(1)
for _ in [None]*10_000:
    qc.x(0)
d = qc.data
%timeit d[-1]

I saw the timings go from ~350ns on my machine to ~280ns with the PR, which is a 20% improvement. That's not as impressive as Qiskit/rustworkx#1096 because we're doing more object-generation work here within the method, but still nothing to sneeze at for a one-line change.

edit: having looked closer - I made this in a big rush at the end of the day, having just spotted the problem on Rustworkx - there's no reason __getitem__ should have shown a speedup here; it manually avoids using SliceOrInt (quite possibly for the exact reason of this PR). Running the numbers again just now (post merge) showed the same behaviour for both this PR and its parent, so it might just be I was tricked by thermal throttling the first time around. Still, this PR should improve __setitem__ and __delitem__.

The order of the variants in `SliceOrInt` determines which is tried
first by the `FromPyObject` derivation.  `Int` is more common in like
99.9+% of cases, so wants to be first.
@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable performance Changelog: None Do not include in 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 Feb 20, 2024
@jakelishman jakelishman added this to the 1.0.1 milestone Feb 20, 2024
@jakelishman jakelishman requested a review from a team as a code owner February 20, 2024 18:53
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

Pull Request Test Coverage Report for Build 7978537934

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.03%) to 89.291%

Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 1 91.94%
Totals Coverage Status
Change from base Build 7968492969: 0.03%
Covered Lines: 58902
Relevant Lines: 65966

💛 - Coveralls

@mtreinish mtreinish added this pull request to the merge queue Feb 20, 2024
Merged via the queue into Qiskit:main with commit 6a8bdb7 Feb 20, 2024
11 of 12 checks passed
mergify bot pushed a commit that referenced this pull request Feb 20, 2024
The order of the variants in `SliceOrInt` determines which is tried
first by the `FromPyObject` derivation.  `Int` is more common in like
99.9+% of cases, so wants to be first.

(cherry picked from commit 6a8bdb7)
@jakelishman jakelishman deleted the getitem-perf branch February 20, 2024 20:39
mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request Feb 20, 2024
After doing some post-merge analysis on the impact of Qiskit#11842 it turns
out the enum being modified in that PR was duplicated in the rust code
unnecessarily. The same enum was already defined in the
euler_one_qubit_decomposer module for it's custom 1 qubit gate sequence
return type. This commit updates the code so it just use the one
defined in the circuit data module that has the fix from Qiskit#11842 applied
already.
github-merge-queue bot pushed a commit that referenced this pull request Feb 20, 2024
The order of the variants in `SliceOrInt` determines which is tried
first by the `FromPyObject` derivation.  `Int` is more common in like
99.9+% of cases, so wants to be first.

(cherry picked from commit 6a8bdb7)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
github-merge-queue bot pushed a commit that referenced this pull request Feb 21, 2024
* Deduplicate SliceOrInt

After doing some post-merge analysis on the impact of #11842 it turns
out the enum being modified in that PR was duplicated in the rust code
unnecessarily. The same enum was already defined in the
euler_one_qubit_decomposer module for it's custom 1 qubit gate sequence
return type. This commit updates the code so it just use the one
defined in the circuit data module that has the fix from #11842 applied
already.

* Move enum to common location
mergify bot pushed a commit that referenced this pull request Feb 21, 2024
* Deduplicate SliceOrInt

After doing some post-merge analysis on the impact of #11842 it turns
out the enum being modified in that PR was duplicated in the rust code
unnecessarily. The same enum was already defined in the
euler_one_qubit_decomposer module for it's custom 1 qubit gate sequence
return type. This commit updates the code so it just use the one
defined in the circuit data module that has the fix from #11842 applied
already.

* Move enum to common location

(cherry picked from commit 802ac51)
github-merge-queue bot pushed a commit that referenced this pull request Feb 21, 2024
* Deduplicate SliceOrInt

After doing some post-merge analysis on the impact of #11842 it turns
out the enum being modified in that PR was duplicated in the rust code
unnecessarily. The same enum was already defined in the
euler_one_qubit_decomposer module for it's custom 1 qubit gate sequence
return type. This commit updates the code so it just use the one
defined in the circuit data module that has the fix from #11842 applied
already.

* Move enum to common location

(cherry picked from commit 802ac51)

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
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 performance Rust This PR or issue is related to Rust code in the repository stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants