3sat and grover fixes

This commit is contained in:
Daniel Tsvetkov 2020-03-09 09:42:55 +01:00
parent fcca3afacc
commit 4e69938d7b
2 changed files with 19 additions and 4 deletions

View File

@ -3,7 +3,7 @@ import itertools
import random
from pprint import pprint
from grover import classical_func_search
from grover import classical_func_search_multi_rv
class X(object):
@ -88,7 +88,7 @@ def classical_3sat(func):
# Generate all possible true/false tupples for the 3-sat problem
input_range = list(itertools.product([True, False], repeat=func.predicates))
random.shuffle(input_range)
return classical_func_search(func, input_range)
return classical_func_search_multi_rv(func, input_range)
def main():

View File

@ -4,7 +4,7 @@ import random
from pprint import pprint
def classical_func_search(func, input_range):
def classical_func_search_multi_rv(func, input_range):
"""Grovers algorithm takes a function, searches through
the implicit list of possible inputs to that function, and
returns inputs that causes the function to return true.
@ -17,6 +17,21 @@ def classical_func_search(func, input_range):
return rv
def classical_func_search_single_rv(func, input_range):
"""Grovers algorithm takes a function, searches through
the implicit list of possible inputs to that function, and
returns EXACTLY ONE input that causes the function to return true.
"""
rv = None
for i, params in enumerate(input_range):
result = func(params)
if result and rv is None:
rv = result
else:
raise Exception("Exactly one result is needed")
return rv
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
@ -32,7 +47,7 @@ def classical_3sat(func):
# Generate all possible true/false tupples for the 3-sat problem
input_range = list(itertools.product([True, False], repeat=3))
random.shuffle(input_range)
return classical_func_search(func, input_range)
return classical_func_search_multi_rv(func, input_range)
def main():