-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_of_life_q.py
More file actions
98 lines (77 loc) · 3.08 KB
/
game_of_life_q.py
File metadata and controls
98 lines (77 loc) · 3.08 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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# Grid size
N = 200
# Initialize complex amplitudes: (a|0⟩ + b|1⟩), random unit-norm complex numbers
def random_state():
theta = np.random.rand() * 2 * np.pi
phi = np.random.rand() * np.pi # for proper normalization
a = np.cos(phi/2)
b = np.sin(phi/2) * np.exp(1j * theta)
return np.array([a, b])
# Initialize grid with random quantum states
grid = np.array([[random_state() for j in range(N)] for i in range(N)], dtype=object)
# Measurement collapse function (probabilistic)
def measure(state):
probs = np.abs(state) ** 2
probs = probs / np.sum(probs) # ensure normalization
return np.random.choice([0, 1], p=probs)
# Apply a simple Hadamard-like "quantum rule"
def quantum_update(state, neighbors):
# Extract the |1⟩ amplitudes from neighbors
total = sum(np.real(n[1]) for n in neighbors) # sum of |1⟩ amplitudes
influence = total / len(neighbors) if len(neighbors) > 0 else 0 # normalize influence
# Apply simple rotation based on influence
a, b = state
rotation_angle = influence * 0.1 # small rotation parameter
# Apply rotation matrix
cos_theta = np.cos(rotation_angle)
sin_theta = np.sin(rotation_angle)
new_a = a * cos_theta - b * sin_theta
new_b = a * sin_theta + b * cos_theta
# Renormalize to maintain unit norm
norm = np.sqrt(np.abs(new_a)**2 + np.abs(new_b)**2)
if norm > 0:
return np.array([new_a, new_b]) / norm
else:
return np.array([1.0, 0.0]) # default state
# Neighborhood function (8-connected)
def get_neighbors(grid, x, y):
neighbors = []
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if not (i == 0 and j == 0): # exclude center cell
nx, ny = (x + i) % N, (y + j) % N
neighbors.append(grid[nx][ny])
return neighbors
# Setup animation
fig, ax = plt.subplots(figsize=(8, 8))
img = ax.imshow(np.zeros((N, N)), cmap='viridis', vmin=0, vmax=1, interpolation='nearest')
ax.set_title('Quantum Cellular Automaton')
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.colorbar(img, ax=ax, label='Measurement Probability')
def update(frame):
global grid
# Create new grid for next state
new_grid = np.empty_like(grid)
# Update each cell based on quantum rules
for i in range(N):
for j in range(N):
neighbors = get_neighbors(grid, i, j)
new_grid[i][j] = quantum_update(grid[i][j], neighbors)
# Update the grid
grid = new_grid
# Collapse for visualization (show |1⟩ probabilities)
collapsed = np.array([[np.abs(grid[i][j][1])**2 for j in range(N)] for i in range(N)])
img.set_data(collapsed)
ax.set_title(f'Quantum Cellular Automaton - Frame {frame}')
return [img]
# Create and run animation
ani = animation.FuncAnimation(fig, update, frames=200, interval=1, blit=True, repeat=True)
# Display the plot
plt.tight_layout()
plt.show()
# Optional: Save as GIF (uncomment the line below)
# ani.save('quantum_ca.gif', writer='pillow', fps=10)