Skip to content

Commit

Permalink
Merge pull request #2151 from Ericgig/misc.cython3
Browse files Browse the repository at this point in the history
Enable cython 3
  • Loading branch information
Ericgig committed Aug 16, 2023
2 parents 5fa0ca6 + 6e05e97 commit b3e489b
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
numpy-requirement: ">=1.20,<1.21"
scipy-requirement: ">=1.5,<1.6"
condaforge: 1
oldcython: 1

# No MKL runs. MKL is now the default for conda installations, but
# not necessarily for pip.
Expand All @@ -73,6 +74,7 @@ jobs:
os: ubuntu-latest
python-version: "3.10"
condaforge: 1
oldcython: 1

# Python 3.11 and latest numpy
# Use conda-forge to provide Python 3.11 and latest numpy
Expand Down Expand Up @@ -115,6 +117,9 @@ jobs:
if [[ -z "${{ matrix.nocython }}" ]]; then
QUTIP_TARGET="$QUTIP_TARGET,runtime_compilation"
fi
if [[ "${{ matrix.oldcython }}" ]]; then
pip install cython==0.29.36
fi
export CI_QUTIP_WITH_OPENMP=${{ matrix.openmp }}
if [[ -z "${{ matrix.nomkl }}" ]]; then
conda install blas=*=mkl "numpy${{ matrix.numpy-requirement }}" "scipy${{ matrix.scipy-requirement }}"
Expand Down
1 change: 1 addition & 0 deletions doc/changes/2151.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enable cython 3
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [
"setuptools",
"packaging",
"wheel",
"cython>=0.29.20,<3.0.0",
"cython>=0.29.20",
# See https://numpy.org/doc/stable/user/depending_on_numpy.html for
# the recommended way to build against numpy's C API:
"oldest-supported-numpy",
Expand Down
5 changes: 0 additions & 5 deletions qutip/core/_brtools.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ cdef class SpectraCoefficient(Coefficient):
return self


@cython.overflowcheck(True)
cdef size_t _mul_checked(size_t a, size_t b) except? -1:
return a * b


cdef Data _apply_trans(Data original, int trans):
"""helper function for matmul_var_data, apply transform."""
cdef Data out
Expand Down
6 changes: 5 additions & 1 deletion qutip/core/cy/_element.pyx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#cython: language_level=3
#cython: boundscheck=False, wraparound=False, initializedcheck=False, cdvision=True
#cython: boundscheck=False
#cython: wraparound=False
#cython: initializedcheck=False
#cython: cdvision=True
#cython: c_api_binop_methods=True

from .. import data as _data
from qutip.core.cy.coefficient import coefficient_function_parameters
Expand Down
2 changes: 2 additions & 0 deletions qutip/core/cy/coefficient.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#cython: language_level=3
#cython: c_api_binop_methods=True

import inspect
import pickle
import scipy
Expand Down
2 changes: 1 addition & 1 deletion qutip/core/data/add.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ __all__ = [
cdef int _ONE=1


cdef void _check_shape(Data left, Data right) nogil except *:
cdef void _check_shape(Data left, Data right) except * nogil:
if left.shape[0] != right.shape[0] or left.shape[1] != right.shape[1]:
raise ValueError(
"incompatible matrix shapes "
Expand Down
1 change: 1 addition & 0 deletions qutip/core/data/base.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#cython: language_level=3
#cython: c_api_binop_methods=True

import numpy as np
cimport numpy as cnp
Expand Down
18 changes: 11 additions & 7 deletions qutip/core/data/dispatch.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cdef class _constructed_specialisation:
cdef readonly str _short_name
cdef public str __doc__
cdef public str __name__
cdef public str __module__
# cdef public str __module__
cdef public object __signature__
cdef readonly str __text_signature__

Expand All @@ -71,7 +71,7 @@ cdef class _constructed_specialisation:
+ "_"
+ "_".join([x.__name__ for x in types])
)
self.__module__ = dispatcher.__module__
# self.__module__ = dispatcher.__module__
self.__signature__ = dispatcher.__signature__
self.__text_signature__ = dispatcher.__text_signature__
self._output = out
Expand Down Expand Up @@ -128,7 +128,7 @@ cdef class Dispatcher:
cdef readonly bint output
cdef public str __doc__
cdef public str __name__
cdef public str __module__
# cdef public str __module__
cdef public object __signature__
cdef readonly str __text_signature__

Expand Down Expand Up @@ -169,6 +169,10 @@ cdef class Dispatcher:
this. If not given and `signature_source` is _not_ an instance of
`inspect.Signature`, then we will attempt to read `__module__` from
there instead.
.. note::
Commented for now because of a bug in cython 3 (cython#5472)
"""
if isinstance(inputs, str):
inputs = (inputs,)
Expand Down Expand Up @@ -197,10 +201,10 @@ cdef class Dispatcher:
self.__name__ = signature_source.__name__
else:
self.__name__ = 'dispatcher'
if module is not None:
self.__module__ = module
elif not isinstance(signature_source, inspect.Signature):
self.__module__ = signature_source.__module__
# if module is not None:
# self.__module__ = module
# elif not isinstance(signature_source, inspect.Signature):
# self.__module__ = signature_source.__module__
self.__text_signature__ = self.__name__ + str(self.__signature__)
if not isinstance(signature_source, inspect.Signature):
self.__doc__ = inspect.getdoc(signature_source)
Expand Down
12 changes: 6 additions & 6 deletions qutip/core/data/expect.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

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

cpdef double complex expect_csr(CSR op, CSR state) nogil except *
cpdef double complex expect_super_csr(CSR op, CSR state) nogil except *
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_dense(CSR op, Dense state) nogil except *
cpdef double complex expect_super_csr_dense(CSR op, Dense state) nogil except *
cpdef double complex expect_csr_dense(CSR op, Dense state) except * nogil
cpdef double complex expect_super_csr_dense(CSR op, Dense state) except * nogil

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

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

cdef void _check_shape_ket(Data op, Data state) nogil except *:
cdef void _check_shape_ket(Data op, Data state) except * nogil:
if (
op.shape[1] != state.shape[0] # Matrix multiplication
or state.shape[1] != 1 # State is ket
Expand All @@ -33,7 +33,7 @@ cdef void _check_shape_ket(Data op, Data state) nogil except *:
raise ValueError("incorrect input shapes "
+ str(op.shape) + " and " + str(state.shape))

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

cdef void _check_shape_super(Data op, Data state) nogil except *:
cdef void _check_shape_super(Data op, Data state) except * nogil:
if state.shape[1] != 1:
raise ValueError("expected a column-stacked matrix")
if (
Expand All @@ -52,7 +52,7 @@ cdef void _check_shape_super(Data op, Data state) nogil except *:
raise ValueError("incompatible shapes " + str(op.shape) + ", " + str(state.shape))


cdef double complex _expect_csr_ket(CSR op, CSR state) nogil except *:
cdef double complex _expect_csr_ket(CSR op, CSR state) except * nogil:
"""
Perform the operation
state.adjoint() @ op @ state
Expand All @@ -75,7 +75,7 @@ cdef double complex _expect_csr_ket(CSR op, CSR state) nogil except *:
out += mul * sum
return out

cdef double complex _expect_csr_dm(CSR op, CSR state) nogil except *:
cdef double complex _expect_csr_dm(CSR op, CSR state) except * nogil:
"""
Perform the operation
tr(op @ state)
Expand All @@ -94,7 +94,7 @@ cdef double complex _expect_csr_dm(CSR op, CSR state) nogil except *:
return out


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


cpdef double complex expect_csr(CSR op, CSR state) nogil except *:
cpdef double complex expect_csr(CSR op, CSR state) except * nogil:
"""
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 @@ -125,8 +125,8 @@ cpdef double complex expect_csr(CSR op, CSR state) nogil except *:
if state.shape[1] == 1:
return _expect_csr_ket(op, state)
return _expect_csr_dm(op, state)
'expect_super_dense',
cdef double complex _expect_csr_dense_ket(CSR op, Dense state) nogil except *:

cdef double complex _expect_csr_dense_ket(CSR op, Dense state) except * nogil:
_check_shape_ket(op, state)
cdef double complex out=0, sum
cdef size_t row, ptr
Expand All @@ -139,7 +139,7 @@ cdef double complex _expect_csr_dense_ket(CSR op, Dense state) nogil except *:
out += sum * conj(state.data[row])
return out

cdef double complex _expect_csr_dense_dm(CSR op, Dense state) nogil except *:
cdef double complex _expect_csr_dense_dm(CSR op, Dense state) except * nogil:
_check_shape_dm(op, state)
cdef double complex out=0
cdef size_t row, ptr_op, ptr_state=0, row_stride, col_stride
Expand All @@ -154,7 +154,7 @@ cdef double complex _expect_csr_dense_dm(CSR op, Dense state) nogil except *:
return out


cdef double complex _expect_dense_ket(Dense op, Dense state) nogil except *:
cdef double complex _expect_dense_ket(Dense op, Dense state) except * nogil:
_check_shape_ket(op, state)
cdef double complex out=0, sum
cdef size_t row, col, op_row_stride, op_col_stride
Expand All @@ -169,7 +169,7 @@ cdef double complex _expect_dense_ket(Dense op, Dense state) nogil except *:
out += sum * conj(state.data[row])
return out

cdef double complex _expect_dense_dense_dm(Dense op, Dense state) nogil except *:
cdef double complex _expect_dense_dense_dm(Dense op, Dense state) except * nogil:
_check_shape_dm(op, state)
cdef double complex out=0
cdef size_t row, col, op_row_stride, op_col_stride
Expand All @@ -186,7 +186,7 @@ cdef double complex _expect_dense_dense_dm(Dense op, Dense state) nogil except *
return out


cpdef double complex expect_csr_dense(CSR op, Dense state) nogil except *:
cpdef double complex expect_csr_dense(CSR op, Dense state) except * nogil:
"""
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 +201,7 @@ cpdef double complex expect_csr_dense(CSR op, Dense state) nogil except *:
return _expect_csr_dense_dm(op, state)


cpdef double complex expect_dense(Dense op, Dense state) nogil except *:
cpdef double complex expect_dense(Dense op, Dense state) except * nogil:
"""
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 @@ -216,7 +216,7 @@ cpdef double complex expect_dense(Dense op, Dense state) nogil except *:
return _expect_dense_dense_dm(op, state)


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


cpdef double complex expect_super_dense(Dense op, Dense state) nogil except *:
cpdef double complex expect_super_dense(Dense op, Dense state) except * nogil:
"""
Perform the operation `tr(op @ state)` where `op` is supplied as a
superoperator, and `state` is a column-stacked operator.
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=*) nogil except *
cpdef double complex inner_op_csr(CSR left, CSR op, CSR right, bint scalar_is_ket=*) nogil except *
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
14 changes: 7 additions & 7 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) nogil except *:
cdef void _check_shape_inner(Data left, Data right) except * nogil:
if (
(left.shape[0] != 1 and left.shape[1] != 1)
or right.shape[1] != 1
Expand All @@ -33,7 +33,7 @@ cdef void _check_shape_inner(Data left, Data right) nogil except *:
+ str(right.shape)
)

cdef void _check_shape_inner_op(Data left, Data op, Data right) nogil except *:
cdef void _check_shape_inner_op(Data left, Data op, Data right) except * 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 Down Expand Up @@ -72,7 +72,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) nogil except *:
cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=False) except * nogil:
"""
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 All @@ -95,7 +95,7 @@ cpdef double complex inner_csr(CSR left, CSR right, bint scalar_is_ket=False) no
return _inner_csr_bra_ket(left, right)
return _inner_csr_ket_ket(left, right)

cpdef double complex inner_dia(Dia left, Dia right, bint scalar_is_ket=False) nogil except *:
cpdef double complex inner_dia(Dia left, Dia right, bint scalar_is_ket=False) except * nogil:
"""
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 @@ -134,7 +134,7 @@ cpdef double complex inner_dia(Dia left, Dia right, bint scalar_is_ket=False) no

return inner

cpdef double complex inner_dense(Dense left, Dense right, bint scalar_is_ket=False) nogil except *:
cpdef double complex inner_dense(Dense left, Dense right, bint scalar_is_ket=False) except * nogil:
"""
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 @@ -194,7 +194,7 @@ cdef double complex _inner_op_csr_ket_ket(CSR left, CSR op, CSR right) nogil:
return out

cpdef double complex inner_op_dia(Dia left, Dia op, Dia right,
bint scalar_is_ket=False) nogil except *:
bint scalar_is_ket=False) except * nogil:
"""
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 Down Expand Up @@ -238,7 +238,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) nogil except *:
bint scalar_is_ket=False) except * nogil:
"""
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 Down
2 changes: 1 addition & 1 deletion 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) nogil except *:
cdef void _check_shape(Data left, Data right, Data out=None) except * nogil:
if left.shape[1] != right.shape[0]:
raise ValueError(
"incompatible matrix shapes "
Expand Down
4 changes: 2 additions & 2 deletions qutip/core/data/norm.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ cpdef double one_csr(CSR matrix) except -1
cpdef double trace_csr(CSR matrix) except -1
cpdef double max_csr(CSR matrix) nogil
cpdef double frobenius_csr(CSR matrix) nogil
cpdef double l2_csr(CSR matrix) nogil except -1
cpdef double l2_csr(CSR matrix) except -1 nogil

cpdef double frobenius_dense(Dense matrix) nogil
cpdef double l2_dense(Dense matrix) nogil except -1
cpdef double l2_dense(Dense matrix) except -1 nogil

cpdef double one_dia(Dia matrix) except -1
cpdef double max_dia(Dia matrix) nogil
Expand Down

0 comments on commit b3e489b

Please sign in to comment.