Skip to content

Commit

Permalink
Merge pull request #2424 from Ericgig/bug.mcfeedback
Browse files Browse the repository at this point in the history
Ensure feedback work for all trajectories
  • Loading branch information
Ericgig committed May 9, 2024
2 parents 7bb79e9 + 66e263b commit 166212f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion qutip/core/cy/qobjevo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,13 @@ cdef class QobjEvo:
f"Desired feedback {key} is not available for the {solver}."
)
new_args[key] = solvers_feeds[feed]
self.arguments(**new_args)

if new_args:
cache = []
self.elements = [
element.replace_arguments(new_args, cache=cache)
for element in self.elements
]

def _update_feedback(QobjEvo self, QobjEvo other=None):
"""
Expand Down
17 changes: 13 additions & 4 deletions qutip/tests/solver/test_mcsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,24 @@ def test_MCSolver_stepping():
assert state.isket


def _coeff_collapse(t, A):
if t == 0:
# New trajectory, was collapse list reset?
assert len(A) == 0
if t > 2.75:
# End of the trajectory, was collapse list was filled?
assert len(A) != 0
return (len(A) < 3) * 1.0


@pytest.mark.parametrize(["func", "kind"], [
pytest.param(
lambda t, A: A-4,
lambda: qutip.MCSolver.ExpectFeedback(qutip.num(10)),
# 7.+0j,
id="expect"
),
pytest.param(
lambda t, A: (len(A) < 3) * 1.0,
_coeff_collapse,
lambda: qutip.MCSolver.CollapseFeedback(),
id="collapse"
),
Expand All @@ -495,9 +504,9 @@ def test_feedback(func, kind):
solver = qutip.MCSolver(
H,
c_ops=[qutip.QobjEvo([a, func], args={"A": kind()})],
options={"map": "serial"}
options={"map": "serial", "max_step": 0.2}
)
result = solver.run(
psi0,np.linspace(0, 3, 31), e_ops=[qutip.num(10)], ntraj=10
psi0, np.linspace(0, 3, 31), e_ops=[qutip.num(10)], ntraj=10
)
assert np.all(result.expect[0] > 4. - tol)

0 comments on commit 166212f

Please sign in to comment.