Quantum++  v1.0
A modern C++11 quantum computing library
exception.h
Go to the documentation of this file.
1 /*
2  * This file is part of Quantum++.
3  *
4  * MIT License
5  *
6  * Copyright (c) 2013 - 2018 Vlad Gheorghiu (vgheorgh@gmail.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
32 #ifndef CLASSES_EXCEPTION1_H_
33 #define CLASSES_EXCEPTION1_H_
34 
35 namespace qpp {
40 namespace exception {
71 class Exception : public std::exception {
72  private:
73  std::string where_;
74  mutable std::string msg_;
75 
76  public:
82  Exception(const std::string& where) : where_{where}, msg_{} {}
83 
89  virtual const char* what() const noexcept override {
90  msg_.clear();
91  msg_ += "IN ";
92  msg_ += where_;
93  msg_ += ": ";
94  msg_ += this->type_description();
95  msg_ += "!";
96 
97  return msg_.c_str();
98  }
99 
105  virtual std::string type_description() const = 0;
106 }; /* class Exception */
107 
108 inline std::string Exception::type_description() const {
109  return "qpp::exception::Exception";
110 }
111 
119 class Unknown : public Exception {
120  public:
121  std::string type_description() const override {
122  return "UNKNOWN EXCEPTION";
123  }
124 
125  using Exception::Exception;
126 };
127 
134 class ZeroSize : public Exception {
135  public:
136  std::string type_description() const override {
137  return "Object has zero size";
138  }
139 
140  using Exception::Exception;
141 };
142 
149 class MatrixNotSquare : public Exception {
150  public:
151  std::string type_description() const override {
152  return "Matrix is not square";
153  }
154 
155  using Exception::Exception;
156 };
157 
164 class MatrixNotCvector : public Exception {
165  public:
166  std::string type_description() const override {
167  return "Matrix is not a column vector";
168  }
169 
170  using Exception::Exception;
171 };
172 
179 class MatrixNotRvector : public Exception {
180  public:
181  std::string type_description() const override {
182  return "Matrix is not a row vector";
183  }
184 
185  using Exception::Exception;
186 };
187 
194 class MatrixNotVector : public Exception {
195  public:
196  std::string type_description() const override {
197  return "Matrix is not a vector";
198  }
199 
200  using Exception::Exception;
201 };
202 
210  public:
211  std::string type_description() const override {
212  return "Matrix is not square nor column vector";
213  }
214 
215  using Exception::Exception;
216 };
217 
225  public:
226  std::string type_description() const override {
227  return "Matrix is not square nor row vector";
228  }
229 
230  using Exception::Exception;
231 };
232 
240  public:
241  std::string type_description() const override {
242  return "Matrix is not square nor vector";
243  }
244 
245  using Exception::Exception;
246 };
247 
255  public:
256  std::string type_description() const override {
257  return "Matrix mismatch subsystems";
258  }
259 
260  using Exception::Exception;
261 };
262 
269 class DimsInvalid : public Exception {
270  public:
271  std::string type_description() const override {
272  return "Invalid dimension(s)";
273  }
274 
275  using Exception::Exception;
276 };
277 
284 class DimsNotEqual : public Exception {
285  public:
286  std::string type_description() const override {
287  return "Dimensions not equal";
288  }
289 
290  using Exception::Exception;
291 };
292 
301  public:
302  std::string type_description() const override {
303  return "Dimension(s) mismatch matrix size";
304  }
305 
306  using Exception::Exception;
307 };
308 
317  public:
318  std::string type_description() const override {
319  return "Dimension(s) mismatch column vector size";
320  }
321 
322  using Exception::Exception;
323 };
324 
333  public:
334  std::string type_description() const override {
335  return "Dimension(s) mismatch row vector size";
336  }
337 
338  using Exception::Exception;
339 };
340 
350  public:
351  std::string type_description() const override {
352  return "Dimension(s) mismatch vector size";
353  }
354 
355  using Exception::Exception;
356 };
357 
366  public:
367  std::string type_description() const override {
368  return "Subsystems mismatch dimensions";
369  }
370 
371  using Exception::Exception;
372 };
373 
380 class PermInvalid : public Exception {
381  public:
382  std::string type_description() const override {
383  return "Invalid permutation";
384  }
385 
386  using Exception::Exception;
387 };
388 
396 class PermMismatchDims : public Exception {
397  public:
398  std::string type_description() const override {
399  return "Permutation mismatch dimensions";
400  }
401 
402  using Exception::Exception;
403 };
404 
411 class NotQubitMatrix : public Exception {
412  public:
413  std::string type_description() const override {
414  return "Matrix is not 2 x 2";
415  }
416 
417  using Exception::Exception;
418 };
419 
426 class NotQubitCvector : public Exception {
427  public:
428  std::string type_description() const override {
429  return "Column vector is not 2 x 1";
430  }
431 
432  using Exception::Exception;
433 };
434 
441 class NotQubitRvector : public Exception {
442  public:
443  std::string type_description() const override {
444  return "Row vector is not 1 x 2";
445  }
446 
447  using Exception::Exception;
448 };
449 
456 class NotQubitVector : public Exception {
457  public:
458  std::string type_description() const override {
459  return "Vector is not 2 x 1 nor 1 x 2";
460  }
461 
462  using Exception::Exception;
463 };
464 
471 class NotQubitSubsys : public Exception {
472  public:
473  std::string type_description() const override {
474  return "Subsystems are not qubits";
475  }
476 
477  using Exception::Exception;
478 };
479 
486 class NotBipartite : public Exception {
487  public:
488  std::string type_description() const override { return "Not bi-partite"; }
489 
490  using Exception::Exception;
491 };
492 
500 class NoCodeword : public Exception {
501  public:
502  std::string type_description() const override {
503  return "Codeword does not exist";
504  }
505 
506  using Exception::Exception;
507 };
508 
515 class OutOfRange : public Exception {
516  public:
517  std::string type_description() const override {
518  return "Parameter out of range";
519  }
520 
521  using Exception::Exception;
522 };
523 
530 class TypeMismatch : public Exception {
531  public:
532  std::string type_description() const override { return "Type mismatch"; }
533 
534  using Exception::Exception;
535 };
536 
543 class SizeMismatch : public Exception {
544  public:
545  std::string type_description() const override { return "Size mismatch"; }
546 
547  using Exception::Exception;
548 };
549 
556 class UndefinedType : public Exception {
557  public:
558  std::string type_description() const override {
559  return "Not defined for this type";
560  }
561 
562  using Exception::Exception;
563 };
564 
571 class CustomException : public Exception {
572  std::string what_{};
573 
574  std::string type_description() const override {
575  return "CUSTOM EXCEPTION " + what_;
576  }
577 
578  public:
579  CustomException(const std::string& where, const std::string& what)
580  : Exception{where}, what_{what} {}
581 };
582 
583 } /* namespace exceptions */
584 } /* namespace qpp */
585 
586 #endif /* CLASSES_EXCEPTION1_H_ */
Dimensions not equal exception.
Definition: exception.h:284
Dimension(s) mismatch matrix size exception.
Definition: exception.h:300
std::string type_description() const override
Exception type description.
Definition: exception.h:136
Matrix is not a vector exception.
Definition: exception.h:194
Matrix is not square nor vector exception.
Definition: exception.h:239
Not defined for this type exception.
Definition: exception.h:556
Custom exception.
Definition: exception.h:571
std::string type_description() const override
Exception type description.
Definition: exception.h:443
std::string type_description() const override
Exception type description.
Definition: exception.h:318
std::string type_description() const override
Exception type description.
Definition: exception.h:502
std::string type_description() const override
Exception type description.
Definition: exception.h:517
Subsystems mismatch dimensions exception.
Definition: exception.h:365
std::string type_description() const override
Exception type description.
Definition: exception.h:545
std::string type_description() const override
Exception type description.
Definition: exception.h:181
Matrix is not a column vector exception.
Definition: exception.h:164
Quantum++ main namespace.
Definition: codes.h:35
std::string type_description() const override
Exception type description.
Definition: exception.h:367
std::string type_description() const override
Exception type description.
Definition: exception.h:196
std::string where_
Definition: exception.h:73
Matrix is not square nor column vector exception.
Definition: exception.h:209
std::string type_description() const override
Exception type description.
Definition: exception.h:211
Invalid dimension(s) exception.
Definition: exception.h:269
Subsystems are not qubits exception.
Definition: exception.h:471
Not bi-partite exception.
Definition: exception.h:486
Dimension(s) mismatch row vector size exception.
Definition: exception.h:332
std::string type_description() const override
Exception type description.
Definition: exception.h:558
Column vector is not 2 x 1 exception.
Definition: exception.h:426
std::string type_description() const override
Exception type description.
Definition: exception.h:488
Invalid permutation exception.
Definition: exception.h:380
std::string type_description() const override
Exception type description.
Definition: exception.h:286
Exception(const std::string &where)
Constructs an exception.
Definition: exception.h:82
Matrix mismatch subsystems exception.
Definition: exception.h:254
Codeword does not exist exception.
Definition: exception.h:500
Vector is not 2 x 1 nor 1 x 2 exception.
Definition: exception.h:456
std::string type_description() const override
Exception type description.
Definition: exception.h:241
std::string type_description() const override
Exception type description.
Definition: exception.h:458
std::string type_description() const override
Exception type description.
Definition: exception.h:256
std::string type_description() const override
Exception type description.
Definition: exception.h:334
Dimension(s) mismatch column vector size exception.
Definition: exception.h:316
std::string type_description() const override
Exception type description.
Definition: exception.h:473
std::string type_description() const override
Exception type description.
Definition: exception.h:428
Type mismatch exception.
Definition: exception.h:530
std::string type_description() const override
Exception type description.
Definition: exception.h:121
std::string type_description() const override
Exception type description.
Definition: exception.h:413
Matrix is not a row vector exception.
Definition: exception.h:179
std::string msg_
Definition: exception.h:74
Row vector is not 1 x 2 exception.
Definition: exception.h:441
Parameter out of range exception.
Definition: exception.h:515
std::string type_description() const override
Exception type description.
Definition: exception.h:226
CustomException(const std::string &where, const std::string &what)
Definition: exception.h:579
Matrix is not 2 x 2 exception.
Definition: exception.h:411
std::string type_description() const override
Exception type description.
Definition: exception.h:351
Unknown exception.
Definition: exception.h:119
std::string type_description() const override
Exception type description.
Definition: exception.h:532
std::string type_description() const override
Exception type description.
Definition: exception.h:271
Dimension(s) mismatch vector size exception.
Definition: exception.h:349
std::string type_description() const override
Exception type description.
Definition: exception.h:302
Permutation mismatch dimensions exception.
Definition: exception.h:396
virtual const char * what() const noexcept override
Overrides std::exception::what()
Definition: exception.h:89
Size mismatch exception.
Definition: exception.h:543
std::string type_description() const override
Exception type description.
Definition: exception.h:574
Matrix is not square exception.
Definition: exception.h:149
std::string type_description() const override
Exception type description.
Definition: exception.h:166
std::string type_description() const override
Exception type description.
Definition: exception.h:151
std::string type_description() const override
Exception type description.
Definition: exception.h:382
Base class for generating Quantum++ custom exceptions.
Definition: exception.h:71
Matrix is not square nor row vector exception.
Definition: exception.h:224
std::string type_description() const override
Exception type description.
Definition: exception.h:398
virtual std::string type_description() const =0
Exception type description.
Definition: exception.h:108
Object has zero size exception.
Definition: exception.h:134