This is the very first tutorial in a series in which we will discuss generative modelling. In a previous series we already discussed how supervised learning works. With these algorithms, the computers learns to classify distinguish data based on some labels we give him. Often we take the example were the algorithm gets labelled images of cats and dogs. At some point it will be able to classify also other cats and dogs.

But what happens when no labels are available ? This is the field of generative modelling is a sub-domain of machine learning (ML). The goal is to learn from (often unlabeled) data, to generate similar but previously unseen data. Generative Models have undergone a series of breakthroughs in the last decade, with state of the art generative models like GANs and Diffusion Models. As a very practical example I asked stable diffusion to provide me “a portrait of two female scientists sketching generative models on a white-board”. And as a result we obtained the provided image. Feel free to try it for yourself here.

But what are generative models and what do they have to do with quantum computers? In this tutorial, we will go over an example of an intuitive generative modelling task. It highlights many of the building blocks that we can later replace with quantum circuits.

The data for generative modelling

Just like in any machine learning task, we start out with data. This will be the training data for our machine learning algorithm.

Here we have 100 numbers stored in a numpy array. You can think of them as 100 images with one pixel each, or any other kind of data which only has 1 dimension.
Lets assume that this is data that we collected in the real world, and that there was a process behind the data generation, i.e., there exists a set of rules that produced this data. In more mathematical terms, this dataset consists of data samples , and the process that generated the data is characterized by the probablity distribution . This probability distribution is generally not known, but it is what we are interested in. Because our goal in the context of generative modelling is to build a model of , which encodes a probability distribution , and which approximates the true data distribution . We then want to generate new data samples using our model which follow similar rules as the true data, but we they are novel. We don’t have to go out into the real world again to collect all possible similar data, we just have to learn the rules behind how the data is created. However, the only evidence we have are the data samples, i.e., the above numpy array consisting of 100 numbers.

Lets take a first look at the data:

fig, ax = plt.subplots(1,1)
ax.scatter(data_samples, np.zeros(len(data_samples)), alpha=0.3)
ax.set_title("Occurences of the Data", fontsize=16)
ax.set_xlabel("Data Value", fontsize=16)
ax.set_ylim(-0.1, 0.3)
ax.set_yticklabels("")
ax.tick_params(labelsize=14)
plt.show()

This plot shows that the samples in the dataset are mostly in the domain [−1,1]. Lets check out the density of the data samples through a histogram plot:

We plot two histograms with wider and narrower bins, which gives us a sense of coarse and fine trends of the dataset distribution.

Designing a simple generative model

Do you see a familiar structure in that data? It may remind you of a gaussian distribution! In this simple example, could it be the case that the data was generated from two gaussians?
Gaussian distributions are characterized by the parameters (the mean of the gaussian) and (the width of the gaussian). The density of the distribution around each point in the data space is then defined as:

If we assume that the true underlying distribution is well-approximated by two gaussian distributions, we might be able to build a model, which represents our best guess of the underlying distribution for an optimal set of parameters and . The model density consisting of two gaussians and parameters can be defined as

Which appear to be reasonable initial guesses for the parameters? To keep it simple, we choose , which are close to the peaks that we observe in the histogram with wider bins. For the width of the gaussians, we remind ourselves that approximately of the area under a gaussian distribution is inside one standard deviation . So how about we choose .

model_mus = [-0.3, 0.6]
model_sigmas = [0.2, 0.2]

When we input these values for and into the model density function, and then scan values of , we get the following plot:

This already looks pretty good for a start into generative modelling! With this, we have implicitly defined the model distribution ! Keep in mind that and are not the same thing. Instead, represents the area below the curve below the function, i.e. its integral.

Generating new data from the model

But one crucial step is missing to make the a generative model: the generation part. Luckily, we known how to efficiently sample random numbers from gaussian distributions, and the numpy library provides simple functionality for this:

We now have a full-fledged generative model for 1-dimensional data samples, where we can tune the parameters and then sample efficiently from .

Summary and outlook

Let’s recap what we have done so far and how this exemplifies common tasks in generative modelling. The moving pieces that we saw were:

The dataset. How does it look like? Here, we had 100 1-dimensional data samples.

The model. We want to learn a model of the true underlying probability distribution of the data,. In other words, we want to learn the distribution from which the data itself come from. In our case the highly specialized gaussian distribution.

After we plotted a histogram with the data, we made an educated guess that the true underlying distribution of the data consists of two gaussian distributions. With reasonable initial parameter guesses, we are now able to generate samples which apparently follow a very similar distribution to the original data samples. But is this machine learning? Not quite. The learning part is missing. We have a generative model. However, we were only able to guess the underlying structure because the task is rather simple. In later tutorials, we will see how we can substitute the gaussian model by quantum circuits with many more parameters.

