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

Add fallback to gesvd if gedd LAPACK fails in numpy backend #962

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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