-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame4.py
More file actions
110 lines (91 loc) · 3.86 KB
/
game4.py
File metadata and controls
110 lines (91 loc) · 3.86 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
import random
from qiskit import QuantumCircuit
from qiskit.quantum_info import Statevector
def run_ion_engine(num_ions, target_state, player_moves):
"""Processes quantum logic as laser pulses on an ion chain."""
qc = QuantumCircuit(num_ions)
# 1. Initialization (Laser Cooling & Superposition)
qc.h(range(num_ions))
# 2. Oracle Logic (Addressing specific ions with lasers)
for move in player_moves:
if move.startswith('X'):
try:
idx = int(move[1:])
if idx < num_ions: qc.x(idx)
except: pass
# The 'LOCK' represents an entangling operation (like a Mølmer-Sørensen sequence)
if move == 'LOCK':
if num_ions == 2:
qc.cz(0, 1)
elif num_ions == 3:
qc.ccz(0, 1, 2)
elif num_ions >= 4:
qc.h(num_ions-1)
qc.mcx(list(range(num_ions-1)), num_ions-1)
qc.h(num_ions-1)
# 3. The Diffuser (Global Laser Pulses)
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)
probs = state.probabilities_dict()
win_prob = probs.get(target_state[::-1], 0)
return win_prob, probs
def display_trap_visual(num_ions, target_state, current_moves):
"""Visualizes the Ion Chain inside the vacuum chamber."""
print("\n--- ❄️ VACUUM CHAMBER (Ion Chain) ---")
# Build the visual chain
visual_ions = []
for i in range(num_ions):
# Check if the last action was an X-gate on this ion
if f"X{i}" in current_moves:
visual_ions.append(f"( Ion_{i}:|1> )")
else:
visual_ions.append(f"( Ion_{i}:|0> )")
print(" " + " <--> ".join(visual_ions))
print("--------------------------------------")
def start_oqd_demo():
stage = 1
num_ions = 2
print("==========================================")
print("⚛️ OPEN QUANTUM DESIGN: ION TRAP DEMO ⚛️")
print("==========================================")
print("Goal: Use laser pulses (X-gates) and entangling")
print("locks to find the target state in the ion chain.")
while num_ions <= 4:
target = format(random.randint(0, (2**num_ions) - 1), f'0{num_ions}b')
threshold = 0.95 if num_ions == 2 else 0.75 if num_ions == 3 else 0.45
print(f"\n📍 STAGE {stage}: {num_ions} Ions in the Trap")
print(f"🎯 MISSION: Isolate State |{target}>")
print(f"COMMANDS: X0-{num_ions-1}, LOCK, RUN, CLEAR, EXIT")
moves = []
while True:
display_trap_visual(num_ions, target, moves)
action = input(f"ION_CONTROL > ").strip().upper()
if action == 'EXIT': return
if action == 'CLEAR':
moves = []
continue
if action == 'RUN':
win_prob, probs = run_ion_engine(num_ions, target, moves)
# Radar Map
print("\n📡 STATE READOUT:")
for s, p in sorted(probs.items()):
bar = "█" * int(p * 40)
print(f" |{s[::-1]}>: {p*100:5.1f}% {bar}")
if win_prob >= threshold:
print(f"\n✨ SUCCESS! State |{target}> isolated with {win_prob*100:.1f}% probability.")
num_ions += 1
stage += 1
break
else:
print(f"\n❌ SIGNAL WEAK ({win_prob*100:.1%}). Adjust your laser sequence.")
else:
moves.append(action)
print("\n🏆 BOUNTY COMPLETE: You've mastered the Trapped-Ion Maze!")
if __name__ == "__main__":
start_oqd_demo()