generate random 3sats

This commit is contained in:
Daniel Tsvetkov 2020-02-21 15:49:03 +01:00
parent 31b9b3640c
commit 5129fd468a
2 changed files with 8 additions and 9 deletions

View File

@ -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__":

View File

@ -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)