Skip to content

Commit

Permalink
Amplification example with Grover
Browse files Browse the repository at this point in the history
  • Loading branch information
davidedellagiustina committed Jan 11, 2024
1 parent 0ad601c commit b2b43a2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
3 changes: 1 addition & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ ci:

# List the available examples
list:
@ls -lA ./src/examples/ | tail -n +2 | awk '{print $9}' | sed 's/.py//' | tr '\n' ' '
@echo
@ls -lA ./src/examples/ | tail -n +2 | awk '{print $9}' | sed 's/.py//'

# Run a selected example from the examples folder
run example:
Expand Down
73 changes: 73 additions & 0 deletions src/examples/grover_search_known_m.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'''Find the models of an ASP program using the naive Grover search algorithm.
'''

from src import qasp

# 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 pause():
'''Pause the program execution and wait for the user to press a key to continue.
'''
_ = input("Press the <ENTER> key to continue...")
print()


def tab(lines: str, striplines: bool = False) -> str:
'''Return a variant of the input string indented by one level.
#### Arguments
lines (str): Lines to be indented.
striplines (bool): Whether to strip() individual lines before re-joining. Defaults to False.
#### Return
str: Indented variant of the provided string.
'''
return '\n'.join([
' ' + (line.strip() if striplines else line)
for line in lines.splitlines(False)
])


if __name__ == '__main__':
print(f'ASP program:\n{tab(PRGM.strip(), striplines = True)}\n')
pause()

# Program parameters
N = len(STABLE_MODELS[0])
M = len(STABLE_MODELS)
print(f'Number of variables: {N}.')
print(f'Number of stable models: {M}.')
print()
pause()

# Initialization algorithm
algorithm = qasp.problems.amplification.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)
print(f'Quantum oracle:\n{tab(str(oracle[1].draw()))}\n')
pause()

# Simulation
(circuit, iters, stable_model) = qasp.problems.amplification.exec_find_one_known_m(
algorithm, oracle, M)
print(f'Circuit:\n\n{circuit.draw()}')
pause()
print(f'Found stable model: {stable_model}.')
print(f'Number of iterations: {iters}.')
print()
12 changes: 8 additions & 4 deletions src/qasp/problems/amplification.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ def __measure_to_model(measurements: str, var_names: str) -> Model:
return model


def exec_find_one_known_m(algorithm: QuantumCircuit, oracle: Oracle, m: int) -> tuple[int, Model]:
def exec_find_one_known_m(
algorithm: QuantumCircuit,
oracle: Oracle,
m: int
) -> tuple[QuantumCircuit, int, Model]:
'''Simulate the amplitude amplification circuit to find one solution to the problem.
#### Arguments
Expand All @@ -169,7 +173,8 @@ def exec_find_one_known_m(algorithm: QuantumCircuit, oracle: Oracle, m: int) ->
m (int): (Known) number of solutions.
#### Return
tuple[int, Model]: Found solution and number of iterations performed.
tuple[QuantumCircuit, int, Model]: Used circuit, number of iterations performed, and found \
solution.
'''
(c_oracle, q_oracle) = oracle

Expand All @@ -181,7 +186,6 @@ def exec_find_one_known_m(algorithm: QuantumCircuit, oracle: Oracle, m: int) ->

# Build circuit
circ = circuit_optimal(algorithm, q_oracle, m)
print(f'Circuit:\n{circ.draw()}\n')

# Run simulation
model, iters = None, 0
Expand All @@ -194,7 +198,7 @@ def exec_find_one_known_m(algorithm: QuantumCircuit, oracle: Oracle, m: int) ->
break

# Map output to readable format
return (iters, model)
return (circ, iters, model)


def exec_find_one_unknown_m(
Expand Down

0 comments on commit b2b43a2

Please sign in to comment.