qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

Warning on `add_bits` with layout information

Open kaldag opened this issue 6 months ago • 3 comments

Summary

The method add_bits does not check if the circuit has layout information. The layout information is not updated when the method add_bits is called by a circuit already having layout information. A warning is added to inform if a circuit has layout information.

Details and comments

A UserWarning provided to add_bits in quantumcircuit.py.

kaldag avatar Jun 11 '25 11:06 kaldag

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

  • @Qiskit/terra-core

qiskit-bot avatar Jun 11 '25 11:06 qiskit-bot

Pull Request Test Coverage Report for Build 16872034759

Details

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.04%) to 87.692%

Files with Coverage Reduction New Missed Lines %
crates/circuit/src/parameter/symbol_expr.rs 1 74.74%
crates/qasm2/src/expr.rs 1 93.63%
crates/qasm2/src/lex.rs 1 92.27%
<!-- Total: 3
Totals Coverage Status
Change from base Build 16837695320: 0.04%
Covered Lines: 82389
Relevant Lines: 93953

💛 - Coveralls

coveralls avatar Jun 11 '25 14:06 coveralls

Thanks for this Kalyan. We should also add a test that the warning is raised, both for directly adding qubits, for adding them implicitly via add_register(QuantumRegister), and also that the warning isn't raised if you only add clbits (which aren't in the layout).

I think the warning could perhaps be clearer about why it's a problem, and we might want to document in add_bits why it's a problem to add qubits to circuit that's been laid out.

Currently in the implementation, we warn but continue, which leaves the circuit in an invalid state. I'm actually thinking now: perhaps this ought to be an exception, not a warning? You need to do QuantumCircuit.layout = None before adding bits, or you're invalidating the coherence?

Added documentation and test cases as suggested.

kaldag avatar Jun 17 '25 12:06 kaldag

Thanks Kalyan - this looks sensible, the review's just minor wording stuff then it's mergeable.

Thanks a lot for the review @jakelishman. I have made all the changes you recommended.

kaldag avatar Jun 27 '25 08:06 kaldag