Ejemplo n.º 1
0
def defaultEnf(env):
    env['sum'] = lambda *args: sum(args)
    env['sqrt'] = lambda x: math.sqrt(x)
    env['print'] = lambda *x: print(*x)
    env['pprint'] = lambda *x: prettyPrint(*x)
    env['car'] = lambda x: myCar(x)
    env['cdr'] = lambda x: myCdr(x)
    env['map'] = lambda x, y: list(map(x, y))
    env['fold'] = lambda x, y: functools.reduce(x, y)
    env['tensor'] = lambda x, y: np.kron(x, y).tolist()
    env['apply'] = lambda x, y: np.dot(x, y).tolist()
    env['outer'] = lambda x, y: np.outer(x, y)
    env['measure'] = lambda x: quantumLib.measure(x)
    env['subsystems'] = lambda state, configuration: quantumLib.splitToSub(
        state, configuration)
    env['eq'] = lambda x, y: x == y
    env['append'] = lambda x, y: [x] + y
    env['prepend'] = lambda x, y: y + [x]
    env['⊗'] = lambda x, y: np.kron(x, y)
    env['·'] = lambda x, y: np.dot(x, y)
    env["-"] = lambda x, y: np.subtract(
        x, y
    )  #numpy add/sub/mult/div works like regular for matrix AND numbers! no need for addMatrix etc
    env["+"] = lambda x, y: np.add(x, y)
    env["*"] = lambda x, y: np.multiply(x, y)
    env["/"] = lambda x, y: np.divide(x, y)
    env["="] = lambda x, y: x == y
    env["<"] = lambda x, y: x < y
    env["len"] = lambda x: len(x)
    env["null?"] = lambda x: len(x) == 0
    env["sqrt"] = lambda x: np.sqrt(x)
    env["reverse"] = lambda x: list(reversed(x))
    env["fold"] = lambda x, y: functools.reduce(x, y)
    env["pi"] = math.pi
    env["exp"] = np.exp  #should add cleanExp like quantum parethesis
    env["oracle"] = lambda fun: oracleLib.generateOracle(fun)
    env["expm"] = lambda matrix: list(scipyAlg.expm(matrix))
    env["logm"] = lambda matrix: list(scipyAlg.logm(matrix))
    env["logTwo"] = lambda x: int(math.log(x, 2))
    env["length"] = lambda x: len(x)
    env["tensorOp"] = lambda x, y: np.kron(x, y)
    env["applyN"] = lambda x, y, z: repeatedApp(x, y, z)
    env["toInt"] = lambda x: int(x)
    env["transpose"] = lambda x: (quantumLib.ctransp(x)).tolist()
Ejemplo n.º 2
0
def defaultEnf(env):
    env["sum"] = lambda *args: sum(args)
    env["sqrt"] = lambda x: math.sqrt(x)
    env["print"] = lambda *x: print(*x)
    env["pprint"] = lambda *x: prettyPrint(*x)
    env["car"] = lambda x: myCar(x)
    env["cdr"] = lambda x: myCdr(x)
    env["map"] = lambda x, y: list(map(x, y))
    env["fold"] = lambda x, y: functools.reduce(x, y)
    env["tensor"] = lambda x, y: np.kron(x, y)
    env["apply"] = lambda x, y: np.dot(x, y)
    env["outer"] = lambda x, y: np.outer(x, y)
    env["measure"] = lambda x: quantumLib.measure(x)
    env["subsystems"] = lambda state, configuration: quantumLib.splitToSub(state, configuration)
    env["eq"] = lambda x, y: x == y
    env["append"] = lambda x, y: [x] + y
    env["prepend"] = lambda x, y: y + [x]
    env["⊗"] = lambda x, y: np.kron(x, y)
    env["·"] = lambda x, y: np.dot(x, y)
    env["-"] = lambda x, y: np.subtract(
        x, y
    )  # numpy add/sub/mult/div works like regular for matrix AND numbers! no need for addMatrix etc
    env["+"] = lambda x, y: np.add(x, y)
    env["*"] = lambda x, y: np.multiply(x, y)
    env["/"] = lambda x, y: np.divide(x, y)
    env["="] = lambda x, y: x == y
    env["len"] = lambda x: len(x)
    env["null?"] = lambda x: len(x) == 0
    env["sqrt"] = lambda x: np.sqrt(x)
    env["reverse"] = lambda x: list(reversed(x))
    env["fold"] = lambda x, y: functools.reduce(x, y)
    env["pi"] = math.pi
    env["exp"] = np.exp  # should add cleanExp like quantum parethesis
    env["oracle"] = lambda fun: oracleLib.generateOracle(fun)
    env["expm"] = lambda matrix: list(scipyAlg.expm(matrix))
    env["logm"] = lambda matrix: list(scipyAlg.logm(matrix))
    env["logTwo"] = lambda x: int(math.log(x, 2))
    env["length"] = lambda x: len(x)
    env["transpose"] = lambda x: (quantumLib.ctransp(x)).tolist()
