From 5129fd468a0b3a814882d3ab0af61c86adfdf8e4 Mon Sep 17 00:00:00 2001 From: Daniel Tsvetkov Date: Fri, 21 Feb 2020 15:49:03 +0100 Subject: [PATCH] generate random 3sats --- 3sat.py | 9 +++++---- grover.py | 8 +++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/3sat.py b/3sat.py index 2d03d1b..8f49a14 100644 --- a/3sat.py +++ b/3sat.py @@ -1,6 +1,7 @@ import inspect import itertools import random +from pprint import pprint from grover import classical_func_search @@ -31,7 +32,7 @@ class SAT(object): def and_(self, and_clauses): def inner_and_(params): for and_clause in and_clauses: - if and_clause(params): + if not and_clause(params): return False return True @@ -63,7 +64,7 @@ class SAT(object): self.inner = inner_generate def __call__(self, args): - self.inner(args) + return self.inner(args) def show(self): for j, _or_clause in enumerate(self.and_clauses): @@ -91,10 +92,10 @@ def classical_3sat(func): def main(): - gen_3sat = SAT(predicates=6) + gen_3sat = SAT(predicates=4) gen_3sat.generate(ands=1) gen_3sat.show() - print(classical_3sat(gen_3sat)) + pprint(classical_3sat(gen_3sat)) if __name__ == "__main__": diff --git a/grover.py b/grover.py index b8c7ed4..01401ae 100644 --- a/grover.py +++ b/grover.py @@ -1,5 +1,4 @@ # http://twistedoakstudios.com/blog/Post2644_grovers-quantum-search-algorithm -import inspect import itertools import random from pprint import pprint @@ -18,9 +17,10 @@ def classical_func_search(func, input_range): return rv -def _3sat(x1, x2, x3): +def _3sat(params): # https://cstheory.stackexchange.com/questions/38538/oracle-construction-for-grovers-algorithm # 3-SAT from here: https://qiskit.org/textbook/ch-applications/satisfiability-grover.html + x1, x2, x3 = params return (not x1 or not x2 or not x3) and \ (x1 or not x2 or x3) and \ (x1 or x2 or not x3) and \ @@ -30,9 +30,7 @@ def _3sat(x1, x2, x3): def classical_3sat(func): # Generate all possible true/false tupples for the 3-sat problem - sig = inspect.signature(func) - params = sig.parameters - input_range = list(itertools.product([True, False], repeat=len(params))) + input_range = list(itertools.product([True, False], repeat=3)) random.shuffle(input_range) return classical_func_search(func, input_range)