Skip to content

qubosolver.vectori

qubosolver.Vectori module-attribute

Vectori: TypeAlias = jaxtyping.Int64[torch.Tensor, 'n']

1-D int64 tensor of shape (n,).

qubosolver.vectori

1-D integer vector utilities for QUBO solvers.

A Vectori is a 1-D torch.int64 tensor of shape (n,) used to represent integer-valued quantities such as indices or counts. Unlike qubosolver.vector, the dtype is fixed to int64 and is not affected by the global float precision setting.

This module provides factory functions for creating and converting integer vectors on the globally configured torch device.

Typical usage:

v = vectori.zeros(4)                     # 1-D zero int64 vector of length 4
v = vectori.tensor([0, 1, 2, 3])         # from a list of integers
v = vectori.as_tensor(some_tensor)       # cast existing tensor to int64, no copy when possible

See also qubosolver.vector for float vectors.

Functions:

  • as_tensor –

    Convenience wrapper for torch.as_tensor that converts data to an integer vector tensor.

  • device –

    Returns the globally configured torch device.

  • dtype –

    Returns the dtype used for integer vectors (torch.int64).

  • tensor –

    Creates an integer vector tensor from the given data.

  • zeros –

    Creates a zero-filled integer vector of length n.

  • zeros_field –

    Creates a dataclass field defaulting to a zero-filled integer vector.

as_tensor

as_tensor(data: Any) -> Vectori

Convenience wrapper for torch.as_tensor that converts data to an integer vector tensor.

Avoids a copy when possible. If data is already a tensor with the right dtype and on the right device, it is returned as-is, sharing the same underlying memory. A numpy array is also shared rather than copied if it already has int64 dtype and the global device is cpu (numpy arrays only live on CPU, so any other dtype or device forces a copy). Lists, tuples, and other array-like inputs are always copied.

Parameters:

  • data (Any) –

    Input data (tensor, numpy array, list, tuple, etc.).

Returns:

  • Vectori –

    A 1-D int64 tensor on the global device.

Source code in qubosolver/types/vectori.py
def as_tensor(data: Any) -> Vectori:  # noqa: ANN401 (array-like input forwarded to torch.as_tensor)
    """Convenience wrapper for `torch.as_tensor` that converts data to an integer vector tensor.

    Avoids a copy when possible. If *data* is already a tensor with the right dtype and on
    the right device, it is returned as-is, sharing the same underlying memory. A numpy
    array is also shared rather than copied if it already has ``int64`` dtype and the
    global device is ``cpu`` (numpy arrays only live on CPU, so any other dtype or device
    forces a copy). Lists, tuples, and other array-like inputs are always copied.

    Args:
        data: Input data (tensor, numpy array, list, tuple, etc.).

    Returns:
        A 1-D ``int64`` tensor on the global device.
    """
    return torch.as_tensor(data, dtype=dtype(), device=device())

device

device() -> torch.device

Returns the globally configured torch device.

Source code in qubosolver/types/vectori.py
def device() -> torch.device:
    """Returns the globally configured torch device."""
    return linalg.device()

dtype

dtype() -> torch.dtype

Returns the dtype used for integer vectors (torch.int64).

Source code in qubosolver/types/vectori.py
def dtype() -> torch.dtype:
    """Returns the dtype used for integer vectors (``torch.int64``)."""
    return torch.int64

tensor

tensor(data: Any, *, device: torch.device | None = None, **kwargs: Any) -> Vectori

Creates an integer vector tensor from the given data.

Parameters:

  • data (Any) –

    Input data (list, tuple, or array-like of integers).

  • device (torch.device | None, default: None ) –

    Torch device for the tensor.

  • **kwargs (Any, default: {} ) –

    Extra keyword arguments forwarded to torch.tensor.

Returns:

  • Vectori –

    A 1-D int64 tensor.

Source code in qubosolver/types/vectori.py
def tensor(
    data: Any,  # noqa: ANN401 (array-like input forwarded to torch.tensor)
    *,
    device: torch.device | None = None,
    **kwargs: Any,  # noqa: ANN401 (forwarded to torch.tensor)
) -> Vectori:
    """Creates an integer vector tensor from the given data.

    Args:
        data: Input data (list, tuple, or array-like of integers).
        device: Torch device for the tensor.
        **kwargs: Extra keyword arguments forwarded to `torch.tensor`.

    Returns:
        A 1-D ``int64`` tensor.
    """
    device = device or _device()
    return torch.tensor(data, dtype=dtype(), device=device, **kwargs)

zeros

zeros(n: int, *, device: torch.device | None = None) -> Vectori

Creates a zero-filled integer vector of length n.

Parameters:

  • n (int) –

    Length of the vector.

  • device (torch.device | None, default: None ) –

    Torch device for the tensor.

Returns:

  • Vectori –

    A 1-D int64 tensor of zeros.

Source code in qubosolver/types/vectori.py
def zeros(n: int, *, device: torch.device | None = None) -> Vectori:
    """Creates a zero-filled integer vector of length *n*.

    Args:
        n: Length of the vector.
        device: Torch device for the tensor.

    Returns:
        A 1-D ``int64`` tensor of zeros.
    """
    device = device or _device()
    return torch.zeros(n, dtype=dtype(), device=device)

zeros_field

zeros_field(n: int, *, device: torch.device | None = None) -> Vectori

Creates a dataclass field defaulting to a zero-filled integer vector.

Parameters:

  • n (int) –

    Length of the vector.

  • device (torch.device | None, default: None ) –

    Torch device for the tensor.

Returns:

  • Vectori –

    A dataclass field (typed as Vectori for the enclosing class) whose

  • Vectori –

    default_factory builds a fresh zero tensor per instance.

Source code in qubosolver/types/vectori.py
@no_runtime_typecheck
def zeros_field(n: int, *, device: torch.device | None = None) -> Vectori:
    """Creates a dataclass field defaulting to a zero-filled integer vector.

    Args:
        n: Length of the vector.
        device: Torch device for the tensor.

    Returns:
        A dataclass field (typed as `Vectori` for the enclosing class) whose
        `default_factory` builds a fresh zero tensor per instance.
    """
    return field(default_factory=lambda: zeros(n, device=device))