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

Issue2099 #2111

Merged
merged 11 commits into from
Mar 22, 2023
1 change: 1 addition & 0 deletions doc/changes/2111.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added __repr__ to QobjEvo
15 changes: 11 additions & 4 deletions qutip/core/cy/qobjevo.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ cdef class QobjEvo:
if compress:
self.compress()

def __repr__(self):
cls = self.__class__.__name__
repr_str = f'{cls}: dims = {self.dims}, shape = {self.shape}, '
repr_str += f'type = {self.type}, superrep = {self.superrep}, '
repr_str += f'isconstant = {self.isconstant}, num_elements = {self.num_elements}'
return repr_str

def _read_element(self, op, copy, tlist, args, order, function_style,
boundary_conditions):
""" Read a Q_object item and return an element for that item. """
Expand Down Expand Up @@ -456,13 +463,13 @@ cdef class QobjEvo:
if isinstance(other, QobjEvo):
if other.dims != self.dims:
raise TypeError("incompatible dimensions" +
str(self.dims) + ", " + str(other.dims))
str(self.dims) + ", " + str(other.dims))
for element in (<QobjEvo> other).elements:
self.elements.append(element)
elif isinstance(other, Qobj):
if other.dims != self.dims:
raise TypeError("incompatible dimensions" +
str(self.dims) + ", " + str(other.dims))
str(self.dims) + ", " + str(other.dims))
self.elements.append(_ConstantElement(other))
elif (
isinstance(other, numbers.Number) and
Expand Down Expand Up @@ -992,9 +999,9 @@ cdef class QobjEvo:
", " + str(state.dims[0]))

return Qobj(self.matmul_data(t, state.data),
dims=[self.dims[0],state.dims[1]],
dims=[self.dims[0], state.dims[1]],
copy=False
)
)

cpdef Data matmul_data(QobjEvo self, object t, Data state, Data out=None):
"""Compute ``out += self(t) @ state``"""
Expand Down
42 changes: 41 additions & 1 deletion qutip/tests/core/test_qobjevo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest
from qutip import (Qobj, QobjEvo, coefficient, qeye, sigmax, sigmaz,
rand_stochastic, rand_herm, rand_ket, liouvillian)
rand_stochastic, rand_herm, rand_ket, liouvillian,
basis, spre, spost, to_choi)
import numpy as np
from numpy.testing import assert_allclose

Expand Down Expand Up @@ -132,6 +133,45 @@ def test_call(pseudo_qevo, coeff_type):
_assert_qobjevo_equivalent(pseudo_qevo, qevo)


# Test the QobjEvo.__repr__()
Ericgig marked this conversation as resolved.
Show resolved Hide resolved
def test_QobjEvo_repr():
# case_n: cases with Objects of QobjEvo with unique __repr__
# expected_repr_n: are the Expected result from the __repr__

case_1 = repr(QobjEvo([qeye(3), lambda t: t]))
expected_repr_1 = 'QobjEvo: dims = [[3], [3]], shape = (3, 3), '
expected_repr_1 += 'type = oper, superrep = None, '
expected_repr_1 += 'isconstant = False, num_elements = 1'
assert case_1 == expected_repr_1

case_2 = repr(QobjEvo(qeye(2)))
expected_repr_2 = 'QobjEvo: dims = [[2], [2]], shape = (2, 2), '
expected_repr_2 += 'type = oper, superrep = None, '
expected_repr_2 += 'isconstant = True, num_elements = 1'
assert case_2 == expected_repr_2

case_3 = repr(QobjEvo(basis(5, 2)))
expected_repr_3 = 'QobjEvo: dims = [[5], [1]], shape = (5, 1), '
expected_repr_3 += 'type = ket, superrep = None, '
expected_repr_3 += 'isconstant = True, num_elements = 1'
assert case_3 == expected_repr_3

X = sigmax()
S = spre(X) * spost(X.dag())
case_4 = repr(QobjEvo(to_choi(S)))
expected_repr_4 = 'QobjEvo: dims = [[[2], [2]], [[2], [2]]], '
expected_repr_4 += 'shape = (4, 4), type = super, superrep = choi, '
expected_repr_4 += 'isconstant = True, num_elements = 1'
assert case_4 == expected_repr_4

case_5 = repr(QobjEvo([[qeye(4), lambda t: t],
[qeye(4), lambda t: t]], compress=False))
expected_repr_5 = 'QobjEvo: dims = [[4], [4]], shape = (4, 4), '
expected_repr_5 += 'type = oper, superrep = None, '
expected_repr_5 += 'isconstant = False, num_elements = 2'
assert case_5 == expected_repr_5


@pytest.mark.parametrize('coeff_type',
['func_coeff', 'string', 'array', 'logarray'])
def test_product_coeff(pseudo_qevo, coeff_type):
Expand Down