Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propagator does not pass t to QobjEvo when constructing Hamiltonian from array coeffecients #2432

Open
NeoFantom opened this issue May 19, 2024 · 0 comments

Comments

@NeoFantom
Copy link

Bug Description

The propagator() function SHOULD support the array-coefficient format:

propagator(H=[[sigmax(), np.cos(tlist)]], tlist=tlist)

However, this code generates an error complaining ValueError: tlist must be the same len as the array to interpolate. This is due to a line of wrong code inside propagator() function:

def propagator(H, t, c_ops=(), args=None, options=None, **kwargs):
    if isinstance(t, numbers.Real):
        tlist = [0, t]
        list_output = False
    else:
        tlist = t
        list_output = True

    if not isinstance(H, (Qobj, QobjEvo)):
        H = QobjEvo(H, args=args, **kwargs)
    ...

In the last line above, the code forgot to pass tlist=tlist to QobjEvo. This is an optional argument only required when no coefficients are supplied as numpy arrays. Therefore, when we feed a numpy array to propagator and then to QobjEvo, it is reported ValueError: tlist must be the same len as the array to interpolate, while the real error here is because no tlist is provided.

You can compare it to the correct code in mesolve:

def mesolve(H, rho0, tlist, c_ops=None, e_ops=None, args=None, options=None, **kwargs):
    options = _solver_deprecation(kwargs, options)
    H = QobjEvo(H, args=args, tlist=tlist)
    ...

Code to Reproduce the Bug

tlist = np.linspace(0, 50, 500)
q.propagator([[q.sigmax(), np.cos(tlist)]], tlist)

Code Output

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[26], line 2
      1 tlist = np.linspace(0, 50, 500)
----> 2 q.propagator([[q.sigmax(), np.cos(tlist)]], tlist).states[-1]

File c:\Users\neohu\.conda\envs\de\Lib\site-packages\qutip\solver\propagator.py:62, in propagator(H, t, c_ops, args, options, **kwargs)
     59     list_output = True
     61 if not isinstance(H, (Qobj, QobjEvo)):
---> 62     H = QobjEvo(H, args=args, **kwargs)
     64 if c_ops:
     65     H = liouvillian(H, c_ops)

File c:\Users\neohu\.conda\envs\de\Lib\site-packages\qutip\core\cy\qobjevo.pyx:223, in qutip.core.cy.qobjevo.QobjEvo.__init__()

File c:\Users\neohu\.conda\envs\de\Lib\site-packages\qutip\core\cy\qobjevo.pyx:272, in qutip.core.cy.qobjevo.QobjEvo._read_element()

File c:\Users\neohu\.conda\envs\de\Lib\site-packages\qutip\core\coefficient.py:175, in coefficient(base, tlist, args, args_ctypes, order, compile_opt, function_style, boundary_conditions, **kwargs)
    173 for type_ in coefficient_builders:
    174     if isinstance(base, type_):
--> 175         return coefficient_builders[type_](base, **kwargs)
    177 if callable(base):
    178     op = FunctionCoefficient(base, args.copy(), style=function_style)

File c:\Users\neohu\.conda\envs\de\Lib\site-packages\qutip\core\cy\coefficient.pyx:418, in qutip.core.cy.coefficient.InterCoefficient.__init__()

ValueError: tlist must be the same len as the array to interpolate

Expected Behaviour

Code should run without error.

Your Environment

Note that this problem is persistent in the latest 5.0.2, because I checked the source code.

QuTiP Version:      5.0.1
Numpy Version:      1.26.4
Scipy Version:      1.12.0
Cython Version:     None
Matplotlib Version: None
Python Version:     3.11.5
Number of CPUs:     12
BLAS Info:          Generic
INTEL MKL Ext:      False
Platform Info:      Windows (AMD64)

Additional Context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant