Skip to content

Gate callers check qubits#25

Merged
emilkovacev merged 2 commits into
Qiskit:mainfrom
MIWdlB:invalid-gates
Feb 8, 2026
Merged

Gate callers check qubits#25
emilkovacev merged 2 commits into
Qiskit:mainfrom
MIWdlB:invalid-gates

Conversation

@MIWdlB

@MIWdlB MIWdlB commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

As discussed in #24, it is currently possible to pass invalid qubit indices to QuantumCircuit::gate().

This PR:

  • Marks gate as unsafe, adding safety docstring.
  • Adds logic to callers of gate, checking for valid qubit indices.
  • Adds tests that callers return QiskitError:MismatchedQubits when the quantum register is too small or invalid qubit indices are used.

Any feedback or comments are very welcome! I'm sure there's a cleaner way to implement the tests but keeping them simple and explicit seemed fine for now.

@CLAassistant

CLAassistant commented Feb 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@emilkovacev emilkovacev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for looking into this! I added a few nits, but overall the tests and code changes seem reasonable.

Comment thread src/qiskit.rs Outdated
Comment thread src/qiskit.rs Outdated
Comment thread src/qiskit.rs Outdated
review updates
circuit ins test error
@MIWdlB

MIWdlB commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

@emilkovacev Thanks for the review, I've been through and updated. The only other change is to test_circuit_instructions as it began with a cx(0,0).

@MIWdlB MIWdlB requested a review from emilkovacev February 3, 2026 14:14

@emilkovacev emilkovacev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, thanks!

Comment thread src/qiskit.rs

/// Check if all qubits are within the range of the circuit.
///
/// Mutable as `num_qubits()` takes a mutable reference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be worth revisiting this, since I see that fn num_instructions uses an immutable self. That being said it's likely out of scope of this change.

@emilkovacev emilkovacev added this pull request to the merge queue Feb 8, 2026
Merged via the queue into Qiskit:main with commit eff8795 Feb 8, 2026
2 checks passed
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