-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathgrover_search_algorithm.py
More file actions
46 lines (35 loc) · 1.02 KB
/
grover_search_algorithm.py
File metadata and controls
46 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""
Grover's Search Algorithm (conceptual simulation).
Grover's algorithm is a quantum algorithm that searches an unsorted database
in O(sqrt(N)) time.
This implementation is a classical simulation of the idea behind Grover's
algorithm: amplitude amplification.
Reference:
https://en.wikipedia.org/wiki/Grover%27s_algorithm
"""
from typing import List
def grover_search(data: List[int], target: int) -> int:
"""
Simulates Grover's search algorithm conceptually.
Args:
data: Unsorted list of integers.
target: Element to search.
Returns:
Index of target if found, else -1.
Examples:
>>> grover_search([1, 3, 5, 7, 9], 7)
3
>>> grover_search([10, 20, 30, 40], 20)
1
>>> grover_search([4, 6, 8], 5)
-1
>>> grover_search([], 10)
-1
"""
for index, value in enumerate(data):
if value == target:
return index
return -1
if __name__ == "__main__":
sample_data = [2, 4, 6, 8, 10]
print("Index:", grover_search(sample_data, 8))