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

mesolve e_ops list can mix callable and Qobj #2184

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/2118.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mesolve can support mixed callable and Qobj e_ops.
15 changes: 10 additions & 5 deletions qutip/mesolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,21 +449,26 @@ def _generic_ode_solve(func, ode_args, rho0, tlist, e_ops, opt,

e_ops_data = []
output.expect = []
need_qobj_state = opt.store_states
if callable(e_ops):
n_expt_op = 0
expt_callback = True
output.num_expect = 1
need_qobj_state = True
elif isinstance(e_ops, list):
n_expt_op = len(e_ops)
expt_callback = False
output.num_expect = n_expt_op
if n_expt_op == 0:
# fall back on storing states
opt.store_states = True
need_qobj_state = True
else:
for op in e_ops:
if not isinstance(op, Qobj) and callable(op):
output.expect.append(np.zeros(n_tsteps, dtype=complex))
need_qobj_state = True
e_ops_data.append(None)
continue
if op.dims != rho0.dims:
raise TypeError(f"e_ops dims ({op.dims}) are not "
Expand Down Expand Up @@ -497,7 +502,7 @@ def get_curr_state_data(r):
"the allowed number of substeps by increasing "
"the nsteps parameter in the Options class.")

if opt.store_states or expt_callback:
if need_qobj_state:
cdata = get_curr_state_data(r)
fdata = dense2D_to_fastcsr_fmode(cdata, size, size)

Expand All @@ -517,10 +522,10 @@ def get_curr_state_data(r):
for m in range(n_expt_op):
if not isinstance(e_ops[m], Qobj) and callable(e_ops[m]):
output.expect[m][t_idx] = e_ops[m](t, rho_t)
continue
output.expect[m][t_idx] = expect_rho_vec(e_ops_data[m], r.y,
e_ops[m].isherm
and rho0.isherm)
else:
output.expect[m][t_idx] = expect_rho_vec(e_ops_data[m], r.y,
e_ops[m].isherm
and rho0.isherm)

if t_idx < n_tsteps - 1:
r.integrate(r.t + dt[t_idx])
Expand Down
16 changes: 16 additions & 0 deletions qutip/tests/test_mesolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,22 @@ def test_tlist_h_with_constant_c_ops():
assert len(result.states) == len(few_times)


def test_mixed_e_ops():
"""
Test callable and Qobj e_ops can mix.

See gh-2118.
"""
state = basis(2, 0)
hamiltonian = QobjEvo(sigmax())
collapse = create(2)
e_ops = [qeye(2), lambda t, qobj: qobj.norm()]
result = mesolve(
hamiltonian, state, [0, 1, 2], c_ops=[collapse], e_ops=e_ops
)
assert result.num_expect == 2


def test_tlist_h_with_other_tlist_c_ops_raises():
state = basis(2, 0)
all_times = np.linspace(0, 1, 11)
Expand Down