Ejemplo n.º 1
0
from proveit import Etcetera
from proveit.logic import Forall, InSet, Equals, NotEquals, Iff, And, SetOfAll
from proveit.number import Integers, Interval, Reals, RealsPos, Complexes
from proveit.number import Abs, Mod, ModAbs, GreaterThanEquals, LessThanEquals, Add, Sub, Neg, Mult, Fraction, IntervalCO
from proveit.common import a, b, c, x, y, N, xEtc, xMulti
from proveit.number.common import zero, one
from proveit import beginTheorems, endTheorems

beginTheorems(locals())

modIntClosure = Forall((a, b), InSet(Mod(a, b), Integers), domain=Integers)
modIntClosure

modInInterval = Forall((a, b),
                       InSet(Mod(a, b), Interval(zero, Sub(b, one))),
                       domain=Integers)
modInInterval

modRealClosure = Forall((a, b), InSet(Mod(a, b), Reals), domain=Reals)
modRealClosure

modAbsRealClosure = Forall((a, b), InSet(ModAbs(a, b), Reals), domain=Reals)
modAbsRealClosure

absComplexClosure = Forall([a], InSet(Abs(a), Reals), domain=Complexes)
absComplexClosure

absNonzeroClosure = Forall([a],
                           InSet(Abs(a), RealsPos),
                           domain=Complexes,
                           conditions=[NotEquals(a, zero)])
Ejemplo n.º 2
0
# m: Random variable for the measurement of Psi as an integer from the register's binary representation.
m_ = Literal(pkg, 'm')

# phase_m: Random variable for the phase result of the quantum phase estimation.
#          phase_m = m / 2^t
phase_m_ = Literal(pkg, 'phase_m', {LATEX: r'\varphi_m'})

# b: The "best" outcome of m such that phase_m is as close as possible to phase.
b_ = Literal(pkg, 'b')

# 2^t
two_pow_t = Exp(two, t_)

# 2^{t-1}
two_pow_t_minus_one = Exp(two, Sub(t_, one))

# amplitude of output register as indexted
alpha_ = Literal(pkg, 'alpha', {STRING: 'alpha', LATEX: r'\alpha'})
alpha_l = SubIndexed(alpha_, l)
abs_alpha_l = Abs(alpha_l)
alpha_l_sqrd = Exp(Abs(alpha_l), two)

# delta: difference between the phase and the best phase_m
delta_ = Literal(pkg, 'delta', {LATEX: r'\delta'})

fullDomain = Interval(Add(Neg(Exp(two, Sub(t_, one))), one),
                      Exp(two, Sub(t_, one)))
negDomain = Interval(Add(Neg(two_pow_t_minus_one), one), Neg(Add(eps, one)))
posDomain = Interval(Add(eps, one), two_pow_t_minus_one)
epsDomain = Interval(one, Sub(two_pow_t_minus_one, two))
Ejemplo n.º 3
0
summationRealClosure

summationComplexClosure = Forall([P, S],
                                 Implies(
                                     Forall(xMulti,
                                            InSet(PxEtc, Complexes),
                                            domain=S),
                                     InSet(Sum(xMulti, PxEtc, domain=S),
                                           Complexes)))
summationComplexClosure

sumSplitAfter = Forall(
    f,
    Forall([a, b, c],
           Equals(
               Sum(x, fx, Interval(a, c)),
               Add(Sum(x, fx, Interval(a, b)),
                   Sum(x, fx, Interval(Add(b, one), c)))),
           domain=Integers,
           conditions=[LessThanEquals(a, b),
                       LessThan(b, c)]))
sumSplitAfter

sumSplitBefore = Forall(
    f,
    Forall([a, b, c],
           Equals(
               Sum(x, fx, Interval(a, c)),
               Add(Sum(x, fx, Interval(a, Sub(b, one))),
                   Sum(x, fx, Interval(b, c)))),
           domain=Integers,
Ejemplo n.º 4
0
    def __init__(self,
                 parameter_or_parameters,
                 body,
                 start_index_or_indices,
                 end_index_or_indices,
                 styles=None,
                 requirements=tuple(),
                 _lambda_map=None):
        '''
        Create an Iter that represents an iteration of the body for the
        parameter(s) ranging from the start index/indices to the end 
        index/indices.  A Lambda expression will be created as its 
        sub-expression that maps the parameter(s) to the body with
        conditions that restrict the parameter(s) to the appropriate interval.
        
        _lambda_map is used internally for efficiently rebuilding an Iter.
        '''
        from proveit.logic import InSet
        from proveit.number import Interval

        if _lambda_map is not None:
            # Use the provided 'lambda_map' instead of creating one.
            lambda_map = _lambda_map
            pos_args = (parameter_or_parameters, body, start_index_or_indices,
                        end_index_or_indices)
            if pos_args != (None, None, None, None):
                raise ValueError(
                    "Positional arguments of the Init constructor "
                    "should be None if lambda_map is provided.")
            parameters = lambda_map.parameters
            body = lambda_map.body
            conditions = lambda_map.conditions
            if len(conditions) != len(parameters):
                raise ValueError(
                    "Inconsistent number of conditions and lambda "
                    "map parameters")
            start_indices, end_indices = [], []
            for param, condition in zip(parameters, conditions):
                invalid_condition_msg = (
                    "Not the right kind of lambda_map condition "
                    "for an iteration")
                if not isinstance(condition,
                                  InSet) or condition.element != param:
                    raise ValueError(invalid_condition_msg)
                domain = condition.domain
                if not isinstance(domain, Interval):
                    raise ValueError(invalid_condition_msg)
                start_index, end_index = domain.lowerBound, domain.upperBound
                start_indices.append(start_index)
                end_indices.append(end_index)
            self.start_indices = ExprTuple(*start_indices)
            self.end_indices = ExprTuple(*end_indices)
            if len(parameters) == 1:
                self.start_index = self.start_indices[0]
                self.end_index = self.end_indices[0]
                self.start_index_or_indices = self.start_index
                self.end_index_or_indices = self.end_index
            else:
                self.start_index_or_indices = self.start_indices
                self.end_index_or_indices = self.end_indices
        else:
            parameters = compositeExpression(parameter_or_parameters)

            start_index_or_indices = singleOrCompositeExpression(
                start_index_or_indices)
            if isinstance(start_index_or_indices,
                          ExprTuple) and len(start_index_or_indices) == 1:
                start_index_or_indices = start_index_or_indices[0]
            self.start_index_or_indices = start_index_or_indices
            if isinstance(start_index_or_indices, Composite):
                # a composite of multiple indices
                self.start_indices = self.start_index_or_indices
            else:
                # a single index
                self.start_index = self.start_index_or_indices
                # wrap a single index in a composite for convenience
                self.start_indices = compositeExpression(
                    self.start_index_or_indices)

            end_index_or_indices = singleOrCompositeExpression(
                end_index_or_indices)
            if isinstance(end_index_or_indices,
                          ExprTuple) and len(end_index_or_indices) == 1:
                end_index_or_indices = end_index_or_indices[0]
            self.end_index_or_indices = end_index_or_indices
            if isinstance(self.end_index_or_indices, Composite):
                # a composite of multiple indices
                self.end_indices = self.end_index_or_indices
            else:
                # a single index
                self.end_index = self.end_index_or_indices
                # wrap a single index in a composite for convenience
                self.end_indices = compositeExpression(
                    self.end_index_or_indices)

            conditions = []
            for param, start_index, end_index in zip(parameters,
                                                     self.start_indices,
                                                     self.end_indices):
                conditions.append(
                    InSet(param, Interval(start_index, end_index)))

            lambda_map = Lambda(parameters, body, conditions=conditions)

        self.ndims = len(self.start_indices)
        if self.ndims != len(self.end_indices):
            raise ValueError(
                "Inconsistent number of 'start' and 'end' indices")

        if len(parameters) != len(self.start_indices):
            raise ValueError(
                "Inconsistent number of indices and lambda map parameters")

        Expression.__init__(self, ['Iter'], [lambda_map],
                            styles=styles,
                            requirements=requirements)
        self.lambda_map = lambda_map
        self._checkIndexedRestriction(body)
Ejemplo n.º 5
0
notInIntsIsBool = Forall(a, InSet(NotInSet(a, Integers), Booleans))
notInIntsIsBool

intsInReals = Forall(a, InSet(a, Reals), domain=Integers)
intsInReals

intsInComplexes = Forall(a, InSet(a, Complexes), domain=Integers)
intsInComplexes

inNaturalsIfNonNeg = Forall(a, InSet(a,Naturals), domain=Integers, conditions=[GreaterThanEquals(a, zero)])
inNaturalsIfNonNeg

inNaturalsPosIfPos = Forall(a, InSet(a,NaturalsPos), domain=Integers, conditions=[GreaterThan(a, zero)])
inNaturalsPosIfPos

intervalInInts = Forall((a, b), Forall(n, InSet(n, Integers), domain=Interval(a, b)), domain=Integers)
intervalInInts          

intervalInNats = Forall((a, b), Forall(n, InSet(n, Naturals), domain=Interval(a, b)), domain=Naturals)
intervalInNats  

intervalInNatsPos = Forall((a, b), Forall(n, InSet(n, NaturalsPos), domain=Interval(a, b)), domain=Integers, conditions=[GreaterThan(a, zero)])
intervalInNatsPos

allInNegativeIntervalAreNegative = Forall((a, b), Forall(n, LessThan(n, zero), domain=Interval(a, b)), domain=Integers, conditions=[LessThan(b, zero)])
allInNegativeIntervalAreNegative

allInPositiveIntervalArePositive = Forall((a, b), Forall(n, GreaterThan(n, zero), domain=Interval(a, b)), domain=Integers, conditions=[GreaterThan(a, zero)])
allInPositiveIntervalArePositive

intervalLowerBound = Forall((a, b), Forall(n, LessThanEquals(a, n), domain=Interval(a, b)), domain=Integers)
Ejemplo n.º 6
0
from proveit.logic import Forall, Equals
from proveit.number import Sum, Integers, Interval, LessThan, Add, Sub
from proveit.common import a, b, f, x, fa, fb, fx
from proveit.number.common import one
from proveit import beginAxioms, endAxioms

beginAxioms(locals())

sumSingle = Forall(f, Forall(a,
                              Equals(Sum(x,fx,Interval(a,a)),
                                     fa),
                              domain=Integers))
sumSingle

sumSplitLast = Forall(f, 
                      Forall([a,b],
                             Equals(Sum(x,fx,Interval(a,b)),
                                    Add(Sum(x,fx,Interval(a,Sub(b, one))),
                                       fb)),
                             domain=Integers, conditions=[LessThan(a, b)]))
sumSplitLast


endAxioms(locals(), __package__)