-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame6.py
More file actions
82 lines (72 loc) · 3.06 KB
/
game6.py
File metadata and controls
82 lines (72 loc) · 3.06 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
import random
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
def run_intermediate_engine(num_ions, player_moves):
qc = QuantumCircuit(num_ions)
for move in player_moves:
if move == 'H_ALL': qc.h(range(num_ions))
elif move.startswith('X'):
try:
idx = int(move[1:])
if idx < num_ions: qc.x(idx)
except: pass
elif move == 'CZ' and num_ions >= 2: qc.cz(0, 1)
elif move == 'CCZ' and num_ions >= 3: qc.ccz(0, 1, 2)
elif move == 'MCCZ' and num_ions >= 4:
qc.h(num_ions-1)
qc.mcx(list(range(num_ions-1)), num_ions-1)
qc.h(num_ions-1)
elif move == 'DIFFUSE':
# Manual Grover Diffuser
qc.h(range(num_ions))
qc.x(range(num_ions))
qc.h(num_ions-1)
qc.mcx(list(range(num_ions-1)), num_ions-1)
qc.h(num_ions-1)
qc.x(range(num_ions))
qc.h(range(num_ions))
state = Statevector.from_instruction(qc)
return state.probabilities_dict(), qc
def start_intermediate_game():
num_ions = 3
target = format(random.randint(0, (2**num_ions) - 1), f'0{num_ions}b')
print("\n" + "="*45)
print("⚛️ OQD INTERMEDIATE: MANUAL ION CONTROL ⚛️")
print("="*45)
print(f"🎯 TARGET MISSION: Isolate State |{target}>")
print("\nINSTRUCTIONS:")
print("1. H_ALL to begin.")
print("2. Use X-gates to 'Address' your target.")
print("3. Use the LOCK (CZ/CCZ) to flip the phase.")
print("4. IMPORTANT: Use X-gates again to 'Un-address'!")
print("5. DIFFUSE and RUN.")
moves = []
while True:
print(f"\n📦 CURRENT PULSE SEQUENCE: {moves}")
cmd = input("ION_COMMAND (H_ALL, X0-2, CZ, CCZ, DIFFUSE, RUN, CLEAR, EXIT) > ").strip().upper()
if cmd == 'EXIT': break
if cmd == 'CLEAR': moves = []; continue
if cmd == 'RUN':
if not moves: continue
probs, final_qc = run_intermediate_engine(num_ions, moves)
# Draw the circuit for educational feedback
print("\n🛠️ CIRCUIT BLUEPRINT:")
print(final_qc.draw(output='text'))
print("\n📡 READOUT:")
# Handle Endianness for display
win_prob = probs.get(target[::-1], 0)
for s, p in sorted(probs.items()):
bar = "█" * int(p * 25)
print(f" |{s[::-1]}>: {p*100:5.1f}% {bar}")
if win_prob > 0.7:
print(f"\n✨ SUCCESS! You manually built the Grover Oracle for |{target}>.")
break
else:
print(f"\n⚠️ SIGNAL WEAK ({win_prob*100:.1f}%).")
print("Hint: Did you forget to flip your X-gates back after the LOCK?")
elif cmd in ['H_ALL', 'CZ', 'CCZ', 'MCCZ', 'DIFFUSE'] or (cmd.startswith('X') and len(cmd)>1):
moves.append(cmd)
else:
print(f"❌ Unknown Command: {cmd}")
if __name__ == "__main__":
start_intermediate_game()