qrisp.QuantumFloat.truncate#

QuantumFloat.truncate(x)[source]#

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

Parameters:
xfloat

A float that is supposed to be truncated.

Returns:
float

The truncated float.

Examples

We create a QuantumFloat and round a value to fit the encoder and subsequently initiate:

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