def square_root_both_sides(self, **defaults_config): from proveit.numbers import frac, one, two, Exp new_rel = self.exponentiate_both_sides(frac(one, two)) if (isinstance(new_rel.lhs, Exp) and new_rel.lhs.exponent == frac(one, two)): new_rel = new_rel.inner_expr().lhs.with_styles(exponent='radical') if (isinstance(new_rel.rhs, Exp) and new_rel.rhs.exponent == frac(one, two)): new_rel = new_rel.inner_expr().rhs.with_styles(exponent='radical') # Match style (e.g., use '>' if 'direction' is 'reversed'). return new_rel.with_mimicked_style(self)
def square_root_both_sides_of_equals(relation, **defaults_config): ''' Take the square root of both sides of the Equals relation. ''' from proveit.numbers import frac, one, two, Exp new_rel = ComplexSet.exponentiate_both_sides_of_equals( relation, frac(one, two)) if isinstance(new_rel.lhs, Exp) and new_rel.lhs.exponent == frac( one, two): new_rel = new_rel.inner_expr().lhs.with_styles(exponent='radical') if isinstance(new_rel.rhs, Exp) and new_rel.lhs.exponent == frac( one, two): new_rel = new_rel.inner_expr().rhs.with_styles(exponent='radical') return new_rel
def formatted(self, format_type, **kwargs): # begin building the inner_str inner_str = self.base.formatted(format_type, fence=True, force_fence=True) if self.get_style('exponent') == 'raised': inner_str = (inner_str + r'^{' + self.exponent.formatted(format_type, fence=False) + '}') elif self.get_style('exponent') == 'radical': if self.exponent == frac(one, two): if format_type == 'string': inner_str = (r'sqrt(' + self.base.formatted( format_type, fence=True, force_fence=True) + ')') elif format_type == 'latex': inner_str = (r'\sqrt{' + self.base.formatted( format_type, fence=True, force_fence=True) + '}') else: raise ValueError( "Unkown radical type, exponentiating to the power " "of %s" % str(self.exponent)) # only fence if force_fence=True (nested exponents is an # example of when fencing must be forced) kwargs['fence'] = (kwargs['force_fence'] if 'force_fence' in kwargs else False) return maybe_fenced_string(inner_str, **kwargs)
def square_root_both_sides(self, *, simplify=True, assumptions=USE_DEFAULTS): from proveit.numbers import frac, one, two, Exp new_rel = self.exponentiate_both_sides(frac(one, two), simplify=simplify, assumptions=assumptions) if (isinstance(new_rel.lhs, Exp) and new_rel.lhs.exponent == frac(one, two)): new_rel = new_rel.inner_expr().lhs.with_styles(exponent='radical') if (isinstance(new_rel.rhs, Exp) and new_rel.rhs.exponent == frac(one, two)): new_rel = new_rel.inner_expr().rhs.with_styles(exponent='radical') # Match style (e.g., use '>' if 'direction' is 'reversed'). return new_rel.with_matching_style(self)
def remake_constructor(self): if self.get_style('exponent') == 'radical': # Use a different constructor if using the 'radical' style. if self.exponent == frac(one, two): return 'sqrt' else: raise ValueError( "Unkown radical type, exponentiating to the power " "of %s" % str(self.exponent)) return Function.remake_constructor(self)
def square_root_both_sides_of_notequals(relation, **defaults_config): ''' Take the square root of both sides of the NotEquals relation. ''' from proveit.numbers import frac, one, two new_rel = ComplexSet.exponentiate_both_sides_of_notequals( relation, frac(one, two)) new_rel = new_rel.inner_expr().lhs.with_styles(exponent='radical') new_rel = new_rel.inner_expr().rhs.with_styles(exponent='radical') return new_rel
def is_irreducible_value(self): ''' This needs work, but we know that sqrt(2) is irreducible as a special case. ''' if isinstance(self.exponent, Div): if self.exponent == frac(one, two): if self.base == two: return True return False # TODO: handle more cases.
def square_root_both_sides_of_equals(relation, assumptions=USE_DEFAULTS): ''' Take the square root of both sides of the Equals relation. ''' from proveit.numbers import frac, one, two new_rel = ComplexSet.exponentiate_both_sides_of_equals( relation, frac(one, two), assumptions=assumptions) new_rel = new_rel.inner_expr().lhs.with_styles(exponent='radical') new_rel = new_rel.inner_expr().rhs.with_styles(exponent='radical') return new_rel
def remake_arguments(self): ''' Yield the argument values or (name, value) pairs that could be used to recreate the Operation. ''' if (self.get_style('exponent', 'raised') == 'radical' and self.exponent == frac(one, two)): yield self.base else: yield self.base yield self.exponent
def style_options(self): ''' Returns the StyleOptions object for this Exp. ''' options = StyleOptions(self) default_exp_style = ('radical' if self.exponent == frac(one, two) else 'raised') options.add_option(name='exponent', description=("'raised': exponent as a superscript; " "'radical': using a radical sign"), default=default_exp_style, related_methods=('with_radical', 'without_radical')) return options
def formatted(self, format_type, **kwargs): # begin building the inner_str inner_str = self.base.formatted( format_type, fence=True, force_fence=True) # if self.get_style('exponent', 'TEST') == 'TEST' and self.exponent == frac(one, two): # self.with_radical() if self.get_style('exponent', 'raised') == 'raised': inner_str = ( inner_str + r'^{' + self.exponent.formatted(format_type, fence=False) + '}') elif self.get_style('exponent') == 'radical': if self.exponent == frac(one, two): if format_type == 'string': inner_str = ( r'sqrt(' + self.base.formatted(format_type, fence=True, force_fence=True) + ')') elif format_type == 'latex': inner_str = ( r'\sqrt{' + self.base.formatted(format_type, fence=True, force_fence=True) + '}') elif isinstance(self.exponent, Div): if format_type == 'string': inner_str = ( self.exponent.denominator.formatted(format_type, fence=False) + r' radical(' + self.base.formatted(format_type, fence=True, force_fence=True) + ')') elif format_type == 'latex': inner_str = ( r'\sqrt[\leftroot{-3}\uproot{3}' + self.exponent.denominator.formatted(format_type, fence=False) + ']{' + self.base.formatted(format_type, fence=True, force_fence=True) + '}') else: raise ValueError( "Unknown radical type, exponentiating to the power " "of %s" % str( self.exponent)) # only fence if force_fence=True (nested exponents is an # example of when fencing must be forced) kwargs['fence'] = ( kwargs['force_fence'] if 'force_fence' in kwargs else False) return maybe_fenced_string(inner_str, **kwargs)
WIRE_DN = Literal(pkg, 'WIRE_DN') # wire goes down to link with another wire # link destination for WIRE_UP or WIRE_DN WIRE_LINK = Literal(pkg, 'WIRE_LINK') QubitSpace = Exp(Complex, two) def QubitRegisterSpace(n): return TensorExp(Exp(Complex, two), n) def RegisterSU(n): return SU(Exp(two, n)) inv_root2 = frac(one, sqrt(two)) B1 = Variable('B1') B2 = Variable('B2') B3 = Variable('B3') C1 = Variable('C1') C2 = Variable('C2') C3 = Variable('C3') I = Variable('I') IB = Variable('IB') IC = Variable('IC') # some Variable labels Ablock = Block(A) Bblock = Block(B) B1block = Block(B1)
def remake_constructor(self): if (self.get_style('exponent', 'raised') == 'radical' and self.exponent == frac(one, two)): return 'sqrt' return Function.remake_constructor(self)
diff_in_exp = Forall([a, b, c], Equals(Exp(a, Sub(b, c)), Mult(Exp(a, b), Exp(a, Neg(c)))), domain=Complex, conditions=[NotEquals(a, zero)]) diff_in_exp diff_in_exp_rev = Forall([a, b, c], Equals(Mult(Exp(a, b), Exp(a, Neg(c))), Exp(a, Sub(b, c))), domain=Complex, conditions=[NotEquals(a, zero)]) diff_in_exp_rev diff_frac_in_exp = Forall([a, b, c, d], Equals(Exp(a, Sub(b, frac(c, d))), Mult(Exp(a, b), Exp(a, frac(Neg(c), d)))), domain=Complex, conditions=[NotEquals(a, zero), NotEquals(d, zero)]) diff_frac_in_exp diff_frac_in_exp_rev = Forall( [a, b, c, d], Equals(Mult(Exp(a, b), Exp(a, frac(Neg(c), d))), Exp(a, Sub(b, frac(c, d)))), domain=Complex, conditions=[NotEquals(a, zero), NotEquals(d, zero)]) diff_frac_in_exp_rev # works because log[a^c b^c] = c log a + c log b
# transferred by wdc 3/11/2020 triangle_inequality = Forall([a, b], LessThanEquals( Abs(Add(a, b)), Add(Abs(a), Abs(b))), domain=Complex) triangle_inequality # transferred by wdc 3/11/2020 abs_prod = Forall(x_etc, Equals(Abs(Mult(x_etc)), Mult(Etcetera(Abs(x_multi)))), domain=Complex) abs_prod # transferred by wdc 3/11/2020 abs_frac = Forall([a, b], Equals(Abs(frac(a, b)), frac(Abs(a), Abs(b))), domain=Complex) abs_frac # transferred by wdc 3/11/2020 mod_abs_scaled = Forall( (a, b, c), Equals( Mult( a, ModAbs( b, c)), ModAbs( Mult( a, b), Mult( a, c))), domain=Real) mod_abs_scaled # transferred by wdc 3/11/2020
begin_theorems(locals()) divide_real_closure = Forall([a, b], InSet(Divide(a, b), Real), domain=Real, conditions=[NotEquals(b, zero)]) divide_real_closure divide_real_pos_closure = Forall([a, b], InSet(Divide(a, b), RealPos), domain=RealPos, conditions=[NotEquals(b, zero)]) divide_real_pos_closure fraction_real_closure = Forall([a, b], InSet(frac(a, b), Real), domain=Real, conditions=[NotEquals(b, zero)]) fraction_real_closure fraction_pos_closure = Forall([a, b], InSet(frac(a, b), RealPos), domain=RealPos, conditions=[NotEquals(b, zero)]) fraction_pos_closure divide_complex_closure = Forall([a, b], InSet(Divide(a, b), Complex), domain=Complex, conditions=[NotEquals(b, zero)]) divide_complex_closure
def sqrt(base): ''' Special function for square root version of an exponential. ''' return Exp(base, frac(one, two))
def deduce_in_number_set(self, number_set, assumptions=USE_DEFAULTS): ''' Given a number set number_set, attempt to prove that the given expression is in that number set using the appropriate closure theorem. This method uses instantiated thms for the sqrt() cases. Created: 2/20/2020 by wdc, based on the same method in the Add class. Last modified: 2/28/2020 by wdc. Added instantiation for sqrt() cases created using the sqrt() fxn. Last Modified: 2/20/2020 by wdc. Creation. Once established, these authorship notations can be deleted. ''' from proveit.logic import InSet from proveit.numbers.exponentiation import ( exp_complex_closure, exp_nat_closure, exp_real_closure, exp_real_closure_exp_non_zero, exp_real_closure_base_pos, exp_real_pos_closure, sqrt_complex_closure, sqrt_real_closure, sqrt_real_pos_closure) from proveit.numbers import (Complex, NaturalPos, RationalPos, Real, RealPos) if number_set == NaturalPos: return exp_nat_closure.instantiate({ a: self.base, b: self.exponent }, assumptions=assumptions) if number_set == RationalPos: # if we have a^b with a Rational and b Integer # if b is proven to be any Integer # if we already know a^b is # if b = 0, then a^b = 1 (if a≠0) # to be continued later pass # the following would be useful to replace the next two Real # closure theorems, once we get the system to deal # effectively with the Or(A, And(B, C)) conditions # if number_set == Real: # return exp_real_closure.instantiate( # {a:self.base, b:self.exponent}, # assumptions=assumptions) if number_set == Real: # Would prefer the more general approach commented-out # above; in the meantime, allowing for 2 possibilities here: # if base is positive real, exp can be any real; # if base is real ≥ 0, exp must be non-zero if self.exponent == frac(one, two): return sqrt_real_closure.instantiate({a: self.base}, assumptions=assumptions) else: try: return exp_real_closure_base_pos.instantiate( { a: self.base, b: self.exponent }, assumptions=assumptions) except ProofFailure: return exp_real_closure_exp_non_zero.instantiate( { a: self.base, b: self.exponent }, assumptions=assumptions) try: return exp_real_closure_exp_non_zero.instantiate( { a: self.base, b: self.exponent }, assumptions=assumptions) except ProofFailure: msg = ('Positive base condition failed ' 'and non-zero exponent condition failed. ' 'Need base ≥ 0 and exponent ≠ 0, OR base > 0 ' 'to prove %s is real.' % self) raise ProofFailure(InSet(self, number_set), assumptions, msg) if number_set == RealPos: if self.exponent == frac(one, two): return sqrt_real_pos_closure.instantiate( {a: self.base}, assumptions=assumptions) else: return exp_real_pos_closure.instantiate( { a: self.base, b: self.exponent }, assumptions=assumptions) if number_set == Complex: if self.exponent == frac(one, two): return sqrt_complex_closure.instantiate( {a: self.base}, assumptions=assumptions) else: return exp_complex_closure.instantiate( { a: self.base, b: self.exponent }, assumptions=assumptions) msg = ("'Exp.deduce_in_number_set' not implemented for the %s set" % str(number_set)) raise ProofFailure(InSet(self, number_set), assumptions, msg)
def exp_neg_2pi_i_on_two_pow_t(*exp_factors): from proveit.physics.quantum.QPE import _two_pow_t return exp(Neg(frac(Mult(*((two, pi, i) + exp_factors)), _two_pow_t)))
def deduce_in_number_set(self, number_set, **defaults_config): ''' Attempt to prove that this exponentiation expression is in the given number set. ''' from proveit.logic import InSet, NotEquals from proveit.numbers.exponentiation import ( exp_complex_closure, exp_natpos_closure, exp_int_closure, exp_rational_closure_nat_power, exp_rational_nonzero_closure, exp_rational_pos_closure, exp_real_closure_nat_power, exp_real_pos_closure, exp_real_non_neg_closure, exp_complex_closure, exp_complex_nonzero_closure, sqrt_complex_closure, sqrt_real_closure, sqrt_real_pos_closure, sqrt_real_non_neg_closure, sqrd_pos_closure, sqrd_non_neg_closure) from proveit.numbers import zero deduce_number_set(self.exponent) if number_set == NaturalPos: return exp_natpos_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == Natural: # Use the NaturalPos closure which applies for # any Natural base and exponent. self.deduce_in_number_set(NaturalPos) return InSet(self, Natural).prove() elif number_set == Integer: return exp_int_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == Rational: power_is_nat = InSet(self.exponent, Natural) if not power_is_nat.proven(): # Use the RationalNonZero closure which works # for negative exponents as well. self.deduce_in_number_set(RationalNonZero) return InSet(self, Rational).prove() return exp_rational_closure_nat_power.instantiate( {a: self.base, b: self.exponent}) elif number_set == RationalNonZero: return exp_rational_nonzero_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == RationalPos: return exp_rational_pos_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == Real: if self.exponent == frac(one, two): return sqrt_real_closure.instantiate( {a: self.base, b: self.exponent}) else: power_is_nat = InSet(self.exponent, Natural) if not power_is_nat.proven(): # Use the RealPos closure which allows # any real exponent but requires a # non-negative base. self.deduce_in_number_set(RealPos) return InSet(self, Real).prove() return exp_real_closure_nat_power.instantiate( {a: self.base, b: self.exponent}) elif number_set == RealPos: if self.exponent == frac(one, two): return sqrt_real_pos_closure.instantiate({a: self.base}) elif self.exponent == two: return sqrd_pos_closure.instantiate({a: self.base}) else: return exp_real_pos_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == RealNonNeg: if self.exponent == frac(one, two): return sqrt_real_non_neg_closure.instantiate({a: self.base}) elif self.exponent == two: return sqrd_non_neg_closure.instantiate({a: self.base}) else: return exp_real_non_neg_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == Complex: if self.exponent == frac(one, two): return sqrt_complex_closure.instantiate( {a: self.base}) else: return exp_complex_closure.instantiate( {a: self.base, b: self.exponent}) elif number_set == ComplexNonZero: return exp_complex_nonzero_closure.instantiate( {a: self.base, b: self.exponent}) raise NotImplementedError( "'Exp.deduce_in_number_set' not implemented for the %s set" % str(number_set))