Safe Haskell | None |
---|
Algorithms.QLS.QDouble
Contents
Description
This modules implements a library for fixed-points real numbers.
- data XDouble x = XDouble Int (SignedInt x)
- type FDouble = XDouble Bool
- type QDouble = XDouble Qubit
- type CDouble = XDouble Bit
- integer_power :: Int -> Int -> Integer
- double_power :: Int -> Int -> Double
- div_round :: Integer -> Integer -> Integer
- xdouble_exponent :: XDouble x -> Int
- xdouble_length :: XDouble x -> Int
- xdouble_extent :: XDouble x -> (Int, Int)
- xdouble_pad_left :: Monad m => m x -> Int -> XDouble x -> m (XDouble x)
- xdouble_pad_right :: Monad m => m x -> Int -> XDouble x -> m (XDouble x)
- xdouble_pad_to_extent :: Monad m => m x -> (Int, Int) -> XDouble x -> m (XDouble x)
- xdouble_truncate_right :: Int -> QDouble -> QDouble
- xdouble_of_sint :: SignedInt x -> XDouble x
- qdouble_pad_left :: Int -> QDouble -> Circ QDouble
- qdouble_pad_right :: Int -> QDouble -> Circ QDouble
- qdouble_pad_to_extent :: (Int, Int) -> QDouble -> Circ QDouble
- qdouble_truncate_right :: Int -> QDouble -> QDouble
- fdouble_pad_right :: Int -> FDouble -> FDouble
- fdouble_pad_to_extent :: (Int, Int) -> FDouble -> FDouble
- fdouble_of_double :: Int -> Int -> Double -> FDouble
- double_of_fdouble :: FDouble -> Double
- fdouble_of_fsint :: FSignedInt -> FDouble
- fdouble_of_integer :: Int -> Int -> Integer -> FDouble
- fdouble :: Double -> FDouble
- show_fdouble :: FDouble -> String
- fdouble_align :: FDouble -> FDouble -> (Int, FSignedInt, FSignedInt)
- qdouble_of_qsint :: QSignedInt -> Circ QDouble
- qdouble_align :: QDouble -> QDouble -> Circ (Int, QSignedInt, QSignedInt)
- q_fromIntegral :: QSignedInt -> Circ QDouble
- q_ceiling :: QDouble -> Circ QSignedInt
- q_div_real :: QDouble -> QDouble -> Circ QDouble
Signed fixed point type
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.
Instances
Eq QDouble | |
Eq FDouble | |
Floating FDouble | |
Fractional FDouble | |
Num FDouble | |
Ord FDouble | |
Real FDouble | |
RealFloat FDouble | |
RealFrac FDouble | |
Typeable1 XDouble | |
QOrd QDouble | |
QNum QDouble | |
QFloating QDouble | |
Show x => Show (XDouble x) | |
QCLeaf x => QCData (XDouble x) | |
QCLeaf x => Labelable (XDouble x) String | |
CircLiftingUnpack (Circ QDouble) (Circ QDouble) | |
Num (FDouble, FDouble) | |
QNum (QDouble, QDouble) |
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=hi−lo.
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_truncate_right :: Int -> QDouble -> QDoubleSource
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
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.
Operations for FDouble
Casts
fdouble_of_double :: Int -> Int -> Double -> FDoubleSource
: Convert x to an fdouble_of_double
k m xFDouble
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.
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 FSignedInt
s 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
qdouble_align :: QDouble -> QDouble -> Circ (Int, QSignedInt, QSignedInt)Source
Express a pair of QDouble
values as a pair of QSignedInt
s with a
common exponent.
Other functions
q_fromIntegral :: QSignedInt -> Circ QDoubleSource
Coercion from QSignedInt
to QDouble
.
q_ceiling :: QDouble -> Circ QSignedIntSource
QDouble of ceiling
: coercion from QDouble
to QSignedInt
.