The Quipper System

Algorithms.QLS.QDouble

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)

Instances

 # Methods(==) :: QDouble -> QDouble -> Bool #(/=) :: QDouble -> QDouble -> Bool # # Methods(==) :: FDouble -> FDouble -> Bool #(/=) :: FDouble -> FDouble -> Bool # # Methods # Methods # Methods(<) :: FDouble -> FDouble -> Bool #(<=) :: FDouble -> FDouble -> Bool #(>) :: FDouble -> FDouble -> Bool #(>=) :: FDouble -> FDouble -> Bool # # Methods # MethodsproperFraction :: Integral b => FDouble -> (b, FDouble) #truncate :: Integral b => FDouble -> b #round :: Integral b => FDouble -> b #ceiling :: Integral b => FDouble -> b #floor :: Integral b => FDouble -> b # # Methods # Methods # Methods Show x => Show (XDouble x) # MethodsshowsPrec :: Int -> XDouble x -> ShowS #show :: XDouble x -> String #showList :: [XDouble x] -> ShowS # QCLeaf x => QCData (XDouble x) # Methodsqcdata_mapM :: Monad m => XDouble x -> (q -> m q') -> (c -> m c') -> QCType q c (XDouble x) -> m (QCType q' c' (XDouble x)) Source #qcdata_zip :: XDouble x -> q -> c -> q' -> c' -> QCType q c (XDouble x) -> QCType q' c' (XDouble x) -> ErrMsg -> QCType (q, q') (c, c') (XDouble x) Source #qcdata_promote :: BType (XDouble x) -> XDouble x -> ErrMsg -> BType (XDouble x) Source # QCLeaf x => Labelable (XDouble x) String # Methodslabel_rec :: XDouble x -> String -> LabelMonad () Source # # Methods # Methods(+) :: (FDouble, FDouble) -> (FDouble, FDouble) -> (FDouble, FDouble) #(-) :: (FDouble, FDouble) -> (FDouble, FDouble) -> (FDouble, FDouble) #(*) :: (FDouble, FDouble) -> (FDouble, FDouble) -> (FDouble, FDouble) #negate :: (FDouble, FDouble) -> (FDouble, FDouble) #abs :: (FDouble, FDouble) -> (FDouble, FDouble) #signum :: (FDouble, FDouble) -> (FDouble, FDouble) # # Methodsq_add :: (QDouble, QDouble) -> (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble), (QDouble, QDouble)) Source #q_mult :: (QDouble, QDouble) -> (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble), (QDouble, QDouble)) Source #q_sub :: (QDouble, QDouble) -> (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble), (QDouble, QDouble)) Source #q_abs :: (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble)) Source #q_negate :: (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble)) Source #q_signum :: (QDouble, QDouble) -> Circ ((QDouble, QDouble), (QDouble, QDouble)) Source # type QTypeB FDouble # type QTypeB FDouble = QDouble type QCType x y (XDouble z) # type QCType x y (XDouble z) = XDouble (QCType x y z)

The parameter type of fixed-point numbers.

The quantum type of fixed-point numbers.

The classical type of fixed-point numbers.

# Auxiliary definitions

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

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

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

Return the exponent k of an XDouble.

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.

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.

Convert a SignedInt to an XDouble with exponent 0.

## Special cases for QDouble

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.

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.

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.

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

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.

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 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.

Convert an FDouble to a Double.

Convert an FSignedInt to an FDouble with exponent 0.

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

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

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.

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

# Casts

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.

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

# Other functions

Coercion from QSignedInt to QDouble.

QDouble of ceiling: coercion from QDouble to QSignedInt.

Real division with QDouble.

# Orphan instances

 # Methods