Skip to content

String compilation fails in editable install #2770

@pmenczel

Description

@pmenczel

Bug Description

If I install qutip in editable mode, string compilation of coefficients does not work. I am pretty sure that it used to work, but unsure when it stopped working.

I create a new environment as follows:

conda create -n test-env python numpy scipy cython setuptools filelock ipython
conda activate test-env
pip install -e .

Code to Reproduce the Bug

import qutip as qt
qt.QobjEvo([[qt.sigmax(), "4*t"]])

Code Output

[1/1] Cythonizing qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx

Error compiling Cython file:
------------------------------------------------------------
...

import numpy as np
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:8:0: 'qutip/core/cy/coefficient.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...

import numpy as np
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:8:0: 'qutip/core/cy/coefficient/Coefficient.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:9:0: 'qutip/core/cy/math.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:9:0: 'qutip/core/cy/math/erf.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import numpy as np
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:9:0: 'qutip/core/cy/math/zerf.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import scipy.special as spe
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
from qutip.core.cy.complex_math cimport *
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:10:0: 'qutip/core/cy/complex_math.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
from qutip.core.cy.complex_math cimport *
from qutip.core.data cimport Data
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:11:0: 'qutip/core/data.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
from scipy.special cimport cython_special
cimport cython
from qutip.core.cy.coefficient cimport Coefficient
from qutip.core.cy.math cimport erf, zerf
from qutip.core.cy.complex_math cimport *
from qutip.core.data cimport Data
^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:11:0: 'qutip/core/data/Data.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...


parsed_code = "self._cte_cpl0 * t"

@cython.auto_pickle(True)
cdef class StrCoefficient(Coefficient):
                          ^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:18:26: First base of 'StrCoefficient' is not an extension type

Error compiling Cython file:
------------------------------------------------------------
...
    def __init__(self, base, var, cte, args):
        self.codeString = base
        self._cte_cpl0 = cte[0]


    cpdef Coefficient copy(self):
          ^
------------------------------------------------------------

qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx:32:10: 'Coefficient' is not a type identifier
---------------------------------------------------------------------------
CompileError                              Traceback (most recent call last)
File ~/qutip/github_master/qutip/core/coefficient.py:619, in compile_code(code, file_name, parsed, c_opt)
    611 coeff_file = Extension(
    612     file_name,
    613     sources=[file_name + ".pyx"],
   (...)    617     language='c++'
    618 )
--> 619 ext_modules = cythonize(
    620     coeff_file, force=True, build_dir=c_opt['build_dir']
    621 )
    622 setup(ext_modules=ext_modules)

File ~/miniconda3/envs/test-env/lib/python3.14t/site-packages/Cython/Build/Dependencies.py:1150, in cythonize(module_list, exclude, nthreads, aliases, quiet, force, language, exclude_failures, show_all_warnings, **options)
   1149     for args in to_compile:
-> 1150         cythonize_one(*args)
   1152 if exclude_failures:

File ~/miniconda3/envs/test-env/lib/python3.14t/site-packages/Cython/Build/Dependencies.py:1294, in cythonize_one(pyx_file, c_file, fingerprint, quiet, options, raise_on_failure, embedded_metadata, full_module_name, show_all_warnings, progress, cache)
   1293 if raise_on_failure:
-> 1294     raise CompileError(None, pyx_file)
   1295 elif os.path.exists(c_file):

CompileError: qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx

The above exception was the direct cause of the following exception:

Exception                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 qt.QobjEvo([[qt.sigmax(), "4*t"]])

File qutip/core/cy/qobjevo.pyx:223, in qutip.core.cy.qobjevo.QobjEvo.__init__()

File qutip/core/cy/qobjevo.pyx:272, in qutip.core.cy.qobjevo.QobjEvo._read_element()

File ~/qutip/github_master/qutip/core/coefficient.py:192, in coefficient(base, tlist, args, args_ctypes, order, compile_opt, function_style, boundary_conditions, **kwargs)
    190 for type_ in coefficient_builders:
    191     if isinstance(base, type_):
--> 192         return coefficient_builders[type_](base, **kwargs)
    194 if callable(base):
    195     args = args or {}

File ~/qutip/github_master/qutip/core/coefficient.py:444, in coeff_from_str(base, args, args_ctypes, compile_opt, **_)
    441 code = make_cy_code(parsed, variables, constants,
    442                     raw, compile_opt)
    443 try:
--> 444     coeff = compile_code(code, file_name, parsed, compile_opt)
    445 except PermissionError:
    446     pass

File ~/qutip/github_master/qutip/core/coefficient.py:627, in compile_code(code, file_name, parsed, c_opt)
    625         for file in glob.glob(file_name + "*"):
    626             os.remove(file)
--> 627     raise Exception("Could not compile") from e
    628 finally:
    629     sys.argv = oldargs

Exception: Could not compile

Expected Behaviour

There is no error.

Your Environment

QuTiP Version:      5.3.0.dev0+f15c8f2
Numpy Version:      2.3.4
Scipy Version:      1.16.2
Cython Version:     3.1.6
Matplotlib Version: None
Python Version:     3.14.0
Number of CPUs:     20
BLAS Info:          blas
INTEL MKL Ext:      None
Platform Info:      Linux (x86_64)
Installation path:  /home/paulm/qutip/github_master/qutip

Additional Context

The error is due to cython not finding qutip's pxd files. Cython looks for those in sys.path, and indeed, the installation directory is not in sys.path for me. I can add the path

sys.path.append('/home/paulm/qutip/github_master')

and this changes the error into

[1/1] Cythonizing qtcoeff_a42fab432721dbd96deac9f1ef8bbc.pyx
running build_ext
building 'qtcoeff_a42fab432721dbd96deac9f1ef8bbc' extension
g++ -pthread -B /home/paulm/miniconda3/envs/test-env/share/python_compiler_compat -fno-strict-overflow -Wsign-compare -DNDEBUG -O2 -Wall -fPIC -O2 -isystem /home/paulm/miniconda3/envs/test-env/include -fPIC -O2 -isystem /home/paulm/miniconda3/envs/test-env/include -fPIC -I/home/paulm/miniconda3/envs/test-env/lib/python3.14t/site-packages/numpy/_core/include -I/home/paulm/miniconda3/envs/test-env/include/python3.14t -c qtcoeff_a42fab432721dbd96deac9f1ef8bbc.cpp -o build/temp.linux-x86_64-cpython-314t/qtcoeff_a42fab432721dbd96deac9f1ef8bbc.o -w -O3 -funroll-loops
qtcoeff_a42fab432721dbd96deac9f1ef8bbc.cpp:1173:10: fatal error: src/intdtype.h: No such file or directory
 1173 | #include "src/intdtype.h"
      |          ^~~~~~~~~~~~~~~~
compilation terminated.
An exception has occurred, use %tb to see the full traceback.

SystemExit: error: command '/usr/bin/g++' failed with exit code 1

That is, now it fails to find the src/intdtype.h header in qutip/core/data. This directory would need to be included in the include_dirs here, I guess - but now I wonder how it does seemingly find this header file in regular qutip installations.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions