Skip to main content

validate_svd_truncation_options

Function validate_svd_truncation_options 

Source
pub fn validate_svd_truncation_options(
    max_bond_dim: Option<usize>,
    policy: Option<SvdTruncationPolicy>,
) -> Result<(), SvdTruncationOptionsError>
Expand description

Validate optional SVD truncation options shared by all callers.

max_bond_dim must be None or at least one, and an explicit SVD policy threshold must be finite and non-negative. Call this at every public truncation, contraction, fit, and adaptive entry point before any mutation or no-op shortcut (empty center, single node, zero sweep, disjoint partition addition, or Naive/Zipup dispatch), so invalid options are rejected with the same typed error on every path.

§Arguments

  • max_bond_dim - Optional maximum retained bond dimension; Some(0) is rejected because a rank cap of zero would silently collapse.
  • policy - Optional explicit SvdTruncationPolicy; when present its threshold must be finite and non-negative.

§Returns

Ok(()) when both optional fields are valid, otherwise the typed error naming the failing field.

§Errors

Returns SvdTruncationOptionsError::ZeroMaxBondDim when max_bond_dim is Some(0), or SvdTruncationOptionsError::InvalidThreshold when the explicit policy threshold is non-finite or negative.

§Examples

use tensor4all_core::{
    validate_svd_truncation_options, SvdTruncationOptionsError,
    SvdTruncationPolicy,
};

assert!(validate_svd_truncation_options(None, None).is_ok());
assert!(validate_svd_truncation_options(
    Some(64),
    Some(SvdTruncationPolicy::new(1e-8)),
)
.is_ok());
assert_eq!(
    validate_svd_truncation_options(Some(0), None),
    Err(SvdTruncationOptionsError::ZeroMaxBondDim)
);
assert!(matches!(
    validate_svd_truncation_options(
        None,
        Some(SvdTruncationPolicy::new(f64::NAN))
    ),
    Err(SvdTruncationOptionsError::InvalidThreshold(_))
));