Tomographer  v5.4
Tomographer C++ Framework Documentation
common.h
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOPY_COMMON_H
29 #define TOMOPY_COMMON_H
30 
31 #include <cstdio>
32 #include <string>
33 
34 #include <type_traits>
35 
36 #include <pybind11/pybind11.h>
37 
38 namespace py = pybind11;
39 
40 
41 #ifdef _WIN32
42 # ifdef _tomographer_cxx_EXPORTS
43 # define TOMOGRAPHER_EXPORT __declspec(dllexport)
44 # else
45 # define TOMOGRAPHER_EXPORT __declspec(dllimport)
46 # endif
47 #else
48 # define TOMOGRAPHER_EXPORT __attribute__((visibility("default")))
49 #endif
50 
51 
52 // DEBUGGING ONLY: set TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT to cause eigen_assert() failures to abort() and dump core
53 #ifndef TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT
54 # define TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
55 #endif
57 
58 // include this AFTER eigen_assert_exception
59 #pragma GCC visibility push(default)
60 #include <pybind11/eigen.h>
61 
62 #ifdef EIGEN_NO_DEBUG
63 # error "TomographerPy requires enabled Eigen assertions, otherwise `TomographerCxxError` won't be raised as documented."
64 #endif
65 
66 #include <Eigen/Core>
67 #pragma GCC visibility pop
68 
69 #include <tomographer/tomographer_version.h>
72 
73 #include <tomographerpy/pylogger.h>
74 
75 
76 // now already provided in <tomographer/tools/cxxutil.h> :
77 //
78 // // get a demangle() function from Boost, either with boost::core::demangle() (boost >=
79 // // 1.56) or boost::units::detail::demangle() (boost before that)
80 // #include <boost/version.hpp>
81 // #if BOOST_VERSION >= 105600
82 // #include <boost/core/demangle.hpp>
83 // #else
84 // #include <boost/units/detail/utility.hpp>
85 // namespace boost { namespace core { using boost::units::detail::demangle; } }
86 // #endif
87 
88 
89 namespace tpy {
90 
92 typedef double RealScalar;
94 typedef double CountRealType;
95 
98 
100 typedef int HistCountIntType;
102 typedef int IterCountIntType; // KEEP EVERYTHING "int" OTHERWISE WE BREAK SOURCE COMPATIBILITY (at least until Tomographer 6)
104 typedef int TaskCountIntType;
106 typedef int FreqCountIntType;
107 
108 
115 typedef double RealType;
116 
124 typedef int CountIntType; // KEEP EVERYTHING "int" OTHERWISE WE BREAK SOURCE COMPATIBILITY (at least until Tomographer 6)
125 
134 
137 
138 
152 inline py::module import_tomographer()
153 {
154  auto tomographer_module = py::module::import("tomographer");
155  if (PyErr_Occurred() != NULL) {
156  throw py::error_already_set();
157  }
158  const std::string module_tomographer_version =
159  tomographer_module.attr("__version__").cast<std::string>();
160  if (module_tomographer_version != TOMOGRAPHER_VERSION) {
161  throw std::runtime_error(
162  "Error: Version of compiled tomographer python module ("+module_tomographer_version +
163  ") does not match version used to compile the current module (" + std::string(TOMOGRAPHER_VERSION)
164  + "). If you updated tomographer, please recompile all dependent modules."
165  ) ;
166  }
167  const std::string this_pybind11_ver =
168  std::to_string(PYBIND11_VERSION_MAJOR) + std::string(".") +
169  std::to_string(PYBIND11_VERSION_MINOR) + std::string(".") +
170  std::to_string(PYBIND11_VERSION_PATCH) ;
171  const std::string module_tomographer_pybind11_ver =
172  tomographer_module.attr("version").attr("compile_info").attr("get")("pybind11", "").cast<std::string>();
173  if (module_tomographer_pybind11_ver != this_pybind11_ver) {
174  throw std::runtime_error(
175  "Error: Compiled tomographer's version of PyBind11 (" + module_tomographer_pybind11_ver +
176  ") does not match version used to compile the current module (" + this_pybind11_ver
177  + "). Please recompile all modules using the same PyBind11 version."
178  ) ;
179  }
180  return tomographer_module;
181 }
182 
183 
184 
185 } // namespace tpy
186 
187 
188 
189 #endif
Eigen::Matrix< RealScalar, Eigen::Dynamic, Eigen::Dynamic > RealMatrixType
Shorthand, a 2-D Eigen::Matrix of RealScalar&#39;s.
Definition: common.h:129
T to_string(T... args)
STL class.
Eigen::Matrix< ComplexScalar, Eigen::Dynamic, Eigen::Dynamic > CplxMatrixType
Shorthand, a 2-D Eigen::Matrix of ComplexScalar&#39;s.
Definition: common.h:133
int CountIntType
Integer type for template arguments (`long&#39; in case of long random walks) – deprecated.
Definition: common.h:124
double RealType
Floating-point type for template arguments – deprecated.
Definition: common.h:115
STL class.
std::complex< RealScalar > ComplexScalar
A shorthand for std::complex<RealScalar>
Definition: common.h:97
int FreqCountIntType
Integer type used for counting the number of measurement outcomes.
Definition: common.h:106
Some C++ utilities, with a tad of C++11 tricks.
double RealScalar
Real type for general calculation precisions (density matrix elements, log-likelihood value calculati...
Definition: common.h:92
int TaskCountIntType
Integer type used for counting the number of tasks.
Definition: common.h:104
Define tools for Eigen&#39;s eigen_assert() to throw an exception instead of assert&#39;ing.
Eigen::Matrix< CountIntType, Eigen::Dynamic, 1 > CountIntVectorType
Shorthand, a 1-D Eigen::Matrix of CountIntType&#39;s – deprecated.
Definition: common.h:136
C++ Classes and Utilities for Python Modules.
Definition: common.h:89
int IterCountIntType
Integer type used for iteration counts in the random walk.
Definition: common.h:102
Eigen::Matrix< RealScalar, Eigen::Dynamic, 1 > RealVectorType
Shorthand, a 1-D Eigen::Matrix of RealScalar&#39;s.
Definition: common.h:127
py::module import_tomographer()
Import tomographer definitions into other Python modules.
Definition: common.h:152
Eigen::Matrix< ComplexScalar, Eigen::Dynamic, 1 > CplxVectorType
Shorthand, a 1-D Eigen::Matrix of ComplexScalar&#39;s.
Definition: common.h:131
Utilities for logging messages.
double CountRealType
Real type for averaged and/or normalized histograms.
Definition: common.h:94
int HistCountIntType
Integer type used for histogram counts.
Definition: common.h:100