1# -*- coding: utf-8 -*-
2# This file is part of QuTiP: Quantum Toolbox in Python.
3#
4#    Copyright (c) 2014 and later, Alexander J G Pitchford
5#    All rights reserved.
6#
7#    Redistribution and use in source and binary forms, with or without
8#    modification, are permitted provided that the following conditions are
9#    met:
10#
11#    1. Redistributions of source code must retain the above copyright notice,
12#       this list of conditions and the following disclaimer.
13#
14#    2. Redistributions in binary form must reproduce the above copyright
15#       notice, this list of conditions and the following disclaimer in the
16#       documentation and/or other materials provided with the distribution.
17#
18#    3. Neither the name of the QuTiP: Quantum Toolbox in Python nor the names
19#       of its contributors may be used to endorse or promote products derived
20#       from this software without specific prior written permission.
21#
22#    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23#    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24#    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25#    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26#    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27#    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28#    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29#    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30#    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31#    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32#    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33###############################################################################
34
35# @author: Alexander Pitchford
36# @email1: agp1@aber.ac.uk
37# @email2: alex.pitchford@gmail.com
38# @organization: Aberystwyth University
39# @supervisor: Daniel Burgarth
40
41"""
42Exception classes for the Quantum Control library
43"""
44
45
46class Error(Exception):
47    """Base class for all qutip control exceptions"""
48
49    def __str__(self):
50        return repr(self.message)
51
52
53class UsageError(Error):
54    """
55    A function has been used incorrectly. Most likely when a base class
56    was used when a sub class should have been.
57        funcname: function name where error occurred
58        msg: Explanation
59
60    """
61    def __init__(self, msg):
62        self.message = msg
63
64
65class FunctionalError(Error):
66    """
67    A function behaved in an unexpected way
68    Attributes:
69        funcname: function name where error occurred
70        msg: Explanation
71
72    """
73    def __init__(self, msg):
74        self.message = msg
75
76
77class OptimizationTerminate(Error):
78    """
79    Superclass for all early terminations from the optimisation algorithm
80
81    """
82    pass
83
84
85class GoalAchievedTerminate(OptimizationTerminate):
86    """
87    Exception raised to terminate execution when the goal has been reached
88    during the optimisation algorithm
89
90    """
91    def __init__(self, fid_err):
92        self.reason = "Goal achieved"
93        self.fid_err = fid_err
94
95
96class MaxWallTimeTerminate(OptimizationTerminate):
97    """
98    Exception raised to terminate execution when the optimisation time has
99    exceeded the maximum set in the config
100
101    """
102    def __init__(self):
103        self.reason = "Max wall time exceeded"
104
105class MaxFidFuncCallTerminate(OptimizationTerminate):
106    """
107    Exception raised to terminate execution when the number of calls to the
108    fidelity error function has exceeded the maximum
109
110    """
111    def __init__(self):
112        self.reason = "Number of fidelity error calls has exceeded the maximum"
113
114class GradMinReachedTerminate(OptimizationTerminate):
115    """
116    Exception raised to terminate execution when the minimum gradient normal
117    has been reached during the optimisation algorithm
118
119    """
120    def __init__(self, gradient):
121        self.reason = "Gradient normal minimum reached"
122        self.gradient = gradient
123