-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroverSearch.py
More file actions
116 lines (91 loc) · 2.91 KB
/
GroverSearch.py
File metadata and controls
116 lines (91 loc) · 2.91 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator
from qiskit.visualization import plot_histogram
with st.sidebar:
st.title("Grover’s Algorithm - Learn It")
st.subheader("What is it?")
st.markdown("""
Grover’s Algorithm is a **quantum search algorithm** that finds a specific item in an unsorted list **faster** than classical search.
- Classical Search: ~N steps
- Quantum Grover: ~√N steps
""")
st.subheader("Real-life Example:")
st.markdown("""
Suppose you have 8 lockers, and **only 1 has the prize**.
- Classical method checks 1 by 1.
- Grover’s quantum magic can find it in just 2 tries.
""")
st.subheader(" How It Works (Simplified):")
st.markdown("""
1. **Create Superposition** – All lockers opened at once
2. **Oracle** – Marks the right locker
3. **Amplify** – Increases chance for marked one
4. **Measure** – Most likely, the correct answer
""")
st.subheader(" Your Role Here")
st.markdown("""
You give a binary string (like `101`) = the target.
Grover builds a quantum circuit to **find it**.
""")
st.subheader(" Why It Matters")
st.markdown("""
- Works on **unsorted data**
- Powerful for **security, AI, optimization**
- Shows real **quantum advantage**
""")
st.markdown("---")
st.markdown("Built using Qiskit + Streamlit")
# Title
st.title("Grover's Algorithm - Custom Oracle")
# Input binary string
target_binary = st.text_input("Enter target binary string (e.g. 101):", "101")
# Validate input
if not all(bit in "01" for bit in target_binary):
st.error("Please enter a valid binary string using only 0s and 1s.")
st.stop()
n = len(target_binary)
qc = QuantumCircuit(n, n)
# Step 1: Initialize to superposition
qc.h(range(n))
# Step 2: Oracle - mark the target state
for i, bit in enumerate(target_binary):
if bit == '0':
qc.x(i)
qc.h(n - 1)
if n == 2:
qc.cx(0, 1)
else:
qc.mcx(list(range(n - 1)), n - 1)
qc.h(n - 1)
for i, bit in enumerate(target_binary):
if bit == '0':
qc.x(i)
# Step 3: Diffusion operator
qc.h(range(n))
qc.x(range(n))
qc.h(n - 1)
if n == 2:
qc.cx(0, 1)
else:
qc.mcx(list(range(n - 1)), n - 1)
qc.h(n - 1)
qc.x(range(n))
qc.h(range(n))
# Step 4: Measurement
qc.measure(range(n), range(n))
# Simulate
simulator = AerSimulator()
compiled_circuit = transpile(qc, simulator)
result = simulator.run(compiled_circuit).result()
counts = result.get_counts()
# Show circuit
st.subheader("Grover Circuit")
st.text(qc.draw(output='text'))
# Show result
st.subheader("Measurement Histogram")
fig = plot_histogram(counts, bar_labels=False, figsize=(8, 5))
st.pyplot(fig)
# Display raw counts
st.subheader("Raw Output Counts")
st.json(counts)