generate random 3sats
This commit is contained in:
parent
31b9b3640c
commit
5129fd468a
9
3sat.py
9
3sat.py
@ -1,6 +1,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
from grover import classical_func_search
|
from grover import classical_func_search
|
||||||
|
|
||||||
@ -31,7 +32,7 @@ class SAT(object):
|
|||||||
def and_(self, and_clauses):
|
def and_(self, and_clauses):
|
||||||
def inner_and_(params):
|
def inner_and_(params):
|
||||||
for and_clause in and_clauses:
|
for and_clause in and_clauses:
|
||||||
if and_clause(params):
|
if not and_clause(params):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -63,7 +64,7 @@ class SAT(object):
|
|||||||
self.inner = inner_generate
|
self.inner = inner_generate
|
||||||
|
|
||||||
def __call__(self, args):
|
def __call__(self, args):
|
||||||
self.inner(args)
|
return self.inner(args)
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
for j, _or_clause in enumerate(self.and_clauses):
|
for j, _or_clause in enumerate(self.and_clauses):
|
||||||
@ -91,10 +92,10 @@ def classical_3sat(func):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
gen_3sat = SAT(predicates=6)
|
gen_3sat = SAT(predicates=4)
|
||||||
gen_3sat.generate(ands=1)
|
gen_3sat.generate(ands=1)
|
||||||
gen_3sat.show()
|
gen_3sat.show()
|
||||||
print(classical_3sat(gen_3sat))
|
pprint(classical_3sat(gen_3sat))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
# http://twistedoakstudios.com/blog/Post2644_grovers-quantum-search-algorithm
|
# http://twistedoakstudios.com/blog/Post2644_grovers-quantum-search-algorithm
|
||||||
import inspect
|
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
@ -18,9 +17,10 @@ def classical_func_search(func, input_range):
|
|||||||
return rv
|
return rv
|
||||||
|
|
||||||
|
|
||||||
def _3sat(x1, x2, x3):
|
def _3sat(params):
|
||||||
# https://cstheory.stackexchange.com/questions/38538/oracle-construction-for-grovers-algorithm
|
# https://cstheory.stackexchange.com/questions/38538/oracle-construction-for-grovers-algorithm
|
||||||
# 3-SAT from here: https://qiskit.org/textbook/ch-applications/satisfiability-grover.html
|
# 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 \
|
return (not x1 or not x2 or not x3) and \
|
||||||
(x1 or not x2 or x3) and \
|
(x1 or not x2 or x3) and \
|
||||||
(x1 or x2 or not x3) and \
|
(x1 or x2 or not x3) and \
|
||||||
@ -30,9 +30,7 @@ def _3sat(x1, x2, x3):
|
|||||||
|
|
||||||
def classical_3sat(func):
|
def classical_3sat(func):
|
||||||
# Generate all possible true/false tupples for the 3-sat problem
|
# Generate all possible true/false tupples for the 3-sat problem
|
||||||
sig = inspect.signature(func)
|
input_range = list(itertools.product([True, False], repeat=3))
|
||||||
params = sig.parameters
|
|
||||||
input_range = list(itertools.product([True, False], repeat=len(params)))
|
|
||||||
random.shuffle(input_range)
|
random.shuffle(input_range)
|
||||||
return classical_func_search(func, input_range)
|
return classical_func_search(func, input_range)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user