sf.apps.train.VGBS¶

class VGBS(A, n_mean, embedding, threshold, samples=None)[source]

Bases: object

Create a variational GBS model for optimization and machine learning.

An input adjacency matrix $$A$$ can be varied using:

$A(\theta) = W(\theta) A W(\theta),$

with $$W$$ a diagonal matrix of weights that depend on a set of parameters $$\theta$$.

By varying $$\theta$$, the distribution of samples from GBS can be trained to solve stochastic optimization and unsupervised machine learning problems.

The above variational model can be used in both the threshold and PNR modes of GBS, which is specified using the threshold flag. An initial value for the mean number of clicks (if threshold=True) or mean number of photons (if threshold=False) is also required. The initial value n_mean should be set high since varying $$\theta$$ can only lower the mean number of clicks or photons.

The mapping from $$\theta$$ to $$W(\theta)$$ is specified using the embedding argument. An embedding from the embed module such as Exp must be used, which uses the simple embedding $$W(\theta) = \exp(-\theta)$$.

Example usage:

>>> g = nx.erdos_renyi_graph(4, 0.7, seed=1967)
>>> A = nx.to_numpy_array(g)
>>> embedding = train.embed.Exp(4)
>>> vgbs = VGBS(A, 3, embedding, threshold=True)
>>> params = np.array([0.05, 0.1, 0.02, 0.01])
>>> vgbs.A(params)
array([[0.        , 0.30298161, 0.31534653, 0.31692721],
[0.30298161, 0.        , 0.30756059, 0.30910225],
[0.31534653, 0.30756059, 0.        , 0.32171695],
[0.31692721, 0.30910225, 0.32171695, 0.        ]])
>>> vgbs.n_mean(params)
2.299036355948707
>>> vgbs.generate_samples(vgbs.A(params), 2)
array([[1, 1, 1, 0],
[0, 1, 0, 1]])
Parameters
• A (array) – the input adjacency matrix $$A$$

• n_mean (float) – the initial mean number of clicks or photons

• embedding – the method of converting from trainable parameters $$\theta$$ to $$W(\theta)$$. Must be an embedding instance from the embed module.

• threshold (bool) – determines whether to use GBS in threshold or PNR mode

• samples (array) – an optional array of samples from $$A$$ used to speed up gradient calculations

 A(params) Calculate the trained adjacency matrix $$A(\theta)$$. W(params) Calculate the diagonal matrix of weights $$W$$ that depends on the trainable parameters $$\theta$$. add_A_init_samples(samples) Add samples of the initial adjacency matrix to A_init_samples. generate_samples(A, n_samples, **kwargs) Generate GBS samples from an input adjacency matrix. get_A_init_samples(n_samples) Get samples from the initial adjacency matrix. mean_clicks_by_mode(params) Calculate the mean number of clicks in each mode when using the trainable parameters $$\theta$$. mean_photons_by_mode(params) Calculate the mean number of photons in each mode when using the trainable parameters $$\theta$$. n_mean(params) Calculates the mean number of clicks or photons. prob_sample(params, sample) Calculate the probability of a given sample being generated by the VGBS system using the trainable parameters $$\theta$$.
A(params)[source]

Calculate the trained adjacency matrix $$A(\theta)$$.

Example usage:

>>> vgbs.A(params)
array([[0.        , 0.30298161, 0.31534653, 0.31692721],
[0.30298161, 0.        , 0.30756059, 0.30910225],
[0.31534653, 0.30756059, 0.        , 0.32171695],
[0.31692721, 0.30910225, 0.32171695, 0.        ]])
Parameters

params (array) – the trainable parameters $$\theta$$

Returns

Return type

array

W(params)[source]

Calculate the diagonal matrix of weights $$W$$ that depends on the trainable parameters $$\theta$$.

Example usage:

>>> vgbs.W(params)
array([[0.97530991, 0.        , 0.        , 0.        ],
[0.        , 0.95122942, 0.        , 0.        ],
[0.        , 0.        , 0.99004983, 0.        ],
[0.        , 0.        , 0.        , 0.99501248]])
Parameters

params (array) – the trainable parameters $$\theta$$

Returns

the diagonal matrix of weights

Return type

array

Warning

The added samples must be from the input adjacency matrix and not the trained one $$A(\theta)$$.

Example usage:

>>> samples = np.array([[0, 1, 0, 0], [0, 1, 1, 1]])
Parameters

samples (array) – samples from the initial adjacency matrix

generate_samples(A, n_samples, **kwargs)[source]

Generate GBS samples from an input adjacency matrix.

Example usage:

>>> vgbs.generate_samples(vgbs.A(params), 2)
array([[1, 1, 1, 0],
[0, 1, 0, 1]])
Parameters
• A (array) – the adjacency matrix

• n_samples (int) – the number of GBS samples to generate

• **kwargs – additional arguments to pass to the sampler from The Walrus

Returns

the generated samples

Return type

array

get_A_init_samples(n_samples)[source]

Get samples from the initial adjacency matrix.

This function checks for pre-generated samples in A_init_samples. If there are fewer than n_samples stored, more samples are generated and added to A_init_samples.

Parameters

n_samples (int) – number of samples to get

Returns

samples from the initial adjacency matrix

Return type

array

mean_clicks_by_mode(params)[source]

Calculate the mean number of clicks in each mode when using the trainable parameters $$\theta$$.

Example usage:

>>> vgbs.mean_clicks_by_mode(params)
array([0.57419812, 0.5680168 , 0.57781579, 0.57900564])
Parameters

params (array) – the trainable parameters $$\theta$$

Returns

a vector giving the mean number of clicks in each mode

Return type

array

mean_photons_by_mode(params)[source]

Calculate the mean number of photons in each mode when using the trainable parameters $$\theta$$.

Example usage:

>>> vgbs.mean_photons_by_mode(params)
array([1.87217857, 1.8217392 , 1.90226515, 1.91225543])
Parameters

params (array) – the trainable parameters $$\theta$$

Returns

a vector giving the mean number of photons in each mode

Return type

array

n_mean(params)[source]

Calculates the mean number of clicks or photons.

Evaluates the mean number of clicks or photons of the VGBS system when using the trainable parameters $$\theta$$. The mean number of clicks is returned when threshold is True, otherwise the mean number of photons is returned.

Example usage:

>>> vgbs.n_mean(params)
2.299036355948707
Parameters

params (array) – the trainable parameters $$\theta$$

Returns

the mean number of clicks or photons

Return type

float

prob_sample(params, sample)[source]

Calculate the probability of a given sample being generated by the VGBS system using the trainable parameters $$\theta$$.

Parameters
• params (array) – the trainable parameters $$\theta$$

• sample (array) – the sample

Returns

the sample probability

Return type

float