Quantum States¶
Positive Wavefunction¶
-
class
qucumber.nn_states.
PositiveWavefunction
(num_visible, num_hidden=None, gpu=True)[source]¶ Bases:
qucumber.nn_states.Wavefunction
Class capable of learning Wavefunctions with no phase.
Parameters: -
amplitude
(v)[source]¶ Compute the (unnormalized) amplitude of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Matrix/vector containing the amplitudes of v Return type: torch.Tensor
-
static
autoload
(location, gpu=False)[source]¶ Initializes a Wavefunction from the parameters in the given location.
Parameters: Returns: A new Wavefunction initialized from the given parameters. The returned Wavefunction will be of whichever type this function was called on.
-
compute_batch_gradients
(k, samples_batch, neg_batch)[source]¶ Compute the gradients of a batch of the training data (samples_batch).
Parameters: - k (int) – Number of contrastive divergence steps in training.
- samples_batch (torch.Tensor) – Batch of the input samples.
- neg_batch (torch.Tensor) – Batch of the input samples for computing the negative phase.
Returns: List containing the gradients of the parameters.
Return type:
-
compute_normalization
(space)[source]¶ Compute the normalization constant of the wavefunction.
Parameters: space (torch.Tensor) – A rank 2 tensor of the entire visible space.
-
device
¶ The device that the model is on.
-
fit
(data, epochs=100, pos_batch_size=100, neg_batch_size=None, k=1, lr=0.001, progbar=False, starting_epoch=1, time=False, callbacks=None, optimizer=<class 'torch.optim.sgd.SGD'>, **kwargs)[source]¶ Train the Wavefunction.
Parameters: - data (np.array) – The training samples
- epochs (int) – The number of full training passes through the dataset. Technically, this specifies the index of the last training epoch, which is relevant if starting_epoch is being set.
- pos_batch_size (int) – The size of batches for the positive phase taken from the data.
- neg_batch_size (int) – The size of batches for the negative phase taken from the data. Defaults to pos_batch_size.
- k (int) – The number of contrastive divergence steps.
- lr (float) – Learning rate
- progbar (bool or str) – Whether or not to display a progress bar. If “notebook” is passed, will use a Jupyter notebook compatible progress bar.
- starting_epoch (int) – The epoch to start from. Useful if continuing training from a previous state.
- callbacks (list[qucumber.callbacks.Callback]) – Callbacks to run while training.
- optimizer (torch.optim.Optimizer) – The constructor of a torch optimizer.
- kwargs – Keyword arguments to pass to the optimizer
-
generate_hilbert_space
(size=None, device=None)[source]¶ Generates Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space. Defaults to the number of visible units.
- device – The device to create the Hilbert space matrix on. Defaults to the device this model is on.
Returns: A tensor with all the basis states of the Hilbert space.
Return type:
-
gradient
(v)[source]¶ Compute the gradient of the effective energy for a batch of states.
Parameters: v (torch.Tensor) – visible states Returns: A single tensor containing all of the parameter gradients. Return type: torch.Tensor
-
load
(location)[source]¶ Loads the Wavefunction parameters from the given location ignoring any metadata stored in the file. Overwrites the Wavefunction’s parameters.
Note
The Wavefunction object on which this function is called must have the same parameter shapes as the one who’s parameters are being loaded.
Parameters: location (str or file) – The location to load the Wavefunction parameters from.
-
max_size
¶ Maximum size of the Hilbert space for full enumeration
-
networks
¶ A list of the names of the internal RBMs.
-
phase
(v)[source]¶ Compute the phase of a given vector/matrix of visible states.
In the case of a Positive Wavefunction, the phase is just zero.
Parameters: v (torch.Tensor) – visible states Returns: Matrix/vector containing the phases of v Return type: torch.Tensor
-
probability
(v, Z)[source]¶ Evaluates the probability of the given vector(s) of visible states.
Parameters: - v (torch.Tensor) – The visible states.
- Z (float) – The partition function.
Returns: The probability of the given vector(s) of visible units.
Return type:
-
psi
(v)[source]¶ Compute the (unnormalized) wavefunction of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Complex object containing the value of the wavefunction for each visible state Return type: torch.Tensor
-
rbm_am
¶ The RBM to be used to learn the wavefunction amplitude.
-
sample
(k, num_samples=1, initial_state=None, overwrite=False)[source]¶ Performs k steps of Block Gibbs sampling. One step consists of sampling the hidden state from the conditional distribution , and sampling the visible state from the conditional distribution .
Parameters: - k (int) – Number of Block Gibbs steps.
- num_samples (int) – The number of samples to generate.
- initial_state (torch.Tensor) – The initial state of the Markov Chain. If given, num_samples will be ignored.
- overwrite (bool) – Whether to overwrite the initial_state tensor, if it is provided.
-
save
(location, metadata=None)[source]¶ Saves the Wavefunction parameters to the given location along with any given metadata.
Parameters:
-
stop_training
¶ If True, will not train.
If this property is set to True during the training cycle, training will terminate once the current batch or epoch ends (depending on when stop_training was set).
-
subspace_vector
(num, size=None, device=None)[source]¶ Generates a single vector from the Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space.
- num (int) – The specific vector to return from the Hilbert space. Since the Hilbert space can be represented by the set of binary strings of length size, num is equivalent to the decimal representation of the returned vector.
- device – The device to create the vector on. Defaults to the device this model is on.
Returns: A state from the Hilbert space.
Return type:
-
Complex Wavefunction¶
-
class
qucumber.nn_states.
ComplexWavefunction
(num_visible, num_hidden=None, unitary_dict=None, gpu=True)[source]¶ Bases:
qucumber.nn_states.Wavefunction
Class capable of learning Wavefunctions with a non-zero phase.
Parameters: - num_visible (int) – The number of visible units, ie. the size of the system being learned.
- num_hidden (int) – The number of hidden units in both internal RBMs. Defaults to the number of visible units.
- unitary_dict (dict[str, torch.Tensor]) – A dictionary mapping unitary names to their matrix representations.
- gpu (bool) – Whether to perform computations on the default gpu.
-
amplitude
(v)[source]¶ Compute the (unnormalized) amplitude of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states . Returns: Vector containing the amplitudes of the given states. Return type: torch.Tensor
-
static
autoload
(location, gpu=False)[source]¶ Initializes a Wavefunction from the parameters in the given location.
Parameters: Returns: A new Wavefunction initialized from the given parameters. The returned Wavefunction will be of whichever type this function was called on.
-
compute_batch_gradients
(k, samples_batch, neg_batch, bases_batch=None)[source]¶ Compute the gradients of a batch of the training data (samples_batch).
If measurements are taken in bases other than the reference basis, a list of bases (bases_batch) must also be provided.
Parameters: - k (int) – Number of contrastive divergence steps in training.
- samples_batch (torch.Tensor) – Batch of the input samples.
- neg_batch (torch.Tensor) – Batch of the input samples for computing the negative phase.
- bases_batch (np.array) – Batch of the input bases corresponding to the samples in samples_batch.
Returns: List containing the gradients of the parameters.
Return type:
-
compute_normalization
(space)[source]¶ Compute the normalization constant of the wavefunction.
Parameters: space (torch.Tensor) – A rank 2 tensor of the entire visible space.
-
device
¶ The device that the model is on.
-
fit
(data, epochs=100, pos_batch_size=100, neg_batch_size=None, k=1, lr=0.001, input_bases=None, progbar=False, starting_epoch=1, time=False, callbacks=None, optimizer=<class 'torch.optim.sgd.SGD'>, **kwargs)[source]¶ Train the Wavefunction.
Parameters: - data (np.array) – The training samples
- epochs (int) – The number of full training passes through the dataset. Technically, this specifies the index of the last training epoch, which is relevant if starting_epoch is being set.
- pos_batch_size (int) – The size of batches for the positive phase taken from the data.
- neg_batch_size (int) – The size of batches for the negative phase taken from the data. Defaults to pos_batch_size.
- k (int) – The number of contrastive divergence steps.
- lr (float) – Learning rate
- input_bases (np.array) – The measurement bases for each sample. Must be provided if training a ComplexWavefunction.
- progbar (bool or str) – Whether or not to display a progress bar. If “notebook” is passed, will use a Jupyter notebook compatible progress bar.
- starting_epoch (int) – The epoch to start from. Useful if continuing training from a previous state.
- callbacks (list[qucumber.callbacks.Callback]) – Callbacks to run while training.
- optimizer (torch.optim.Optimizer) – The constructor of a torch optimizer.
- kwargs – Keyword arguments to pass to the optimizer
-
generate_hilbert_space
(size=None, device=None)[source]¶ Generates Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space. Defaults to the number of visible units.
- device – The device to create the Hilbert space matrix on. Defaults to the device this model is on.
Returns: A tensor with all the basis states of the Hilbert space.
Return type:
-
gradient
(basis, sample)[source]¶ Compute the gradient of a sample, measured in different bases.
Parameters: - basis (np.array) – A set of bases.
- sample (np.array) – A sample to compute the gradient of.
Returns: A list of 2 tensors containing the parameters of each of the internal RBMs.
Return type:
-
load
(location)[source]¶ Loads the Wavefunction parameters from the given location ignoring any metadata stored in the file. Overwrites the Wavefunction’s parameters.
Note
The Wavefunction object on which this function is called must have the same parameter shapes as the one who’s parameters are being loaded.
Parameters: location (str or file) – The location to load the Wavefunction parameters from.
-
max_size
¶ Maximum size of the Hilbert space for full enumeration
-
networks
¶ A list of the names of the internal RBMs.
-
phase
(v)[source]¶ Compute the phase of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states . Returns: Vector containing the phases of the given states. Return type: torch.Tensor
-
probability
(v, Z)[source]¶ Evaluates the probability of the given vector(s) of visible states.
Parameters: - v (torch.Tensor) – The visible states.
- Z (float) – The partition function.
Returns: The probability of the given vector(s) of visible units.
Return type:
-
psi
(v)[source]¶ Compute the (unnormalized) wavefunction of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Complex object containing the value of the wavefunction for each visible state Return type: torch.Tensor
-
rbm_am
¶ The RBM to be used to learn the wavefunction amplitude.
-
rbm_ph
¶ RBM used to learn the wavefunction phase.
-
sample
(k, num_samples=1, initial_state=None, overwrite=False)[source]¶ Performs k steps of Block Gibbs sampling. One step consists of sampling the hidden state from the conditional distribution , and sampling the visible state from the conditional distribution .
Parameters: - k (int) – Number of Block Gibbs steps.
- num_samples (int) – The number of samples to generate.
- initial_state (torch.Tensor) – The initial state of the Markov Chain. If given, num_samples will be ignored.
- overwrite (bool) – Whether to overwrite the initial_state tensor, if it is provided.
-
save
(location, metadata=None)[source]¶ Saves the Wavefunction parameters to the given location along with any given metadata.
Parameters:
-
stop_training
¶ If True, will not train.
If this property is set to True during the training cycle, training will terminate once the current batch or epoch ends (depending on when stop_training was set).
-
subspace_vector
(num, size=None, device=None)[source]¶ Generates a single vector from the Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space.
- num (int) – The specific vector to return from the Hilbert space. Since the Hilbert space can be represented by the set of binary strings of length size, num is equivalent to the decimal representation of the returned vector.
- device – The device to create the vector on. Defaults to the device this model is on.
Returns: A state from the Hilbert space.
Return type:
Abstract Wavefunction¶
Note
This is an Abstract Base Class, it is not meant to be used directly. The following API reference is mostly for developers.
-
class
qucumber.nn_states.
Wavefunction
[source]¶ Bases:
abc.ABC
Abstract Base Class for Wavefunctions.
-
amplitude
(v)[source]¶ Compute the (unnormalized) amplitude of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Matrix/vector containing the amplitudes of v Return type: torch.Tensor
-
static
autoload
(location, gpu=False)[source]¶ Initializes a Wavefunction from the parameters in the given location.
Parameters: Returns: A new Wavefunction initialized from the given parameters. The returned Wavefunction will be of whichever type this function was called on.
-
compute_batch_gradients
(k, samples_batch, neg_batch, bases_batch=None)[source]¶ Compute the gradients of a batch of the training data (samples_batch).
If measurements are taken in bases other than the reference basis, a list of bases (bases_batch) must also be provided.
Parameters: - k (int) – Number of contrastive divergence steps in training.
- samples_batch (torch.Tensor) – Batch of the input samples.
- neg_batch (torch.Tensor) – Batch of the input samples for computing the negative phase.
- bases_batch (np.array) – Batch of the input bases corresponding to the samples in samples_batch.
Returns: List containing the gradients of the parameters.
Return type:
-
compute_normalization
(space)[source]¶ Compute the normalization constant of the wavefunction.
Parameters: space (torch.Tensor) – A rank 2 tensor of the entire visible space.
-
device
¶ The device that the model is on.
-
fit
(data, epochs=100, pos_batch_size=100, neg_batch_size=None, k=1, lr=0.001, input_bases=None, progbar=False, starting_epoch=1, time=False, callbacks=None, optimizer=<class 'torch.optim.sgd.SGD'>, **kwargs)[source]¶ Train the Wavefunction.
Parameters: - data (np.array) – The training samples
- epochs (int) – The number of full training passes through the dataset. Technically, this specifies the index of the last training epoch, which is relevant if starting_epoch is being set.
- pos_batch_size (int) – The size of batches for the positive phase taken from the data.
- neg_batch_size (int) – The size of batches for the negative phase taken from the data. Defaults to pos_batch_size.
- k (int) – The number of contrastive divergence steps.
- lr (float) – Learning rate
- input_bases (np.array) – The measurement bases for each sample. Must be provided if training a ComplexWavefunction.
- progbar (bool or str) – Whether or not to display a progress bar. If “notebook” is passed, will use a Jupyter notebook compatible progress bar.
- starting_epoch (int) – The epoch to start from. Useful if continuing training from a previous state.
- callbacks (list[qucumber.callbacks.Callback]) – Callbacks to run while training.
- optimizer (torch.optim.Optimizer) – The constructor of a torch optimizer.
- kwargs – Keyword arguments to pass to the optimizer
-
generate_hilbert_space
(size=None, device=None)[source]¶ Generates Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space. Defaults to the number of visible units.
- device – The device to create the Hilbert space matrix on. Defaults to the device this model is on.
Returns: A tensor with all the basis states of the Hilbert space.
Return type:
-
load
(location)[source]¶ Loads the Wavefunction parameters from the given location ignoring any metadata stored in the file. Overwrites the Wavefunction’s parameters.
Note
The Wavefunction object on which this function is called must have the same parameter shapes as the one who’s parameters are being loaded.
Parameters: location (str or file) – The location to load the Wavefunction parameters from.
-
max_size
¶ Maximum size of the Hilbert space for full enumeration
-
networks
¶ A list of the names of the internal RBMs.
-
phase
(v)[source]¶ Compute the phase of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Matrix/vector containing the phases of v Return type: torch.Tensor
-
probability
(v, Z)[source]¶ Evaluates the probability of the given vector(s) of visible states.
Parameters: - v (torch.Tensor) – The visible states.
- Z (float) – The partition function.
Returns: The probability of the given vector(s) of visible units.
Return type:
-
psi
(v)[source]¶ Compute the (unnormalized) wavefunction of a given vector/matrix of visible states.
Parameters: v (torch.Tensor) – visible states Returns: Complex object containing the value of the wavefunction for each visible state Return type: torch.Tensor
-
rbm_am
¶ The RBM to be used to learn the wavefunction amplitude.
-
sample
(k, num_samples=1, initial_state=None, overwrite=False)[source]¶ Performs k steps of Block Gibbs sampling. One step consists of sampling the hidden state from the conditional distribution , and sampling the visible state from the conditional distribution .
Parameters: - k (int) – Number of Block Gibbs steps.
- num_samples (int) – The number of samples to generate.
- initial_state (torch.Tensor) – The initial state of the Markov Chain. If given, num_samples will be ignored.
- overwrite (bool) – Whether to overwrite the initial_state tensor, if it is provided.
-
save
(location, metadata=None)[source]¶ Saves the Wavefunction parameters to the given location along with any given metadata.
Parameters:
-
stop_training
¶ If True, will not train.
If this property is set to True during the training cycle, training will terminate once the current batch or epoch ends (depending on when stop_training was set).
-
subspace_vector
(num, size=None, device=None)[source]¶ Generates a single vector from the Hilbert space of dimension .
Parameters: - size (int) – The size of each element of the Hilbert space.
- num (int) – The specific vector to return from the Hilbert space. Since the Hilbert space can be represented by the set of binary strings of length size, num is equivalent to the decimal representation of the returned vector.
- device – The device to create the vector on. Defaults to the device this model is on.
Returns: A state from the Hilbert space.
Return type:
-