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_VECTOR_H 00018 #define QUCOSI_VECTOR_H 00019 00020 #include <Eigen/Array> 00021 00022 #include "Aux" 00023 00024 namespace QuCoSi { 00025 00026 /** \class Vector 00027 * 00028 * \brief Dynamic size vector of complex numbers 00029 * 00030 * The Vector class is the base for all vectors and qubits used in QuCoSi. It 00031 * is of dynamic size and uses complex numbers. Besides the standard methods 00032 * that are inherited from the Eigen base class it offers some convenient 00033 * methods like isNormalized() and randomize(). The most important feature of 00034 * this class is the tensor product tensorDot() and tensorDotSet(). 00035 * 00036 * \sa Qubit 00037 */ 00038 class Vector : public VectorXc 00039 { 00040 public: 00041 /** \brief Constructs the two-dimensional vector (1 0)<sup>T</sup> 00042 */ 00043 inline Vector() : VectorXc(2) 00044 { 00045 *this << 1, 0; 00046 } 00047 00048 /** \brief Constructs the null vector of dimension \p dim 00049 * 00050 * \param dim the dimension of this vector 00051 */ 00052 inline Vector(const int dim) : VectorXc(dim) {} 00053 00054 /** \brief Constructs the two-dimensional vector (\p c0 \p c1)<sup>T</sup> 00055 * 00056 * \param c0 the first component of this vector 00057 * \param c1 the second component of this vector 00058 */ 00059 inline Vector(const field& c0, const field& c1) : VectorXc(2) 00060 { 00061 *this << c0, c1; 00062 } 00063 00064 inline Vector& operator=(const VectorXc& v) 00065 { 00066 VectorXc::operator=(v); 00067 return *this; 00068 } 00069 00070 /** \brief Checks if this vector is an unit vector 00071 * 00072 * \return true if this vector is an unit vector 00073 */ 00074 inline bool isNormalized() const 00075 { 00076 return is_one(norm()); 00077 } 00078 00079 /** \brief Sets all coefficients of this vector to random values 00080 * 00081 * \return a reference to \c *this 00082 */ 00083 inline Vector& randomize() 00084 { 00085 do setRandom().normalize(); while (isZero() == true); 00086 return *this; 00087 } 00088 00089 /** \brief Computes the tensor product of this vector with \p v 00090 * 00091 * The tensor product \f$x \otimes y\f$ of the vectors \f$x \in K^n\f$ 00092 * and \f$y \in K^m\f$ is defined as: 00093 * \f[ 00094 * \left(\begin{array}{c} 00095 * x_1 \\ x_2 \\ \vdots \\ x_n 00096 * \end{array}\right) 00097 * \otimes 00098 * \left(\begin{array}{c} 00099 * y_1 \\ y_2 \\ \vdots \\ y_m 00100 * \end{array}\right) 00101 * = 00102 * \left(\begin{array}{c} 00103 * x_1 y_1 \\ \vdots \\ x_1 y_m \\ 00104 * x_2 y_1 \\ \vdots \\ x_2 y_m \\ 00105 * \vdots \\ x_n y_m 00106 * \end{array}\right) \in K^{nm} \ . 00107 * \f] 00108 * 00109 * \param v the right hand side operand of the tensor product 00110 * \return the tensor product of this vector with Vector \p v 00111 */ 00112 inline Vector tensorDot(const Vector& v) const 00113 { 00114 Vector w(size()*v.size()); 00115 for (int i = 0, k = 0; i < size(); ++i) { 00116 for (int j = 0; j < v.size(); ++j, ++k) { 00117 w(k) = (*this)(i)*v(j); 00118 } 00119 } 00120 return w; 00121 } 00122 00123 /** \brief Sets the tensor product of this vector and \p v as this vector 00124 * 00125 * This method computes the tensor product of this vector and Vector 00126 * \p v and sets the result as this vector. For two vectors \c x and \c y 00127 * \code x.tensorDotSet(y) \endcode is practically identical to 00128 * \code x = x.tensorDot(y) \endcode 00129 * 00130 * \param v the right hand side operand of the tensor product 00131 * \return a reference to \c *this 00132 * \sa tensorDot() 00133 */ 00134 inline Vector& tensorDotSet(const Vector& v) 00135 { 00136 *this = tensorDot(v); 00137 return *this; 00138 } 00139 }; 00140 00141 } // namespace QuCoSi 00142 00143 #endif // QUCOSI_VECTOR_H 00144 00145 // vim: filetype=cpp shiftwidth=2 textwidth=78