The Quipper System

Safe HaskellNone

Algorithms.CL.Types

Contents

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 = IntMSource

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

type CLIntP = IntegerSource

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

type CLRational = RationalSource

Rational numbers for the Class Number code.

type CLReal = FPRealSource

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

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

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

is_valid_d :: Integral a => a -> BoolSource

Check if d correctly defines a quadratic number field.

Query: [Jozsa 2003] simply gives the condition as “positive, square-free”. But >1 seems to also be assumed; possibly >2 or >3? For now we allow 1,2,3.

is_valid_bigD :: Integral a => a -> BoolSource

Check if Δ is a valid discriminant. Note: see query on is_valid_d.

all_small_ds :: Integral int => [int]Source

The (infinite, lazy) list of all valid (i.e. positive, square-free) integers d, starting from 5.

all_bigDs :: Integral int => [int]Source

The (infinite, lazy) list of all valid discriminants Δ, apart from the dubious 1, 8, 12 (see note at is_valid_d).

Field elements

data AlgNumberGen a Source

A data type describing a number in the algebraic number field K = ℚ[√Δ]: AlgNumber 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 AlgNumberGen 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 AlgNumberQ 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

AlgNumber a a CLIntP 
AlgNumber_indet a 

Instances

(Eq a, Num a) => Eq (AlgNumberGen a) 
(Ord a, Fractional a) => Fractional (AlgNumberGen a) 
(Ord a, Num a) => Num (AlgNumberGen a) 
(Ord a, Num a) => Ord (AlgNumberGen a) 
Real a => Real (AlgNumberGen a) 
RealFrac a => RealFrac (AlgNumberGen a) 
Show a => Show (AlgNumberGen a) 

type AlgNumber = AlgNumberGen CLRationalSource

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

fst_Number :: AlgNumberGen a -> aSource

Extract the first co-ordinate of an AlgNumberGen

snd_Number :: Num a => AlgNumberGen a -> aSource

Extract the second co-ordinate of an AlgNumberGen

pretty_show_Number :: Show a => AlgNumberGen a -> StringSource

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

floating_of_Number :: (Real a, Floating b) => AlgNumberGen a -> bSource

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

number_promote :: Num a => AlgNumberGen a -> AlgNumberGen b -> ErrMsg -> AlgNumberGen aSource

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 => AlgNumberGen a -> AlgNumberGen aSource

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

is_alg_int :: (Ord a, RealFrac a) => AlgNumberGen a -> BoolSource

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, Prop. 14.)

is_unit :: (Ord a, RealFrac a) => AlgNumberGen a -> BoolSource

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

omega_of_bigD :: CLIntP -> AlgNumberSource

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) 

type Ideal = IdealX BoolSource

Classical parameter specifying an ideal.

type IdealQ = IdealX QubitSource

Quantum circuit-type counterpart of Ideal.

type IdealC = IdealX BitSource

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) 

type IdealRed = IdealRedX BoolSource

Classical parameter specifying a reduced ideal.

type IdealRedQ = IdealRedX QubitSource

Quantum circuit-type counterpart of IdealRed.

type IdealRedC = IdealRedX BitSource

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

d_of_Ideal :: IdealX a -> CLIntPSource

Extract the d component from an IdealQ.

d_of_IdealRed :: IdealRedX a -> CLIntPSource

Extract the d component from an IdealRedQ.

bigD_of_Ideal :: IdealX a -> CLIntPSource

Extract Δ from an IdealQ.

bigD_of_IdealRed :: IdealRedX a -> CLIntPSource

Extract Δ from an IdealRedQ.

delta :: IdDist -> CLRealSource

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

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

is_standard :: Ideal -> BoolSource

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

is_reduced :: Ideal -> BoolSource

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, Prop. 20)).

is_really_reduced :: IdealRed -> BoolSource

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

to_reduced :: Ideal -> IdealRedSource

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

assert_reduced :: Ideal -> a -> aSource

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

assert_really_reduced :: IdealRed -> a -> aSource

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

q_is_reduced :: IdealQ -> Circ (IdealQ, Qubit)Source

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

q_is_really_reduced :: IdealRedQ -> Circ (IdealRedQ, Qubit)Source

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, Prop. 20).

q_forget_reduced :: IdealRedQ -> Circ IdealQSource

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

q_assert_reduced :: IdealQ -> Circ IdealRedQSource

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

q_assert_really_reduced :: IdealRedQ -> Circ IdealRedQSource

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

length_for_ab :: CLIntP -> IntSource

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.

length_for_ml :: CLIntP -> IntSource

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

TODO: bound this! Neither Hallgren nor Jozsa 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 -> intSource

Given Δ, return the precision n = log2N for the input/output of fN.

precision_for_fN :: CLIntP -> Int -> Int -> IntSource

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

(“Precision” here is given as the number of binary digits after the point).

TODO: bound this more carefully. The Jozsa paper 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.

fix_sizes_Ideal :: Ideal -> IdealSource

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

fix_sizes_IdealRed :: IdealRed -> IdealRedSource

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

Sample data

Some fairly arbitrarily chosen sample elements of these types, for convenience in testing circuit functions.