Exemple #1
0
def _check_output(start_diagram, end_diagram, variables):
    present = set([str(v) for v in VariableFinder(start_diagram).walk()])
    remaining = set([str(v) for v in VariableFinder(end_diagram).walk()])
    variables = [str(v) for v in variables]
    if not remaining.issubset(present):
        raise RuntimeError("New variables ({}) have been introduced in the diagram (which contained {}) after summation"
                           .format(list(remaining - present), present))
    if not present.issubset(remaining | set(variables)):
        raise RuntimeError("Some variables ({}) have been erroneously removed: they are not present in the diagram "
                           "after summation ({}) nor in the variables to remove ({})"
                           .format(list(present - remaining - set(variables)), list(remaining), variables))
    if len(remaining & set(variables)) != 0:
        raise RuntimeError("Some variables ({}) that should be summed out ({}) still appear in the diagram after "
                           "summation. The diagram started out with variables {} and now contains {}"
                           .format(list(remaining & set(variables)), variables, list(present), list(remaining)),
                           end_diagram)
Exemple #2
0
def norm(variables, diagram, l_norm=None):
    from pyxadd.matrix_vector import sum_out
    if l_norm is None:
        l_norm = 2
    elif l_norm != 2 and l_norm != 1:
        raise RuntimeError(
            "Unsupported norm:{}, should be 1 or 2 [default: None => 2]".
            format(norm))

    if l_norm == 2:
        node_id = SquareWalker(diagram, diagram.profile).walk()
    else:
        node_id = AbsoluteValueWalker(diagram, diagram.profile).walk()
    normed = sum_out(diagram.pool, node_id, variables)
    diagram = Diagram(diagram.pool, normed)
    try:
        value = diagram.evaluate({})
        if value < 0:
            raise RuntimeError(
                "The squared sum is negative ({})".format(value))
        if l_norm == 2:
            return sympy.sqrt(value)
        else:
            return value
    except RuntimeError as e:
        found = VariableFinder(diagram).walk()
        if len(found) > 0:
            raise RuntimeError(
                "Variables {f} were not eliminated (given variables were {v})".
                format(f=list(found), v=variables))
        else:
            raise e
    def _test_find_variables_symbolic_xor(self):
        from tests import test_matrix_vector as mv
        n = 200
        diagram = mv.build_symbolic_xor(n)
        expected_variables = {"x", "c"
                              } | {"c{}".format(i)
                                   for i in range(1, n + 1)}

        stop_watch = timer.Timer()
        stop_watch.start("Bottom up")
        self.assertEquals(expected_variables, VariableFinder(diagram).walk())
        stop_watch.start("Top Down")
        self.assertEquals(expected_variables,
                          TopDownVariableFinder(diagram).walk())
        stop_watch.stop()
Exemple #4
0
 def _get_variables(self, node_id):
     return VariableFinder(Diagram(self.pool, node_id)).walk()
 def test_find_variables(self):
     for variables, diagram in self.diagrams:
         self.assertEqual(variables, VariableFinder(diagram).walk())