-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e966375
commit d50d84c
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'''Count the number of stable models of an ASP program using the naive Quantum Counting algorithm. | ||
Example 4.1.2 in the thesis shows how to use the Quantum Counting algorithm to count the stable \ | ||
models of a given ASP program. This example implements that technique. | ||
''' | ||
|
||
import math | ||
from src import qasp | ||
from src.examples.util import tab, pause | ||
|
||
# ASP program | ||
PRGM = ''' | ||
p :- not q. | ||
q :- not p. | ||
r :- p. | ||
r :- q. | ||
''' | ||
|
||
# Hardcoded list of stable models | ||
STABLE_MODELS = [ | ||
[('p', True), ('q', False), ('r', True)], # {p, r} | ||
[('p', False), ('q', True), ('r', True)], # {q, r} | ||
] | ||
|
||
|
||
def main(): | ||
'''Entrypoint. | ||
''' | ||
print(f'ASP program:\n{tab(PRGM.strip(), striplines=True)}\n') | ||
pause() | ||
|
||
# Program parameters | ||
n = len(STABLE_MODELS[0]) | ||
print(f'Number of variables: {n}.') | ||
print() | ||
pause() | ||
|
||
# Initialization algorithm | ||
algorithm = qasp.init_algorithm.alg_grover(n) # Walsh-Hadamard | ||
print(f'Initialization algorithm:\n{tab(str(algorithm.draw()))}\n') | ||
pause() | ||
|
||
# Oracle | ||
oracle = qasp.oracle.from_asp_stable_models( | ||
STABLE_MODELS, var_order=['p', 'q', 'r']) | ||
print(f'Quantum oracle:\n{tab(str(oracle[1].draw()))}\n') | ||
pause() | ||
|
||
# Simulation | ||
m = math.ceil(n/2) | ||
eps = 1/6 | ||
# pylint: disable=invalid-name | ||
(circuit, _, M) = qasp.problems.estimation.exec_count(algorithm, oracle, m, eps) | ||
print(f'Used circuit:\n{tab(str(circuit.draw()))}\n') | ||
pause() | ||
print( | ||
f'Estimated number of solutions: {M.lower:.2f} <= M <= {M.upper:.2f}.') | ||
print() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |