def p_command_multiplecommands(p): ''' command : command ';' command ''' command_name = "sequence" if (p[3].name == command_name): p[0] = AST(command_name, p[1], *p[3]) else: p[0] = AST(command_name, p[1], p[3])
def __init__(self, *, allowed_functions, allowed_terminals, **kwargs): try: self.max_depth = kwargs['max_depth'] if 'max_depth' in kwargs.keys( ) else kwargs['length_gen'] except KeyError: raise KeyError( "You must provide a max depth parameter for the Individual") super().__init__(self.max_depth) self.allowed_functions = allowed_functions self.allowed_terminals = allowed_terminals self.tree_gen = AST(allowed_functions=allowed_functions, allowed_terminals=allowed_terminals) self.tree = self.tree_gen(max_depth=self.max_depth)
from arboles import * from abstract_syntax_tree import AST import unittest import random random.seed(41) allowed_functions = [AddNode, SubNode] allowed_terminals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ast_generator = AST(allowed_functions=allowed_functions, allowed_terminals=allowed_terminals) sample_tree_1 = ast_generator(max_depth=3) # this has repeated terminals sample_tree_2 = ast_generator(max_depth=2) # this doesn't have repeated terminals class TestIsPureMethod(unittest.TestCase): def test_false_pure(self): self.assertFalse(sample_tree_1.is_pure(feed_dict={'values': []})) def test_true_pure(self): self.assertTrue(sample_tree_2.is_pure(feed_dict={'values': []})) class TestGetDepth(unittest.TestCase): def test_length(self): self.assertEqual(sample_tree_1.get_depth(), 3) self.assertEqual(sample_tree_2.get_depth(), 2)
def p_command_if(p): ''' command : IF QUBITEXPRESSION THEN command ''' p[0] = AST(p[1], p[2], p[4], AST("skip"))
def p_command_ifthenelse(p): ''' command : IF QUBITEXPRESSION THEN command ELSE command ''' p[0] = AST(p[1], p[2], p[4], p[6])
def p_command_gate(p): ''' command : IDENTIFIER '(' args ')' ''' p[0] = AST(p[1], *p[3])
def p_command_skip(p): ''' command : SKIP ''' p[0] = AST(p[1])