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.solve(...) throws if y0 is a np.array #350

Closed donsano33 closed 8 months ago

donsano33 commented 8 months ago

Informations

What is the current behavior?

Calling DynamicsBackend.solve(...) with parameter y0 of type np.array throws the exception:

ValueError                                Traceback (most recent call last)
Cell In[18], line 12
      9 with pulse.build() as example_schedule:
     10     pulse.play(pulse.Constant(duration, 0.1), pulse.DriveChannel(0))
---> 12 backend.solve([example_schedule], t_span=[0, duration * FakeNairobi().configuration().dt], y0=np.eye(9))
     14 # backend.options.solver.model.static_operator.shape
     15 
     16 # backend.options.solver.solve(t_span=[0, duration * FakeNairobi().configuration().dt], y0=np.eye(9), signals=[example_schedule])

File ~/Workspace/git/quantum-optimal-control-algorithms/.venv/lib/python3.11/site-packages/qiskit_dynamics/backend/dynamics_backend.py:383, in DynamicsBackend.solve(self, solve_input, t_span, y0, convert_results, validate)
    381 if y0 is None:
    382     y0 = self.options.initial_state
--> 383 if y0 == "ground_state":
    384     y0 = Statevector(self._dressed_states[:, 0])
    386 solver_results = self.options.solver.solve(
    387     t_span=t_span,
    388     y0=y0,
   (...)
    391     **self.options.solver_options,
    392 )

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

This might also happen when using a Jax Array or so, which I haven't tried.

Steps to reproduce the problem

import numpy as np
from qiskit import pulse
from qiskit_ibm_runtime.fake_provider import FakeNairobi
from qiskit_dynamics import DynamicsBackend

backend = DynamicsBackend.from_backend(FakeNairobi(), subsystem_list=[0,1])
with pulse.build() as example_schedule:
    pulse.play(pulse.Constant(64, 0.1), pulse.DriveChannel(0))

backend.solve([example_schedule], t_span=[0, 1 * FakeNairobi().configuration().dt], y0=np.eye(9))

What is the expected behavior?

Return the solution without exception.

Suggested solutions

First naive thought would be, adding a check for str in DynamicsBackend.solve(...) before Line 383 to avoid the == between str and np.array type.