Skip to content

Commit

Permalink
Merge branch 'qutip:master' into parity
Browse files Browse the repository at this point in the history
  • Loading branch information
gsuarezr authored Oct 27, 2023
2 parents 5f0003c + 67eec67 commit 22cdee6
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 44 deletions.
2 changes: 1 addition & 1 deletion doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
traitlets==5.9.0
urllib3==1.26.17
urllib3==1.26.18
wcwidth==0.2.6
wheel==0.38.4
3 changes: 2 additions & 1 deletion qutip/core/data/add.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ __all__ = [
cdef int _ONE=1


cdef void _check_shape(Data left, Data right) except * nogil:
cdef int _check_shape(Data left, Data right) except -1 nogil:
if left.shape[0] != right.shape[0] or left.shape[1] != right.shape[1]:
raise ValueError(
"incompatible matrix shapes "
+ str(left.shape)
+ " and "
+ str(right.shape)
)
return 0


cdef idxint _add_csr(Accumulator *acc, CSR a, CSR b, CSR c, double tol) nogil:
Expand Down
6 changes: 3 additions & 3 deletions qutip/core/data/csr.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ cdef inline Accumulator acc_alloc(size_t size):
acc._sorted = True
return acc

cdef inline void acc_scatter(Accumulator *acc, double complex value, base.idxint position) nogil:
cdef inline void acc_scatter(Accumulator *acc, double complex value, base.idxint position) noexcept nogil:
"""
Add a value to the accumulator for this row, in column `position`. The
value is added on to any value already scattered into this position.
Expand All @@ -85,7 +85,7 @@ cdef inline void acc_scatter(Accumulator *acc, double complex value, base.idxint
acc._sorted &= acc.nnz == 0 or acc.nonzero[acc.nnz - 1] < position
acc.nnz += 1

cdef inline base.idxint acc_gather(Accumulator *acc, double complex *values, base.idxint *indices, double tol=0) nogil:
cdef inline base.idxint acc_gather(Accumulator *acc, double complex *values, base.idxint *indices, double tol=0) noexcept nogil:
"""
Copy all the accumulated values into this row into the output pointers.
This will always output its values in sorted order. The pointers should
Expand Down Expand Up @@ -115,7 +115,7 @@ cdef inline base.idxint acc_gather(Accumulator *acc, double complex *values, bas
nnz += 1
return nnz

cdef inline void acc_reset(Accumulator *acc) nogil:
cdef inline void acc_reset(Accumulator *acc) noexcept nogil:
"""Prepare the accumulator to accept the next row of input."""
# We actually don't need to do anything to reset other than to change
# our sentinel values; the sentinel `_cur_row` makes it easy to detect
Expand Down
4 changes: 2 additions & 2 deletions qutip/core/data/csr.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ cpdef CSR copy_structure(CSR matrix):
return out


cpdef inline base.idxint nnz(CSR matrix) nogil:
cpdef inline base.idxint nnz(CSR matrix) noexcept nogil:
"""Get the number of non-zero elements of a CSR matrix."""
return matrix.row_index[matrix.shape[0]]

Expand All @@ -311,7 +311,7 @@ ctypedef fused _swap_data:
double complex
base.idxint

cdef inline void _sorter_swap(_swap_data *a, _swap_data *b) nogil:
cdef inline void _sorter_swap(_swap_data *a, _swap_data *b) noexcept nogil:
a[0], b[0] = b[0], a[0]

cdef class Sorter:
Expand Down
1 change: 1 addition & 0 deletions qutip/core/data/dia.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ cdef Dia diags_(
The shape of the output. The result does not need to be square, but
the diagonals must be of the correct length to fit in.
"""
cdef base.idxint i
out = empty(n_rows, n_cols, len(offsets))
out.num_diag = len(offsets)
for i in range(len(offsets)):
Expand Down
8 changes: 4 additions & 4 deletions qutip/core/data/expect.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

from qutip.core.data cimport CSR, Dense, Data, Dia

cpdef double complex expect_csr(CSR op, CSR state) except * nogil
cpdef double complex expect_super_csr(CSR op, CSR state) except * nogil
cpdef double complex expect_csr(CSR op, CSR state) except *
cpdef double complex expect_super_csr(CSR op, CSR state) except *

cpdef double complex expect_csr_dense(CSR op, Dense state) except * nogil
cpdef double complex expect_csr_dense(CSR op, Dense state) except *
cpdef double complex expect_super_csr_dense(CSR op, Dense state) except * nogil

cpdef double complex expect_dense(Dense op, Dense state) except * nogil
cpdef double complex expect_dense(Dense op, Dense state) except *
cpdef double complex expect_super_dense(Dense op, Dense state) except * nogil

cpdef double complex expect_dia(Dia op, Dia state) except *
Expand Down
20 changes: 12 additions & 8 deletions qutip/core/data/expect.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,36 @@ __all__ = [
'expect_super_csr_dense', 'expect_super_dia_dense', 'expect_super_data',
]

cdef void _check_shape_ket(Data op, Data state) except * nogil:
cdef int _check_shape_ket(Data op, Data state) except -1 nogil:
if (
op.shape[1] != state.shape[0] # Matrix multiplication
or state.shape[1] != 1 # State is ket
or op.shape[0] != op.shape[1] # op must be square matrix
):
raise ValueError("incorrect input shapes "
+ str(op.shape) + " and " + str(state.shape))
return 0

cdef void _check_shape_dm(Data op, Data state) except * nogil:
cdef int _check_shape_dm(Data op, Data state) except -1 nogil:
if (
op.shape[1] != state.shape[0] # Matrix multiplication
or state.shape[0] != state.shape[1] # State is square
or op.shape[0] != op.shape[1] # Op is square
):
raise ValueError("incorrect input shapes "
+ str(op.shape) + " and " + str(state.shape))
return 0

cdef void _check_shape_super(Data op, Data state) except * nogil:
cdef int _check_shape_super(Data op, Data state) except -1 nogil:
if state.shape[1] != 1:
raise ValueError("expected a column-stacked matrix")
if (
op.shape[1] != state.shape[0] # Matrix multiplication
or op.shape[0] != op.shape[1] # Square matrix
):
raise ValueError("incompatible shapes " + str(op.shape) + ", " + str(state.shape))
raise ValueError("incompatible shapes "
+ str(op.shape) + ", " + str(state.shape))
return 0


cdef double complex _expect_csr_ket(CSR op, CSR state) except * nogil:
Expand Down Expand Up @@ -94,7 +98,7 @@ cdef double complex _expect_csr_dm(CSR op, CSR state) except * nogil:
return out


cpdef double complex expect_super_csr(CSR op, CSR state) except * nogil:
cpdef double complex expect_super_csr(CSR op, CSR state) except *:
"""
Perform the operation `tr(op @ state)` where `op` is supplied as a
superoperator, and `state` is a column-stacked operator.
Expand All @@ -112,7 +116,7 @@ cpdef double complex expect_super_csr(CSR op, CSR state) except * nogil:
return out


cpdef double complex expect_csr(CSR op, CSR state) except * nogil:
cpdef double complex expect_csr(CSR op, CSR state) except *:
"""
Get the expectation value of the operator `op` over the state `state`. The
state can be either a ket or a density matrix.
Expand Down Expand Up @@ -186,7 +190,7 @@ cdef double complex _expect_dense_dense_dm(Dense op, Dense state) except * nogil
return out


cpdef double complex expect_csr_dense(CSR op, Dense state) except * nogil:
cpdef double complex expect_csr_dense(CSR op, Dense state) except *:
"""
Get the expectation value of the operator `op` over the state `state`. The
state can be either a ket or a density matrix.
Expand All @@ -201,7 +205,7 @@ cpdef double complex expect_csr_dense(CSR op, Dense state) except * nogil:
return _expect_csr_dense_dm(op, state)


cpdef double complex expect_dense(Dense op, Dense state) except * nogil:
cpdef double complex expect_dense(Dense op, Dense state) except *:
"""
Get the expectation value of the operator `op` over the state `state`. The
state can be either a ket or a density matrix.
Expand Down
4 changes: 2 additions & 2 deletions qutip/core/data/inner.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

from qutip.core.data.csr cimport CSR

cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=*) except * nogil
cpdef double complex inner_op_csr(CSR left, CSR op, CSR right, bint scalar_is_ket=*) except * nogil
cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=*) except *
cpdef double complex inner_op_csr(CSR left, CSR op, CSR right, bint scalar_is_ket=*) except *
12 changes: 7 additions & 5 deletions qutip/core/data/inner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ __all__ = [
]


cdef void _check_shape_inner(Data left, Data right) except * nogil:
cdef int _check_shape_inner(Data left, Data right) except -1 nogil:
if (
(left.shape[0] != 1 and left.shape[1] != 1)
or right.shape[1] != 1
Expand All @@ -32,8 +32,9 @@ cdef void _check_shape_inner(Data left, Data right) except * nogil:
+ " and "
+ str(right.shape)
)
return 0

cdef void _check_shape_inner_op(Data left, Data op, Data right) except * nogil:
cdef int _check_shape_inner_op(Data left, Data op, Data right) except -1 nogil:
cdef bint left_shape = left.shape[0] == 1 or left.shape[1] == 1
cdef bint left_op = (
(left.shape[0] == 1 and left.shape[1] == op.shape[0])
Expand All @@ -50,6 +51,7 @@ cdef void _check_shape_inner_op(Data left, Data op, Data right) except * nogil:
" and ",
str(right.shape),
]))
return 0

cdef double complex _inner_csr_bra_ket(CSR left, CSR right) nogil:
cdef size_t col, ptr_bra, ptr_ket
Expand All @@ -72,7 +74,7 @@ cdef double complex _inner_csr_ket_ket(CSR left, CSR right) nogil:
out += conj(left.data[ptr_l]) * right.data[ptr_r]
return out

cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=False) except * nogil:
cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=False) except *:
"""
Compute the complex inner product <left|right>. The shape of `left` is
used to determine if it has been supplied as a ket or a bra. The result of
Expand Down Expand Up @@ -238,7 +240,7 @@ cpdef double complex inner_op_dia(Dia left, Dia op, Dia right,
return inner

cpdef double complex inner_op_csr(CSR left, CSR op, CSR right,
bint scalar_is_ket=False) except * nogil:
bint scalar_is_ket=False) except *:
"""
Compute the complex inner product <left|op|right>. The shape of `left` is
used to determine if it has been supplied as a ket or a bra. The result of
Expand All @@ -251,7 +253,7 @@ cpdef double complex inner_op_csr(CSR left, CSR op, CSR right,
"""
_check_shape_inner_op(left, op, right)
cdef double complex l
if left.shape[0] == left.shape[1] == op.shape[0] == op.shape[1] == right.shape[1] == 1:
if 1 == left.shape[1] == left.shape[0] == op.shape[0] == op.shape[1] == right.shape[1]:
if not (csr.nnz(left) and csr.nnz(op) and csr.nnz(right)):
return 0
l = conj(left.data[0]) if scalar_is_ket else left.data[0]
Expand Down
4 changes: 2 additions & 2 deletions qutip/core/data/matmul.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ __all__ = [
]


cdef void _check_shape(Data left, Data right, Data out=None) except * nogil:
cdef int _check_shape(Data left, Data right, Data out=None) except -1 nogil:
if left.shape[1] != right.shape[0]:
raise ValueError(
"incompatible matrix shapes "
Expand All @@ -78,7 +78,7 @@ cdef void _check_shape(Data left, Data right, Data out=None) except * nogil:
+ " but needed "
+ str((left.shape[0], right.shape[1]))
)

return 0

cdef idxint _matmul_csr_estimate_nnz(CSR left, CSR right):
"""
Expand Down
6 changes: 4 additions & 2 deletions qutip/core/data/project.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ __all__ = [
]


cdef void _project_ket_csr(CSR ket, CSR out) nogil:
cdef int _project_ket_csr(CSR ket, CSR out) except -1 nogil:
"""
Calculate the projection of the given ket, and place the output in out.
"""
Expand All @@ -33,9 +33,10 @@ cdef void _project_ket_csr(CSR ket, CSR out) nogil:
for ptr in range(nnz_in):
out.data[offset + ptr] = ket.data[row] * conj(ket.data[ptr])
offset += nnz_in
return 0


cdef void _project_bra_csr(CSR bra, CSR out) nogil:
cdef int _project_bra_csr(CSR bra, CSR out) except -1 nogil:
"""
Calculate the projection of the given bra, and place the output in out.
"""
Expand Down Expand Up @@ -66,6 +67,7 @@ cdef void _project_bra_csr(CSR bra, CSR out) nogil:
# Handle all zero rows after the last non-zero entry.
for row_out in range(row_out, out.shape[0]):
out.row_index[row_out + 1] = cur
return 0


cpdef CSR project_csr(CSR state):
Expand Down
6 changes: 4 additions & 2 deletions qutip/core/data/trace.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ __all__ = [
]


cdef void _check_shape(Data matrix) except * nogil:
cdef int _check_shape(Data matrix) except -1 nogil:
if matrix.shape[0] != matrix.shape[1]:
raise ValueError("".join([
"matrix shape ", str(matrix.shape), " is not square.",
]))
return 0


cdef void _check_shape_oper_ket(int N, Data matrix) except * nogil:
cdef int _check_shape_oper_ket(int N, Data matrix) except -1 nogil:
if matrix.shape[0] != N * N or matrix.shape[1] != 1:
raise ValueError("".join([
"matrix ", str(matrix.shape), " is not a stacked square matrix."
]))
return 0


cpdef double complex trace_csr(CSR matrix) except * nogil:
Expand Down
5 changes: 3 additions & 2 deletions qutip/solver/brmesolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
from ..core.blochredfield import bloch_redfield_tensor, SpectraCoefficient
from ..core.cy.coefficient import InterCoefficient
from ..core import data as _data
from .solver_base import Solver
from .solver_base import Solver, _solver_deprecation
from .options import _SolverOptions


def brmesolve(H, psi0, tlist, a_ops=[], e_ops=[], c_ops=[],
args={}, sec_cutoff=0.1, options=None):
args={}, sec_cutoff=0.1, options=None, **kwargs):
"""
Solves for the dynamics of a system using the Bloch-Redfield master
equation, given an input Hamiltonian, Hermitian bath-coupling terms and
Expand Down Expand Up @@ -142,6 +142,7 @@ def brmesolve(H, psi0, tlist, a_ops=[], e_ops=[], c_ops=[],
the ``a_ops``, but it is usually less efficient than manually choosing
it.
"""
options = _solver_deprecation(kwargs, options, "br")
H = QobjEvo(H, args=args, tlist=tlist)

c_ops = c_ops if c_ops is not None else []
Expand Down
6 changes: 4 additions & 2 deletions qutip/solver/mcsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import numpy as np
from ..core import QobjEvo, spre, spost, Qobj, unstack_columns
from .multitraj import MultiTrajSolver
from .solver_base import Solver, Integrator
from .solver_base import Solver, Integrator, _solver_deprecation
from .result import McResult, McTrajectoryResult, McResultImprovedSampling
from .mesolve import mesolve, MESolver
import qutip.core.data as _data
from time import time


def mcsolve(H, state, tlist, c_ops=(), e_ops=None, ntraj=500, *,
args=None, options=None, seeds=None, target_tol=None, timeout=None):
args=None, options=None, seeds=None, target_tol=None, timeout=None,
**kwargs):
r"""
Monte Carlo evolution of a state vector :math:`|\psi \rangle` for a
given Hamiltonian and sets of collapse operators. Options for the
Expand Down Expand Up @@ -130,6 +131,7 @@ def mcsolve(H, state, tlist, c_ops=(), e_ops=None, ntraj=500, *,
The simulation will end when the first end condition is reached between
``ntraj``, ``timeout`` and ``target_tol``.
"""
options = _solver_deprecation(kwargs, options, "mc")
H = QobjEvo(H, args=args, tlist=tlist)
if not isinstance(c_ops, (list, tuple)):
c_ops = [c_ops]
Expand Down
6 changes: 4 additions & 2 deletions qutip/solver/mesolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
from .. import (Qobj, QobjEvo, isket, liouvillian, ket2dm, lindblad_dissipator)
from ..core import stack_columns, unstack_columns
from ..core.data import to
from .solver_base import Solver
from .solver_base import Solver, _solver_deprecation
from .sesolve import sesolve, SESolver


def mesolve(H, rho0, tlist, c_ops=None, e_ops=None, args=None, options=None):
def mesolve(H, rho0, tlist, c_ops=None, e_ops=None, args=None, options=None,
**kwargs):
"""
Master equation evolution of a density matrix for a given Hamiltonian and
set of collapse operators, or a Liouvillian.
Expand Down Expand Up @@ -125,6 +126,7 @@ def mesolve(H, rho0, tlist, c_ops=None, e_ops=None, args=None, options=None):
is an empty list of `store_states=True` in options].
"""
options = _solver_deprecation(kwargs, options)
H = QobjEvo(H, args=args, tlist=tlist)

c_ops = c_ops if c_ops is not None else []
Expand Down
2 changes: 1 addition & 1 deletion qutip/solver/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,5 +389,5 @@ def loky_pmap(task, values, task_args=None, task_kwargs=None,
"parallel": parallel_map,
"serial_map": serial_map,
"serial": serial_map,
"loky": loky_pmap
"loky": loky_pmap,
}
Loading

0 comments on commit 22cdee6

Please sign in to comment.