qbm4eo.rbm module


This file is a part of qbm4eo.rst project.

https://github.com/FeralQubits/qbm4eo


It has been modified as a part of the EuroHPC PL project funded at the Smart Growth Operational Programme 2014-2020, Measure 4.2 under the grant agreement no. POIR.04.02.00-00-D014/20-00.


class qbm4eo.rbm.AnnealingRBMTrainer(num_steps: int, sampler: Sampler, qubo_scale: float = 1.0, learning_rate: float = 0.01, **sampler_kwargs: Dict[str, Any])[source]

Bases: RBMTrainer

A class for training the RBM using the annealing samplers (D-Wave of CIMSampler).

A default constructor for the AnnealingRBMTrainer class instances.

Parameters:
  • num_steps – Number of training steps.

  • sampler – A sampler to be used during the training.

  • qubo_scale – A scaling factor for the QUBO.

  • learning_rate – A learning rate.

  • sampler_kwargs – Additional keyword arguments to be passed to the sampler.

training_step(rbm: RBM, batch: ndarray[Any, dtype[float32]]) None[source]

A single training step.

Parameters:
  • rbm – An RBM to be trained.

  • batch – The RBMs’ visible layer neurons’ states.

class qbm4eo.rbm.CD1Trainer(num_steps: int, learning_rate: float = 0.01)[source]

Bases: RBMTrainer

A class for training the RBM using the Contrastive Divergence 1 algorithm.

A default constructor for the CD1Trainer class instances.

Parameters:
  • num_steps – Number of training steps.

  • learning_rate – A learning rate.

training_step(rbm: RBM, batch: ndarray[Any, dtype[float32]]) None[source]

A single training step.

Parameters:
  • rbm – An RBM to be trained.

  • batch – The RBMs’ visible layer neurons’ states. TODO: There seems to be a problem when batch_size is 1. Investigate.

class qbm4eo.rbm.RBM(num_visible: int, num_hidden: int, rng: Generator | None = None)[source]

Bases: object

A class implementing the Restricted Boltzmann Machine.

A default constructor for the RMB class instances.

Parameters:
  • num_visible – Number of neurons in the visible layer.

  • num_hidden – Number of neurons in the hidden layer.

  • rng – Random number generator to be used by the RBM.

h_probs_given_v(v_batch: ndarray[Any, dtype[float32]]) ndarray[Any, dtype[float32]][source]

Compute the probabilities of the hidden layer neurons being active given the visible layer neurons’ states.

Parameters:

v_batch – A batch of visible layer neurons’ states.

Returns:

A batch of probabilities of the hidden layer neurons being active.

classmethod load(file: str | BytesIO) RBM[source]

Load the RBM from a file.

Parameters:

file – A file to load the RBM from.

Returns:

A loaded RBM.

reconstruct(v_batch: ndarray[Any, dtype[float32]]) ndarray[Any, dtype[float32]][source]

Reconstruct the visible layer neurons’ states given the visible layer neurons’ states.

Parameters:

v_batch – A batch of visible layer neurons’ states.

Returns:

A batch of reconstructed visible layer neurons’ states.

sample_h_given_v(v_batch: ndarray[Any, dtype[float32]]) ndarray[Any, dtype[float32]][source]

Sample the hidden layer neurons’ states given the visible layer neurons’ states.

Parameters:

v_batch – A batch of visible layer neurons’ states.

Returns:

A batch of hidden layer neurons’ states.

sample_v_given_h(h_batch: ndarray[Any, dtype[float32]]) ndarray[Any, dtype[float32]][source]

Sample the visible layer neurons’ states given the hidden layer neurons’ states.

Parameters:

h_batch – A batch of hidden layer neurons’ states.

Returns:

A batch of visible layer neurons’ states.

save(file: str | BytesIO) None[source]

Save the RBM to a file.

Parameters:

file – A file to save the RBM to.

v_probs_given_h(h_batch: ndarray[Any, dtype[float32]]) ndarray[Any, dtype[float32]][source]

Compute the probabilities of the visible layer neurons’ states given the hidden layer neurons’ states.

Parameters:

h_batch – A batch of hidden layer neurons’ states.

Returns:

A batch of probabilities of the visible layer neurons’ states.

class qbm4eo.rbm.RBMTrainer(num_steps: int)[source]

Bases: object

A base class for implementing the RBM training algorithms.

TODO TR: There are some nasty casting between np.matrix and np.ndarray that

is there to ensure proper transposition or multiplications when one of the matrix dimension is 1. This probable could be done more elegantly.

A default constructor for the RBMTrainer class instances.

Parameters:

num_steps – Number of training steps.

fit(rbm: RBM, data_loader: DataLoader[Tuple[Any, Any]] | Generator[Tuple[Any, Any], Any, Any], callback: Callable[[int, RBM, float], None] = None, verbose: bool = False) None[source]

Fits the RBM to the data.

Parameters:
  • rbm – An RBM to be trained.

  • data_loader – A data loader.

  • callback – A callback function to be called after each training step.

abstractmethod training_step(rbm: RBM, batch: ndarray[Any, dtype[float32]]) None[source]

A single training step.

Parameters:
  • rbm – An RBM to be trained.

  • batch – A batch of data.

qbm4eo.rbm.infinite_dataloader_generator(data_loader: DataLoader[Tuple[Any, Any]] | Generator[Tuple[Any, Any], Any, Any]) Generator[Tuple[Any, Any], None, None][source]

A generator that infinitely yields data batches from the given dataloader.

Parameters:

data_loader – A PyTorch DataLoader initialized with the desired dataset.

Returns:

Yields an index of a batch and the data from the data_loader.

qbm4eo.rbm.qubo_from_rbm_coefficients(weights: Tensor | ndarray[Any, dtype[float32]], v_bias: Tensor | ndarray[Any, dtype[float32]], h_bias: Tensor | ndarray[Any, dtype[float32]]) BinaryQuadraticModel[source]

Create a QUBO problem representing RBM with given coefficients.

Parameters:
  • weights – A square interaction matrix.

  • v_bias – An N-element visible layer bias vector.

  • h_bias – An M-element hidden layer bias vector.

Returns:

A QUBO, represented as dimod.BQM with N+M variables, such that:
  • variables 0,…,N-1 correspond to hidden layer

  • variables 0,…,M-1 correspond to visible layer

  • visible layer biases correspond to linear coefficients of first N variables

  • hidden layer biases correspond to linear coefficients of second M variables

  • weights correspond to interaction terms between first N and second M variables.

Note

This function does not allow for manipulating how RBM variables are mapped to the QUBO variables. This is not a problem if QUBO is to be used with an unstructured sampler. For sampler, the intended usage is to wrap it in the EmbeddingComposite.