b_asic.quantization

B-ASIC quantization module.

class b_asic.quantization.Overflow(value)

Overflow types.

SATURATION = 2

Two’s complement saturation.

Overflow return the most positive/negative number.

TWOS_COMPLEMENT = 1

Two’s complement overflow, i.e., remove the more significant bits.

class b_asic.quantization.Quantization(value)

Quantization types.

JAMMING = 4

Jamming/von Neumann rounding, i.e., set the LSB to one.

MAGNITUDE_TRUNCATION = 3

Magnitude truncation, i.e., round towards zero.

ROUNDING = 1

Standard two’s complement rounding, i.e, tie rounds towards infinity.

TRUNCATION = 2

Two’s complement truncation, i.e., round towards negative infinity.

UNBIASED_JAMMING = 6

Unbiased jamming/von Neumann rounding.

UNBIASED_ROUNDING = 5

Unbiased rounding, i.e., tie rounds towards even.

b_asic.quantization.quantize(value: int | float | complex, fractional_bits: int, integer_bits: int = 1, quantization: Quantization = Quantization.TRUNCATION, overflow: Overflow = Overflow.TWOS_COMPLEMENT)

Quantize value assuming two’s complement representation.

Quantization happens before overflow, so, e.g., rounding may lead to an overflow.

The total number of bits is fractional_bits + integer_bits. However, there is no check that this will be a positive number. Note that the sign bit is included in these bits. If integer_bits is not given, then use 1, i.e., the result is between

\[-1 \leq \text{value} \leq 1-2^{-\text{fractional_bits}}\]

If value is a complex number, the real and imaginary parts are quantized separately.

Parameters:
valueint, float, complex

The value to be quantized.

fractional_bitsint

Number of fractional bits, can be negative.

integer_bitsint, default: 1

Number of integer bits, can be negative.

quantizationQuantization, default: Quantization.TRUNCATION

Type of quantization.

overflowOverflow, default: Overflow.TWOS_COMPLEMENT

Type of overflow.

Returns:
int, float, complex

The quantized value.

Examples

>>> from b_asic.quantization import quantize, Quantization, Overflow
...
... quantize(0.3, 4)  # Truncate 0.3 using four fractional bits and one integer bit
0.25
>>> quantize(0.3, 4, quantization=Quantization.ROUNDING)  # As above, but round
0.3125
>>> quantize(1.3, 4)  # Will overflow
-0.75
>>> quantize(1.3, 4, 2)  # Use two integer bits
1.25
>>> quantize(1.3, 4, overflow=Overflow.SATURATION)  # use saturation
0.9375
>>> quantize(0.3, 4, -1)  # Three bits in total, will overflow
-0.25