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

Commit 6e77a2b

Browse files
committed
Add fallback to gesvd if gesdd LAPACK fails in numpy backend
1 parent e12580f commit 6e77a2b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

tensornetwork/backends/numpy/decompositions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
"""Tensor Decomposition Numpy Implementation."""
1515

1616
from typing import Optional, Any, Tuple
17+
import warnings
18+
1719
import numpy
20+
import scipy
21+
1822
Tensor = Any
1923

2024

@@ -33,7 +37,14 @@ def svd(
3337
right_dims = tensor.shape[pivot_axis:]
3438

3539
tensor = np.reshape(tensor, [numpy.prod(left_dims), numpy.prod(right_dims)])
36-
u, s, vh = np.linalg.svd(tensor, full_matrices=False)
40+
try:
41+
u, s, vh = np.linalg.svd(tensor, full_matrices=False)
42+
except np.linalg.LinAlgError:
43+
warnings.warn("NumPy SVD with the fast 'gesdd' LAPACK routine failed. " \
44+
+ "Matrix might be badly conditioned. Employing the SciPy SVD " \
45+
+ "with more stable 'gesvd' LAPACK routine instead.")
46+
u, s, vh = scipy.linalg.svd(
47+
tensor, full_matrices=False, lapack_driver='gesvd')
3748

3849
if max_singular_values is None:
3950
max_singular_values = np.size(s)

0 commit comments

Comments
 (0)