qbm.models package

Submodules

qbm.models.BQRBM module

class qbm.models.BQRBM.BQRBM(V_train, n_hidden, A_freeze, B_freeze, beta_initial=1.0, beta_range=[0.1, 10], annealer_params=None, simulation_params=None, seed=0)

Bases: QBMBase

Bound-based Quantum Restricted Boltzmann Machine

property J

Ising J values. Correspond to w_ij = -β * B_freeze * J_ij

property h

Ising h values. Correspond to b_i = -β * B_freeze * h_i

static load(file_path, initialize_annealer=True)

Loads the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.

Parameters:
  • file_path – Path to the model to load. Must be a Path object or a string with “.pkl” file extension.

  • initialize_annealer – If True and has attribute self.annealer_params, then will call self._initialize_annealer().

Returns:

BQRBM instance loaded from the file path.

sample(n_samples, answer_mode='raw', use_gauge=True, binary=False)

Generate samples using the model, either exact or from the annealer.

Parameters:
  • n_samples – Number of samples to generate (num_reads param in sample_ising).

  • answer_mode – “raw” or “histogram”.

  • use_gauge – If True will use a random gauge transformation (recommended for more robust sample generation).

  • binary – If true will convert the state vector values from {+1, -1} to {0, 1}.

Returns:

Dictionary (exact) or Ocean SDK SampleSet object (annealer).

save(file_path, reinitialize_annealer=True)

Saves the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.

Parameters:
  • file_path – Path to save the model to. Must be a Path object or a string with “.pkl” file extension.

  • reinitialize_annealer – If True and has attribute self.annealer_params, then will call self._initialize_annealer() after saving.

train(n_epochs=100, learning_rate=0.1, learning_rate_beta=0.1, mini_batch_size=10, n_samples=10000, callback=None)

Fits the model to the training data.

Parameters:
  • n_epochs – Number of epochs to train for.

  • learning_rate – Learning rate. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs.

  • learning_rate_beta – Learning rate for the effective temperature. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs. Note: It might be useful to use a larger learning_rate_beta in the beginning to help the model find a good temperature, then drop it after a number of epochs.

  • mini_batch_size – Size of the mini-batches.

  • n_samples – Number of samples to generate after every epoch. Used for computing β gradient, as well as the callback.

  • callback – A function called at the end of each epoch. It takes the arguments (model, samples), and returns a dictionary with required keys [“value”, “print”], where the “print” value is a string to be printed at the end of each epoch.

qbm.models.QBMBase module

class qbm.models.QBMBase.QBMBase(V_train, n_hidden, seed)

Bases: ABC

Abstract base class for Quantum Boltzmann Machines

Theory based on Quantum Boltzmann Machine by Amin et al. https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.021050

Module contents

class qbm.models.BQRBM(V_train, n_hidden, A_freeze, B_freeze, beta_initial=1.0, beta_range=[0.1, 10], annealer_params=None, simulation_params=None, seed=0)

Bases: QBMBase

Bound-based Quantum Restricted Boltzmann Machine

property J

Ising J values. Correspond to w_ij = -β * B_freeze * J_ij

property h

Ising h values. Correspond to b_i = -β * B_freeze * h_i

static load(file_path, initialize_annealer=True)

Loads the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.

Parameters:
  • file_path – Path to the model to load. Must be a Path object or a string with “.pkl” file extension.

  • initialize_annealer – If True and has attribute self.annealer_params, then will call self._initialize_annealer().

Returns:

BQRBM instance loaded from the file path.

sample(n_samples, answer_mode='raw', use_gauge=True, binary=False)

Generate samples using the model, either exact or from the annealer.

Parameters:
  • n_samples – Number of samples to generate (num_reads param in sample_ising).

  • answer_mode – “raw” or “histogram”.

  • use_gauge – If True will use a random gauge transformation (recommended for more robust sample generation).

  • binary – If true will convert the state vector values from {+1, -1} to {0, 1}.

Returns:

Dictionary (exact) or Ocean SDK SampleSet object (annealer).

save(file_path, reinitialize_annealer=True)

Saves the BQRBM model at file_path. Necessary because of pickling issues with the qpu and sampler objects.

Parameters:
  • file_path – Path to save the model to. Must be a Path object or a string with “.pkl” file extension.

  • reinitialize_annealer – If True and has attribute self.annealer_params, then will call self._initialize_annealer() after saving.

train(n_epochs=100, learning_rate=0.1, learning_rate_beta=0.1, mini_batch_size=10, n_samples=10000, callback=None)

Fits the model to the training data.

Parameters:
  • n_epochs – Number of epochs to train for.

  • learning_rate – Learning rate. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs.

  • learning_rate_beta – Learning rate for the effective temperature. If a list or array, then it will represent the learning rate over the epochs, must be of length n_epochs. Note: It might be useful to use a larger learning_rate_beta in the beginning to help the model find a good temperature, then drop it after a number of epochs.

  • mini_batch_size – Size of the mini-batches.

  • n_samples – Number of samples to generate after every epoch. Used for computing β gradient, as well as the callback.

  • callback – A function called at the end of each epoch. It takes the arguments (model, samples), and returns a dictionary with required keys [“value”, “print”], where the “print” value is a string to be printed at the end of each epoch.

class qbm.models.QBMBase(V_train, n_hidden, seed)

Bases: ABC

Abstract base class for Quantum Boltzmann Machines

Theory based on Quantum Boltzmann Machine by Amin et al. https://journals.aps.org/prx/abstract/10.1103/PhysRevX.8.021050