-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQISA.py
More file actions
443 lines (360 loc) · 16.2 KB
/
QISA.py
File metadata and controls
443 lines (360 loc) · 16.2 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
import cmath
import torchquantum as tq
import torchquantum.functional as tqf
from torchquantum.measurement import expval_joint_analytical
import random
def pauli_matrix(op: str) -> torch.Tensor:
mapping = {
'I': torch.eye(2, dtype=torch.complex64),
'X': torch.tensor([[0, 1], [1, 0]], dtype=torch.complex64),
'Y': torch.tensor([[0, -1j], [1j, 0]], dtype=torch.complex64),
'Z': torch.tensor([[1, 0], [0, -1]], dtype=torch.complex64)
}
return mapping[op].to('cuda')
def rx_matrix(theta: float) -> torch.Tensor:
"""
Returns the RX rotation matrix for a given angle.
Args:
theta (float): The rotation angle in radians.
Returns:
torch.Tensor: A 2x2 complex tensor representing the RX gate.
"""
return torch.tensor([[math.cos(theta/2), -1j*math.sin(theta/2)],
[-1j*math.sin(theta/2), math.cos(theta/2)]],
dtype=torch.complex64)
def ry_matrix(theta: float) -> torch.Tensor:
"""
Returns the RY rotation matrix for a given angle.
Args:
theta (float): The rotation angle in radians.
Returns:
torch.Tensor: A 2x2 complex tensor representing the RY gate.
"""
theta = nn.Parameter(theta, requires_grad=True)
theta = theta.type(torch.complex64)
co = torch.cos(theta / 2)
si = torch.sin(theta / 2)
mat = torch.stack(
[torch.cat([co, -si], dim=-1), torch.cat([si, co], dim=-1)], dim=-2
).squeeze(0)
return mat
def cnot_matrix() -> torch.Tensor:
"""
Returns the 4x4 matrix representation of the CNOT gate.
Returns:
torch.Tensor: A 4x4 complex tensor representing the CNOT gate.
"""
return torch.tensor([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0]], dtype=torch.complex64)
def expand_operator(gate: torch.Tensor, target_qubits: list, n: int) -> torch.Tensor:
"""
Expands a given gate operator to a full operator acting on n qubits.
The gate is assumed to be a 2^m x 2^m matrix acting on m target qubits.
Args:
gate (torch.Tensor): The operator matrix of size (2**m, 2**m).
target_qubits (list): List of qubit indices on which the gate acts.
n (int): Total number of qubits.
Returns:
torch.Tensor: The full operator matrix of size (2**n, 2**n).
"""
dim = 1 << n # 2^n
U_full = torch.zeros((dim, dim), dtype=gate.dtype, device=gate.device)
m = len(target_qubits)
for i in range(dim):
# Represent the integer i as a binary vector of length n (MSB first)
bits = [(i >> (n - 1 - b)) & 1 for b in range(n)]
sub_index = 0
for qubit in target_qubits:
sub_index = (sub_index << 1) | bits[qubit]
for j in range(2**m):
# Represent j as a binary vector of length m (MSB first)
replacement_bits = [(j >> (m - 1 - b)) & 1 for b in range(m)]
new_bits = bits.copy()
for idx, qubit in enumerate(target_qubits):
new_bits[qubit] = replacement_bits[idx]
new_i = 0
for b in range(n):
new_i = (new_i << 1) | new_bits[b]
U_full[new_i, i] = gate[j, sub_index]
return U_full
class QSA(nn.Module):
def __init__(
self,
n_embed: int,
n_context: int,
head_size: int,
layer_id: int,
head_id: int,
version=3
):
"""
Quantum Self-Attention layer.
The constructor now takes two additional parameters:
- layer_id: the unique transformer layer number
- head_id: the head index within that layer
These parameters are used to generate unique names for the unitary (U) matrices.
Args:
n_embed (int): Embedding dimension.
n_context (int): Context length (number of tokens).
head_size (int): Number of measurement outcomes per head.
layer_id (int): Unique transformer layer number.
head_id (int): Head index within the transformer layer.
Returns:
QSA: An instance of the quantum self-attention layer.
"""
super().__init__()
assert version in [1, 2, 3], "version must be 1, 2 or 3"
print(f"Instantiate QSA v{version} for the head {head_id}")
self.version = version
self.__use_kv_one_measurement = True if version == 1 else False
self.__precomputed = False
self.head_size = head_size
self.n_context = n_context
self.transformer_layer_id = layer_id # transformer layer number
self.head_id = head_id # head index
self.register_buffer('tril', torch.tril(torch.ones(n_context, n_context)))
self.n_wires = int(np.ceil(np.log2(n_embed)))
self.ops = [self.choose_op() for _ in range(self.head_size)]
print(f"Generated Pauli strings for layer {layer_id} head {head_id}: ", self.ops)
if version != 3:
self.q_fast = ValueLayerFast(self.n_wires, self.ops, self.head_size,
self.transformer_layer_id, self.head_id, qk=self.__use_kv_one_measurement)
self.k_fast = ValueLayerFast(self.n_wires, self.ops, self.head_size,
self.transformer_layer_id, self.head_id, qk=self.__use_kv_one_measurement)
self.q_slow = ValueLayerSlow(self.n_wires, self.ops, self.head_size, qk=self.__use_kv_one_measurement)
self.k_slow = ValueLayerSlow(self.n_wires, self.ops, self.head_size, qk=self.__use_kv_one_measurement)
else:
self.q_linear = nn.Linear(n_embed, head_size)
self.k_linear = nn.Linear(n_embed, head_size)
self.v_linear = nn.Linear(n_embed, n_embed, bias=False)
self.v_fast = ValueLayerFast(self.n_wires, self.ops, self.head_size, n_embed,
self.transformer_layer_id, self.head_id)
self.v_slow = ValueLayerSlow(self.n_wires, self.ops, self.head_size)
# Slow branch for training
self.unitary_count = 0
def choose_op(self) -> str:
"""
Randomly chooses a Pauli string operator for measurement.
Returns:
str: A Pauli string (e.g., 'IXZ') where at least one character is non-identity.
"""
prob_identity = 0.9
non_identity_ops = ['X', 'Y', 'Z']
while True:
op_chars = []
for _ in range(self.n_wires):
if random.random() < prob_identity:
op_chars.append('I')
else:
op_chars.append(random.choice(non_identity_ops))
op = ''.join(op_chars)
if op != 'I' * self.n_wires:
return op
def _forward_slow(self, x: torch.Tensor) -> torch.Tensor:
"""
Slow forward pass for training that simulates the full quantum circuit step-by-step.
Args:
x (torch.Tensor): Input tensor of shape (B, T, C).
Returns:
torch.Tensor: Output tensor of shape (B, T, head_size) after applying quantum self-attention.
"""
B, T, C = x.shape
qdev = tq.QuantumDevice(n_wires=self.n_wires, bsz=B * T, device=x.device)
if self.version == 3:
q = self.q_linear(x)
k = self.k_linear(x)
v = self.v_linear(x)
else:
q = self.q_slow(x, qdev)
k = self.k_slow(x, qdev)
v = self.v_slow(v, qdev)
q = q.unsqueeze(2)
k = k.unsqueeze(1)
alpha = torch.exp(-((q - k) ** 2).sum(dim=-1))
alpha = alpha.masked_fill(self.tril[:T, :T] == 0, float('-inf'))
normalized_alpha = F.softmax(alpha, dim=-1)
out = normalized_alpha.permute(0, 2, 1) @ v
return out
def _forward_fast(self, x: torch.Tensor) -> torch.Tensor:
"""
Fast forward pass for inference using precomputed unitary matrices.
Args:
x (torch.Tensor): Input tensor of shape (B, T, C).
Returns:
torch.Tensor: Output tensor of shape (B, T, head_size) after applying quantum self-attention.
"""
B, T, C = x.shape
if self.version == 3:
q = self.q_linear(x)
k = self.k_linear(x)
else:
q = self.q_fast(x)
k = self.k_fast(x)
v = self.v_fast(x)
q = q.unsqueeze(2)
k = k.unsqueeze(1)
alpha = torch.exp(-((q - k) ** 2).sum(dim=-1))
alpha = alpha.masked_fill(self.tril[:T, :T] == 0, float('-inf'))
normalized_alpha = F.softmax(alpha, dim=-1)
out = normalized_alpha.permute(0, 2, 1) @ v
return out
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Forward pass of the QSA layer. Uses the slow branch during training and the fast branch during inference.
Args:
x (torch.Tensor): Input tensor of shape (B, T, C).
Returns:
torch.Tensor: Output tensor after applying quantum self-attention.
"""
if self.training:
self.__precomputed = False
return self._forward_slow(x)
else:
if not self.__precomputed:
self.precompute()
self.__precomputed = True
return self._forward_fast(x)
def precompute(self):
"""
Precomputes and registers the unitary operators for the fast branch by copying parameters
from the slow branch. The unitary matrices are given unique names based on the transformer
layer and head identifiers.
Then precomputes observables as O' = <psi| U^dagger O U |psi>
Returns:
None
"""
super().eval()
# Copy parameters from slow to fast for quantum parts
if self.version != 3:
self.q_fast.rx0_params = self.q_slow.rx0
self.q_fast.ry0_params = self.q_slow.ry0
self.q_fast.ry1_params = self.q_slow.ry1
self.k_fast.rx0_params = self.k_slow.rx0
self.k_fast.ry0_params = self.k_slow.ry0
self.k_fast.ry1_params = self.k_slow.ry1
# Note: No copy needed for v_fast, as unitaries are removed
# Build unitaries (skip for v_fast)
if self.version != 3:
self.q_fast._build_unitary(self.unitary_count, "q_fast", self.transformer_layer_id, self.head_id)
self.unitary_count += 1
self.k_fast._build_unitary(self.unitary_count, "k_fast", self.transformer_layer_id, self.head_id)
self.unitary_count += 1
print("[INFO] Precomputation of Unitaries completed.")
# Precompute observables (only for v_fast, with absorption)
self.v_fast.precompute_observables(self.v_linear)
print("[INFO] Precomputation of observables completed.")
self.unitary_count += 1
# ----------------- Fast Branch: ValueLayerFast -----------------
class ValueLayerFast(nn.Module):
"""
Modified fast value layer with NO unitaries.
- Uses raw amplitude loading (no normalization) for exact absorption of the classical linear layer.
- Precomputes transformed observables O' = L^\dagger O L after training.
- At inference: direct measurement on raw encoded x, no classical linear needed.
"""
def __init__(self, n_wires: int, ops: list[str], head_size: int, n_embed: int,
transformer_layer_id: int, head_id: int):
super().__init__()
self.n_wires = n_wires
self.ops = ops
self.head_size = head_size
self.n_embed = n_embed
self.transformer_layer_id = transformer_layer_id
self.head_id = head_id
self.dim = 1 << n_wires
# Precomputed transformed observables O' (one per measurement outcome)
self.obs_primes = nn.ParameterList([
nn.Parameter(torch.zeros((self.dim, self.dim), dtype=torch.complex64), requires_grad=False)
for _ in range(self.head_size)
])
def precompute_observables(self, v_linear: nn.Linear):
"""
Absorb the classical linear layer into the observables.
Computes O' = L^\dagger O L for each fixed Pauli string O,
where L = v_linear.weight (the linear transformation matrix such that v_col = L @ x_col).
Pads L to the full 2**n_wires dimension (assuming zero-padding in encoding).
"""
device = v_linear.weight.device
# Linear transformation: v = x @ weight.t() , but for columns, v_col = weight @ x_col => L = weight
L = v_linear.weight.to(dtype=torch.complex64) # (n_embed, n_embed)
# Pad to full Hilbert space (data subspace only)
L_full = torch.zeros((self.dim, self.dim), dtype=torch.complex64, device=device)
L_full[:self.n_embed, :self.n_embed] = L
L_dag = L_full.conj().t()
for idx, op in enumerate(self.ops):
# Build observable O from Pauli string
mats = [pauli_matrix(c) for c in op]
O = mats[0]
for m in mats[1:]:
O = torch.kron(O, m).to(device)
# Transformed observable O' = L^\dagger O L
O_prime = L_dag @ O @ L_full
self.obs_primes[idx].data.copy_(O_prime)
print(f"[INFO] Precomputed absorbed observables for value in layer {self.transformer_layer_id} head {self.head_id}")
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""
Fast inference forward: encode raw x (no linear, no normalization), measure precomputed O'.
Output shape: (B, T, head_size)
"""
B, T, C = x.shape
assert C == self.n_embed, f"Expected channel dim {self.n_embed}, got {C}"
x_flat = x.view(B * T, C) # (B*T, n_embed)
device = x.device
dim = self.dim
# Raw amplitude loading: place data in first n_embed basis states, pad zeros, NO normalization
psi = torch.zeros((B * T, dim), dtype=torch.complex64, device=device)
psi[:, :self.n_embed] = x_flat.to(dtype=torch.complex64)
# Compute raw expectations psi^\dagger O' psi for each transformed observable
exps = []
for O_prime in self.obs_primes:
O_psi = torch.matmul(O_prime, psi.t()).t() # (B*T, dim)
exp = (psi.conj() * O_psi).sum(dim=-1).real # scalar per batch element
exps.append(exp)
out = torch.stack(exps, dim=-1) # (B*T, head_size)
return out.view(B, T, self.head_size)
# ------------------ Slow Branch: ValueLayerSlow ------------------
class ValueLayerSlow(nn.Module):
"""
Simplified slow value layer for training (no gates, raw classical computation without normalization).
"""
def __init__(self, n_wires: int, ops: list[str], hidden_dim: int):
super().__init__()
self.hidden_dim = hidden_dim
self.n_wires = n_wires
self.ops = ops
self.dim = 1 << n_wires
def forward(self, x: torch.Tensor, qdev: tq.QuantumDevice = None) -> torch.Tensor:
"""
Forward pass of the slow value layer (classical simulation without normalization).
Args:
x (torch.Tensor): Input tensor of shape (B, T, C) or (B, C).
qdev (tq.QuantumDevice, optional): Ignored (for compatibility).
Returns:
torch.Tensor: Output tensor of shape (B, T, hidden_dim) containing measurement results.
"""
if x.dim() == 2:
x = x.unsqueeze(1)
B, T, C = x.shape
device = x.device
x_flat = x.view(B * T, C) # (B*T, n_embed)
states = torch.zeros((B * T, self.dim), dtype=torch.complex64, device=device)
states[:, :C] = x_flat.to(dtype=torch.complex64)
exps = []
for op in self.ops:
mats = [pauli_matrix(c) for c in op]
O = mats[0]
for m in mats[1:]:
O = torch.kron(O, m).to(device)
O = O.to(dtype=torch.complex64)
O_psi = torch.matmul(O, states.t()).t()
exp = (states.conj() * O_psi).sum(dim=-1).real
exps.append(exp)
out = torch.stack(exps, dim=-1)
return out.view(B, T, self.hidden_dim)