From 7ee33332fc86205c5b667d0ca7e2bd29dd725c1f Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Fri, 5 Jan 2024 13:26:55 +0000 Subject: [PATCH] Ensure error-checking happens before any modification 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. --- qiskit/circuit/quantumcircuit.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py index 2800f539b650..1d03ea090902 100644 --- a/qiskit/circuit/quantumcircuit.py +++ b/qiskit/circuit/quantumcircuit.py @@ -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] = {} @@ -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 )