-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtn_hhl.py
More file actions
239 lines (145 loc) · 6.37 KB
/
tn_hhl.py
File metadata and controls
239 lines (145 loc) · 6.37 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
import torch
import numpy as np
def qft_op(n_eigen: int, sign: int) -> torch.Tensor:
"""
Creates a Quantum Fourier Transform (QFT) matrix or its inverse.
This function constructs the QFT matrix using the formula:
QFT = (1/√m) * Σ_{x,y=0}^{m-1} exp(2πi * xy/m) |x⟩⟨y|
For inverse QFT, the sign is negative: exp(-2πi * xy/m)
Parameters:
-----------
n_eigen : int
Size of the QFT matrix (n_eigen x n_eigen).
sign : int
Sign for the phase factor: +1 for QFT, -1 for inverse QFT.
Returns:
--------
torch.Tensor
The QFT matrix (normalized by 1/√n_eigen factor).
Notes:
------
- The matrix is constructed using a recurrence relation for efficiency
- The result is normalized with the 1/√n_eigen prefactor
"""
matrix = torch.ones((n_eigen, n_eigen), dtype=torch.complex128)
# Phase factor for the QFT: 2πi/n_eigen (positive) or -2πi/n_eigen (negative)
angle = 1j * sign * 2.0 * np.pi / n_eigen
# Initialize the first row with phase factors (matrix[1,0] = 1 already set)
for j in range(1, n_eigen):
matrix[1, j] = np.exp(angle * j)
# Use recurrence relation: row[i] = row[i-1] * row[1] (element-wise)
# This exploits the periodicity of the QFT matrix structure
for i in range(2, n_eigen):
matrix[i] = matrix[i-1] * matrix[1]
return matrix / np.sqrt(n_eigen)
def phase_kickback_op(b_vector:torch.Tensor, n_eigen:int, U_matrix: torch.Tensor) -> torch.Tensor:
"""
Function that creates the Phase KickBack tensor for QPE.
This function computes U^0*b, U^1*b, U^2*b, ..., U^(n_eigen-1)*b where b is the input vector.
The result is stored in a matrix where each row represents a different power of U applied to b.
Parameters:
-----------
b_vector : torch.Tensor
Input vector to be transformed.
n_eigen : int
Number of eigenvalues (determines the number of powers to compute).
U_matrix : torch.Tensor
Unitary matrix to be raised to powers.
Returns:
--------
torch.Tensor
Matrix where each row i contains U^i * b_vector.
"""
# Multiply the previous by U
n_elems = U_matrix.shape[0]
phase_kick_matrix = torch.zeros((n_eigen, n_elems), dtype=torch.complex128)
phase_kick_matrix[0] = b_vector.clone()
ans = b_vector.clone() # Clone to avoid modifying the original
for i in range(1, n_eigen):
ans = torch.matmul(U_matrix, ans)
phase_kick_matrix[i] = ans
return phase_kick_matrix
def phase_kickback_op_inv(n_eigen:int, U_matrix: torch.Tensor) -> torch.Tensor:
"""
Function that creates the inverse Phase KickBack tensor for QPE.
This function computes U^0, U^1, U^2, ..., U^(n_eigen-1) where U is the unitary matrix.
The result is stored in a 3D tensor where each slice represents a different power of U.
Parameters:
-----------
n_eigen : int
Number of eigenvalues (determines the number of powers to compute).
U_matrix : torch.Tensor
Unitary matrix to be raised to powers.
Returns:
--------
torch.Tensor
3D tensor where each slice i contains U^i.
"""
# Multiply the previous by U
n_elems = U_matrix.shape[0]
phase_kick_tensor = torch.zeros((n_eigen, n_elems, n_elems), dtype=torch.complex128)
phase_kick_tensor[0] = torch.eye(n_elems, dtype=torch.complex128)
ans = phase_kick_tensor[0].clone() # Clone to avoid modifying the original
for i in range(1, n_eigen):
ans = torch.matmul(ans, U_matrix)
phase_kick_tensor[i] = ans
return phase_kick_tensor
def inversor(n_eigen: int, t: float) -> torch.Tensor:
"""
Function that creates the inversion matrix.
This function creates a diagonal matrix with eigenvalues 1/i and -1/i,
where i ranges from 1 to n_eigen//2. This is used for eigenvalue inversion
in the HHL algorithm.
Parameters:
-----------
n_eigen : int
Size of the square matrix to create.
t : float
Exponential scaling factor.
Returns:
--------
torch.Tensor
The matrix with inversion values on the diagonal.
"""
matrix = torch.zeros((n_eigen, n_eigen), dtype=torch.complex128)
# Assign the values
for i in range(1, (n_eigen // 2 + 1)): # The +1 is for the extra in case n_eigen is even.
matrix[-i, -i] = -1 / i # Negative eigenvalues
matrix[i, i] = 1 / i # Positive eigenvalues
return matrix * t / n_eigen
def tracer(W_matrix: torch.Tensor, U_matrix: torch.Tensor):
'''
This function performs the trace after the last product.
Parameters:
- W_matrix, torch.Tensor: W matrix.
- U_matrix, torch.Tensor: inverse U tensor.
Returns:
- C_matrix, torch.Tensor: tensor with unscaled solution.
'''
C_matrix = torch.tensordot(W_matrix, U_matrix, dims=[[0,1], [0,1]]) # C_k = W_ij U^(-1)_ijk
return C_matrix
#-----------------------------------------------------------------
def tensornetwork_HHL(num_eigen: int, t: float, b_vector: torch.Tensor, A_matrix: torch.Tensor):
'''
Function that generates tensors and contracts them.
Parameters:
- num_eigen, int: number of eigenvalues.
- t, float: exponential scaling factor.
- b_vector, torch.Tensor: vector b.
- A_matrix, torch.Tensor: matrix A.
Returns:
- solution, torch.Tensor: the solution x
'''
# Calculate dimensions
n_elements = len(b_vector)
# Calculate complex exponentials
U_matrix_inv = torch.matrix_exp(-(2j*np.pi*t/num_eigen)*A_matrix)
U_matrix = torch.conj(U_matrix_inv).T # its transpose
# Product PKB-QFT_inverse-inversor-QFT, careful with
# indices, done to avoid transpositions
W_matrix = torch.matmul( qft_op(num_eigen, sign=1), \
torch.matmul( inversor(num_eigen, t), \
torch.matmul(qft_op(num_eigen, sign=-1), \
phase_kickback_op(b_vector, num_eigen, U_matrix)) ) )
# Rescaled result
return tracer(W_matrix, phase_kickback_op_inv(num_eigen, U_matrix_inv)).real