def apply_to_backend_request(self, block: Block, level: Tuple[Factor, Union[SimpleLevel, DerivedLevel]], backend_request: BackendRequest ) -> None: sublists = self._build_variable_sublists(block, level, self.k) implications = [] # Handle the regular cases (1 => 2 ^ ... ^ n ^ ~n+1) trim = len(sublists) if self.k > 1 else len(sublists) - 1 for idx, l in enumerate(sublists[:trim]): if idx > 0: p_list = [Not(sublists[idx-1][0]), l[0]] p = And(p_list) if len(p_list) > 1 else p_list[0] else: p = l[0] if idx < len(sublists) - 1: q_list = cast(List[Any], l[1:]) + [Not(sublists[idx+1][-1])] q = And(q_list) if len(q_list) > 1 else q_list[0] else: q = And(l[1:]) if len(l[1:]) > 1 else l[self.k - 1] implications.append(If(p, q)) # Handle the tail if len(sublists[-1]) > 1: tail = sublists[-1] tail.reverse() for idx in range(len(tail) - 1): implications.append(If(l[idx], l[idx + 1])) (cnf, new_fresh) = block.cnf_fn(And(implications), backend_request.fresh) backend_request.cnfs.append(cnf) backend_request.fresh = new_fresh
def __apply_derivation(self, block: Block, backend_request: BackendRequest) -> None: trial_size = block.variables_per_trial() cross_size = block.trials_per_sample() iffs = [] for n in range(cross_size): or_clause = Or(list(And(list(map(lambda x: x + (n * trial_size) + 1, l))) for l in self.dependent_idxs)) iffs.append(Iff(self.derived_idx + (n * trial_size) + 1, or_clause)) (cnf, new_fresh) = block.cnf_fn(And(iffs), backend_request.fresh) backend_request.cnfs.append(cnf) backend_request.fresh = new_fresh
def apply_to_backend_request(self, block: Block, level: Tuple[Factor, Union[SimpleLevel, DerivedLevel]], backend_request: BackendRequest) -> None: # Request sublists for k+1 to allow us to determine the transition sublists = self._build_variable_sublists(block, level, self.k + 1) implications = [] if sublists: # Starting corner case implications.append(If(sublists[0][0], And(sublists[0][1:-1]))) for sublist in sublists: implications.append(If(And([Not(sublist[0]), sublist[1]]), And(sublist[2:]))) # Ending corner case implications.append(If(sublists[-1][-1], And(sublists[-1][1:-1]))) (cnf, new_fresh) = block.cnf_fn(And(implications), backend_request.fresh) backend_request.cnfs.append(cnf) backend_request.fresh = new_fresh
def __apply_derivation_with_complex_window(self, block: Block, backend_request: BackendRequest) -> None: trial_size = block.variables_per_trial() trial_count = block.trials_per_sample() iffs = [] f = self.factor window = f.levels[0].window t = 0 for n in range(trial_count): if not f.applies_to_trial(n + 1): continue num_levels = len(f.levels) get_trial_size = lambda x: trial_size if x < block.grid_variables() else len(block.decode_variable(x+1)[0].levels) or_clause = Or(list(And(list(map(lambda x: x + (t * window.stride * get_trial_size(x) + 1), l))) for l in self.dependent_idxs)) iffs.append(Iff(self.derived_idx + (t * num_levels) + 1, or_clause)) t += 1 (cnf, new_fresh) = block.cnf_fn(And(iffs), backend_request.fresh) backend_request.cnfs.append(cnf) backend_request.fresh = new_fresh