From ad14a64d45964cc099c64c87a9d222a1a31b0287 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Thu, 21 Dec 2023 18:38:42 +0000 Subject: [PATCH] Fix parametric global phase in `QuantumCircuit.remove_final_measurements` This was overlooked in the changes of 70901b25; the trick is that `remove_final_measurements` does fairly deep in-place modifications to the circuit, despite not logically being an in-place operation, so a lot of internal data has to be touched and kept in sync. --- qiskit/circuit/quantumcircuit.py | 2 ++ test/python/circuit/test_circuit_operations.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py index 11070059c927..cbba34886402 100644 --- a/qiskit/circuit/quantumcircuit.py +++ b/qiskit/circuit/quantumcircuit.py @@ -2756,6 +2756,8 @@ def remove_final_measurements(self, inplace: bool = True) -> Optional["QuantumCi # Clear instruction info circ._data = CircuitData(qubits=circ._data.qubits, reserve=len(circ._data)) circ._parameter_table.clear() + # Repopulate the parameter table with any global-phase entries. + circ.global_phase = circ.global_phase # We must add the clbits first to preserve the original circuit # order. This way, add_register never adds clbits and just diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py index 7d7a930f7cec..f794a13ab504 100644 --- a/test/python/circuit/test_circuit_operations.py +++ b/test/python/circuit/test_circuit_operations.py @@ -836,6 +836,24 @@ def test_remove_final_measurements_bit_locations(self): self.assertEqual(circuit.find_bit(c0[0]), BitLocations(0, [(c0, 0)])) self.assertEqual(circuit.find_bit(c3[0]), BitLocations(1, [(c3, 0)])) + def test_remove_final_measurements_parametric_global_phase(self): + """Test that a parametric global phase is respected in the table afterwards.""" + a = Parameter("a") + qc = QuantumCircuit(2, 2, global_phase=a) + qc.h(0) + qc.cx(0, 1) + qc.measure([0, 1], [0, 1]) + + expected = QuantumCircuit(2, global_phase=1) + expected.h(0) + expected.cx(0, 1) + + self.assertEqual( + qc.remove_final_measurements(inplace=False).assign_parameters({a: 1}), expected + ) + qc.remove_final_measurements(inplace=True) + self.assertEqual(qc.assign_parameters({a: 1}), expected) + def test_reverse(self): """Test reverse method reverses but does not invert.""" qc = QuantumCircuit(2, 2)