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)