The Quipper System

Algorithms.CL.Types

Description

This module defines the specialized datatypes of the Class Number algorithm, and basic utility functions on these types.

Synopsis

# Type synonyms

First, we define some type synonyms for arithmetic types, selecting which will be used in the functions for the Class Number algorithm.

We use three different integer types. For interfacing with quantum computation, we use CLInt := IntM. For efficient classical (i.e. circuit-generation time) computation on potentially large integers, we use CLIntP := Integer, Haskell’s arbitrary-precision integers. (Δ, for instance, is taken to be a CLIntP). For small classical integers (typically for register sizes), we use Int, Haskell’s bounded-precision integers.

For the first two of these, we define type synonyms, so that they can be swapped out to other types if desired (they are to a large extent modular). For Int we do not, since we make un-coerced use of built-in Haskell functions like length which give it specifically.

Where not dictated by these conventions, integer types are generalized, i.e., (Integral a) =>

Rational and real numbers have not yet been similarly stratified.

type CLInt = IntM Source #

Integers that may be passed into or received out of quantum computations.

type CLIntP = Integer Source #

Integers that will be used for parameter computation only, potentially large.

Rational numbers for the Class Number code.

type CLReal = FPReal Source #

Real numbers for the Class Number code.

# Algebraic number fields

## Discriminants

The functions of this subsection are needed only for circuit-generation-time classical computation, not for quantum circuit computation.

bigD_of_d :: Integral a => a -> a Source #

Compute Δ, given d. (See [Jozsa 2003], Prop. 6 et seq. We use Δ, or in code bigD, where Jozsa uses D.)

d_of_bigD :: Integral a => a -> a Source #

Compute d, given Δ. (Again, see [Jozsa 2003], Prop. 6 et seq.)

is_valid_d :: Integral a => a -> Bool Source #

Check if d is a valid input to Hallgren’s algorithm, i.e. correctly defines a real quadratic number field.

is_valid_bigD :: Integral a => a -> Bool Source #

