Skip to content

tools/tests.py

class Space

Linear-algebra helpers for vector spaces and bipartite decompositions.

gramSchmidt

Gram-Schmidt orthonormalization.

Given vectors {vi}, constructs an orthonormal set {ui} spanning the same subspace (dropping near-zero residuals).

py
def gramSchmidt(vectors: pt.Tensor) -> pt.Tensor [static]
Implementation
python
def gramSchmidt(vectors: pt.Tensor) -> pt.Tensor:
    ortho = []
    for v in vectors:
        w = v - sum((pt.dot(v, u.conj()) * u for u in ortho))
        if pt.linalg.norm(w) > 1e-08:
            ortho.append(w / pt.linalg.norm(w))
    return pt.stack(ortho)

schmidtDecompose

Schmidt decomposition via SVD.

Treats state as a bipartite coefficient matrix Ψ and returns singular triplets (λk,|uk,|vk) with Ψ=kλk|ukvk|.

py
def schmidtDecompose(state: pt.Tensor) -> list [static]
Implementation
python
def schmidtDecompose(state: pt.Tensor) -> list:
    U, S, Vh = pt.linalg.svd(state, full_matrices=True)
    dims = int(min(state.shape))
    return sorted([(S[k], U[:, k], Vh[k]) for k in range(dims)], key=lambda dec: dec[0], reverse=True)

schmidtRank

Schmidt rank (matrix rank) of a bipartite coefficient matrix.

py
def schmidtRank(mat: pt.Tensor) -> int [static]
Implementation
python
def schmidtRank(mat: pt.Tensor) -> int:
    return int(pt.linalg.matrix_rank(mat))

PPT

Peres-Horodecki (PPT) separability test for a bipartite density matrix.

Performs a blockwise partial transpose on subsystem blocks of size sub and checks positivity: ρTB0.

Note: current implementation overrides sub to 3.

py
def PPT(rho: pt.Tensor, sub: int) -> bool [static]
Implementation
python
def PPT(rho: pt.Tensor, sub: int) -> bool:
    side = rho.shape[0]
    sub = 3
    if side % sub != 0:
        raise ValueError(f'Matrix side ({side}) not divisible by sub ({sub})')
    mat0 = rho.clone()
    for i in range(0, mat0.shape[0], sub):
        for j in range(0, mat0.shape[1], sub):
            mat0[i:i + sub, j:j + sub] = mat0[i:i + sub, j:j + sub].T
    return bool(pt.all(pt.linalg.eigvals(mat0).real >= 0))