QuLab 2.0.9__cp312-cp312-macosx_10_9_universal2.whl → 2.1.1__cp312-cp312-macosx_10_9_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
qulab/scan/space.py ADDED
@@ -0,0 +1,172 @@
1
+ from typing import Type
2
+
3
+ import numpy as np
4
+ import skopt
5
+ from skopt.space import Categorical, Integer, Real
6
+
7
+
8
+ class Space():
9
+
10
+ def __init__(self, function, *args, **kwds):
11
+ self.function = function
12
+ self.args = args
13
+ self.kwds = kwds
14
+
15
+ def __repr__(self):
16
+ if self.function == 'asarray':
17
+ return repr(self.args[0])
18
+ args = ', '.join(map(repr, self.args))
19
+ kwds = ', '.join(f'{k}={v!r}' for k, v in self.kwds.items())
20
+ return f"{self.function}({args}, {kwds})"
21
+
22
+ def __len__(self):
23
+ return len(self.toarray())
24
+
25
+ @classmethod
26
+ def fromarray(cls, array):
27
+ if isinstance(array, Space):
28
+ return array
29
+ if isinstance(array, (list, tuple)):
30
+ array = np.array(array)
31
+ try:
32
+ a = np.linspace(array[0], array[-1], len(array), dtype=array.dtype)
33
+ if np.allclose(a, array):
34
+ return cls('linspace',
35
+ array[0],
36
+ array[-1],
37
+ len(array),
38
+ dtype=array.dtype)
39
+ except:
40
+ pass
41
+ try:
42
+ a = np.logspace(np.log10(array[0]),
43
+ np.log10(array[-1]),
44
+ len(array),
45
+ base=10,
46
+ dtype=array.dtype)
47
+ if np.allclose(a, array):
48
+ return cls('logspace',
49
+ np.log10(array[0]),
50
+ np.log10(array[-1]),
51
+ len(array),
52
+ base=10,
53
+ dtype=array.dtype)
54
+ except:
55
+ pass
56
+ try:
57
+ a = np.logspace(np.log2(array[0]),
58
+ np.log2(array[-1]),
59
+ len(array),
60
+ base=2,
61
+ dtype=array.dtype)
62
+ if np.allclose(a, array):
63
+ return cls('logspace',
64
+ np.log2(array[0]),
65
+ np.log2(array[-1]),
66
+ len(array),
67
+ base=2,
68
+ dtype=array.dtype)
69
+ except:
70
+ pass
71
+ try:
72
+ a = np.geomspace(array[0],
73
+ array[-1],
74
+ len(array),
75
+ dtype=array.dtype)
76
+ if np.allclose(a, array):
77
+ return cls('geomspace',
78
+ array[0],
79
+ array[-1],
80
+ len(array),
81
+ dtype=array.dtype)
82
+ except:
83
+ pass
84
+ return array
85
+
86
+ def toarray(self):
87
+ return getattr(np, self.function)(*self.args, **self.kwds)
88
+
89
+
90
+ def logspace(start, stop, num=50, endpoint=True, base=10):
91
+ return Space('logspace', start, stop, num, endpoint=endpoint, base=base)
92
+
93
+
94
+ def linspace(start, stop, num=50, endpoint=True):
95
+ return Space('linspace', start, stop, num, endpoint=endpoint)
96
+
97
+
98
+ def geomspace(start, stop, num=50, endpoint=True):
99
+ return Space('geomspace', start, stop, num, endpoint=endpoint)
100
+
101
+
102
+ class OptimizeSpace():
103
+
104
+ def __init__(self, optimizer: 'Optimizer', space):
105
+ self.optimizer = optimizer
106
+ self.space = space
107
+ self.name = None
108
+
109
+ def __len__(self):
110
+ return self.optimizer.maxiter
111
+
112
+
113
+ class Optimizer():
114
+
115
+ def __init__(self,
116
+ scanner,
117
+ name: str,
118
+ level: int,
119
+ method: str | Type = skopt.Optimizer,
120
+ maxiter: int = 1000,
121
+ minimize: bool = True,
122
+ **kwds):
123
+ self.scanner = scanner
124
+ self.method = method
125
+ self.maxiter = maxiter
126
+ self.dimensions = {}
127
+ self.name = name
128
+ self.level = level
129
+ self.kwds = kwds
130
+ self.minimize = minimize
131
+
132
+ def create(self):
133
+ return self.method(list(self.dimensions.values()), **self.kwds)
134
+
135
+ def Categorical(self,
136
+ categories,
137
+ prior=None,
138
+ transform=None,
139
+ name=None) -> OptimizeSpace:
140
+ return OptimizeSpace(self,
141
+ Categorical(categories, prior, transform, name))
142
+
143
+ def Integer(self,
144
+ low,
145
+ high,
146
+ prior="uniform",
147
+ base=10,
148
+ transform=None,
149
+ name=None,
150
+ dtype=np.int64) -> OptimizeSpace:
151
+ return OptimizeSpace(
152
+ self, Integer(low, high, prior, base, transform, name, dtype))
153
+
154
+ def Real(self,
155
+ low,
156
+ high,
157
+ prior="uniform",
158
+ base=10,
159
+ transform=None,
160
+ name=None,
161
+ dtype=float) -> OptimizeSpace:
162
+ return OptimizeSpace(
163
+ self, Real(low, high, prior, base, transform, name, dtype))
164
+
165
+ def __getstate__(self) -> dict:
166
+ state = self.__dict__.copy()
167
+ del state['scanner']
168
+ return state
169
+
170
+ def __setstate__(self, state: dict) -> None:
171
+ self.__dict__.update(state)
172
+ self.scanner = None
qulab/scan/utils.py CHANGED
@@ -1,11 +1,59 @@
1
1
  import ast
2
2
  import asyncio
3
3
  import inspect
4
+ import warnings
4
5
  from typing import Any, Callable
5
6
 
7
+ import dill
8
+
6
9
  from .expression import Env, Expression
7
10
 
8
11
 
12
+ class Unpicklable:
13
+
14
+ def __init__(self, obj):
15
+ self.type = str(type(obj))
16
+ self.id = id(obj)
17
+
18
+ def __repr__(self):
19
+ return f'<Unpicklable: {self.type} at 0x{id(self):x}>'
20
+
21
+
22
+ class TooLarge:
23
+
24
+ def __init__(self, obj):
25
+ self.type = str(type(obj))
26
+ self.id = id(obj)
27
+
28
+ def __repr__(self):
29
+ return f'<TooLarge: {self.type} at 0x{id(self):x}>'
30
+
31
+
32
+ def dump_globals(ns=None, *, size_limit=10 * 1024 * 1024, warn=False):
33
+ import __main__
34
+
35
+ if ns is None:
36
+ ns = __main__.__dict__
37
+
38
+ namespace = {}
39
+
40
+ for name, value in ns.items():
41
+ try:
42
+ buf = dill.dumps(value)
43
+ except:
44
+ namespace[name] = Unpicklable(value)
45
+ if warn:
46
+ warnings.warn(f'Unpicklable: {name} {type(value)}')
47
+ if len(buf) > size_limit:
48
+ namespace[name] = TooLarge(value)
49
+ if warn:
50
+ warnings.warn(f'TooLarge: {name} {type(value)}')
51
+ else:
52
+ namespace[name] = buf
53
+
54
+ return namespace
55
+
56
+
9
57
  def is_valid_identifier(s: str) -> bool:
10
58
  """
11
59
  Check if a string is a valid identifier.
qulab/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "2.0.9"
1
+ __version__ = "2.1.1"
File without changes
File without changes