qubosolver.tensor
qubosolver.Tensor
module-attribute
Section titled “
qubosolver.Tensor
module-attribute
”Tensor = TensorfArbitrary-rank float tensor using the globally configured precision (float32 by default).
qubosolver.tensor
Section titled “
qubosolver.tensor
”Arbitrary-rank tensor utilities for QUBO solvers.
A Tensor here is an arbitrary-rank float tensor using the globally configured
dtype (float32 by default, float64 when double precision is enabled).
This module provides factory functions for creating and converting such tensors
on the globally configured torch device.
Typical usage:
t = tensor.zeros(2, 3) # 2x3 zero tensort = tensor.tensor([[1.0, 0.0], [0.0, 1.0]]) # from nested listt = tensor.as_tensor(some_tensor) # cast existing tensor, no copy when possibleFor rank-specific aliases see qubosolver.vector (1-D) and
qubosolver.matrix (2-D square).
Functions:
-
as_tensor–Convenience wrapper for
torch.as_tensorthat converts data to a tensor. -
device–Returns the globally configured torch device.
-
dtype–Returns the globally configured float dtype.
-
tensor–Creates a tensor from the given data.
-
zeros–Creates a zero-filled tensor with the given shape.
-
zeros_field–Creates a dataclass field defaulting to a zero-filled tensor with the given shape.
as_tensor
Section titled “
as_tensor
”as_tensor(data: Any) -> qubosolver.Tensor
module-attribute (qubosolver.types.linalg.Tensor)" href="#qubosolver.Tensor">TensorConvenience wrapper for torch.as_tensor that converts data to a 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:
-
Tensor–A tensor on the global dtype and device.
Source code in qubosolver/types/tensor.py
def as_tensor(data: Any) -> Tensor: # noqa: ANN401 (array-like input forwarded to torch.as_tensor) """Convenience wrapper for `torch.as_tensor` that converts data to a 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 tensor on the global dtype and device. """ return torch.as_tensor(data, dtype=dtype(), device=device())
device
Section titled “
device
”device() -> torch.deviceReturns the globally configured torch device.
Source code in qubosolver/types/tensor.py
def device() -> torch.device: """Returns the globally configured torch device.""" return linalg.device()
dtype
Section titled “
dtype
”dtype() -> torch.dtypeReturns the globally configured float dtype.
Source code in qubosolver/types/tensor.py
def dtype() -> torch.dtype: """Returns the globally configured float dtype.""" return linalg.dtype()
tensor
Section titled “
tensor
”tensor(data: Any, *, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any) -> torch.TensorCreates a tensor from the given data.
Parameters:
-
data(Any (external)) –Input data (list, tuple, or array-like).
-
dtype(torch (external).dtype (external) | None, default:None) –Data type of the tensor.
-
device(torch (external).device (external) | None, default:None) –Torch device for the tensor.
-
**kwargs(Any (external), default:{}) –Extra keyword arguments forwarded to
torch.tensor.
Returns:
-
torch (external).Tensor (external)–A tensor with the specified dtype and device.
Source code in qubosolver/types/tensor.py
def tensor( data: Any, *, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any,) -> torch.Tensor: """Creates a 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 tensor with the specified dtype and device. """ dtype = dtype or _dtype() device = device or _device() return torch.tensor(data, dtype=dtype, device=device, **kwargs)
zeros
Section titled “
zeros
”zeros(*args: Any, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any) -> torch.TensorCreates a zero-filled tensor with the given shape.
Parameters:
-
*args(Any (external), default:()) –Shape dimensions (e.g.
zeros(2, 3)orzeros((2, 3))). -
dtype(torch (external).dtype (external) | None, default:None) –Data type of the tensor.
-
device(torch (external).device (external) | None, default:None) –Torch device for the tensor.
-
**kwargs(Any (external), default:{}) –Extra keyword arguments forwarded to
torch.zeros.
Returns:
-
torch (external).Tensor (external)–A tensor of zeros with the specified shape.
Source code in qubosolver/types/tensor.py
def zeros( *args: Any, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any,) -> torch.Tensor: """Creates a zero-filled tensor with the given shape.
Args: *args: Shape dimensions (e.g. ``zeros(2, 3)`` or ``zeros((2, 3))``). dtype: Data type of the tensor. device: Torch device for the tensor. **kwargs: Extra keyword arguments forwarded to `torch.zeros`.
Returns: A tensor of zeros with the specified shape. """ dtype = dtype or _dtype() device = device or _device() return torch.zeros(*args, dtype=dtype, device=device, **kwargs)
zeros_field
Section titled “
zeros_field
”zeros_field(*args: Any, dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any) -> qubosolver.Tensor
module-attribute (qubosolver.types.linalg.Tensor)" href="#qubosolver.Tensor">TensorCreates a dataclass field defaulting to a zero-filled tensor with the given shape.
Parameters:
-
*args(Any (external), default:()) –Shape dimensions (e.g.
zeros_field(2, 3)orzeros_field((2, 3))). -
dtype(torch (external).dtype (external) | None, default:None) –Data type of the tensor.
-
device(torch (external).device (external) | None, default:None) –Torch device for the tensor.
-
**kwargs(Any (external), default:{}) –Extra keyword arguments forwarded to
torch.zeros.
Returns:
-
Tensor–A dataclass field (typed as
Tensorfor the enclosing class) whose -
Tensor–default_factorybuilds a fresh zero tensor per instance.
Source code in qubosolver/types/tensor.py
@no_runtime_typecheckdef zeros_field( *args: Any, # noqa: ANN401 (forwarded to torch.zeros) dtype: torch.dtype | None = None, device: torch.device | None = None, **kwargs: Any, # noqa: ANN401 (forwarded to torch.zeros)) -> Tensor: """Creates a dataclass field defaulting to a zero-filled tensor with the given shape.
Args: *args: Shape dimensions (e.g. ``zeros_field(2, 3)`` or ``zeros_field((2, 3))``). dtype: Data type of the tensor. device: Torch device for the tensor. **kwargs: Extra keyword arguments forwarded to `torch.zeros`.
Returns: A dataclass field (typed as `Tensor` for the enclosing class) whose `default_factory` builds a fresh zero tensor per instance. """ return field(default_factory=lambda: zeros(*args, dtype=dtype, device=device, **kwargs))