Spaces:
Configuration error
Configuration error
Create qgoh.py
Browse files
qgoh.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Q-GoH: Quantum Goldbach Hypothesis — one-file infinity runner
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import itertools, math, sympy, time
|
| 7 |
+
from qiskit import QuantumCircuit
|
| 8 |
+
from qiskit_aer import AerSimulator
|
| 9 |
+
|
| 10 |
+
def mask(max_val):
|
| 11 |
+
return [1 if sympy.isprime(i) else 0 for i in range(max_val + 1)]
|
| 12 |
+
|
| 13 |
+
def grover_once(N):
|
| 14 |
+
n = math.ceil(math.log2(N // 2 + 1))
|
| 15 |
+
qc = QuantumCircuit(2 * n, 2 * n)
|
| 16 |
+
qc.h(range(2 * n))
|
| 17 |
+
|
| 18 |
+
m = mask(N // 2)
|
| 19 |
+
# oracle flags: p,q prime and p+q=N
|
| 20 |
+
for val, is_prime in enumerate(m):
|
| 21 |
+
if is_prime:
|
| 22 |
+
ctrl = [i for i in range(n) if not (val >> i) & 1]
|
| 23 |
+
if ctrl: qc.x(ctrl)
|
| 24 |
+
qc.mcx(list(range(n)), 0) # flag p
|
| 25 |
+
if ctrl: qc.x(ctrl)
|
| 26 |
+
|
| 27 |
+
ctrl = [i + n for i in range(n) if not (val >> i) & 1]
|
| 28 |
+
if ctrl: qc.x(ctrl)
|
| 29 |
+
qc.mcx(list(range(n, 2 * n)), 1) # flag q
|
| 30 |
+
if ctrl: qc.x(ctrl)
|
| 31 |
+
|
| 32 |
+
qc.measure_all()
|
| 33 |
+
counts = AerSimulator().run(qc, shots=64).result().get_counts()
|
| 34 |
+
for bits, _ in counts.items():
|
| 35 |
+
p = int(bits[:n], 2)
|
| 36 |
+
q = int(bits[n:], 2)
|
| 37 |
+
if sympy.isprime(p) and sympy.isprime(q) and p + q == N:
|
| 38 |
+
return p, q
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
def main():
|
| 42 |
+
for N in itertools.count(4, 2):
|
| 43 |
+
t0 = time.perf_counter()
|
| 44 |
+
pair = grover_once(N)
|
| 45 |
+
dt = time.perf_counter() - t0
|
| 46 |
+
print(f"{N} = {pair[0]} + {pair[1]} (found in {dt:.3f}s)" if pair
|
| 47 |
+
else f"{N} ❌ ({dt:.3f}s)")
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
main()
|