Repository: Qiskit/qiskit

Issue:
ParameterExpression with unbound parameters (dict_keys([Parameter(__gθ0)])) cannot be cast to a float
Qiskit Algorithms runs a couple of CI jobs against the main branch here to keep an eye on ongoing changes here. Last nights CI nightly run failed https://github.com/qiskit-community/qiskit-algorithms/actions/runs/7282997053/job/19846228142
Could this be related to #11428 ? @jakelishman 

```
Captured traceback:
~~~~~~~~~~~~~~~~~~~
    Traceback (most recent call last):

      File "/home/runner/work/qiskit-algorithms/qiskit-algorithms/qiskit_algorithms/gradients/lin_comb/lin_comb_sampler_gradient.py", line 125, in _run_unique
    results = job.result()

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/primitives/primitive_job.py", line 55, in result
    return self._future.result()

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/_base.py", line 444, in result
    return self.__get_result()

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/_base.py", line 389, in __get_result
    raise self._exception

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/primitives/sampler.py", line 101, in _call
    probabilities = [

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/primitives/sampler.py", line 102, in <listcomp>
    Statevector(bound_circuit_to_instruction(circ)).probabilities_dict(

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/quantum_info/states/statevector.py", line 91, in __init__
    self._data = Statevector.from_instruction(data).data

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/quantum_info/states/statevector.py", line 768, in from_instruction
    return Statevector._evolve_instruction(vec, instruction)

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/quantum_info/states/statevector.py", line 948, in _evolve_instruction
    statevec._data *= np.exp(1j * float(obj.definition.global_phase))

      File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/qiskit/circuit/parameterexpression.py", line 415, in __float__
    raise TypeError(

    TypeError: ParameterExpression with unbound parameters (dict_keys([Parameter(__gθ0)])) cannot be cast to a float.
```    


Discussion:
It's almost certainly related - I'll have to do a little digging and see what's going on, but I feel like I saw something similar while initially making the change, which I started before algorithms was removed.  I assumed I'd fixed all of those issues, but I can look into it.

Resolving patch:
diff --git a/qiskit/circuit/quantumcircuit.py b/qiskit/circuit/quantumcircuit.py
--- 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


Test patch:
diff --git a/test/python/circuit/test_circuit_operations.py b/test/python/circuit/test_circuit_operations.py
--- 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)