Tomographer  v5.3
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 <pybind11/pybind11.h>
35 
36 namespace py = pybind11;
37 
38 
39 #ifdef _WIN32
40 # ifdef _tomographer_cxx_EXPORTS
41 # define TOMOGRAPHER_EXPORT __declspec(dllexport)
42 # else
43 # define TOMOGRAPHER_EXPORT __declspec(dllimport)
44 # endif
45 #else
46 # define TOMOGRAPHER_EXPORT __attribute__((visibility("default")))
47 #endif
48 
49 
50 // DEBUGGING ONLY: set TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT to cause eigen_assert() failures to abort() and dump core
51 #ifndef TOMOGRAPHERPY_DEBUG_EIGEN_ASSERT_CAUSES_ABORT
52 # define TOMOGRAPHER_EIGEN_ASSERT_EXCEPTION
53 #endif
55 
56 // include this AFTER eigen_assert_exception
57 #pragma GCC visibility push(default)
58 #include <pybind11/eigen.h>
59 
60 #ifdef EIGEN_NO_DEBUG
61 # error "TomographerPy requires enabled Eigen assertions, otherwise `TomographerCxxError` won't be raised as documented."
62 #endif
63 
64 #include <Eigen/Core>
65 #pragma GCC visibility pop
66 
67 #include <tomographer/tomographer_version.h>
70 
71 #include <tomographerpy/pylogger.h>
72 
73 
74 // now already provided in <tomographer/tools/cxxutil.h> :
75 //
76 // // get a demangle() function from Boost, either with boost::core::demangle() (boost >=
77 // // 1.56) or boost::units::detail::demangle() (boost before that)
78 // #include <boost/version.hpp>
79 // #if BOOST_VERSION >= 105600
80 // #include <boost/core/demangle.hpp>
81 // #else
82 // #include <boost/units/detail/utility.hpp>
83 // namespace boost { namespace core { using boost::units::detail::demangle; } }
84 // #endif
85 
86 
87 namespace tpy {
88 
90 typedef double RealScalar;
92 typedef double CountRealType;
93 
96 
98 typedef int HistCountIntType;
100 typedef int IterCountIntType; // KEEP EVERYTHING "int" OTHERWISE WE BREAK SOURCE COMPATIBILITY (at least until Tomographer 6)
102 typedef int TaskCountIntType;
104 typedef int FreqCountIntType;
105 
106 
113 typedef double RealType;
114 
122 typedef int CountIntType; // KEEP EVERYTHING "int" OTHERWISE WE BREAK SOURCE COMPATIBILITY (at least until Tomographer 6)
123 
132 
135 
136 
150 inline py::module import_tomographer()
151 {
152  auto tomographer_module = py::module::import("tomographer");
153  if (PyErr_Occurred() != NULL) {
154  throw py::error_already_set();
155  }
156  const std::string module_tomographer_version =
157  tomographer_module.attr("__version__").cast<std::string>();
158  if (module_tomographer_version != TOMOGRAPHER_VERSION) {
159  throw std::runtime_error(
160  "Error: Version of compiled tomographer python module ("+module_tomographer_version +
161  ") does not match version used to compile the current module (" + std::string(TOMOGRAPHER_VERSION)
162  + "). If you updated tomographer, please recompile all dependent modules."
163  ) ;
164  }
165  const std::string this_pybind11_ver =
166  std::to_string(PYBIND11_VERSION_MAJOR) + std::string(".") +
167  std::to_string(PYBIND11_VERSION_MINOR) + std::string(".") +
168  std::to_string(PYBIND11_VERSION_PATCH) ;
169  const std::string module_tomographer_pybind11_ver =
170  tomographer_module.attr("version").attr("compile_info").attr("get")("pybind11", "").cast<std::string>();
171  if (module_tomographer_pybind11_ver != this_pybind11_ver) {
172  throw std::runtime_error(
173  "Error: Compiled tomographer's version of PyBind11 (" + module_tomographer_pybind11_ver +
174  ") does not match version used to compile the current module (" + this_pybind11_ver
175  + "). Please recompile all modules using the same PyBind11 version."
176  ) ;
177  }
178  return tomographer_module;
179 }
180 
181 
182 
183 } // namespace tpy
184 
185 
186 
187 #endif
Eigen::Matrix< RealScalar, Eigen::Dynamic, Eigen::Dynamic > RealMatrixType
Shorthand, a 2-D Eigen::Matrix of RealScalar&#39;s.
Definition: common.h:127
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:131
int CountIntType
Integer type for template arguments (`long&#39; in case of long random walks) – deprecated.
Definition: common.h:122
double RealType
Floating-point type for template arguments – deprecated.
Definition: common.h:113
STL class.
std::complex< RealScalar > ComplexScalar
A shorthand for std::complex<RealScalar>
Definition: common.h:95
int FreqCountIntType
Integer type used for counting the number of measurement outcomes.
Definition: common.h:104
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:90
int TaskCountIntType
Integer type used for counting the number of tasks.
Definition: common.h:102
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:134
C++ Classes and Utilities for Python Modules.
Definition: common.h:87
int IterCountIntType
Integer type used for iteration counts in the random walk.
Definition: common.h:100
Eigen::Matrix< RealScalar, Eigen::Dynamic, 1 > RealVectorType
Shorthand, a 1-D Eigen::Matrix of RealScalar&#39;s.
Definition: common.h:125
py::module import_tomographer()
Import tomographer definitions into other Python modules.
Definition: common.h:150
Eigen::Matrix< ComplexScalar, Eigen::Dynamic, 1 > CplxVectorType
Shorthand, a 1-D Eigen::Matrix of ComplexScalar&#39;s.
Definition: common.h:129
Utilities for logging messages.
double CountRealType
Real type for averaged and/or normalized histograms.
Definition: common.h:92
int HistCountIntType
Integer type used for histogram counts.
Definition: common.h:98