Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Commit 0aa710f

Browse files
mganahlChase Roberts
andauthored
Eigsh (#592)
* fix call signature * revert * ??? * ???? * fix test * fix argument order in matvec * fix linter * fix doc Co-authored-by: Chase Roberts <chaseriley@google.com>
1 parent df180d6 commit 0aa710f

9 files changed

Lines changed: 49 additions & 49 deletions

File tree

tensornetwork/backends/base_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,10 @@ def eigsh_lanczos(self,
389389
of `A`.
390390
Args:
391391
A: A (sparse) implementation of a linear operator.
392-
Call signature of `A` is `res = A(*args, vector)`, where `vector`
392+
Call signature of `A` is `res = A(vector, *args)`, where `vector`
393393
can be an arbitrary `Tensor`, and `res.shape` has to be `vector.shape`.
394394
arsg: A list of arguments to `A`. `A` will be called as
395-
`res = A(*args, initial_state)`.
395+
`res = A(initial_state, *args)`.
396396
initial_state: An initial vector for the Lanczos algorithm. If `None`,
397397
a random initial `Tensor` is created using the `backend.randn` method
398398
shape: The shape of the input-dimension of `A`.

tensornetwork/backends/jax/jax_backend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,10 @@ def A(H,x):
277277
278278
Args:
279279
A: A (sparse) implementation of a linear operator.
280-
Call signature of `A` is `res = A(*args, vector)`, where `vector`
280+
Call signature of `A` is `res = A(vector, *args)`, where `vector`
281281
can be an arbitrary `Tensor`, and `res.shape` has to be `vector.shape`.
282282
arsg: A list of arguments to `A`. `A` will be called as
283-
`res = A(*args, initial_state)`.
283+
`res = A(initial_state, *args)`.
284284
initial_state: An initial vector for the Lanczos algorithm. If `None`,
285285
a random initial `Tensor` is created using the `backend.randn` method
286286
shape: The shape of the input-dimension of `A`.

tensornetwork/backends/jax/jax_backend_test.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,10 @@ def test_eigsh_valid_init_operator_with_shape(dtype):
308308
tmp = backend.randn((D, D), dtype=dtype, seed=10)
309309
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
310310

311-
def matvec(H, x):
311+
def mv(x, H):
312312
return jax.numpy.dot(H, x)
313313

314-
eta1, U1 = backend.eigsh_lanczos(matvec, [H], init)
314+
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
315315
eta2, U2 = np.linalg.eigh(H)
316316
v2 = U2[:, 0]
317317
v2 = v2 / sum(v2)
@@ -326,10 +326,10 @@ def test_eigsh_small_number_krylov_vectors():
326326
init = np.array([1, 1], dtype=np.float64)
327327
H = np.array([[1, 2], [3, 4]], dtype=np.float64)
328328

329-
def matvec(H, x):
329+
def mv(x, H):
330330
return jax.numpy.dot(H, x)
331331

332-
eta1, _ = backend.eigsh_lanczos(matvec, [H], init, num_krylov_vecs=1)
332+
eta1, _ = backend.eigsh_lanczos(mv, [H], init, num_krylov_vecs=1)
333333
np.testing.assert_allclose(eta1[0], 5)
334334

335335

@@ -342,7 +342,7 @@ def test_eigsh_lanczos_1(dtype):
342342
tmp = backend.randn((D, D), dtype=dtype, seed=10)
343343
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
344344

345-
def mv(H, x):
345+
def mv(x, H):
346346
return jax.numpy.dot(H, x)
347347

348348
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
@@ -363,7 +363,7 @@ def test_eigsh_lanczos_2(dtype):
363363
tmp = backend.randn((D, D), dtype=dtype, seed=10)
364364
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
365365

366-
def mv(H, x):
366+
def mv(x, H):
367367
return jax.numpy.dot(H, x)
368368

369369
eta1, U1 = backend.eigsh_lanczos(mv, [H], shape=(D,), dtype=dtype)
@@ -385,11 +385,11 @@ def test_eigsh_lanczos_reorthogonalize(dtype, numeig):
385385
tmp = backend.randn((D, D), dtype=dtype, seed=10)
386386
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
387387

388-
def matvec(H, x):
388+
def mv(x, H):
389389
return jax.numpy.dot(H, x)
390390

391391
eta1, U1 = backend.eigsh_lanczos(
392-
matvec, [H],
392+
mv, [H],
393393
shape=(D,),
394394
dtype=dtype,
395395
numeig=numeig,

tensornetwork/backends/jax/jitted_functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def _generate_jitted_eigsh_lanczos(jax):
1919
```
2020
`matvec`: A callable implementing the matrix-vector product of a
2121
linear operator. `arguments`: Arguments to `matvec` additional to
22-
an input vector. `matvec` will be called as `matvec(*args, init)`.
22+
an input vector. `matvec` will be called as `matvec(init, *args)`.
2323
`init`: An initial input state to `matvec`.
2424
`ncv`: Number of krylov iterations (i.e. dimension of the Krylov space).
2525
`neig`: Number of eigenvalue-eigenvector pairs to be computed.
@@ -43,7 +43,7 @@ def jax_lanczos(matvec, arguments, init, ncv, neig, landelta, reortho):
4343
matvec: A callable implementing the matrix-vector product of a
4444
linear operator.
4545
arguments: Arguments to `matvec` additional to an input vector.
46-
`matvec` will be called as `matvec(*args, init)`.
46+
`matvec` will be called as `matvec(init, *args)`.
4747
init: An initial input state to `matvec`.
4848
ncv: Number of krylov iterations (i.e. dimension of the Krylov space).
4949
neig: Number of eigenvalue-eigenvector pairs to be computed.
@@ -75,7 +75,7 @@ def body_lanczos(vals):
7575
lambda x: jax.lax.fori_loop(0, i, body_modified_gram_schmidt,
7676
[normalized_vector, krylov_vectors]),
7777
False, lambda x: [normalized_vector, krylov_vectors])
78-
Av = matvec(*args, normalized_vector)
78+
Av = matvec(normalized_vector, *args)
7979

8080
diag_element = jax.numpy.dot(
8181
jax.numpy.conj(jax.numpy.ravel(normalized_vector)),

tensornetwork/backends/numpy/numpy_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ def eigsh_lanczos(self,
279279
of a linear operator `A`.
280280
Args:
281281
A: A (sparse) implementation of a linear operator.
282-
Call signature of `A` is `res = A(*args, vector)`, where `vector`
282+
Call signature of `A` is `res = A(vector, *args)`, where `vector`
283283
can be an arbitrary `Tensor`, and `res.shape` has to be `vector.shape`.
284284
arsg: A list of arguments to `A`. `A` will be called as
285-
`res = A(*args, initial_state)`.
285+
`res = A(initial_state, *args)`.
286286
initial_state: An initial vector for the Lanczos algorithm. If `None`,
287287
a random initial `Tensor` is created using the `backend.randn` method
288288
shape: The shape of the input-dimension of `A`.
@@ -346,7 +346,7 @@ def eigsh_lanczos(self,
346346
for v in krylov_vecs:
347347
vector_n -= np.dot(np.ravel(np.conj(v)), np.ravel(vector_n)) * v
348348
krylov_vecs.append(vector_n)
349-
A_vector_n = A(*args, vector_n)
349+
A_vector_n = A(vector_n, *args)
350350
diag_elements.append(
351351
np.dot(np.ravel(np.conj(vector_n)), np.ravel(A_vector_n)))
352352

tensornetwork/backends/numpy/numpy_backend_test.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ def test_eigsh_valid_init_operator_with_shape(dtype):
287287
tmp = backend.randn((D, D), dtype=dtype, seed=10)
288288
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
289289

290-
def mv(x):
291-
return np.dot(H, x)
290+
def mv(x, mat):
291+
return np.dot(mat, x)
292292

293-
eta1, U1 = backend.eigsh_lanczos(mv, [], init)
293+
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
294294
eta2, U2 = np.linalg.eigh(H)
295295
v2 = U2[:, 0]
296296
v2 = v2 / sum(v2)
@@ -305,10 +305,10 @@ def test_eigsh_small_number_krylov_vectors():
305305
init = np.array([1, 1], dtype=np.float64)
306306
H = np.array([[1, 2], [3, 4]], dtype=np.float64)
307307

308-
def mv(x):
309-
return np.dot(H, x)
308+
def mv(x, mat):
309+
return np.dot(mat, x)
310310

311-
eta1, _ = backend.eigsh_lanczos(mv, [], init, num_krylov_vecs=1)
311+
eta1, _ = backend.eigsh_lanczos(mv, [H], init, num_krylov_vecs=1)
312312
np.testing.assert_allclose(eta1[0], 5)
313313

314314

@@ -321,10 +321,10 @@ def test_eigsh_lanczos_1(dtype):
321321
tmp = backend.randn((D, D), dtype=dtype, seed=10)
322322
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
323323

324-
def mv(x):
325-
return np.dot(H, x)
324+
def mv(x, mat):
325+
return np.dot(mat, x)
326326

327-
eta1, U1 = backend.eigsh_lanczos(mv, [], init)
327+
eta1, U1 = backend.eigsh_lanczos(mv, [H], init)
328328
eta2, U2 = np.linalg.eigh(H)
329329
v2 = U2[:, 0]
330330
v2 = v2 / sum(v2)
@@ -342,10 +342,10 @@ def test_eigsh_lanczos_2(dtype):
342342
tmp = backend.randn((D, D), dtype=dtype, seed=10)
343343
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
344344

345-
def mv(x):
346-
return np.dot(H, x)
345+
def mv(x, mat):
346+
return np.dot(mat, x)
347347

348-
eta1, U1 = backend.eigsh_lanczos(mv, [], shape=(D,), dtype=dtype)
348+
eta1, U1 = backend.eigsh_lanczos(mv, [H], shape=(D,), dtype=dtype)
349349
eta2, U2 = np.linalg.eigh(H)
350350
v2 = U2[:, 0]
351351
v2 = v2 / sum(v2)
@@ -364,11 +364,11 @@ def test_eigsh_lanczos_reorthogonalize(dtype, numeig):
364364
tmp = backend.randn((D, D), dtype=dtype, seed=10)
365365
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
366366

367-
def mv(x):
368-
return np.dot(H, x)
367+
def mv(x, mat):
368+
return np.dot(mat, x)
369369

370370
eta1, U1 = backend.eigsh_lanczos(
371-
mv, [],
371+
mv, [H],
372372
shape=(D,),
373373
dtype=dtype,
374374
numeig=numeig,

tensornetwork/backends/pytorch/pytorch_backend.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ def eigsh_lanczos(self,
187187
of a `LinearOperator` `A`.
188188
Args:
189189
A: A (sparse) implementation of a linear operator.
190-
Call signature of `A` is `res = A(*args, vector)`, where `vector`
190+
Call signature of `A` is `res = A(vector, *args)`, where `vector`
191191
can be an arbitrary `Tensor`, and `res.shape` has to be `vector.shape`.
192192
arsg: A list of arguments to `A`. `A` will be called as
193-
`res = A(*args, initial_state)`.
193+
`res = A(initial_state, *args)`.
194194
initial_state: An initial vector for the Lanczos algorithm. If `None`,
195195
a random initial `Tensor` is created using the `torch.randn` method
196196
shape: The shape of the input-dimension of `A`.
@@ -257,7 +257,7 @@ def eigsh_lanczos(self,
257257
vector_n -= (v.view(-1).dot(vector_n.view(-1))) * torchlib.reshape(
258258
v, vector_n.shape)
259259
krylov_vecs.append(vector_n)
260-
A_vector_n = A(*args, vector_n)
260+
A_vector_n = A(vector_n, *args)
261261
diag_elements.append(vector_n.view(-1).dot(A_vector_n.view(-1)))
262262

263263
if ((it > 0) and (it % ndiag) == 0) and (len(diag_elements) >= numeig):

tensornetwork/backends/pytorch/pytorch_backend_test.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ def test_eigsh_lanczos_1():
259259
tmp = backend.randn((D, D), dtype=dtype)
260260
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
261261

262-
def mv(x):
263-
return H.mv(x)
262+
def mv(x, mat):
263+
return mat.mv(x)
264264

265-
eta1, U1 = backend.eigsh_lanczos(mv, [], init, num_krylov_vecs=D)
265+
eta1, U1 = backend.eigsh_lanczos(mv, [H], init, num_krylov_vecs=D)
266266
eta2, U2 = H.symeig(eigenvectors=True)
267267
v2 = U2[:, 0]
268268
v2 = v2 / sum(v2)
@@ -277,10 +277,10 @@ def test_eigsh_small_number_krylov_vectors():
277277
init = backend.convert_to_tensor(np.array([1, 1], dtype=np.float64))
278278
H = backend.convert_to_tensor(np.array([[1, 2], [3, 4]], dtype=np.float64))
279279

280-
def mv(x):
281-
return H.mv(x)
280+
def mv(x, mat):
281+
return mat.mv(x)
282282

283-
eta1, _ = backend.eigsh_lanczos(mv, [], init, num_krylov_vecs=1)
283+
eta1, _ = backend.eigsh_lanczos(mv, [H], init, num_krylov_vecs=1)
284284
np.testing.assert_allclose(eta1[0], 5)
285285

286286

@@ -293,11 +293,11 @@ def test_eigsh_lanczos_reorthogonalize(numeig):
293293
tmp = backend.randn((D, D), dtype=dtype, seed=10)
294294
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
295295

296-
def mv(x):
297-
return H.mv(x)
296+
def mv(x, mat):
297+
return mat.mv(x)
298298

299299
eta1, U1 = backend.eigsh_lanczos(
300-
mv, [],
300+
mv, [H],
301301
shape=(D,),
302302
dtype=dtype,
303303
numeig=numeig,
@@ -325,11 +325,11 @@ def test_eigsh_lanczos_2():
325325
tmp = backend.randn((D, D), dtype=dtype)
326326
H = tmp + backend.transpose(backend.conj(tmp), (1, 0))
327327

328-
def mv(x):
329-
return H.mv(x)
328+
def mv(x, mat):
329+
return mat.mv(x)
330330

331331
eta1, U1 = backend.eigsh_lanczos(
332-
mv, [],
332+
mv, [H],
333333
shape=(D,),
334334
dtype=dtype,
335335
reorthogonalize=True,

tensornetwork/matrixproductstates/dmrg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def __init__(self, mps: BaseMPS, mpo: BaseMPO, left_boundary: Tensor,
7474
'right_boundary.dtype = {} is different from BaseDMRG.dtype = {}'
7575
.format(self.right_envs[0].dtype, self.dtype))
7676

77-
def _single_site_matvec(L, mpotensor, R, mpstensor):
77+
def _single_site_matvec(mpstensor, L, mpotensor, R):
7878
return ncon([L, mpstensor, mpotensor, R],
7979
[[3, 1, -1], [1, 2, 4], [3, 5, -2, 2], [5, 4, -3]],
8080
backend=self.backend.name)

0 commit comments

Comments
 (0)