Skip to content

Commit

Permalink
Merge pull request qutip#1783 from Lala5th/master
Browse files Browse the repository at this point in the history
Extra check for shape in Qobj to fix qutip#1782
  • Loading branch information
hodgestar committed Feb 2, 2022
2 parents b428d52 + b50418e commit f5bdb6d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions qutip/qobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,17 @@ def __init__(self, inpt=None, dims=None, shape=None,
if self.type == 'super' and self.superrep is None:
self.superrep = 'super'

# While the obvious check would be != that would fail valid
# use cases such as enr_fock and other enr_ functions.
# This does leave open the possibility of data still being
# misused such as Qobj(complex[n**2][1], dims = [[n],[n]])
if (self._data.shape[0] > np.prod(np.hstack(self.dims[0])) or
self._data.shape[1] > np.prod(np.hstack(self.dims[1]))) and \
self.type != 'super':

raise ValueError(f"Qobj has smaller dims {self.dims} " +
f"than underlying shape {self._data.shape}")

# clear type cache
self._type = None

Expand Down
8 changes: 4 additions & 4 deletions qutip/tests/test_bofin_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ def mk_ados(self, bath_dims, max_depth):

def mk_rho_and_soln(self, ados, rho_dims):
n_ados = len(ados.labels)
ado_soln = np.random.rand(n_ados, *[2**d for d in rho_dims])
rho = Qobj(ado_soln[0, :], dims=[2, 2])
ado_soln = np.random.rand(n_ados, *[np.product(d) for d in rho_dims])
rho = Qobj(ado_soln[0, :], dims=rho_dims)
return rho, ado_soln

def test_create(self):
ados = self.mk_ados([2, 3], max_depth=2)
rho, ado_soln = self.mk_rho_and_soln(ados, [2, 2])
rho, ado_soln = self.mk_rho_and_soln(ados, [[2], [2]])
ado_state = HierarchyADOsState(rho, ados, ado_soln)
assert ado_state.rho == rho
assert ado_state.labels == ados.labels
Expand All @@ -195,7 +195,7 @@ def test_create(self):

def test_extract(self):
ados = self.mk_ados([2, 3], max_depth=2)
rho, ado_soln = self.mk_rho_and_soln(ados, [2, 2])
rho, ado_soln = self.mk_rho_and_soln(ados, [[2], [2]])
ado_state = HierarchyADOsState(rho, ados, ado_soln)
ado_state.extract((0, 0)) == rho
ado_state.extract(0) == rho
Expand Down

0 comments on commit f5bdb6d

Please sign in to comment.