The Quipper System

Safe HaskellNone

Algorithms.QLS.QDouble

Contents

Description

This modules implements a library for fixed-points real numbers.

Synopsis

Signed fixed point type

data XDouble x Source

Data type for fixed point arithmetic.

XDouble k n represents the number n⋅2-k, where n is a signed integer and k is an integer parameter.

We refer to k as the exponent of the fixed point number. When we speak of the length, we mean the total number of digits (excluding the sign), i.e., the length, in binary digits, of the underlying n.

Constructors

XDouble Int (SignedInt x) 

type FDouble = XDouble BoolSource

The parameter type of fixed-point numbers.

type QDouble = XDouble QubitSource

The quantum type of fixed-point numbers.

type CDouble = XDouble BitSource

The classical type of fixed-point numbers.

Auxiliary definitions

integer_power :: Int -> Int -> IntegerSource

Compute the power of an integer by a non-negative integer.

double_power :: Int -> Int -> DoubleSource

Compute the power of an integer by another, possibly negative one.

div_round :: Integer -> Integer -> IntegerSource

Divide one integer by another, and round the result to the closest integer. If the result is half way between two integers, round to the even one (this is the same behavior as Haskell's round function). This function has unlimited precision.

Operation for length and exponent

Generic functions for XDouble

xdouble_exponent :: XDouble x -> IntSource

Return the exponent k of an XDouble.

xdouble_length :: XDouble x -> IntSource

Return the length m of an XDouble.

xdouble_extent :: XDouble x -> (Int, Int)Source

Return the "extent" of an XDouble. The extent of a fixed-point number x is, by definition, the pair (hi,lo) of integers such that the most significant bit of x has positional index hi-1 (in other words, the value of this bit is 2hi-1), and the least significant bit of x has positional index lo (in other words, the value of this bit is 2lo. Typically, but not necessarily, hi ≥ 0 and lo ≤ 0. In this case, one can also think of hi as "the number of digits before the radix point" and −lo as "the number of digits after the radix point".

The exponent k, length m, and extent (hi,lo) are related by k=-lo and m=hilo.

Examples:

  • a number represented in the form xxxx.yyy has extent (4,-3), exponent 3, and length 7.
  • a number represented in the form xxxx000. has extent (7,3), exponent -3, and length 4.
  • a number represented in the form .000xxxx has extent (-3,-7), exponent 7, and length 4.

If we regard extents as intervals, ordered by inclusion, then it is always possible to losslessly cast a fixed-point number from a smaller to a larger extent.

xdouble_pad_left :: Monad m => m x -> Int -> XDouble x -> m (XDouble x)Source

Add n zeros to the high bits of the XDouble. This sends xxx.yyy to 000xxx.yyy. This increases the length without changing the exponent or value.

xdouble_pad_right :: Monad m => m x -> Int -> XDouble x -> m (XDouble x)Source

Add n zeros to the low bits of the XDouble. This sends xxx.yyy to xxx.yyy000. This increases the length and the exponent without changing the value.

xdouble_pad_to_extent :: Monad m => m x -> (Int, Int) -> XDouble x -> m (XDouble x)Source

Pad an XDouble on both sides to reach the desired extent. This increases the length and exponent without changing the value (it is a lossless operation). It is an error to call this function if the selected extent does not contain the extent of the input XDouble.

xdouble_truncate_right :: Int -> QDouble -> QDoubleSource

Remove the n low bits of an XDouble. This sends xxx.yyyzzz to xxx.yyy. It is an error to call this function when the XDouble has fewer than n digits.

xdouble_of_sint :: SignedInt x -> XDouble xSource

Convert a SignedInt to an XDouble with exponent 0.

Special cases for QDouble

qdouble_pad_left :: Int -> QDouble -> Circ QDoubleSource

Add n zeros to the high bits of the QDouble. This sends xxx.yyy to 000xxx.yyy. This increases the length without changing the exponent or value. This function does not return a fresh copy; it reuses part of its input.

qdouble_pad_right :: Int -> QDouble -> Circ QDoubleSource

Add n zeros to the low bits of the QDouble. This sends xxx.yyy to xxx.yyy000. This increases the length and the exponent without changing the value. This function does not return a fresh copy; it reuses part of its input.

qdouble_pad_to_extent :: (Int, Int) -> QDouble -> Circ QDoubleSource

Pad a QDouble on both sides to reach the desired extent. This increases the length and exponent without changing the value (it is a lossless operation). It is an error to call this function if the selected extent does not contain the extent of the input QDouble. This function does not return a fresh copy; it reuses part of its input.

qdouble_truncate_right :: Int -> QDouble -> QDoubleSource

Remove the n low bits of a QDouble. This sends xxx.yyyzzz to xxx.yyy. Note that the n low qubits are not terminated and become garbage. It is an error to call this function when the QDouble has fewer than n digits.

Special cases for FDouble

fdouble_pad_right :: Int -> FDouble -> FDoubleSource

Add n zeros to the low bits of the FDouble. This sends xxx.yyy to xxx.yyy000. This increases the length and the exponent without changing the value.

fdouble_pad_to_extent :: (Int, Int) -> FDouble -> FDoubleSource

Pad a FDouble on both sides to reach the desired extent. This increases the length and exponent without changing the value (it is a lossless operation). It is an error to call this function if the selected extent does not contain the extent of the input FDouble.

Operations for FDouble

Casts

fdouble_of_double :: Int -> Int -> Double -> FDoubleSource

fdouble_of_double k m x: Convert x to an FDouble of exponent k and length m ≥ 0. Note that the exponent does not need to be between 0 and m; it can even be negative.

fdouble_of_fsint :: FSignedInt -> FDoubleSource

Convert an FSignedInt to an FDouble with exponent 0.

fdouble_of_integer :: Int -> Int -> Integer -> FDoubleSource

Make an FDouble value of exponent k, length m, and value a2-k.

fdouble :: Double -> FDoubleSource

Construct a Double from an FDouble, using some arbitrary method to guess the length and exponent.

show_fdouble :: FDouble -> StringSource

Convert an FDouble to a string in human-readable form.

Type class instances

We make FDouble an instance of Eq, Ord, Real, Num, Fractional, and RealFrac. See the source code for details.

fdouble_align :: FDouble -> FDouble -> (Int, FSignedInt, FSignedInt)Source

Express a pair of FDouble values as a pair of FSignedInts with a common exponent.

Casts

qdouble_of_qsint :: QSignedInt -> Circ QDoubleSource

Convert a QSignedInt to a QDouble with exponent 0. This function does not return a fresh copy; instead, it uses the input qubits.

Type class instances

We make QDouble an instance of QOrd.

qdouble_align :: QDouble -> QDouble -> Circ (Int, QSignedInt, QSignedInt)Source

Express a pair of QDouble values as a pair of QSignedInts with a common exponent.

Other functions

q_ceiling :: QDouble -> Circ QSignedIntSource

QDouble of ceiling: coercion from QDouble to QSignedInt.

q_div_real :: QDouble -> QDouble -> Circ QDoubleSource

Real division with QDouble.