Skip to content

Commit

Permalink
Ensure error-checking happens before any modification
Browse files Browse the repository at this point in the history
After the move of `QuantumCircuit.data` to Rust-space, some of the state
modification had started happening before the error checking, which
could potentially leave an in-place modification in a partially applied
state if an exception triggered during processing.

This also ensures that the qubits and clbits arguments are checked, even
if the actual data being composed is empty.
  • Loading branch information
jakelishman committed Jan 5, 2024
1 parent 675ee04 commit 7ee3333
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,21 +997,6 @@ def compose(
"Trying to compose with another QuantumCircuit which has more 'in' edges."
)

for gate, cals in other.calibrations.items():
dest._calibrations[gate].update(cals)

dest.global_phase += other.global_phase

if not other.data:
# Nothing left to do. Plus, accessing 'data' here is necessary
# to trigger any lazy building since we now access '_data'
# directly.
return None if inplace else dest

# The 'qubits' and 'clbits' used for 'dest'.
mapped_qubits: list[Qubit]
mapped_clbits: list[Clbit]

# Maps bits in 'other' to bits in 'dest'. Used only for
# adjusting bits in variables (e.g. condition and target).
edge_map: dict[Qubit | Clbit, Qubit | Clbit] = {}
Expand Down Expand Up @@ -1047,6 +1032,21 @@ def compose(
)
edge_map.update(zip(other.clbits, dest.cbit_argument_conversion(clbits)))

for gate, cals in other.calibrations.items():
dest._calibrations[gate].update(cals)

dest.global_phase += other.global_phase

if not other.data:
# Nothing left to do. Plus, accessing 'data' here is necessary
# to trigger any lazy building since we now access '_data'
# directly.
return None if inplace else dest

# The 'qubits' and 'clbits' used for 'dest'.
mapped_qubits: list[Qubit]
mapped_clbits: list[Clbit]

variable_mapper = _classical_resource_map.VariableMapper(
dest.cregs, edge_map, dest.add_register
)
Expand Down

0 comments on commit 7ee3333

Please sign in to comment.