Check if Δ is a valid input to Hallgren’s algorithm, i.e. is the discriminant of a real quadratic number field. (Cf. http://en.wikipedia.org/wiki/Fundamental_discriminant)

all_small_ds :: Integral int => [int] Source #

The (infinite, lazy) list of all valid inputs d, i.e. of all square-free integers above 2.

all_bigDs :: Integral int => [int] Source #

The (infinite, lazy) list of all valid inputs Δ, i.e. of all discriminants of real quadratic number fields.

## Field elements

data AlgNumGen a Source #

A data type describing a number in the algebraic number field K = ℚ[√Δ]: AlgNum a b Δ represents a + b√Δ.

In general, the type of coefficients may be any type of (classical or quantum) numbers, i.e. an instance of the Num or QNum class. Given this, the algebraic numbers with a fixed Δ will in turn be an instance of Num or QNum.

A value a :: x may also be used as an AlgNumGen x, with no Δ specified, to represent simply a + 0√Δ; this can be considered polymorphic over all possible values of Δ.

This is similar to the use of IntMs or FPReals of indeterminate size, although unlike for them, we do not restrict this to the classical case. However, the question of whether an AlgNumQ has specified √Δ is (like e.g. the length of a list) is a parameter property, known at circuit generation time, not a purely quantum property.

Constructors

 AlgNum a a CLIntP AlgNum_indet a

Instances

 (Eq a, Num a) => Eq (AlgNumGen a) # Methods(==) :: AlgNumGen a -> AlgNumGen a -> Bool #(/=) :: AlgNumGen a -> AlgNumGen a -> Bool # (Ord a, Fractional a) => Fractional (AlgNumGen a) # Methods(/) :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a #recip :: AlgNumGen a -> AlgNumGen a # (Ord a, Num a) => Num (AlgNumGen a) # Methods(+) :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a #(-) :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a #(*) :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a #negate :: AlgNumGen a -> AlgNumGen a #abs :: AlgNumGen a -> AlgNumGen a #signum :: AlgNumGen a -> AlgNumGen a # (Ord a, Num a) => Ord (AlgNumGen a) # Methodscompare :: AlgNumGen a -> AlgNumGen a -> Ordering #(<) :: AlgNumGen a -> AlgNumGen a -> Bool #(<=) :: AlgNumGen a -> AlgNumGen a -> Bool #(>) :: AlgNumGen a -> AlgNumGen a -> Bool #(>=) :: AlgNumGen a -> AlgNumGen a -> Bool #max :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a #min :: AlgNumGen a -> AlgNumGen a -> AlgNumGen a # Real a => Real (AlgNumGen a) # Methods RealFrac a => RealFrac (AlgNumGen a) # MethodsproperFraction :: Integral b => AlgNumGen a -> (b, AlgNumGen a) #truncate :: Integral b => AlgNumGen a -> b #round :: Integral b => AlgNumGen a -> b #ceiling :: Integral b => AlgNumGen a -> b #floor :: Integral b => AlgNumGen a -> b # Show a => Show (AlgNumGen a) # MethodsshowsPrec :: Int -> AlgNumGen a -> ShowS #show :: AlgNumGen a -> String #showList :: [AlgNumGen a] -> ShowS #

The specific instance of AlgNumGen used for classical (parameter) computation.

fst_AlgNum :: AlgNumGen a -> a Source #

Extract the first co-ordinate of an AlgNumGen

snd_AlgNum :: Num a => AlgNumGen a -> a Source #

Extract the second co-ordinate of an AlgNumGen

Print a Number in human-readable (though not Haskell-readable) format, as e.g.

floating_of_AlgNum :: (Real a, Floating b) => AlgNumGen a -> b Source #

Realize an algebraic number as a real number (of any Floating type).

number_promote :: Num a => AlgNumGen a -> AlgNumGen b -> ErrMsg -> AlgNumGen a Source #

Coerce one algebraic number into the field of a second, if possible. If not possible (i.e. if their Δ’s mismatch), throw an error.

conjugate :: Num a => AlgNumGen a -> AlgNumGen a Source #

The algebraic conjugate: sends a + b √Δ to a - b √Δ.

is_alg_int :: (Ord a, RealFrac a) => AlgNumGen a -> Bool Source #

Test whether an algebraic number is an algebraic integer.

(A number is an algebraic integer iff it can be written in the form m + n(Δ + √Δ)/2, where m, n are integers. See [Jozsa 2003], proof of Prop. 14.)

is_unit :: (Ord a, RealFrac a) => AlgNumGen a -> Bool Source #

Test whether an algebraic number is a unit of the ring of algebraic integers.

The number ω associated to the field K.

# Ideals

data IdealX x Source #

Data specifying an ideal in an algebraic number field. An ideal is described by a tuple (Δ,m,l,a,b), representing the ideal

m/l (aZ + (b+√Δ)/2 Z),

where moreover we assume and ensure always that the ideal is in standard form ([Jozsa 2003], p.11, Prop. 16). Specifically,

• a,k,l > 0;
• 4a | b2 – Δ;
• b = τ(a,b);
• gcd(k,l) = 1

In particular, this gives us bounds on the size of a and b, and hence tells us the sizes needed for these registers (see length_for_ab below).

Constructors

 Ideal CLIntP (XInt x) (XInt x) (XInt x) (XInt x)

Instances

 # Methods(==) :: Ideal -> Ideal -> Bool #(/=) :: Ideal -> Ideal -> Bool # # MethodsshowsPrec :: Int -> Ideal -> ShowS #show :: Ideal -> String #showList :: [Ideal] -> ShowS # # Methodslabel_rec :: IdealQ -> (String, String, String, String) -> LabelMonad () Source # Eq x => Eq (IdealX x) # Methods(==) :: IdealX x -> IdealX x -> Bool #(/=) :: IdealX x -> IdealX x -> Bool # Show x => Show (IdealX x) # MethodsshowsPrec :: Int -> IdealX x -> ShowS #show :: IdealX x -> String #showList :: [IdealX x] -> ShowS # QCLeaf x => QCData (IdealX x) # Methodsqcdata_mapM :: Monad m => IdealX x -> (q -> m q') -> (c -> m c') -> QCType q c (IdealX x) -> m (QCType q' c' (IdealX x)) Source #qcdata_zip :: IdealX x -> q -> c -> q' -> c' -> QCType q c (IdealX x) -> QCType q' c' (IdealX x) -> ErrMsg -> QCType (q, q') (c, c') (IdealX x) Source #qcdata_promote :: BType (IdealX x) -> IdealX x -> ErrMsg -> BType (IdealX x) Source # QCLeaf x => Labelable (IdealX x) String # Methodslabel_rec :: IdealX x -> String -> LabelMonad () Source # type QTypeB Ideal # type QTypeB Ideal = IdealQ type QCType x y (IdealX z) # type QCType x y (IdealX z) = IdealX (QCType x y z)

Classical parameter specifying an ideal.

Quantum circuit-type counterpart of Ideal.

Classical circuit-type counterpart of Ideal.

data IdealRedX x Source #

Data specifying a reduced ideal, by a tuple (Δ,a,b); this corresponds to the ideal specified by (Δ,1,a,a,b), i.e., Z + (b+√Δ)/2a Z.

Constructors

 IdealRed CLIntP (XInt x) (XInt x)

Instances

 # Methods # MethodsshowList :: [IdealRed] -> ShowS # Show x => Show (IdealRedX x) # MethodsshowsPrec :: Int -> IdealRedX x -> ShowS #show :: IdealRedX x -> String #showList :: [IdealRedX x] -> ShowS # QCLeaf x => QCData (IdealRedX x) # Methodsqcdata_mapM :: Monad m => IdealRedX x -> (q -> m q') -> (c -> m c') -> QCType q c (IdealRedX x) -> m (QCType q' c' (IdealRedX x)) Source #qcdata_zip :: IdealRedX x -> q -> c -> q' -> c' -> QCType q c (IdealRedX x) -> QCType q' c' (IdealRedX x) -> ErrMsg -> QCType (q, q') (c, c') (IdealRedX x) Source #qcdata_promote :: BType (IdealRedX x) -> IdealRedX x -> ErrMsg -> BType (IdealRedX x) Source # # Methods QCLeaf x => Labelable (IdealRedX x) (String, String) # Methodslabel_rec :: IdealRedX x -> (String, String) -> LabelMonad () Source # type QTypeB IdealRed # type QCType x y (IdealRedX z) # type QCType x y (IdealRedX z) = IdealRedX (QCType x y z)

Classical parameter specifying a reduced ideal.

Quantum circuit-type counterpart of IdealRed.

Classical circuit-type counterpart of IdealRed.

type IdDist = (Ideal, FPReal) Source #

An ideal I, together with a distance δ for it — that is, some representative, mod R, for δ(I) as defined on G p.4. Most functions described as acting on ideals need in fact to be seen as a pair of an ideal and a distance for it.

type IdDistQ = (IdealQ, FPRealQ) Source #

Quantum analogue of IdDist.

type IdRedDist = (IdealRed, FPReal) Source #

A reduced ideal I, together with a distance δ for it.

type IdRedDistQ = (IdealRedQ, FPRealQ) Source #

Quantum analogue of IdRedDist.

## Trivial access functions

Extract the d component from an IdealQ.

Extract the d component from an IdealRedQ.

Extract Δ from an IdealQ.

Extract Δ from an IdealRedQ.

Extract the delta part from an ideal/distance pair.

## Assertions, coercions

Elements of the types Ideal, IdealRed, etc are assumed to satisfy certain extra conditions. This section includes functions for checking that these conditions are satisfied, and for safely coercing between these types.

tau :: (Integral int, Integral int') => int' -> int -> int -> int Source #

tau Δ b a: the function τ(b,a). Gives the representative for b mod 2a, in a range dependent on a and √Δ.

(This doesn't quite belong here, but is included as a prerequisite of the assertions).

Return True if the given ideal is in standard form. (Functions should always keep ideals in standard form).

Test whether an Ideal is reduced. (An ideal <m,l,a,b> is reduced iff m = 1, l = a, b ≥ 0 and b + √Δ > 2a ([Jozsa 2003], Prop. 20)).

Test whether an IdealRed is really reduced. (An ideal <1,a,a,b> is reduced iff b ≥ 0 and b + √Δ > 2a ([Jozsa 2003], Prop. 20)).

Coerce an IdealRed to an Ideal.

Coerce an Ideal to an IdealRed, if it is reduced, or throw an error otherwise. Cf. [Jozsa 2003], Prop. 20.

assert_reduced :: Ideal -> a -> a Source #

Throw an error if an Ideal is not reduced; otherwise, the identity function.

assert_really_reduced :: IdealRed -> a -> a Source #

Throw an error if an IdealRed is not really reduced; otherwise, the identity function.

q_tau :: CLIntP -> QDInt -> QDInt -> Circ (QDInt, QDInt, QDInt) Source #

Quantum analogue of tau. q_tau Δ qb qa: compute the representative for qb mod 2qa, in a range dependent on qa and √Δ.

Test whether a given IdealQ is reduced. <m,l,a,b> is reduced iff m = 1, l = a, b ≥ 0 and b + √Δ > 2a ([Jozsa 2003], Prop. 20).

Test whether a given IdealQ is really reduced (as it should always be, if code is written correctly). An ideal <1,a,a,b> is reduced iff b ≥ 0 and b + √Δ > 2a ([Jozsa 2003], Prop. 20).

Coerce an IdealRedQ to an IdealQ, initializing the extra components appropriately.

Coerce an IdealQ to an IdealRedQ, assertively terminating the extra components (and hence throwing an error at quantum runtime if the input is not reduced).

Throw a (quantum-runtime) error if an IdealRedQ is not really reduced; otherwise, do nothing.

Compare assert_reduced, q_is_really_reduced in Algorithms.CL.RegulatorQuantum, and [Jozsa 2003] Prop. 20.

## Bounds on coefficient sizes

Given Δ, how much space should be allocated for the coefficients of ideals? Most of these bounds are currently missing or uncertain, as documented below. Note these bounds are intended to be sufficient for the calculations occurring in this algorithm, not for representing arbitrary ideals.

Given Δ, return the size of integers to be used for the coefficients a, b of reduced ideals.

Note: can we bound this more carefully? In reduced ideals, we always have 0 ≤ a,b ≤ √Δ (see notes on is_standard, is_reduced), and the outputs of ρ, ρ–1 and dot-products of reduced ideals always keep |a| ≤ Δ. However, intermediate calculations may involve larger values, so we allocate a little more space. For now, this padding is a seat-of-the-pants estimate.

Given Δ, return the size of integers to be used for the coefficients m, l of general ideals.

TODO: bound this! Neither Hallgren nor [Jozsa 2003] discusses bounds on the values of m and l that will appear, and we do not yet have a bound. For now we use the same length as for a and b, for convenience; this should be considered a dummy bound, quite possibly not sufficient in general.

n_of_bigD :: Integral int => CLIntP -> int Source #

Given Δ, return the precision n = log2N to be used for discretizing the quasi-periodic function f to fN.

(“Precision” here means the number of binary digits after the point).

Taken to ensure 1/N < 3/(32 Δ log Δ). (Cf. [Jozsa 2003], Prop. 36 (iii).)

Given Δ, n, l (as for fN, q_fN), return the precision required for intermediate distance calculations during the computation of fN.

TODO: bound this more carefully. [Jozsa 2003] asks for the final output to be precision n, but does not discuss intermediate precision, and we have not yet got a confident answer. For now, just a back-of-the-envelope estimate, which should be sufficient and O(correct), but is almost certainly rather larger than necessary.

Set the IntM coefficients of an Ideal to the standard lengths, if they are not already fixed incompatibly. The standard lengths are determined by length_for_ml, length_for_ab. (Compare intm_promote, etc.)

Set the IntM coefficients of an IdealRed to the standard lengths, if they are not already fixed incompatibly. The standard lengths are determined by length_for_ml, length_for_ab. (Compare intm_promote, etc.)