00001 // QuCoSi - Quantum Computer Simulation 00002 // Copyright © 2009 Frank S. Thomas <frank@blue-dwarf.de> 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #ifndef QUCOSI_AUX_H 00018 #define QUCOSI_AUX_H 00019 00020 #include <limits> 00021 00022 #include <Eigen/Core> 00023 00024 namespace QuCoSi { 00025 00026 const double c_pi = 00027 3.141592653589793238462643383279502884197169399375105820974944; 00028 const double c_sqrt1_2 = 00029 0.707106781186547524400844362104849039284835937688474036588339; 00030 00031 typedef double fptype; 00032 typedef std::complex<fptype> field; 00033 typedef Eigen::Matrix<field, Eigen::Dynamic, 1> VectorXc; 00034 typedef Eigen::Matrix<field, Eigen::Dynamic, Eigen::Dynamic> MatrixXc; 00035 00036 /** \brief Checks if \p x is approximately zero 00037 * 00038 * \return true if \p x is approximately zero 00039 */ 00040 inline bool is_zero(const fptype x) 00041 { 00042 return std::abs(x) <= std::numeric_limits<fptype>::epsilon(); 00043 } 00044 00045 /** \brief Checks if \p x is approximately one 00046 * 00047 * \return true if \p x is approximately one 00048 */ 00049 inline bool is_one(const fptype x) 00050 { 00051 return is_zero(x-1.); 00052 } 00053 00054 /** \brief Computes the binary logarithm of the integer \p value 00055 * 00056 * \return the binary logarithm of \p value and -1 if \p value == 0 00057 */ 00058 inline int log2(const unsigned value) 00059 { 00060 unsigned l = 0; 00061 while ((value >> l) != 0) { 00062 ++l; 00063 } 00064 return l-1; 00065 } 00066 00067 /** \brief Computes the modulo-2 sum of the products of corresponding bits of 00068 * the integers \p x and \p y 00069 * 00070 * Let \p x and \p y be positive integers and \f$x_i, y_i\f$ are the bits 00071 * of \p x and \p y in the binary numeral system (that is \f$x = \sum_{i=0}^n 00072 * x_i \, 2^i \f$), then this function computes the modulo-2 sum of the 00073 * products of corresponding bits of \p x and \p y denoted by \f$x \cdot y\f$: 00074 * \f[ 00075 * x \cdot y = \bigoplus_{i=0}^n x_i \, y_i 00076 * = x_0 \, y_0 \oplus x_1 \, y_1 \oplus \ldots \oplus x_n \, y_n \ . 00077 * \f] 00078 * 00079 * \return the modulo-2 sum of the products of corresponding bits of \p x 00080 * and \p y 00081 */ 00082 inline int bwise_bin_dot(const long x, const long y) 00083 { 00084 long a = (x & y); 00085 // Count the number of set bits in a. 00086 unsigned int c = 0; 00087 for (; a; ++c) { 00088 a &= a-1; 00089 } 00090 // Return the parity of a. 00091 return c%2; 00092 } 00093 00094 } // namespace QuCoSi 00095 00096 #endif // QUCOSI_AUX_H 00097 00098 // vim: filetype=cpp shiftwidth=2 textwidth=78