qrisp.QuantumFloat.truncate#

QuantumFloat.truncate(value: int | float | bool | integer | floating | Tracer) int | float | Array[source]#

Receives a regular float and returns the float that is closest to the input but can still be encoded.

Parameters:
valueint, float, bool, np.integer, np.floating, or jax.core.Tracer

A real-valued number that is supposed to be truncated.

Returns:
int, float, or jax.Array

The truncated value: an int or float outside of tracing mode (depending on whether the exponent is non-negative, see decoder), or a traced jax.Array while tracing.

Examples

We create a QuantumFloat and round a value to the closest one it can represent. Note that directly encoding an unrepresentable value (like 0.5102341 below, which doesn’t fit this QuantumFloat’s precision of \(2^{-1} = 0.5\)) already truncates silently, so truncate is most useful when you want to know the resulting value ahead of time:

>>> from qrisp import QuantumFloat
>>> qf = QuantumFloat(4, -1)
>>> value = 0.5102341
>>> rounded_value = qf.truncate(value)
>>> rounded_value
0.5
>>> qf[:] = rounded_value
>>> print(qf)
{0.5: 1.0}