Newer
Older
B-ASIC Core Operations Module.
TODO: More info.
"""
from typing import Any
from numpy import conjugate, sqrt, abs as np_abs
Angus Lothian
committed
from b_asic.operation import AbstractOperation
Angus Lothian
committed
from b_asic.graph_component import Name, TypeName
Angus Lothian
committed
@property
def type_name(self) -> TypeName:
return "in"
Angus Lothian
committed
class Constant(AbstractOperation):
Angus Lothian
committed
def __init__(self, value: Number = 0, name: Name = ""):
super().__init__(name)
def evaluate(self) -> Any:
return self.param("value")
Angus Lothian
committed
class Addition(AbstractOperation):
Angus Lothian
committed
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
Angus Lothian
committed
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
return a + b
@property
def type_name(self) -> TypeName:
return "add"

Jacob Wahlman
committed
class Subtraction(AbstractOperation):
"""Binary subtraction operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
return a - b
@property
def type_name(self) -> GraphIDType:
return "sub"
class Multiplication(AbstractOperation):
"""Binary multiplication operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
return a * b
@property
def type_name(self) -> GraphIDType:
return "mul"
class Division(AbstractOperation):
"""Binary division operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
return a / b
@property
def type_name(self) -> GraphIDType:
return "div"
class SquareRoot(AbstractOperation):
"""Unary square root operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return sqrt((complex)(a))
@property
def type_name(self) -> GraphIDType:
return "sqrt"
class ComplexConjugate(AbstractOperation):
"""Unary complex conjugate operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return conjugate(a)
@property
def type_name(self) -> GraphIDType:
return "conj"
class Max(AbstractOperation):
"""Binary max operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
assert not isinstance(a, complex) and not isinstance(b, complex), \
("core_operation.Max does not support complex numbers.")
return a if a > b else b
@property
def type_name(self) -> GraphIDType:
return "max"
class Min(AbstractOperation):
"""Binary min operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, source2: OutputPort = None, name: Name = ""):
super().__init__(name)
Angus Lothian
committed
self._input_ports = [InputPort(0, self), InputPort(1, self)]
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
if source2 is not None:
self._input_ports[1].connect_to_port(source2)
def evaluate(self, a, b) -> Any:
assert not isinstance(a, complex) and not isinstance(b, complex), \
("core_operation.Min does not support complex numbers.")
return a if a < b else b
@property
def type_name(self) -> GraphIDType:
return "min"
class Absolute(AbstractOperation):
"""Unary absolute value operation.
TODO: More info.
"""
def __init__(self, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return np_abs(a)
@property
def type_name(self) -> GraphIDType:
return "abs"
Angus Lothian
committed
class ConstantMultiplication(AbstractOperation):
"""Unary constant multiplication operation.
TODO: More info.
"""
Angus Lothian
committed
def __init__(self, coefficient: Number, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
Angus Lothian
committed
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return a * self.param("coefficient")
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
return "cmul"
class ConstantAddition(AbstractOperation):
"""Unary constant addition operation.
TODO: More info.
"""
def __init__(self, coefficient: Number, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
self._parameters["coefficient"] = coefficient
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return a + self.param("coefficient")
@property
def type_name(self) -> GraphIDType:
return "cadd"
class ConstantSubtraction(AbstractOperation):
"""Unary constant subtraction operation.
TODO: More info.
"""
def __init__(self, coefficient: Number, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
self._parameters["coefficient"] = coefficient
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return a - self.param("coefficient")
@property
def type_name(self) -> GraphIDType:
return "csub"
class ConstantDivision(AbstractOperation):
"""Unary constant division operation.
TODO: More info.
"""
def __init__(self, coefficient: Number, source1: OutputPort = None, name: Name = ""):
super().__init__(name)
self._input_ports = [InputPort(0, self)]
self._output_ports = [OutputPort(0, self)]
self._parameters["coefficient"] = coefficient
if source1 is not None:
self._input_ports[0].connect_to_port(source1)
def evaluate(self, a) -> Any:
return a / self.param("coefficient")
@property
def type_name(self) -> GraphIDType:
return "cdiv"