In more realistic cases, we won’t be able to guess such good initial models. This is why we build modern ML models with up to billions of parameters. Then we train them according to a loss function. And this is the topic of our next tutorial.

This is the very first tutorial in a series in which we will discuss generative modelling. In a previous series we already discussed how supervised learning works. With these algorithms, the computers learns to classify distinguish data based on some labels we give him. Often we take the example were the algorithm gets labelled images of cats and dogs. At some point it will be able to classify also other cats and dogs.

But what happens when no labels are available ? This is the field of generative modelling is a sub-domain of machine learning (ML). The goal is to learn from (often unlabeled) data, to generate similar but previously unseen data. Generative Models have undergone a series of breakthroughs in the last decade, with state of the art generative models like GANs and Diffusion Models. As a very practical example I asked stable diffusion to provide me

“a portrait of two female scientists sketching generative models on a white-board”. And as a result we obtained the provided image. Feel free to try it for yourself here.But what are generative models and what do they have to do with quantum computers? In this tutorial, we will go over an example of an intuitive generative modelling task. It highlights many of the building blocks that we can later replace with quantum circuits.

## The data for generative modelling

Just like in any machine learning task, we start out with data. This will be the

training datafor our machine learning algorithm.Here we have 100 numbers stored in a

`numpy`

array. You can think of them as 100 images with one pixel each, or any other kind of data which only has 1 dimension. Lets assume that this is data that we collected in the real world, and that there was a process behind the data generation, i.e., there exists a set of rules that produced this data. In more mathematical terms, this dataset consists of data samples , and the process that generated the data is characterized by the probablity distribution . This probability distribution is generally not known, but it is what we are interested in. Because our goal in the context of generative modelling is to build amodelof , which encodes a probability distribution , and which approximates the true data distribution . We then want to generate new data samples using our model which follow similar rules as the true data, but we they are novel. We don’t have to go out into the real world again to collect all possible similar data, we just have to learn the rules behind how the data is created. However, the only evidence we have are the data samples, i.e., the above numpy array consisting of 100 numbers.Lets take a first look at the data:

This plot shows that the samples in the dataset are mostly in the domain [−1,1]. Lets check out the

densityof the data samples through a histogram plot:We plot two histograms with wider and narrower bins, which gives us a sense of coarse and fine trends of the dataset distribution.

## Designing a simple generative model

Do you see a familiar structure in that data? It may remind you of a gaussian distribution! In this simple example, could it be the case that the data was generated from two gaussians?

Gaussian distributions are characterized by the parameters (the mean of the gaussian) and (the width of the gaussian). The

densityof the distribution around each point in the data space is then defined as:Lets code it up:

If we assume that the true underlying distribution is well-approximated by two gaussian distributions, we might be able to build a

model, which represents our best guess of the underlying distribution for an optimal set of parameters and . The model density consisting of two gaussians and parameters can be defined asWhich appear to be reasonable initial guesses for the parameters? To keep it simple, we choose , which are close to the peaks that we observe in the histogram with wider bins. For the width of the gaussians, we remind ourselves that approximately of the area under a gaussian distribution is inside one standard deviation . So how about we choose .

When we input these values for and into the model density function, and then scan values of , we get the following plot:

This already looks pretty good for a start into generative modelling! With this, we have implicitly defined the model distribution ! Keep in mind that and are not the same thing. Instead, represents the area below the curve below the function, i.e. its integral.

## Generating new data from the model

But one crucial step is missing to make the a generative model: the

generationpart. Luckily, we known how to efficiently sample random numbers from gaussian distributions, and the`numpy`

library provides simple functionality for this:We now have a full-fledged generative model for 1-dimensional data samples, where we can tune the parameters and then sample efficiently from .

## Summary and outlook

Let’s recap what we have done so far and how this exemplifies common tasks in generative modelling. The moving pieces that we saw were:

The dataset. How does it look like? Here, we had 100 1-dimensional data samples.The model. We want to learn a model of the true underlying probability distribution of the data,. In other words, we want to learn the distribution from which the data itself come from. In our case the highly specialized gaussian distribution.After we plotted a histogram with the data, we made an educated guess that the true underlying distribution of the data consists of two gaussian distributions. With reasonable initial parameter guesses, we are now able to generate samples which apparently follow a very similar distribution to the original data samples. But is this machine learning? Not quite. The

learningpart is missing. We have a generative model. However, we were only able to guess the underlying structure because the task is rather simple. In later tutorials, we will see how we can substitute the gaussian model by quantum circuits with many more parameters.In more realistic cases, we won’t be able to guess such good initial models. This is why we build modern ML models with up to billions of parameters. Then we train them according to a

loss function. And this is the topic of our next tutorial.