Skip to content

Results are limited to the current section: Application solving tools

Product news

qubosolver.vector

Vector = Vectorf

1-D float tensor using the globally configured precision (float32 by default).

1-D vector utilities for QUBO solvers.

A Vector is a 1-D float tensor of shape (n,) using the globally configured dtype (float32 by default, float64 when double precision is enabled). This module provides factory functions for creating and converting such vectors on the globally configured torch device.

Typical usage:

v = vector.zeros(4) # 1-D zero vector of length 4
v = vector.tensor([1.0, 0.5, -1.0]) # from a list
v = vector.as_tensor(some_tensor) # cast existing tensor, no copy when possible

For higher-rank variants see qubosolver.matrix (2-D square) and qubosolver.tensor (arbitrary rank).

Functions:

  • as_tensor –

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

  • device –

    Returns the globally configured torch device.

  • dtype –

    Returns the globally configured float dtype.

  • tensor –

    Creates a 1-D vector tensor from the given data.

  • zeros –

    Creates a zero-filled 1-D vector of length n.

  • zeros_field –

    Creates a dataclass field defaulting to a zero-filled 1-D vector.

as_tensor(data: Any) -> qubosolver.Vector
module-attribute
(qubosolver.types.linalg.Vector)" href="#qubosolver.Vector">Vector

Convenience wrapper for torch.as_tensor that converts data to a 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 the global float 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 (external)) –

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

Returns:

  • Vector –

    A 1-D tensor on the global dtype and device.

Source code in qubosolver/types/vector.py
def as_tensor(data: Any) -> Vector: # noqa: ANN401 (array-like input forwarded to torch.as_tensor)
"""Convenience wrapper for `torch.as_tensor` that converts data to a 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 the global float 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 tensor on the global dtype and device.
"""
return torch.as_tensor(data, dtype=dtype(), device=device())
device() -> torch.device

Returns the globally configured torch device.

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

Returns the globally configured float dtype.

Source code in qubosolver/types/vector.py
def dtype() -> torch.dtype:
"""Returns the globally configured float dtype."""
return linalg.dtype()
tensor(data: Any, *, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any) -> torch.Tensor

Creates a 1-D vector tensor from the given data.

Parameters:

Returns:

Source code in qubosolver/types/vector.py
def tensor(
data: Any,
*,
dtype: torch.dtype | None = None,
device: torch.device | None = None,
**kwargs: Any,
) -> torch.Tensor:
"""Creates a 1-D vector tensor from the given data.
Args:
data: Input data (list, tuple, or array-like).
dtype: Data type of the tensor.
device: Torch device for the tensor.
**kwargs: Extra keyword arguments forwarded to `torch.tensor`.
Returns:
A 1-D tensor.
"""
dtype = dtype or _dtype()
device = device or _device()
return torch.tensor(data, dtype=dtype, device=device, **kwargs)
zeros(n: int, *, dtype: torch.dtype | None = None, device: torch.device | None = None) -> torch.Tensor

Creates a zero-filled 1-D vector of length n.

Parameters:

Returns:

Source code in qubosolver/types/vector.py
def zeros(
n: int, *, dtype: torch.dtype | None = None, device: torch.device | None = None
) -> torch.Tensor:
"""Creates a zero-filled 1-D vector of length *n*.
Args:
n: Length of the vector.
dtype: Data type of the tensor.
device: Torch device for the tensor.
Returns:
A 1-D tensor of zeros.
"""
dtype = dtype or _dtype()
device = device or _device()
return torch.zeros(n, dtype=dtype, device=device)
zeros_field(n: int, *, dtype: torch.dtype | None = None, device: torch.device | None = None) -> qubosolver.Vector
module-attribute
(qubosolver.types.linalg.Vector)" href="#qubosolver.Vector">Vector

Creates a dataclass field defaulting to a zero-filled 1-D vector.

Parameters:

Returns:

  • Vector –

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

  • Vector –

    default_factory builds a fresh zero tensor per instance.

Source code in qubosolver/types/vector.py
@no_runtime_typecheck
def zeros_field(
n: int, *, dtype: torch.dtype | None = None, device: torch.device | None = None
) -> Vector:
"""Creates a dataclass field defaulting to a zero-filled 1-D vector.
Args:
n: Length of the vector.
dtype: Data type of the tensor.
device: Torch device for the tensor.
Returns:
A dataclass field (typed as `Vector` for the enclosing class) whose
`default_factory` builds a fresh zero tensor per instance.
"""
return field(default_factory=lambda: zeros(n, dtype=dtype, device=device))