xbtorch.devices.utils

Utility functions for device modeling, dataset generation, and interpolation.

This module provides low-level helpers for XBTorch device simulations, including:

  • Conductance table search and nearest-neighbor functions.

  • Stochastic rounding for pulse calculations.

  • Device conductance sweeps for SET and RESET phases.

  • Synthetic dataset generation for device characterization.

  • Kriging-based interpolation and CDF construction for jump table models.

Functions

Functions

construct_cdf(G_axis, dG_axis, mean_profile, ...)

Construct a cumulative distribution function (CDF) array for a jump table.

find_nearest(array, values)

Find the indices of the nearest elements in a 1D array for each value.

find_nearest_2d(array, values)

Find nearest indices in a 2D array for each value (broadcastable).

get_kriging_profiles(g, dg[, axis_size, epsilon])

Generate mean and standard deviation profiles using Kriging interpolation.

stochastic_round(x)

Stochastically round values to the nearest integer with probability proportional to fractional part.

sweep_conductance_full(device[, group_param_idx])

Sweep conductance trajectories of a device for SET and RESET operations.

synthesize_G_dG_dataset(device[, set, ...])

Generate a synthetic (G, dG) dataset for a device model.

xbtorch.devices.utils.construct_cdf(G_axis, dG_axis, mean_profile, std_profile)[source]

Construct a cumulative distribution function (CDF) array for a jump table.

Parameters:
  • G_axis (np.ndarray) – The G axis of the jump table.

  • dG_axis (np.ndarray) – The dG axis of the jump table.

  • mean_profile (np.ndarray) – Mean conductance change profile.

  • std_profile (np.ndarray) – Standard deviation profile.

Returns:

2D array mapping G to dG probabilities (CDF).

Return type:

np.ndarray

xbtorch.devices.utils.find_nearest(array, values)[source]

Find the indices of the nearest elements in a 1D array for each value.

Parameters:
  • array (torch.Tensor) – 1D tensor of reference values.

  • values (torch.Tensor) – Tensor of values to match.

Returns:

Indices in array closest to each element in values.

Return type:

torch.Tensor

xbtorch.devices.utils.find_nearest_2d(array, values)[source]

Find nearest indices in a 2D array for each value (broadcastable).

Parameters:
  • array (torch.Tensor) – 1D or 2D tensor of reference values.

  • values (torch.Tensor or array-like) – Values to match against the array.

Returns:

Indices of the nearest elements along the last axis.

Return type:

torch.Tensor

xbtorch.devices.utils.get_kriging_profiles(g, dg, axis_size=100, epsilon=1e-10, **kwargs)[source]

Generate mean and standard deviation profiles using Kriging interpolation.

Parameters:
  • g (list[float]) – Initial conductances.

  • dg (list[float]) – Conductance changes corresponding to g.

  • axis_size (int) – Size of G and dG axes for the jump table.

  • epsilon (float) – Small value to ensure non-zero standard deviation.

  • kwargs (dict) – Additional parameters for SMT KRG surrogate model.

Returns:

G_axis, dG_axis, mean_profile, std_profile arrays.

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

xbtorch.devices.utils.stochastic_round(x)[source]

Stochastically round values to the nearest integer with probability proportional to fractional part.

Parameters:

x (torch.Tensor) – Input tensor of floating-point numbers.

Returns:

Stochastically rounded integers.

Return type:

torch.Tensor

xbtorch.devices.utils.sweep_conductance_full(device, group_param_idx=(0, 0))[source]

Sweep conductance trajectories of a device for SET and RESET operations.

SET starts from minimum conductance and applies positive pulses. RESET starts from maximum conductance and applies negative pulses.

Parameters:
  • device (xbtorch.devices.GenericDevice) – An XBTorch device model.

  • group_param_idx (tuple(int, int)) – Optional internal index used during neural network simulations.

Returns:

Conductance trajectories for SET and RESET as a tuple.

Return type:

tuple[list[float], list[float]]

xbtorch.devices.utils.synthesize_G_dG_dataset(device, set=True, num_points=1000, min_delta_ratio=0.001, disable_filtering=False)[source]

Generate a synthetic (G, dG) dataset for a device model.

Iteratively samples delta conductance points from the device by applying pulses.

Parameters:
  • device (xbtorch.devices.GenericDevice) – The target device model.

  • set (bool) – Sample in SET (True) or RESET (False) direction.

  • num_points (int) – Number of points to generate.

  • min_delta_ratio (float) – Minimum ratio deltaG/G to consider a point valid.

  • disable_filtering (bool) – If True, include all points without filtering for physical realism.

Returns:

Array of shape (num_points, 2) containing (G, dG) points.

Return type:

np.ndarray