-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrover.py
More file actions
44 lines (38 loc) · 1.11 KB
/
Copy pathGrover.py
File metadata and controls
44 lines (38 loc) · 1.11 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
from qiskit import QuantumCircuit, ClassicalRegister
from qiskit_aer import AerSimulator
from qiskit import transpile
n = 3
qc = QuantumCircuit(n, n)
# --- Oracle marking |101> (q2 q1 q0 interpretation = [2,1,0]) ---
oracle = QuantumCircuit(n, name="Oracle")
# Flip the qubits that should be 0 (q1) so that the marked state maps to |111>
oracle.x(1)
# Implement multi-controlled Z using H-CCX-H on the last qubit
oracle.h(2)
oracle.ccx(0, 1, 2)
oracle.h(2)
# Undo the X
oracle.x(1)
oracle_gate = oracle.to_gate()
# --- Diffuser (Grover diffusion) ---
def diffuser(num):
d = QuantumCircuit(num, name="Diffuser")
d.h(range(num))
d.x(range(num))
d.h(num - 1)
d.mcx(list(range(num - 1)), num - 1)
d.h(num - 1)
d.x(range(num))
d.h(range(num))
return d.to_gate()
# --- Grover iteration ---
qc.h(range(n))
qc.append(oracle_gate, range(n))
qc.append(diffuser(n), range(n))
# Measure
qc.measure(range(n), range(n))
# Transpile and run on Aer
backend = AerSimulator()
tqc = transpile(qc, backend=backend, optimization_level=3)
result = backend.run(tqc, shots=2048).result()
print(result.get_counts())