Quantum++  v0.8.6
C++11 quantum computing library
input_output.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2016 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef INPUT_OUTPUT_H_
28 #define INPUT_OUTPUT_H_
29 
30 // input/output
31 
32 namespace qpp
33 {
34 
43 template<typename Derived>
44 internal::IOManipEigen disp(const Eigen::MatrixBase<Derived>& A,
45  double chop = qpp::chop)
46 {
47  return internal::IOManipEigen(A, chop);
48 }
49 
60 {
61  return internal::IOManipEigen(z, chop);
62 }
63 
74 template<typename InputIterator>
76  InputIterator last,
77  const std::string& separator,
78  const std::string& start = "[",
79  const std::string& end = "]")
80 {
82  first, last, separator, start, end);
83 }
84 
95 template<typename Container>
97  const Container& c, const std::string& separator,
98  const std::string& start = "[", const std::string& end = "]")
99 {
101  std::begin(c), std::end(c), separator, start, end);
102 }
103 
114 template<typename PointerType>
116  const std::string& separator,
117  const std::string& start = "[",
118  const std::string& end = "]")
119 {
120  return internal::IOManipPointer<PointerType>(p, n, separator, start, end);
121 }
122 
131 template<typename Derived>
132 void save(const Eigen::MatrixBase<Derived>& A, const std::string& fname)
133 {
134  const dyn_mat<typename Derived::Scalar>& rA = A;
135 
136  // EXCEPTION CHECKS
137 
138  // check zero-size
140  throw Exception("qpp::save()", Exception::Type::ZERO_SIZE);
141 
142  std::fstream fout;
143  fout.open(fname, std::ios::out | std::ios::binary);
144 
145  if (fout.fail())
146  {
147  throw std::runtime_error(
148  "qpp::save(): Error writing output file \""
149  + std::string(fname) + "\"!");
150  }
151  // END EXCEPTION CHECKS
152 
153  // write the header to file
154  const char _header[] = "TYPE::Eigen::Matrix";
155  fout.write(_header, sizeof(_header));
156 
157  idx rows = static_cast<idx>(rA.rows());
158  idx cols = static_cast<idx>(rA.cols());
159  fout.write((char*) &rows, sizeof(rows));
160  fout.write((char*) &cols, sizeof(cols));
161 
162  fout.write((char*) rA.data(),
163  sizeof(typename Derived::Scalar) * rows * cols);
164 
165  fout.close();
166 }
167 
186 template<typename Derived>
187 dyn_mat<typename Derived::Scalar> load(const std::string& fname)
188 {
189  std::fstream fin;
190  fin.open(fname, std::ios::in | std::ios::binary);
191 
192  // EXCEPTION CHECKS
193 
194  if (fin.fail())
195  {
196  throw std::runtime_error(
197  "qpp::load(): Error opening input file \""
198  + std::string(fname) + "\"!");
199  }
200 
201  const char _header[] = "TYPE::Eigen::Matrix";
202  char* _fheader = new char[sizeof(_header)];
203 
204  // read the header from file
205  fin.read(_fheader, sizeof(_header));
206  if (strcmp(_fheader, _header))
207  {
208  delete[] _fheader;
209  throw std::runtime_error(
210  "qpp::load(): Input file \"" + std::string(fname)
211  + "\" is corrupted!");
212  }
213  delete[] _fheader;
214  // END EXCEPTION CHECKS
215 
216  idx rows, cols;
217  fin.read((char*) &rows, sizeof(rows));
218  fin.read((char*) &cols, sizeof(cols));
219 
220  dyn_mat<typename Derived::Scalar> A(rows, cols);
221 
222  fin.read((char*) A.data(),
223  sizeof(typename Derived::Scalar) * rows * cols);
224 
225  fin.close();
226 
227  return A;
228 }
229 
230 } /* namespace qpp */
231 
232 #endif /* INPUT_OUTPUT_H_ */
constexpr double chop
Used in qpp::disp() for setting to zero numbers that have their absolute value smaller than qpp::chop...
Definition: constants.h:65
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > dyn_mat
Dynamic Eigen matrix over the field specified by Scalar.
Definition: types.h:83
Definition: iomanip.h:132
Quantum++ main namespace.
Definition: codes.h:30
Definition: iomanip.h:215
std::complex< double > cplx
Complex number in double precision.
Definition: types.h:51
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
bool _check_nonzero_size(const T &x) noexcept
Definition: util.h:113
Definition: iomanip.h:176
dyn_mat< typename Derived::Scalar > load(const std::string &fname)
Loads Eigen matrix from a binary file (internal format) in double precision.
Definition: input_output.h:187
void save(const Eigen::MatrixBase< Derived > &A, const std::string &fname)
Saves Eigen expression to a binary file (internal format) in double precision.
Definition: input_output.h:132
std::size_t idx
Non-negative integer index.
Definition: types.h:36
internal::IOManipEigen disp(const Eigen::MatrixBase< Derived > &A, double chop=qpp::chop)
Eigen expression ostream manipulator.
Definition: input_output.h:44