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

Fix quadratic dependency in QuantumCircuit.add_bits #11546

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

jakelishman
Copy link
Member

@jakelishman jakelishman commented Jan 11, 2024

Summary

This is in the Python space component only. When add_bits is called many times, it constructs as temporary set of the union of all qubits and clbits present in the circuit in order to calculate an intersection with the input. This causes the method to be linear in the number of bits already present in the circuit, whereas it should be amortised linear in the to be added.

This commit fixes the intersection to be a manual calculation that does not construct three temporary set objects, removing the quadratic cost.

Details and comments

This has been around for ages, but I noticed it when seeing excessive runtimes for parsing OQ2 and OQ3 programs constructed as

qubit q0;
qubit q1;
qubit q2;
// ...
qubit q999999;

Adding jillions of registers is still quadratic in the number of registers because there's no constant-time lookup for them in QuantumCircuit (still), but that's a known problem.

This is in the Python space component only.  When `add_bits` is called
many times, it constructs as temporary set of the union of all qubits
and clbits present in the circuit in order to calculate an intersection
with the input.  This causes the method to be linear in the number of
bits already present in the circuit, whereas it should be amortised
linear in the _to be added_.

This commit fixes the intersection to be a manual calculation that does
not construct three temporary set objects, removing the quadratic cost.
@jakelishman jakelishman added Changelog: None Do not include in changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jan 11, 2024
@jakelishman jakelishman added this to the 1.0.0 milestone Jan 11, 2024
@jakelishman jakelishman requested a review from a team as a code owner January 11, 2024 16:44
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

@coveralls
Copy link

Pull Request Test Coverage Report for Build 7491854130

Warning: This coverage report may be inaccurate.

We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
To ensure accuracy in future PRs, please see these guidelines.
A quick fix for this PR: rebase it; your next report should be accurate.

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.02%) to 89.278%

Totals Coverage Status
Change from base Build 7490243021: -0.02%
Covered Lines: 59512
Relevant Lines: 66659

💛 - Coveralls

Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

LGTM.

I'd be surprised if we don't make the same mistake in other places where we consider the set of all wires in the circuit.

@kevinhartman kevinhartman added this pull request to the merge queue Jan 11, 2024
Merged via the queue into Qiskit:main with commit 5d5bfa9 Jan 11, 2024
@jakelishman jakelishman deleted the fix-quadratic-add-bits branch January 11, 2024 18:44
ShellyGarion pushed a commit to ShellyGarion/qiskit-terra that referenced this pull request Jan 18, 2024
This is in the Python space component only.  When `add_bits` is called
many times, it constructs as temporary set of the union of all qubits
and clbits present in the circuit in order to calculate an intersection
with the input.  This causes the method to be linear in the number of
bits already present in the circuit, whereas it should be amortised
linear in the _to be added_.

This commit fixes the intersection to be a manual calculation that does
not construct three temporary set objects, removing the quadratic cost.
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants