Skip to content

qten.linalg

Package reference for qten.linalg.

linalg

Linear-algebra routines built on top of QTen tensors.

This package contains decomposition algorithms and tensor-aware numerical helpers that operate on Tensor objects while preserving symbolic dimension metadata.

Decompositions
  • eig General eigendecomposition for square tensor-valued matrices.
  • eigh Hermitian eigendecomposition.
  • eigvals General eigenvalues only.
  • eigvalsh Hermitian eigenvalues only.
  • qr QR factorization.
  • svd Singular value decomposition.
Convenience re-exports

Exported API

eig

eig(tensor: Tensor) -> EigH

Perform eigendecomposition on general square matrix axes.

This function applies torch.linalg.eig to the final two dimensions of a Tensor. The last two dimensions must span the same Hilbert space up to ray ordering so they can be interpreted as a square operator. Any leading dimensions are treated as batch dimensions and are preserved in both outputs.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form square matrices.

required

Returns:

Type Description
EigH

EigH containing: - eigenvalues, whose dtype is the complex dtype associated with the input and whose dims replace the matrix axes with one IndexSpace. - eigenvectors, whose dtype matches that complex dtype and whose dims are the leading batch dims followed by (row_dim, spectrum).

Examples:

result = eig(tensor)
values = result.eigenvalues
vectors = result.eigenvectors
Notes

torch.linalg.eig does not guarantee any ordering of the eigenvalues. This function sorts eigenvalues lexicographically by (real, imag) and applies the same reordering to eigenvectors.

The returned tensors satisfy \(A V = V\Lambda\), where \(\Lambda\) is the diagonal matrix of eigenvalues. If the input matrix is diagonalizable, this gives the reconstruction \(A = V\Lambda V^{-1}\). In code, \(V\) is eigenvectors and \(\Lambda\) is the diagonal matrix built from eigenvalues.

Source code in src/qten/linalg/decompose.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def eig(tensor: Tensor) -> EigH:
    r"""
    Perform eigendecomposition on general square matrix axes.

    This function applies [`torch.linalg.eig`](https://pytorch.org/docs/stable/generated/torch.linalg.eig.html)
    to the final two dimensions of a
    [`Tensor`][qten.linalg.tensors.Tensor]. The last two dimensions must span
    the same Hilbert space up to ray ordering so they can be interpreted as a
    square operator. Any leading dimensions are treated as batch dimensions and
    are preserved in both outputs.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form square matrices.

    Returns
    -------
    EigH
        [`EigH`][qten.linalg.decompose.EigH] containing:
        - `eigenvalues`, whose dtype is the complex dtype associated with the
          input and whose dims replace the matrix axes with one
          [`IndexSpace`][qten.symbolics.state_space.IndexSpace].
        - `eigenvectors`, whose dtype matches that complex dtype and whose dims
          are the leading batch dims followed by `(row_dim, spectrum)`.

    Examples
    --------
    ```python
    result = eig(tensor)
    values = result.eigenvalues
    vectors = result.eigenvectors
    ```

    Notes
    -----
    `torch.linalg.eig` does not guarantee any ordering of the eigenvalues. This
    function sorts eigenvalues lexicographically by `(real, imag)` and applies
    the same reordering to eigenvectors.

    The returned tensors satisfy \(A V = V\Lambda\), where \(\Lambda\) is the
    diagonal matrix of eigenvalues. If the input matrix is diagonalizable, this
    gives the reconstruction \(A = V\Lambda V^{-1}\). In code, \(V\) is
    `eigenvectors` and \(\Lambda\) is the diagonal matrix built from
    `eigenvalues`.
    """
    _assert_eig_dims(tensor)

    dim0 = tensor.dims[-2]
    target = tensor.align(-1, dim0)  # Align column space to match the row space
    eigenvalues, eigenvectors = torch.linalg.eig(target.data)
    eigenvalues, eigenvectors = _sort_eigenpairs(eigenvalues, eigenvectors)

    spectrum = IndexSpace.linear(eigenvalues.shape[-1])

    eigvals = Tensor(
        data=eigenvalues,
        dims=target.dims[:-2] + (spectrum,),
    )
    eigvecs = Tensor(
        data=eigenvectors,
        dims=target.dims[:-2] + (dim0, spectrum),
    )

    return EigH(eigvals, eigvecs)

eigh

eigh(tensor: Tensor) -> EigH

Perform Hermitian eigendecomposition on the last two tensor dimensions.

This function applies torch.linalg.eigh to the matrix axes of a Tensor. The final two dimensions must span the same Hilbert space up to ray ordering so they can be interpreted as a Hermitian operator. Any leading dimensions are treated as batch dimensions and are preserved in both outputs.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form Hermitian matrices.

required

Returns:

Type Description
EigH

EigH containing: - eigenvalues, whose dtype is the real dtype associated with the input and whose dims replace the matrix axes with one IndexSpace. - eigenvectors, whose dtype matches the input dtype and whose dims are the leading batch dims followed by (row_dim, spectrum).

Examples:

result = eigh(tensor)
eigenvalues = result.eigenvalues
eigenvectors = result.eigenvectors
Notes

torch.linalg.eigh is differentiable for Hermitian inputs, but the gradients can be ill-defined or unstable when eigenvalues are degenerate or nearly degenerate. If you use this in autograd, consider stabilizing the spectrum (e.g., with a small perturbation) or avoiding backpropagation through eigenvectors when bands are expected to merge.

The original matrix is recovered by forming a diagonal matrix from eigenvalues and evaluating \(V\Lambda V^\dagger\). In code, this is eigenvectors @ W @ eigenvectors.h(-2, -1).

Source code in src/qten/linalg/decompose.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def eigh(tensor: Tensor) -> EigH:
    r"""
    Perform Hermitian eigendecomposition on the last two tensor dimensions.

    This function applies [`torch.linalg.eigh`](https://pytorch.org/docs/stable/generated/torch.linalg.eigh.html)
    to the matrix axes of a [`Tensor`][qten.linalg.tensors.Tensor]. The final
    two dimensions must span the same Hilbert space up to ray ordering so they
    can be interpreted as a Hermitian operator. Any leading dimensions are
    treated as batch dimensions and are preserved in both outputs.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form Hermitian matrices.

    Returns
    -------
    EigH
        [`EigH`][qten.linalg.decompose.EigH] containing:
        - `eigenvalues`, whose dtype is the real dtype associated with the
          input and whose dims replace the matrix axes with one
          [`IndexSpace`][qten.symbolics.state_space.IndexSpace].
        - `eigenvectors`, whose dtype matches the input dtype and whose dims
          are the leading batch dims followed by `(row_dim, spectrum)`.

    Examples
    --------
    ```python
    result = eigh(tensor)
    eigenvalues = result.eigenvalues
    eigenvectors = result.eigenvectors
    ```

    Notes
    -----
    `torch.linalg.eigh` is differentiable for Hermitian inputs, but the gradients
    can be ill-defined or unstable when eigenvalues are degenerate or nearly
    degenerate. If you use this in autograd, consider stabilizing the spectrum
    (e.g., with a small perturbation) or avoiding backpropagation through
    eigenvectors when bands are expected to merge.

    The original matrix is recovered by forming a diagonal matrix from
    `eigenvalues` and evaluating \(V\Lambda V^\dagger\). In code, this is
    `eigenvectors @ W @ eigenvectors.h(-2, -1)`.
    """
    _assert_eig_dims(tensor)

    dim0 = tensor.dims[-2]
    target = tensor.align(-1, dim0)  # Align column space to match the row space
    eigenvalues, eigenvectors = torch.linalg.eigh(target.data)

    spectrum = IndexSpace.linear(eigenvalues.shape[-1])

    eigvals = Tensor(
        data=eigenvalues,
        dims=target.dims[:-2] + (spectrum,),
    )
    eigvecs = Tensor(
        data=eigenvectors,
        dims=target.dims[:-2] + (dim0, spectrum),
    )

    return EigH(eigvals, eigvecs)

eigvalsh

eigvalsh(tensor: Tensor) -> Tensor

Compute Hermitian eigenvalues on the last two tensor dimensions.

This is the eigenvalues-only companion to eigh. The last two dimensions must span the same Hilbert space up to ray ordering and represent a Hermitian operator. Leading dimensions are treated as batch dimensions.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form Hermitian matrices.

required

Returns:

Type Description
Tensor

Eigenvalues as a Tensor whose dtype matches the real dtype associated with the input and whose dims keep the leading batch dimensions while replacing the matrix axes with a single IndexSpace.

Examples:

values = eigvalsh(tensor)
Source code in src/qten/linalg/decompose.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def eigvalsh(tensor: Tensor) -> Tensor:
    """
    Compute Hermitian eigenvalues on the last two tensor dimensions.

    This is the eigenvalues-only companion to
    [`eigh`][qten.linalg.decompose.eigh]. The last two dimensions must span
    the same Hilbert space up to ray ordering and represent a Hermitian
    operator. Leading dimensions are treated as batch dimensions.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form Hermitian matrices.

    Returns
    -------
    Tensor
        Eigenvalues as a [`Tensor`][qten.linalg.tensors.Tensor] whose dtype
        matches the real dtype associated with the input and whose dims keep
        the leading batch dimensions while replacing the matrix axes with a
        single [`IndexSpace`][qten.symbolics.state_space.IndexSpace].

    Examples
    --------
    ```python
    values = eigvalsh(tensor)
    ```
    """
    _assert_eig_dims(tensor)

    dim0 = tensor.dims[-2]
    target = tensor.align(-1, dim0)  # Align column space to match the row space
    eigenvalues = torch.linalg.eigvalsh(target.data)

    spectrum = IndexSpace.linear(eigenvalues.shape[-1])

    vals = Tensor(
        data=eigenvalues,
        dims=target.dims[:-2] + (spectrum,),
    )

    return vals

eigvals

eigvals(tensor: Tensor) -> Tensor

Compute eigenvalues of general square matrix axes.

This is the eigenvalues-only companion to eig. The last two dimensions must span the same Hilbert space up to ray ordering and represent a square operator. Leading dimensions are treated as batch dimensions.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form square matrices.

required

Returns:

Type Description
Tensor

Eigenvalues as a Tensor whose dtype matches the complex dtype associated with the input and whose dims keep the leading batch dimensions while replacing the matrix axes with a single IndexSpace.

Notes

torch.linalg.eigvals does not guarantee any ordering of the eigenvalues. This function sorts eigenvalues lexicographically by (real, imag).

Examples:

values = eigvals(tensor)
Source code in src/qten/linalg/decompose.py
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
def eigvals(tensor: Tensor) -> Tensor:
    """
    Compute eigenvalues of general square matrix axes.

    This is the eigenvalues-only companion to
    [`eig`][qten.linalg.decompose.eig]. The last two dimensions must span the
    same Hilbert space up to ray ordering and represent a square operator.
    Leading dimensions are treated as batch dimensions.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form square matrices.

    Returns
    -------
    Tensor
        Eigenvalues as a [`Tensor`][qten.linalg.tensors.Tensor] whose dtype
        matches the complex dtype associated with the input and whose dims keep
        the leading batch dimensions while replacing the matrix axes with a
        single [`IndexSpace`][qten.symbolics.state_space.IndexSpace].

    Notes
    -----
    `torch.linalg.eigvals` does not guarantee any ordering of the eigenvalues.
    This function sorts eigenvalues lexicographically by `(real, imag)`.

    Examples
    --------
    ```python
    values = eigvals(tensor)
    ```
    """
    _assert_eig_dims(tensor)

    dim0 = tensor.dims[-2]
    target = tensor.align(-1, dim0)  # Align column space to match the row space
    eigenvalues = torch.linalg.eigvals(target.data)
    eigenvalues, _ = _sort_eigenpairs(eigenvalues)

    spectrum = IndexSpace.linear(eigenvalues.shape[-1])

    vals = Tensor(
        data=eigenvalues,
        dims=target.dims[:-2] + (spectrum,),
    )

    return vals

qr

qr(tensor: Tensor) -> QR

Perform reduced QR decomposition on the last two tensor dimensions.

This function applies torch.linalg.qr with mode="reduced" to the matrix axes of the input tensor. The last two dimensions may be rectangular. Any leading dimensions are treated as batch dimensions and are preserved in both outputs.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form matrices.

required

Returns:

Type Description
QR

QR containing: - Q, a Tensor with orthonormal columns and dims (..., row_dim, factor). - R, an upper-triangular Tensor with dims (..., factor, col_dim).

Examples:

result = qr(tensor)
q = result.Q
r = result.R
Notes

The shared factor axis is represented by an IndexSpace whose size equals the reduced QR bond dimension. The original matrix is recovered as \(Q R\), via Q @ R in code.

Source code in src/qten/linalg/decompose.py
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
def qr(tensor: Tensor) -> QR:
    r"""
    Perform reduced QR decomposition on the last two tensor dimensions.

    This function applies [`torch.linalg.qr`](https://pytorch.org/docs/stable/generated/torch.linalg.qr.html)
    with `mode="reduced"` to the matrix axes of the input tensor. The last two
    dimensions may be rectangular. Any leading dimensions are treated as batch
    dimensions and are preserved in both outputs.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form matrices.

    Returns
    -------
    QR
        [`QR`][qten.linalg.decompose.QR] containing:
        - `Q`, a [`Tensor`][qten.linalg.tensors.Tensor] with orthonormal
          columns and dims `(..., row_dim, factor)`.
        - `R`, an upper-triangular
          [`Tensor`][qten.linalg.tensors.Tensor] with dims
          `(..., factor, col_dim)`.

    Examples
    --------
    ```python
    result = qr(tensor)
    q = result.Q
    r = result.R
    ```

    Notes
    -----
    The shared `factor` axis is represented by an
    [`IndexSpace`][qten.symbolics.state_space.IndexSpace] whose size equals the
    reduced QR bond dimension. The original matrix is recovered as \(Q R\), via
    `Q @ R` in code.
    """
    if tensor.rank() < 2:
        raise ValueError(
            "Input tensor must have at least two dimensions for matrix decomposition."
        )

    row_dim = tensor.dims[-2]
    col_dim = tensor.dims[-1]

    q_data, r_data = torch.linalg.qr(tensor.data, mode="reduced")
    spectral_dim = IndexSpace.linear(q_data.shape[-1])

    q = Tensor(
        data=q_data,
        dims=tensor.dims[:-2] + (row_dim, spectral_dim),
    )
    r = Tensor(
        data=r_data,
        dims=tensor.dims[:-2] + (spectral_dim, col_dim),
    )

    return QR(q, r)

svd

svd(
    tensor: Tensor,
    values_as_matrix: bool = False,
    full_matrices: bool = False,
) -> SVD

Perform singular value decomposition on the last two tensor dimensions.

This function applies torch.linalg.svd to the matrix axes of the input tensor and returns symbolic dimensions that distinguish reduced and full factorizations. The last two dimensions may be rectangular. Any leading dimensions are treated as batch dimensions and are preserved in all outputs.

Parameters:

Name Type Description Default
tensor Tensor

Input tensor whose last two dimensions form matrices.

required
values_as_matrix bool

If True, return singular values as an explicit diagonal matrix tensor. If False, return them as a vector on a single spectral axis.

`False`
full_matrices bool

If True, compute full-sized U and Vh. If False, compute the reduced SVD.

`False`

Returns:

Type Description
SVD

SVD containing: - U, with dims (..., row_dim, factor) for reduced SVD or (..., row_dim, left_factor) for full SVD. - S, with dims (..., factor) by default, (..., factor, factor) when values_as_matrix=True in reduced mode, or (..., left_factor, right_factor) in full matrix form. - Vh, with dims (..., factor, col_dim) for reduced SVD or (..., right_factor, col_dim) for full SVD.

Examples:

result = svd(tensor)
u = result.U
s = result.S
vh = result.Vh
Notes

In reduced mode, factor is the shared singular-value IndexSpace. In full mode, left_factor and right_factor are sized to the full row and column spaces of the input matrix axes. The original matrix is recovered as \(U\Sigma V^\dagger\), using U @ Sigma @ Vh in code. Here Sigma is either the returned S tensor (values_as_matrix=True) or the diagonal matrix formed from the returned singular-value vector.

Source code in src/qten/linalg/decompose.py
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
def svd(
    tensor: Tensor,
    values_as_matrix: bool = False,
    full_matrices: bool = False,
) -> SVD:
    r"""
    Perform singular value decomposition on the last two tensor dimensions.

    This function applies [`torch.linalg.svd`](https://pytorch.org/docs/stable/generated/torch.linalg.svd.html)
    to the matrix axes of the input tensor and returns symbolic dimensions that
    distinguish reduced and full factorizations. The last two dimensions may be
    rectangular. Any leading dimensions are treated as batch dimensions and are
    preserved in all outputs.

    Parameters
    ----------
    tensor : Tensor
        Input tensor whose last two dimensions form matrices.
    values_as_matrix : bool, default `False`
        If `True`, return singular values as an explicit diagonal matrix
        tensor. If `False`, return them as a vector on a single spectral axis.
    full_matrices : bool, default `False`
        If `True`, compute full-sized `U` and `Vh`. If `False`, compute the
        reduced SVD.

    Returns
    -------
    SVD
        [`SVD`][qten.linalg.decompose.SVD] containing:
        - `U`, with dims `(..., row_dim, factor)` for reduced SVD or
          `(..., row_dim, left_factor)` for full SVD.
        - `S`, with dims `(..., factor)` by default, `(..., factor, factor)`
          when `values_as_matrix=True` in reduced mode, or
          `(..., left_factor, right_factor)` in full matrix form.
        - `Vh`, with dims `(..., factor, col_dim)` for reduced SVD or
          `(..., right_factor, col_dim)` for full SVD.

    Examples
    --------
    ```python
    result = svd(tensor)
    u = result.U
    s = result.S
    vh = result.Vh
    ```

    Notes
    -----
    In reduced mode, `factor` is the shared singular-value
    [`IndexSpace`][qten.symbolics.state_space.IndexSpace]. In full mode,
    `left_factor` and `right_factor` are sized to the full row and column
    spaces of the input matrix axes. The original matrix is recovered as
    \(U\Sigma V^\dagger\), using `U @ Sigma @ Vh` in code. Here `Sigma` is
    either the returned `S` tensor (`values_as_matrix=True`) or the diagonal
    matrix formed from the returned singular-value vector.
    """
    if tensor.rank() < 2:
        raise ValueError(
            "Input tensor must have at least two dimensions for matrix decomposition."
        )

    row_dim = tensor.dims[-2]
    col_dim = tensor.dims[-1]

    u_data, s_data, vh_data = torch.linalg.svd(tensor.data, full_matrices=full_matrices)

    factor = IndexSpace.linear(s_data.shape[-1])

    if full_matrices:
        left_factor = IndexSpace.linear(row_dim.dim)
        right_factor = IndexSpace.linear(col_dim.dim)
        u = Tensor(
            data=u_data,
            dims=tensor.dims[:-2] + (row_dim, left_factor),
        )
    else:
        u = Tensor(
            data=u_data,
            dims=tensor.dims[:-2] + (row_dim, factor),
        )
    if values_as_matrix:
        if full_matrices:
            k = s_data.shape[-1]
            s_mat = torch.zeros(
                *s_data.shape[:-1],
                left_factor.dim,
                right_factor.dim,
                dtype=s_data.dtype,
                device=s_data.device,
            )
            diag = torch.diag_embed(s_data)
            s_mat[..., :k, :k] = diag
            s = Tensor(
                data=s_mat,
                dims=tensor.dims[:-2] + (left_factor, right_factor),
            )
        else:
            s_mat = torch.diag_embed(s_data)
            s = Tensor(
                data=s_mat,
                dims=tensor.dims[:-2] + (factor, factor),
            )
    else:
        s = Tensor(
            data=s_data,
            dims=tensor.dims[:-2] + (factor,),
        )
    if full_matrices:
        vh = Tensor(
            data=vh_data,
            dims=tensor.dims[:-2] + (right_factor, col_dim),
        )
    else:
        vh = Tensor(
            data=vh_data,
            dims=tensor.dims[:-2] + (factor, col_dim),
        )

    return SVD(u, s, vh)

einsum

einsum(equation: str, *operands: Tensor) -> Tensor

Contract tensors with Einstein summation while aligning symbolic dims.

This mirrors torch.einsum at the numeric level, but first reconciles every labeled axis through QTen's StateSpace semantics:

  • same labeled axes must be symbolically compatible,
  • BroadcastSpace() may expand to a concrete labeled space,
  • axes with the same rays but different ordering are permuted into a shared target ordering before contraction.
Equation guide

The equation uses the same label syntax as torch.einsum, with labels restricted to ASCII letters [A-Za-z].

  • Each input operand is described by one comma-separated label term.
  • Labels that appear in multiple operands identify axes that should be multiplied together.
  • Labels omitted from the output are summed out.
  • Labels kept in the output determine the order of the output dims.
  • ... is supported and follows torch's broadcast convention for unnamed axes. It may appear at the start, middle, or end of a term.
  • ...ij means "match the last two labeled axes and let ... absorb the preceding axes"; i...j means "match the first labeled axis, the last labeled axis, and let ... absorb the axes in between".
  • If any input term uses ..., QTen normalizes any input term that omits it by prepending a leading ellipsis in the internal torch.einsum equation. This lets mixed equations such as "...ij,ij->...ij" behave like torch-style broadcasted contractions.
  • When a term originally omits ..., QTen pads that operand with leading singleton broadcast axes (via unsqueeze(0)) and uses the leading-ellipsis form in the normalized torch.einsum equation. For example, "i...j,ij->i...j" is internally reconciled by treating the ij operand as a leading-ellipsis term rather than inserting singleton axes into the middle of that operand.
  • QTen only normalizes input terms. The explicit output term, if present, is preserved as written.
  • Repeating a label within one operand follows diagonal semantics. Those axes must already have matching sizes before any cross-operand alignment, and QTen does not expand a singleton BroadcastSpace() axis just to satisfy a repeated subscript within the same operand.

For example:

  • "ij,ij->ij" means elementwise multiplication.
  • "ij,jk->ik" means matrix multiplication over the shared j axis.
  • "abc,dbe->acde" means multiply over the shared b axis and keep the surviving axes in the explicit output order (a, c, d, e).
  • "...ij,ij->...ij" means "broadcast the ij operand across the leading unnamed axes of the other operand, then keep those axes in the output".
  • "i...j,ij->i...j" means "broadcast the ij operand across the unnamed middle axes between i and j".
Symbolic alignment rules

Before dispatching to torch.einsum, QTen aligns axes label-by-label:

  • if two axes with the same label already share the same StateSpace, nothing changes,
  • if they have the same rays in a different order, the operand is permuted to the first compatible non-BroadcastSpace() ordering encountered for that label,
  • if one side is BroadcastSpace(), it is expanded to the concrete shared space,
  • if two same-labeled concrete axes are not symbolically compatible, the call raises ValueError.

When multiple same-ray orderings are possible, swapping operand order can therefore change the output dims ordering for shared labels.

This means einsum compatibility is stricter than raw shape-only tensor math: matching sizes alone are not enough when a label represents a symbolic axis.

Examples:

Elementwise product with broadcast:

left = Tensor(data=torch.randn(2, 1), dims=(row_space, BroadcastSpace()))
right = Tensor(data=torch.randn(1, 3), dims=(BroadcastSpace(), col_space))

out = einsum("ij,ij->ij", left, right)
# out.dims == (row_space, col_space)

Matrix multiplication:

left = Tensor(data=torch.randn(m.dim, k.dim), dims=(m, k))
right = Tensor(data=torch.randn(k.dim, n.dim), dims=(k, n))

out = einsum("ij,jk->ik", left, right)
# out.dims == (m, n)

Mixed leading ellipsis:

batch = IndexSpace.linear(5)
i = IndexSpace.linear(2)
j = IndexSpace.linear(3)

left = Tensor(data=torch.randn(batch.dim, i.dim, j.dim), dims=(batch, i, j))
right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

out = einsum("...ij,ij->...ij", left, right)
# Equivalent numeric contraction: torch.einsum("...ij,...ij->...ij", ...)
# out.dims == (batch, i, j)

Mixed middle ellipsis:

i = IndexSpace.linear(2)
middle = IndexSpace.linear(4)
j = IndexSpace.linear(3)

left = Tensor(data=torch.randn(i.dim, middle.dim, j.dim), dims=(i, middle, j))
right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

out = einsum("i...j,ij->i...j", left, right)
# Equivalent numeric contraction: torch.einsum("i...j,i...j->i...j", ...)
# out.dims == (i, middle, j)

Mixed trailing ellipsis:

i = IndexSpace.linear(2)
j = IndexSpace.linear(3)
tail = IndexSpace.linear(4)

left = Tensor(data=torch.randn(i.dim, j.dim, tail.dim), dims=(i, j, tail))
right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

out = einsum("ij...,ij->ij...", left, right)
# Equivalent numeric contraction: torch.einsum("ij...,ij...->ij...", ...)
# out.dims == (i, j, tail)

Repeated labels within one operand:

tensor = Tensor(
    data=torch.randn(space_ab.dim, space_ba.dim),
    dims=(space_ab, space_ba),
)

out = einsum("ii->i", tensor)
# The second axis is aligned to the first before taking the diagonal.
# out.dims == (space_ab,)

Repeated labels do not use BroadcastSpace expansion:

tensor = Tensor(
    data=torch.randn(1, shared.dim),
    dims=(BroadcastSpace(), shared),
)

# Raises ValueError because repeated-label axes must already match in size.
out = einsum("ii->i", tensor)

Higher-rank contraction over one shared index:

out = einsum("abc,dbe->acde", x, y)

This computes \(out[a, c, d, e] = \sum_b x[a, b, c] \; y[d, b, e]\).

Higher-rank contraction over multiple shared indices:

out = einsum("abcd,bcde->ae", x, y)

This sums over the shared b, c, and d labels and keeps only the surviving a and e axes in the output.

Parameters:

Name Type Description Default
equation str

Einstein summation equation in the same format accepted by torch.einsum. Label characters must be ASCII letters [A-Za-z].

required
*operands Tensor

Input tensors whose ranks must match the equation terms after any ... expansion.

()

Returns:

Type Description
Tensor

Tensor whose data is computed by torch.einsum on the aligned operand data and whose dims follow the einsum output labels.

Raises:

Type Description
ValueError

If the equation uses unsupported label characters, does not match the operand ranks, or if any shared label maps to incompatible symbolic dimensions.

See Also

matmul Specialized contraction helper for standard matrix multiplication. align Axis-alignment primitive used to reconcile same-labeled symbolic dims.

Source code in src/qten/linalg/tensors.py
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
def einsum(equation: str, *operands: Tensor) -> Tensor:
    r"""
    Contract tensors with Einstein summation while aligning symbolic dims.

    This mirrors `torch.einsum` at the numeric level, but first reconciles
    every labeled axis through QTen's [`StateSpace`][qten.symbolics.state_space.StateSpace]
    semantics:

    - same labeled axes must be symbolically compatible,
    - [`BroadcastSpace()`][qten.symbolics.state_space.BroadcastSpace] may
      expand to a concrete labeled space,
    - axes with the same rays but different ordering are permuted into a
      shared target ordering before contraction.

    Equation guide
    --------------
    The equation uses the same label syntax as `torch.einsum`, with labels
    restricted to ASCII letters `[A-Za-z]`.

    - Each input operand is described by one comma-separated label term.
    - Labels that appear in multiple operands identify axes that should be
      multiplied together.
    - Labels omitted from the output are summed out.
    - Labels kept in the output determine the order of the output `dims`.
    - `...` is supported and follows torch's broadcast convention for unnamed
      axes. It may appear at the start, middle, or end of a term.
    - `...ij` means "match the last two labeled axes and let `...` absorb the
      preceding axes"; `i...j` means "match the first labeled axis, the last
      labeled axis, and let `...` absorb the axes in between".
    - If any input term uses `...`, QTen normalizes any input term that omits
      it by prepending a leading ellipsis in the internal `torch.einsum`
      equation. This lets mixed equations such as `"...ij,ij->...ij"` behave
      like torch-style broadcasted contractions.
    - When a term originally omits `...`, QTen pads that operand with leading
      singleton broadcast axes (via `unsqueeze(0)`) and uses the leading-ellipsis
      form in the normalized `torch.einsum` equation. For example,
      `"i...j,ij->i...j"` is internally reconciled by treating the `ij` operand
      as a leading-ellipsis term rather than inserting singleton axes into the
      middle of that operand.
    - QTen only normalizes input terms. The explicit output term, if present,
      is preserved as written.
    - Repeating a label within one operand follows diagonal semantics. Those
      axes must already have matching sizes before any cross-operand
      alignment, and QTen does not expand a singleton
      [`BroadcastSpace()`][qten.symbolics.state_space.BroadcastSpace] axis just
      to satisfy a repeated subscript within the same operand.

    For example:

    - `"ij,ij->ij"` means elementwise multiplication.
    - `"ij,jk->ik"` means matrix multiplication over the shared `j` axis.
    - `"abc,dbe->acde"` means multiply over the shared `b` axis and keep the
      surviving axes in the explicit output order `(a, c, d, e)`.
    - `"...ij,ij->...ij"` means "broadcast the `ij` operand across the leading
      unnamed axes of the other operand, then keep those axes in the output".
    - `"i...j,ij->i...j"` means "broadcast the `ij` operand across the unnamed
      middle axes between `i` and `j`".

    Symbolic alignment rules
    ------------------------
    Before dispatching to `torch.einsum`, QTen aligns axes label-by-label:

    - if two axes with the same label already share the same
      [`StateSpace`][qten.symbolics.state_space.StateSpace], nothing changes,
    - if they have the same rays in a different order, the operand is
      permuted to the first compatible non-[`BroadcastSpace()`][qten.symbolics.state_space.BroadcastSpace]
      ordering encountered for that label,
    - if one side is [`BroadcastSpace()`][qten.symbolics.state_space.BroadcastSpace],
      it is expanded to the concrete shared space,
    - if two same-labeled concrete axes are not symbolically compatible, the
      call raises `ValueError`.

    When multiple same-ray orderings are possible, swapping operand order can
    therefore change the output `dims` ordering for shared labels.

    This means einsum compatibility is stricter than raw shape-only tensor
    math: matching sizes alone are not enough when a label represents a
    symbolic axis.

    Examples
    --------
    Elementwise product with broadcast:

    ```python
    left = Tensor(data=torch.randn(2, 1), dims=(row_space, BroadcastSpace()))
    right = Tensor(data=torch.randn(1, 3), dims=(BroadcastSpace(), col_space))

    out = einsum("ij,ij->ij", left, right)
    # out.dims == (row_space, col_space)
    ```

    Matrix multiplication:

    ```python
    left = Tensor(data=torch.randn(m.dim, k.dim), dims=(m, k))
    right = Tensor(data=torch.randn(k.dim, n.dim), dims=(k, n))

    out = einsum("ij,jk->ik", left, right)
    # out.dims == (m, n)
    ```

    Mixed leading ellipsis:

    ```python
    batch = IndexSpace.linear(5)
    i = IndexSpace.linear(2)
    j = IndexSpace.linear(3)

    left = Tensor(data=torch.randn(batch.dim, i.dim, j.dim), dims=(batch, i, j))
    right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

    out = einsum("...ij,ij->...ij", left, right)
    # Equivalent numeric contraction: torch.einsum("...ij,...ij->...ij", ...)
    # out.dims == (batch, i, j)
    ```

    Mixed middle ellipsis:

    ```python
    i = IndexSpace.linear(2)
    middle = IndexSpace.linear(4)
    j = IndexSpace.linear(3)

    left = Tensor(data=torch.randn(i.dim, middle.dim, j.dim), dims=(i, middle, j))
    right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

    out = einsum("i...j,ij->i...j", left, right)
    # Equivalent numeric contraction: torch.einsum("i...j,i...j->i...j", ...)
    # out.dims == (i, middle, j)
    ```

    Mixed trailing ellipsis:

    ```python
    i = IndexSpace.linear(2)
    j = IndexSpace.linear(3)
    tail = IndexSpace.linear(4)

    left = Tensor(data=torch.randn(i.dim, j.dim, tail.dim), dims=(i, j, tail))
    right = Tensor(data=torch.randn(i.dim, j.dim), dims=(i, j))

    out = einsum("ij...,ij->ij...", left, right)
    # Equivalent numeric contraction: torch.einsum("ij...,ij...->ij...", ...)
    # out.dims == (i, j, tail)
    ```

    Repeated labels within one operand:

    ```python
    tensor = Tensor(
        data=torch.randn(space_ab.dim, space_ba.dim),
        dims=(space_ab, space_ba),
    )

    out = einsum("ii->i", tensor)
    # The second axis is aligned to the first before taking the diagonal.
    # out.dims == (space_ab,)
    ```

    Repeated labels do not use BroadcastSpace expansion:

    ```python
    tensor = Tensor(
        data=torch.randn(1, shared.dim),
        dims=(BroadcastSpace(), shared),
    )

    # Raises ValueError because repeated-label axes must already match in size.
    out = einsum("ii->i", tensor)
    ```

    Higher-rank contraction over one shared index:

    ```python
    out = einsum("abc,dbe->acde", x, y)
    ```

    This computes
    \(out[a, c, d, e] = \sum_b x[a, b, c] \; y[d, b, e]\).

    Higher-rank contraction over multiple shared indices:

    ```python
    out = einsum("abcd,bcde->ae", x, y)
    ```

    This sums over the shared `b`, `c`, and `d` labels and keeps only the
    surviving `a` and `e` axes in the output.

    Parameters
    ----------
    equation : str
        Einstein summation equation in the same format accepted by
        `torch.einsum`. Label characters must be ASCII letters `[A-Za-z]`.
    *operands : Tensor
        Input tensors whose ranks must match the equation terms after any
        `...` expansion.

    Returns
    -------
    Tensor
        Tensor whose data is computed by `torch.einsum` on the aligned operand
        data and whose `dims` follow the einsum output labels.

    Raises
    ------
    ValueError
        If the equation uses unsupported label characters, does not match the
        operand ranks, or if any shared label maps to incompatible symbolic
        dimensions.

    See Also
    --------
    [`matmul`][qten.linalg.tensors.matmul]
        Specialized contraction helper for standard matrix multiplication.
    [`align`][qten.linalg.tensors.align]
        Axis-alignment primitive used to reconcile same-labeled symbolic dims.
    """
    (
        normalized_equation,
        expanded_terms,
        output_labels,
        normalized_operands,
    ) = _normalize_einsum_operands(equation, operands)

    for term, operand in zip(expanded_terms, normalized_operands):
        _validate_einsum_repeated_labels(term, operand)

    label_dims: Dict[str, StateSpace] = {}
    for term, operand in zip(expanded_terms, normalized_operands):
        for axis, label in enumerate(term):
            label_dims[label] = _merge_einsum_dim(
                label_dims.get(label), operand.dims[axis], label
            )

    aligned_operands: list[Tensor] = []
    for term, operand in zip(expanded_terms, normalized_operands):
        current = operand
        for axis, label in enumerate(term):
            current = current.align(axis, label_dims[label])
        aligned_operands.append(current)

    promoted_operands = _promote_einsum_operands(aligned_operands)
    data = torch.einsum(
        normalized_equation, *(operand.data for operand in promoted_operands)
    )
    dims = tuple(label_dims[label] for label in output_labels)
    return Tensor(data=data, dims=dims)

norm

norm(
    tensor: TensorType,
    ord: int | float | str | None = None,
    dim: int | tuple[int, int] | None = None,
) -> TensorType | Tensor

Compute a vector or matrix norm with metadata-aware dimension reduction.

This forwards to torch.linalg.norm for the numeric computation, then removes the reduced axes from the symbolic output dims.

See Also

torch.linalg.norm Official PyTorch reference for the underlying numeric operation. torch.linalg.vector_norm Clearer vector-only norm API in PyTorch. torch.linalg.matrix_norm Clearer matrix-only norm API in PyTorch.

Behavior

The interpretation of ord depends on dim:

  • dim is an int: compute a vector norm along that axis.
  • dim is a 2-tuple: compute a matrix norm over those two axes.
  • dim is None: follow PyTorch's torch.linalg.norm rules. In particular, ord=None flattens the tensor and computes a vector 2-norm, while ord != None expects PyTorch's documented 1D/2D behavior.
Supported ord values

Vector-norm forms (dim is an int) - None - 0 - any finite int or float - float("inf") - -float("inf")

Matrix-norm forms (dim is a 2-tuple) - None - "fro" - "nuc" - 1, -1 - 2, -2 - float("inf") - -float("inf")

Parameters:

Name Type Description Default
tensor Tensor

The tensor to reduce.

required
ord Optional[Union[int, float, str]]

Order of the norm forwarded to torch.linalg.norm.

Common examples: - ord=2 for the Euclidean vector norm or spectral matrix norm - ord=1 for an L1 vector norm or induced 1 matrix norm - ord=float("inf") for max-based norms - ord="fro" for the Frobenius matrix norm - ord="nuc" for the nuclear matrix norm

None
dim Optional[Union[int, Tuple[int, int]]]

Reduction axis or axes.

  • int: vector norm
  • Tuple[int, int]: matrix norm
  • None: use PyTorch's default torch.linalg.norm behavior
None

Returns:

Type Description
Union[TensorType, Tensor]

Tensor containing the requested norm values with reduced axes removed from dims. For subclasses marked with strict_dims, this may return a plain Tensor.

Raises:

Type Description
IndexError

If any requested reduction axis is out of range for the tensor rank.

ValueError

If dim contains duplicate axes.

Source code in src/qten/linalg/tensors.py
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
def norm(
    tensor: TensorType,
    ord: Optional[Union[int, float, str]] = None,
    dim: Optional[Union[int, Tuple[int, int]]] = None,
) -> Union[TensorType, Tensor]:
    """
    Compute a vector or matrix norm with metadata-aware dimension reduction.

    This forwards to `torch.linalg.norm` for the numeric computation, then
    removes the reduced axes from the symbolic output dims.

    See Also
    --------
    [`torch.linalg.norm`](https://docs.pytorch.org/docs/stable/generated/torch.linalg.norm.html)
        Official PyTorch reference for the underlying numeric operation.
    [`torch.linalg.vector_norm`](https://docs.pytorch.org/docs/stable/generated/torch.linalg.vector_norm.html)
        Clearer vector-only norm API in PyTorch.
    [`torch.linalg.matrix_norm`](https://docs.pytorch.org/docs/stable/generated/torch.linalg.matrix_norm.html)
        Clearer matrix-only norm API in PyTorch.

    Behavior
    --------
    The interpretation of `ord` depends on `dim`:

    - `dim` is an `int`: compute a vector norm along that axis.
    - `dim` is a 2-tuple: compute a matrix norm over those two axes.
    - `dim is None`: follow PyTorch's `torch.linalg.norm` rules. In
      particular, `ord=None` flattens the tensor and computes a vector 2-norm,
      while `ord != None` expects PyTorch's documented 1D/2D behavior.

    Supported `ord` values
    ----------------------
    Vector-norm forms (`dim` is an `int`)
    - `None`
    - `0`
    - any finite `int` or `float`
    - `float("inf")`
    - `-float("inf")`

    Matrix-norm forms (`dim` is a 2-tuple)
    - `None`
    - `"fro"`
    - `"nuc"`
    - `1`, `-1`
    - `2`, `-2`
    - `float("inf")`
    - `-float("inf")`

    Parameters
    ----------
    tensor : Tensor
        The tensor to reduce.
    ord : Optional[Union[int, float, str]], optional
        Order of the norm forwarded to `torch.linalg.norm`.

        Common examples:
        - `ord=2` for the Euclidean vector norm or spectral matrix norm
        - `ord=1` for an L1 vector norm or induced 1 matrix norm
        - `ord=float("inf")` for max-based norms
        - `ord="fro"` for the Frobenius matrix norm
        - `ord="nuc"` for the nuclear matrix norm
    dim : Optional[Union[int, Tuple[int, int]]], optional
        Reduction axis or axes.

        - `int`: vector norm
        - `Tuple[int, int]`: matrix norm
        - `None`: use PyTorch's default `torch.linalg.norm` behavior

    Returns
    -------
    Union[TensorType, Tensor]
        Tensor containing the requested norm values with reduced axes removed
        from `dims`. For subclasses marked with
        [`strict_dims`][qten.linalg.tensors.strict_dims], this may return a
        plain [`Tensor`][qten.linalg.tensors.Tensor].

    Raises
    ------
    IndexError
        If any requested reduction axis is out of range for the tensor rank.
    ValueError
        If `dim` contains duplicate axes.
    """
    reduced = torch.linalg.norm(tensor.data, ord=ord, dim=dim)
    if dim is None:
        return cast(
            TensorType,
            _wrap_tensor_result(
                tensor,
                data=reduced,
                dims=(),
                preserve_strict=False,
            ),
        )

    rank_ = tensor.rank()
    dims_tuple: Tuple[int, ...]
    if isinstance(dim, int):
        dims_tuple = (dim,)
    else:
        dims_tuple = dim

    normalized_dims: list[int] = []
    for d in dims_tuple:
        nd = d
        if nd < 0:
            nd += rank_
        if nd < 0 or nd >= rank_:
            raise IndexError(f"Dimension index {d} out of range for rank {rank_}")
        if nd in normalized_dims:
            raise ValueError("norm dim entries must be unique")
        normalized_dims.append(nd)

    reduced_dims_set = set(normalized_dims)
    new_dims = tuple(
        current_dim
        for idx, current_dim in enumerate(tensor.dims)
        if idx not in reduced_dims_set
    )
    return cast(
        TensorType,
        _wrap_tensor_result(
            tensor,
            data=reduced,
            dims=new_dims,
            preserve_strict=False,
        ),
    )