Skip to content

Commit

Permalink
Fix qutip Qobj handling for qutip 5.0.0 (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPuzzuoli authored Apr 9, 2024
1 parent 5f40346 commit 1c1bf87
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
10 changes: 2 additions & 8 deletions qiskit_dynamics/arraylias/register_functions/asarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ def _isinstance_qutip_qobj(obj):
Returns:
Bool: True if obj is qutip Qobj
"""
if (
type(obj).__name__ == "Qobj"
and hasattr(obj, "_data")
and type(obj._data).__name__ == "fast_csr_matrix"
):
return True
return False
return type(obj).__name__ == "Qobj"


def register_asarray(alias):
Expand All @@ -44,7 +38,7 @@ def register_asarray(alias):
@alias.register_default(path="asarray")
def _(arr):
if _isinstance_qutip_qobj(arr):
return csr_matrix(arr)
return arr.full()
return np.asarray(arr)

@alias.register_function(lib="scipy_sparse", path="asarray")
Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/qutip-qobj-handling-5ed79d8e4b5e96a7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
upgrade:
- |
To enable compatibility with QuTiP 5.0.0, ``qutip.Qobj`` instances are now converted to a raw
array type in Dynamics via the ``qutip.Qobj.full`` method. This method always returns a dense
array, and hence ``qutip.Qobj`` instances are no longer turned into ``scipy.sparse.csr_matrix``
instances in Dynamics.
4 changes: 2 additions & 2 deletions test/dynamics/arraylias/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,5 @@ def test_qutip_conversion(self):
qobj = Qobj([[0, 1], [1, 0]])

out = unp.asarray(qobj)
self.assertTrue(isinstance(out, csr_matrix))
self.assertAllClose(out.todense(), np.array([[0.0, 1.0], [1.0, 0.0]]))
self.assertTrue(isinstance(out, np.ndarray))
self.assertAllClose(out, np.array([[0.0, 1.0], [1.0, 0.0]]))
10 changes: 5 additions & 5 deletions test/dynamics/models/test_rotating_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,16 +521,16 @@ def test_state_transformations_no_frame_qobj_type(self):
t = 0.123
y = qutip.Qobj([[1.0, 1]])
out = rotating_frame.state_into_frame(t, y)
self.assertTrue(isinstance(out, csr_matrix))
self.assertTrue(isinstance(out, np.ndarray))
out = rotating_frame.state_out_of_frame(t, y)
self.assertTrue(isinstance(out, csr_matrix))
self.assertTrue(isinstance(out, np.ndarray))

t = 100.12498
y = csr_matrix(np.eye(2))
y = qutip.Qobj(np.eye(2))
out = rotating_frame.state_into_frame(t, y)
self.assertTrue(isinstance(out, csr_matrix))
self.assertTrue(isinstance(out, np.ndarray))
out = rotating_frame.state_out_of_frame(t, y)
self.assertTrue(isinstance(out, csr_matrix))
self.assertTrue(isinstance(out, np.ndarray))

def test_state_transformations_no_frame_Operator_types(self):
"""Test frame transformations with no frame."""
Expand Down

0 comments on commit 1c1bf87

Please sign in to comment.