Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.05k stars 2.33k forks source link

ParameterExpression with unbound parameters (dict_keys([Parameter(__gθ0)])) cannot be cast to a float #11452

Closed woodsp-ibm closed 8 months ago

woodsp-ibm commented 9 months ago

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.
jakelishman commented 9 months ago

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.

jakelishman commented 9 months ago

Ok, one bug is in QuantumCircuit.remove_final_measurements, which I could have sworn I fixed, but maybe got lost in a rebase. That's fixed in #11455.

The other is in BlueprintCircuit.copy, or perhaps more completely, it's because the data model and principle of BlueprintCircuit is at best flawed, and at worst just invalid. It's totally unreliable to append to, or generally otherwise mutate a BlueprintCircuit (which the algorithms tests do), but that's something that people try all the time, because there's no indication that you can't. Any time something does something that BlueprintCircuit considers a "modification", it invalidates itself, which involves scrubbing (some of) its state. In this case, the part of QuantumCircuit.copy_empty_like that refreshes the qregs list is counted as a modification, so BlueprintCircuit scrubs itself, including zeroing out the global phase, but copy_empty_like doesn't know that it's done that (nor should it need to know), so it doesn't restore it. This will have been zeroing the global phase out before #11428, but that PR caused it to get noticed. I've put in an override of QuantumCircuit.copy_empty_like in #11456, but this won't be the last time we find some weirdness where BlueprintCircuit is invalidating itself and throwing away data.

alexanderivrii commented 8 months ago

Which BlueprintCircuit classes are most commonly used in the algorithms? For instance, in #11463 I am adding QftGate class that should in principle replace the QFT bluecircuit class.

jakelishman commented 8 months ago

Sasha: in my informal survey, it's like 90% NLocal and subclasses like RealAmplitudes. Those could have probably been factory functions for regular QuantumCircuit instances, or potentially very simple subclasses adding an extra attribute, but I think the ship has sailed on that for Qiskit 1.0.