From 6e77a2be31dd6ecbcce3a4da7802f76f8183cc61 Mon Sep 17 00:00:00 2001 From: "Gerald E. Fux" Date: Mon, 9 May 2022 22:02:38 +0100 Subject: [PATCH] Add fallback to gesvd if gesdd LAPACK fails in numpy backend --- tensornetwork/backends/numpy/decompositions.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tensornetwork/backends/numpy/decompositions.py b/tensornetwork/backends/numpy/decompositions.py index 29feb63d0..7031ca665 100644 --- a/tensornetwork/backends/numpy/decompositions.py +++ b/tensornetwork/backends/numpy/decompositions.py @@ -14,7 +14,11 @@ """Tensor Decomposition Numpy Implementation.""" from typing import Optional, Any, Tuple +import warnings + import numpy +import scipy + Tensor = Any @@ -33,7 +37,14 @@ def svd( right_dims = tensor.shape[pivot_axis:] tensor = np.reshape(tensor, [numpy.prod(left_dims), numpy.prod(right_dims)]) - u, s, vh = np.linalg.svd(tensor, full_matrices=False) + try: + u, s, vh = np.linalg.svd(tensor, full_matrices=False) + except np.linalg.LinAlgError: + warnings.warn("NumPy SVD with the fast 'gesdd' LAPACK routine failed. " \ + + "Matrix might be badly conditioned. Employing the SciPy SVD " \ + + "with more stable 'gesvd' LAPACK routine instead.") + u, s, vh = scipy.linalg.svd( + tensor, full_matrices=False, lapack_driver='gesvd') if max_singular_values is None: max_singular_values = np.size(s)