Exemplo n.º 1
0
 def visit_pow(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     if (expr.expr[0].type == T_NUMBER and expr.expr[0].str == "e"):
         comp = expr.expr[1].value
         expr.value = e**comp
     else:
         expr.value = expr.expr[0].value**expr.expr[1].value
Exemplo n.º 2
0
 def visit_tensor(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     if (expr[1].type == T_NUMBER):
         expr.value = np.kron(expr[0].value, expr[0].value)
         for _ in range(int(expr[1].value) - 2):
             expr.value = np.kron(expr.value, expr[0].value)
     else:
         expr.value = np.kron(expr[0].value, expr[1].value)
Exemplo n.º 3
0
    def visit_mult(self, expr):
        for exp in expr.expr:
            exp.visit(self)
        if (expr[0].type == T_MEASUREMENT and expr[1].type == T_KET):
            try:
                expr.value = 1 / sqrt(expr[1].value.conj().T.dot(
                    expr[0].value.conj().T).dot(expr[0].value).dot(
                        expr[1].value)[0][0]) * expr[0].value.dot(
                            expr[1].value)
            except ZeroDivisionError:
                expr.value = None
        elif (expr[0].type == T_MATRIX and expr[1].type == T_KET):
            (_, mt) = expr[0].value.shape
            (k, _) = expr[1].value.shape
            while (mt > k):
                expr[1].value = np.kron(expr[1].value, q[0].T)
                (k, _) = expr[1].value.shape
            while (mt < k):
                expr[0].value = np.kron(expr[0].value, np.identity(2))
                (_, mt) = expr[0].value.shape
            expr.value = expr[0].value.dot(expr.expr[1].value)
        elif (expr[0].type == T_MATRIX and expr[1].type == T_MATRIX):
            (_, m1) = expr[0].value.shape
            (m2, _) = expr[1].value.shape
            while (m1 > m2):
                expr[1].value = np.kron(expr[1].value, np.identity(2))
                (m2, _) = expr[1].value.shape
            while (m1 < m2):
                expr[0].value = np.kron(expr[0].value, np.identity(2))
                (_, m1) = expr[0].value.shape
            expr.value = expr[0].value * expr[1].value
        else:
            expr.value = expr.expr[0].value * expr.expr[1].value

        if (isinstance(expr.value, np.ndarray) and expr.value.shape == (1, 1)):
            expr.value = expr.value.A1[0]
Exemplo n.º 4
0
 def visit_div(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     expr.value = expr.expr[0].value
     for exp in expr.expr[1:]:
         expr.value = expr.value / exp.value
Exemplo n.º 5
0
 def visit_dot(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     expr.value = expr.expr[0].value
     for exp in expr.expr[1:]:
         expr.value = expr.value.dot(exp.value)
Exemplo n.º 6
0
 def visit_sub(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     expr.value = expr.expr[0].value
     for exp in expr.expr[1:]:
         expr.value = expr.value - exp.value
Exemplo n.º 7
0
 def visit_add(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     expr.value = expr.expr[0].value
     for exp in expr.expr[1:]:
         expr.value = expr.value + exp.value
Exemplo n.º 8
0
 def visit_inner(self, expr):
     for exp in expr.expr:
         exp.visit(self)
     expr.value = expr.expr[0].value.dot(expr.expr[1].value)[0][0]