|
| 1 | +# This code is part of Qiskit. |
| 2 | +# |
| 3 | +# (C) Copyright IBM 2026. |
| 4 | +# |
| 5 | +# This code is licensed under the Apache License, Version 2.0. You may |
| 6 | +# obtain a copy of this license in the LICENSE.txt file in the root directory |
| 7 | +# of this source tree or at https://www.apache.org/licenses/LICENSE-2.0. |
| 8 | +# |
| 9 | +# Any modifications or derivative works of this code must retain this |
| 10 | +# copyright notice, and modified files need to carry a notice indicating |
| 11 | +# that they have been altered from the originals. |
| 12 | + |
| 13 | +r""" |
| 14 | +================================================ |
| 15 | +C API interface from Python (:mod:`qiskit.capi`) |
| 16 | +================================================ |
| 17 | +
|
| 18 | +.. currentmodule:: qiskit.capi |
| 19 | +
|
| 20 | +This module provides Python-space interactions with Qiskit's public C API. |
| 21 | +
|
| 22 | +For documentation on the C API itself, see `Qiskit C API |
| 23 | +<https://quantum.cloud.ibm.com/docs/api/qiskit-c/>`__. |
| 24 | +
|
| 25 | +Build-system interaction |
| 26 | +======================== |
| 27 | +
|
| 28 | +The Python package :mod:`qiskit` contains all of the Qiskit C API header files, and a compiled |
| 29 | +shared-object library that includes all of the C-API functions. You can access the locations of |
| 30 | +these two objects with the functions :func:`get_include` and :func:`get_lib` respectively. |
| 31 | +
|
| 32 | +.. warning:: |
| 33 | + You typically *should not* link directly against the output of :func:`get_lib`, unless you know |
| 34 | + what you are doing. In particular, directly linking against this object is not a safe way to |
| 35 | + build a distributable Python extension module that uses Qiskit's C API. |
| 36 | +
|
| 37 | + However, if you understand all the caveats of direct linking, you can use the function to get |
| 38 | + the location of the library. |
| 39 | +
|
| 40 | +.. autofunction:: get_include |
| 41 | +.. autofunction:: get_lib |
| 42 | +""" |
| 43 | + |
| 44 | +__all__ = ["get_include", "get_lib"] |
| 45 | + |
| 46 | +from pathlib import Path |
| 47 | + |
| 48 | +import qiskit._accelerate |
| 49 | + |
| 50 | + |
| 51 | +def get_include() -> str: |
| 52 | + """Get the directory containing the ``qiskit.h`` C header file and the internal |
| 53 | + ``qiskit/*.h`` auxiliary files. |
| 54 | +
|
| 55 | + When using Qiskit as a build dependency, you typically want to include this directory on the |
| 56 | + include search path of your compiler, such as: |
| 57 | +
|
| 58 | + .. code-block:: bash |
| 59 | +
|
| 60 | + qiskit_include=$(python -c 'import qiskit.capi; print(qiskit.capi.get_include())') |
| 61 | + gcc -I "$qiskit_include" my_bin.c -o my_bin |
| 62 | +
|
| 63 | + The location of this directory within the Qiskit package data is not fixed, and may change |
| 64 | + between Qiskit versions. You should always use this function to retrieve the directory. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + an absolute path to the package include-files directory. |
| 68 | + """ |
| 69 | + return str(Path(__file__).parent.absolute() / "include") |
| 70 | + |
| 71 | + |
| 72 | +def get_lib() -> str: |
| 73 | + """Get the path to a shared-object library that contains all the C-API exported symbols. |
| 74 | +
|
| 75 | + .. warning:: |
| 76 | + You typically *should not* link directly against this object. In particular, directly |
| 77 | + linking against this object is not a safe way to build a Python extension module that uses |
| 78 | + Qiskit's C API. |
| 79 | +
|
| 80 | + You can, if you choose, use :mod:`ctypes` to access the C API symbols contained in this object, |
| 81 | + though beware that the C-API types declared in the header file are not interchangeable with the |
| 82 | + Python objects that correspond to them. |
| 83 | +
|
| 84 | + The location and name of this file within the Qiskit package data is not fixed, and may change |
| 85 | + between Qiskit versions. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + an absolute path to the shared-object library containing the C-API exported symbols. |
| 89 | + """ |
| 90 | + return str(Path(qiskit._accelerate.__file__).absolute()) |
0 commit comments