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

Commit

Permalink
Add fallback to gesvd if gesdd LAPACK fails in numpy backend
Browse files Browse the repository at this point in the history
  • Loading branch information
gefux committed May 10, 2022
1 parent e12580f commit 6e77a2b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion tensornetwork/backends/numpy/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
"""Tensor Decomposition Numpy Implementation."""

from typing import Optional, Any, Tuple
import warnings

import numpy
import scipy

Tensor = Any


Expand All @@ -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)
Expand Down

0 comments on commit 6e77a2b

Please sign in to comment.