qiskit-community / qiskit-dynamics

Tools for building and solving models of quantum systems in Qiskit
https://qiskit-community.github.io/qiskit-dynamics/
Apache License 2.0
105 stars 61 forks source link

DynamicsBackend.from_backend throws a ValueError when given a float rwa_cutoff_freq parameter #309

Open donsano33 opened 9 months ago

donsano33 commented 9 months ago

Informations

What is the current behavior?

The call to DynamicsBackend.from_backend throws a ValueError if given the parameter rwa_cutoff_freq=5e9. The docstring states, that a float parameter can be given.

Error Message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File [~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/array/array.py:212](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/array/array.py:212), in Array.__float__(self)
    [211](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/array/array.py:211)     raise TypeError("only size-1 Arrays can be converted to Python scalars.")
--> [212](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/array/array.py:212) return float(self._data)

TypeError: float() argument must be a string or a real number, not 'complex'

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[4], [line 5](vscode-notebook-cell:?execution_count=4&line=5)
      [2](vscode-notebook-cell:?execution_count=4&line=2) from qiskit_dynamics.backend import DynamicsBackend
      [4](vscode-notebook-cell:?execution_count=4&line=4) fake_backend = FakeNairobi()
----> [5](vscode-notebook-cell:?execution_count=4&line=5) sim_backend = DynamicsBackend.from_backend(fake_backend, subsystem_list=[0,1], rwa_cutoff_freq=5e9)

File [~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:723](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:723), in DynamicsBackend.from_backend(cls, backend, subsystem_list, rotating_frame, evaluation_mode, rwa_cutoff_freq, **options)
    [719](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:719) else:
    [720](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:720)     # config is guaranteed to have a dt
    [721](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:721)     dt = backend_config.dt
--> [723](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:723) solver = Solver(
    [724](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:724)     static_hamiltonian=static_hamiltonian,
    [725](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:725)     hamiltonian_operators=hamiltonian_operators,
    [726](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:726)     hamiltonian_channels=hamiltonian_channels,
    [727](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:727)     channel_carrier_freqs=channel_freqs,
...
--> [269](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/models/rotating_wave_approximation.py:269)     carrier_freqs[i] = sig.carrier_freq
    [271](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/models/rotating_wave_approximation.py:271) num_components = len(carrier_freqs)
    [272](https://file+.vscode-resource.vscode-cdn.net/home/luja_sa/Workspace/git/quantum-optimal-control-algorithms/src/notebooks/~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/models/rotating_wave_approximation.py:272) n = current_ops[0].shape[-1]

ValueError: setting an array element with a sequence.

Steps to reproduce the problem

from qiskit.providers.fake_provider import FakeNairobi
from qiskit_dynamics.backend import DynamicsBackend

fake_backend = FakeNairobi()
sim_backend = DynamicsBackend.from_backend(fake_backend, subsystem_list=[0,1], rwa_cutoff_freq=5e9)

What is the expected behavior?

No exception is thrown and the rwa_cutoff_freq is used by the Solver instance.

Suggested solutions

None, I have not checked the cause of the error.

donsano33 commented 9 months ago

Root cause

The problem originates from the line 269 in rotating_wave_approximation.py:

carrier_freqs[i] = sig.carrier_freq

which throws as it expects a float assignment but actually receives a complex. All carrier frequency values corresponding the control channels U* are of type complex while the ones for the drive channels D* are float. The values for sig.carrier_freq are extracted from the backend_config in DynamicsBackend.from_backend in line 677 of dynamics_backend.py by calling _get_backend_channel_freqs(...).

For control channels the values are calculated in line 1059.

freq += drive_frequencies[channel_lo.q] * channel_lo.scale

where channel_lo.scale is of type complex and therefore causes the issue.

Suggested solution

As the datatype of UchannelLO.scale is fixed at complex (https://docs.quantum.ibm.com/api/qiskit/qiskit.providers.models.UchannelLO) I would just use the real part of the scale factor .

for channel_lo in u_channel_lo[idx]:
    freq += drive_frequencies[channel_lo.q] * channel_lo.scale.real

I do not really understand why this is a complex number, maybe you have some insight in this?