def _extract_factors(tens, ndims):
"""Extract iteratively the leftmost MPO tensor with given number of
legs by a qr-decomposition
:param np.ndarray tens: Full tensor to be factorized
:param ndims: Number of physical legs per site or iterator over number of
physical legs
:returns: List of local tensors with given number of legs yielding a
factorization of tens
"""
current = next(ndims) if isinstance(ndims, collections.Iterator) else ndims
if tens.ndim == current + 2:
return [tens]
elif tens.ndim < current + 2:
raise AssertionError("Number of remaining legs insufficient.")
else:
unitary, rest = qr(tens.reshape((np.prod(tens.shape[:current + 1]), -1)))
unitary = unitary.reshape(tens.shape[:current + 1] + rest.shape[:1])
rest = rest.reshape(rest.shape[:1] + tens.shape[current + 1:])
return [unitary] + _extract_factors(rest, ndims)
评论列表
文章目录