Skip to content

Remove redundant name check when adding QuantumCircuit registers. - #16678

Merged
raynelfss merged 12 commits into
Qiskit:mainfrom
davidfcohen:create-register-perf
Jul 29, 2026
Merged

Remove redundant name check when adding QuantumCircuit registers.#16678
raynelfss merged 12 commits into
Qiskit:mainfrom
davidfcohen:create-register-perf

Conversation

@davidfcohen

@davidfcohen davidfcohen commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Description

QuantumCircuit wraps RegisterData on the Rust side. RegisterData::add_register returns the RegisterAlreadyExists when the name is in use. This error is properly raised on the Python side without manual checking.

The error is handled on the Rust side; thus, the check is redundant in the Python code. I removed the redundant check in qiskit/circuit/quantumcircuit.py.

Generally, the overall time complexity of checking whether a given register name exists was reduced from O(n) to O(1).

Fix #7133

Performance

Registers Added Before (ms) After (ms)
100 7 5
1000 478 22
10000 45211 216

AI/LLM disclosure

  • I didn't use LLM tooling, or only used it privately.
  • I used the following tool to help write this PR description:
  • I used the following tool to generate or modify code:

@davidfcohen
davidfcohen requested a review from raynelfss July 28, 2026 18:55
@CLAassistant

CLAassistant commented Jul 28, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@davidfcohen davidfcohen changed the title [WIP] Remove redundant name check when adding registers for QuantumCircuit. [WIP] Remove redundant name check when adding QuantumCircuit registers. Jul 28, 2026
@raynelfss raynelfss self-assigned this Jul 28, 2026
@davidfcohen davidfcohen added Changelog: None Do not include in the GitHub Release changelog. Changelog: Performance Performance improvements without API and semantic changes. and removed Changelog: None Do not include in the GitHub Release changelog. labels Jul 28, 2026
@davidfcohen
davidfcohen marked this pull request as ready for review July 28, 2026 19:55
@davidfcohen
davidfcohen requested a review from a team as a code owner July 28, 2026 19:55
@qiskit-bot

Copy link
Copy Markdown
Collaborator

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

  • @Qiskit/terra-core

@raynelfss raynelfss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a good start. Thank you for being so steadfast.

The fix itself is good. However, there are some nuances I pointed out during the review here.

You may also want to drop the "[WIP]" from PR title.

Comment thread releasenotes/notes/create-register-performance-fe392209793d151f.yaml Outdated
Comment thread create-registers.py Outdated
Comment thread releasenotes/notes/create-register-performance-fe392209793d151f.yaml Outdated
@davidfcohen davidfcohen changed the title [WIP] Remove redundant name check when adding QuantumCircuit registers. Remove redundant name check when adding QuantumCircuit registers. Jul 28, 2026

@raynelfss raynelfss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is about ready to merge. I have a couple formatting suggestions for your release note here.

Comment thread releasenotes/notes/create-register-performance-fe392209793d151f.yaml Outdated
Comment thread releasenotes/notes/create-register-performance-fe392209793d151f.yaml Outdated
davidfcohen and others added 2 commits July 29, 2026 10:30
…f.yaml

Co-authored-by: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com>
Co-authored-by: Raynel Sanchez <87539502+raynelfss@users.noreply.github.com>

@raynelfss raynelfss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This LGTM, thank you the quick turnaround.

@raynelfss raynelfss added performance mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jul 29, 2026
@raynelfss
raynelfss enabled auto-merge July 29, 2026 14:51
@raynelfss
raynelfss added this pull request to the merge queue Jul 29, 2026
Merged via the queue into Qiskit:main with commit 2595d8d Jul 29, 2026
33 checks passed
@Cryoris

Cryoris commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

@davidfcohen quick follow up note: it seems the error triggered now has a word repetition/typo, could we fix that while we're at it?

from qiskit import QuantumCircuit, QuantumRegister

qr1 = QuantumRegister(1, "a")
qr2 = QuantumRegister(1, "a")

qc = QuantumCircuit(qr1, qr2)

gives now

qiskit.circuit.exceptions.CircuitError: 'register name register name "a" already exists already exists'

(the "already exists" is duplicated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Changelog: Performance Performance improvements without API and semantic changes. 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.

circuit initialization scales quadratically in number of registers

5 participants