Ejemplo n.º 3
0
def add_globals(self):
    "Add some Scheme standard procedures."
    import math, cmath, operator as op
    self.update(vars(cmath))
    self.update(vars(math))
    self.update({
     'expC': lambda x: cmath.exp(x),
     'cleanExp': lambda x: autoOp.clean_complex(cmath.exp(x)),
     '+':op.add,
     '-':op.sub,
     '*':op.mul,
     '/':op.truediv,
     'not':op.not_,
     'or': lambda x,y: x or y,
     '>':op.gt,
     '<':op.lt,
     '>=':op.ge,
     '<=':op.le,
     '=':op.eq, 
     'equal?':op.eq,
     'eq?':op.is_,
     'length':len,
     'cons':cons,
     'car':lambda x:x[0],
     'cdr':lambda x:x[1:],
     'append':op.add,
     'len' : lambda x: len(x),
     'list':lambda *x:list(x),
     'list?': lambda x:isa(x,list),
     'null?':lambda x:x==[],
     'symbol?':lambda x: isa(x, Symbol),
     'boolean?':lambda x: isa(x, bool),
     'pair?':is_pair, 
     'port?': lambda x:isa(x,file),
     'applyE':lambda proc,l: proc(*l), 
     'eval':lambda x: eval(expand(x)),
     'load':lambda fn: load(fn),
     'call/cc':callcc,
     'open-input-file':open,
     'close-input-port':lambda p: p.file.close(), 
     'open-output-file':lambda f:open(f,'w'),
     'close-output-port':lambda p: p.close(),
     'eof-object?':lambda x:x is eof_object,
     'read-char':readchar,
     'apply':   lambda x,y: quantumLib.np.dot(x,y),
     'outer' : lambda x,y : quantumLib.np.outer(x,y),
     'tensor':  lambda *x: quantumLib.stateComp(x).tolist(),
     'measure': lambda x: quantumLib.measure(x),
     'map' : lambda x,y : list(map(x,y)),
     'fold' : lambda x,y : functools.reduce(x,y),
     'foldI' : lambda x,y,z: functools.reduce(x,y,z),
     'ones' : lambda x : quantumLib.np.ones(x).tolist(),
     'scalarM' : lambda x,y : quantumLib.np.multiply(x,y).tolist(),
     'addM' : lambda x,y : quantumLib.np.add(x,y).tolist(),
     'toInt' : lambda x: int(x),
     'read':read, 'write':lambda x,port=sys.stdout:(port.write(to_string(x)), None)[1],
     'transpose' : lambda x: (quantumLib.ctransp(x)).tolist(),
     'hermitian?' : lambda x :quantumLib.checkH(x),
     'unitary?' : lambda x : quantumLib.checkU(x),
     'oracle' : lambda fun: oracleLib.generateOracle(fun),
     'subsystems': lambda state,configuration: quantumLib.splitToSub(state,configuration),
     'cart':  lambda *x: list(itertools.product(*x)),
     'display':lambda x,port=sys.stdout:(port.write(x if isa(x,str) else to_string(x)), None)[1]})
    return self