microsoft / QuantumLibraries

Q# libraries for the Quantum Development Kit

Home Page:https://docs.microsoft.com/quantum

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fixed point truncation

msoeken opened this issue · comments

Fixed point truncation

Conceptual overview

Truncate fixed point register to lower precision.

Current status

Needs to be done manually and is very errorprone.

Proposal

New and modified functions, operations, and UDTs

Functions to be added to the Numerics package.

namespace Microsoft.Quantum.Arithmetic {

    /// # Summary
    /// Truncates a fixed point register to fewer precision
    ///
    /// # Input
    /// ## integerBits
    /// New number of integer bits, cannot be larger than current number of integer bits
    /// ## fractionalBits
    /// New number of fractional bits, cannot be larger than current number of fractional bits
    /// ## fp
    /// Input fixed point register
    ///
    /// # Output
    /// New fixed point register (based on qubits of input fixed point register) with adjusted
    /// precision.  The sign bit is preserved and counted as part of `integerBits`.
    ///
    /// # Example
    /// ```qsharp
    /// use qubits = Qubit[10];
    /// let fp = FixedPoint(7, qubits);
    /// let truncated = TruncateFxP(7, 1, fp);
    /// PrepareFxP(3.875, fp);
    /// NearEqualityFactD(MeasureFxP(truncated), 3.5);
    /// ```
    function TruncateFxP(integerBits : Int, fractionalBits : Int, fp : FixedPoint) : FixedPoint { ... }

}
  • Fewer precision -> smaller precision
  • Let's add periods at the end of the summary and the input descriptions to match the rest of the APIs
  • Can you add a bit more detail on how the truncation happens in the # Output section? I can guess that for the fractional bits the least significant bits are truncated, but for the integer bits it's a little less obvious. Are the bits truncated the most significant bits, excluding the sign bit? It would also be good to expand the example to cover integer bit truncation as well.
  • I couldn't find in the API docs the limitations on the number of integer bits - do we require it to be >= 0 or => 1? Are there any limitations on the number of integer bits imposed by that here? For example, can we truncate the number to 0 integer bits, losing the sign bit in the process?

Other than that, looks good!