Path: blob/main/notebooks/quantum-machine-learning/qbm.ipynb
3855 views
Quantum Boltzmann Machine
In this section we will introduce a probabilistic model such as the Boltzmann Machine in its quantum version, which generates a distribution, , from a set of between-samples and can generate new samples.
Introduction
One focus of machine learning is probabilistic modelling, in which a probability distribution is obtained from a finite set of samples. If the training process is successful, the learned distribution has sufficient similarity to the actual distribution of the data that it can make correct predictions about unknown situations. Depending on the details of the distributions and the approximation technique, machine learning can be used to perform classification, clustering, compression, denoising, inpainting, or other tasks 1. In recent years the popularity of different applications using quantum computing has increased, this tutorial is based on generating one of the machine learning models in order to facilitate training. The purpose of this tutorial is to explain a probabilistic model based on the Boltzmann distribution, i.e. a Quantum Boltzmann Machine (QBM).
Classical Boltzmann Machines
Boltzmann Machines (BMs) offer a powerful framework for modelling probability distributions. These types of neural networks use an undirected graph structure to encode relevant information. More precisely,the respective information is stored in bias coefficients and connection weights of network nodes, which are typically related to binary spin-systems and grouped into those that determine the output, the visible nodes, and those that act as latent variables, the hidden nodes [1], [2].
Applications
Applications have been studied in a large variety of domains such as the analysis of quantum many-body systems, statistics, biochemistry, social networks, signal processing and finance [2].
Quantum Boltzmann Machine
Figure 1. General structure of a QBM.
Quantum Boltzmann Machines (QBMs) are a natural adaption of BMs to the quantum computing framework. Instead of an energy function with nodes being represented by binary spin values, QBMs define the underlying network using a Hermitian operator, a parameterized Hamiltonian.
Initialize circuit with random parameters
Measurements
Estimate mismatch between data and quantum outcomes
Update , and repeat 2 through 4 until covergence
The image below outlines the processes in a quantum Boltzmann machine:
Implementation
This example is based on the following article : Benedetti, M., Garcia-Pintos, D., Perdomo, O. et al. A generative modeling approach for benchmarking and training shallow quantum circuits. npj Quantum Inf 5, 45 (2019). https://doi.org/10.1038/s41534-019-0157-8
To begin this Tutorial we must call the necessary methods to generate circuits and that can be variational as is the case of the class ParameterVector.
Dataset
Considering the Bars and Stripes (or BAS) dataset, which is composed of binary images of size 2x2, for this tutorial only a subset that is enclosed in a circle is used, as follows
Figure 3. BAS dataset.
Mapping image to qubits
There are various ways of mapping values to qubits as we saw in the Data Encoding section, as these are binary images, i.e. they can only take 2 values, black-white, 0-255, 0-1. To work on the quantum computer, the Basis Encoding method will be used to convert images into states, i.e. each pixel is represented as a qubit, being as described in the following figure
Figure 4. Image to quantum state.
now consider the next conditions:
if the pixel is white, then the qubit value state is 0,
if the pixel is Black, then the qubit value state is 1.
For example in Figure 4, the image of size can be rewritten into a matrix
Based on the conditions, it can be seen that for the pixel at position is white, and this is equivalent to the qubit , then its state value is ; this logic is performed with pixels ,, which are the qubits respectively, as they are all white color their state would be for all of them. The result is the quantum state of the 4 qubits.
Performing this process for each of the images of the subset would look like this
Figura 5. Mapping the size in qubits.
in total are six quantum states, this can be rewritten as the linear combination, for this purpose it is necessary to consider the characteristic that
where is the number of the qubits and are the scalar values of each state, for this case we consider them purely real, and as each state has the same probability of being measured the following quantum state remains ,
which represents a probability distribution .
Note: Check that each quantum state representate a binary number and that in a decimal value, i.e.:
Question What happens if we use other state of interes with image of size 3x3?
Starting with the variablepx_output to generate the equivalent state it is necessary that
it is important to prove that the state vector satisfies the characteristics of eq(2), this is possible if we use the next line
The result must be a uniform distribution for the 6 states:0000, 0011, 0101, 1010, 1100, 1111 otherwise it is 0, the probability can be obtained from the following expression .
Design a Variational Quantum Circuit (Layer)
The QBM design based on [3] requires a Variational Quantum Circuit or ansatz that we can name as a layer and this can be repeated L times in order to obtain the desired distribution, in our case ofpx_output
Qiskit has some ansatz already implemented as is the case of RealAmplitudes, for this method requires the parameters number of qubits and the number of repetitions or L number of layers.
It is important to know that there are other ansatz other than RealAmplitudes in Qiskit here, except for this tutorial it will be based on an ansatz that follows the idea of the paper [3],"in this work, we use arbitrary single qubit rotations for the odd layers, and Mølmer-Sørensen XX gates for the even layers", we need remark the paper works in a trapped ion architecture. Design the ansatzes must use ParameterVector class, in order to generate gates that can vary their values.
Question What happens if we use a predefined ansatz for this problem?
The odd layer is possible consider the arbitrary single qubit rotation has the form . This is possible if we use the U gate, with a list of parameters.
For the case of the even layer, MGS XX can be represented in [4], it tells us that from the gate summation in all qubits of the quantum circuit we can obtain such a circuit.
Applying n layers
Due to a Qiskit method in the QuantumCircuit object our both ansatz can be converted into a quantum gate and we can indicate the number of repeats with the variablenum_layersthat are required to fit the expected output distribution.
We are going to make a quantum circuit with 3 layers, at the same time it is important to consider that there are two gates that are interleaved for each layer and these have different amount of parameters, so we look for the one that has more of these, i.e. max(paremers_odd,parameters_even), and that only the necessary ones are read per layer.
In order to verify that they are interleaving we use the decompose() method, to see that the same circuit is repeated in layer 1 and layer 3, using the odd layer, and the second circuit is the even layer.
Suggestion play with the number of layers and see how is the draw output
Build all the algorithm
At this point we have the data mapping process and the quantum circuit that represents the QBM, for this we need the optimization section and the cost function to be evaluated, for this we use the advantages of quantum computing in the model and the classical one in the optimization, as shown in Figure 6.
Figure 6. Hybrid algorithm process.
Cost Function
Consider the data set BAS, our goal is obtain an approximation to the target probability distributionpx_outputor . This is possible with a quantum circuit with gates parameterized by a vector ,where the layer index runs from 0 to , with the maximum depth of the quantum circuit [5], prepares a wave function from which probabilities are obtained to .Minimization of this quantity is directly related to the minimization of a well known cost function: the negative log-likelihood Is important consider that all the probabilities are estimated from a finite number of measurements and a way to avoid singularities in the cost function [3], we use a simple variant
(3)
where is a small number to be chosen. Following this equation we have a method calledboltzman_machine(params) which is the function that integrates all the quantum process and the cost function required to perform the optimization.
Having the quantum process that returns the cost, we use the classical process implemented in Qiskit, which has a series of classical optimizers.
Consider 10 epoch with 500 iterations.
From the plot of each result we can see that the best optimizer is COBYLA for this algorithm.
Qiskit has the opportunity to be able to work with more optimisers that are here .
Suggestion Try changing the value of themaxitervariable and the optimisers in order to identify the best case.
the boltzman_machine_valid method is performed to give us
Visualization
We obtain the output of the three different optimizers
In order to compare all the results of each optimizer with the expected output, the plot_histogram method is used and we can see at a glance the similarities of each distribution.
It can be seen that of all the visualization methods, the closest is the one used with COBYLA, which will be considered to generate new samples from this dataset. But you are still invited to look for other optimizers and change the number of the maxiter variable.
Using our QBM
To apply this circuit we use the final parameters and see what kind of results they produce as images. This is shown below.
Check how many images do not follow the expected distribution.
Depending on the Dial distributor we expect with higher probability the desired states, but if there is a percentage of an incorrect state it may sometimes appear with its respective probability.
Noise model
We managed to realize a quantum circuit that is a QBM and to see its effectiveness in a more complex environment than the simulation we will perform the process in a noise model, for this we must consider having our paltaform key in our system.
For this example we will use the real ibmq_lima gate, but you can choose from the ones found here.
We can use the ibmq_lima features for our simulator and it can perform at the same characteristics of this one.
To know more about the features of the NoiseModel method here
We performed the same process that we did previously in simulation but adapted it with the noise model variables.
Consider run but using a new variable callnoise_params and we can consider the best optimizer,COBYLAin stead of using the others one.
At this point we have the distribution result of the Noise model
The expected output, the best simulated result and the best simulated result using a NoiseModel are compared.
Having each result follow the trend of the expected output, but with certain errors, we can see that our circuit is working, and we can continue this by trying to obtain new samples from the circuit with noise.
Check how many images do not follow the expected distribution.
Real quantum computer
The quantum instance for real free quantum computer (try to change the value for provider.get_backend( )), in this tutorial we use the ibmq_lima the same that i nthe noise model. More information about this quantum computer you can find here.
We follow the same processing of the simulation, but in this case we use the backend which is the real computer.
For this process being a real free computer, anyone with their IBM account can use any of the free computers, so the process can take some time, for this tutorial we will only use10 iterations
In this point we have the result of the real quantum computer for our QBM.
We can compare all the result in a same plot_histogram, and check the worst case is the result of the quantum real computer, that could be solve, using more iteration, mitigate the error, modified the ansatz or both.
Just to confirm what are the possible outputs of our circuit using the real computer, this is given by the type of distribution obtained from the computer.
Check how many images do not follow the expected distribution.
Another perspective
For this part we can do the same procces and using the reference [5] with the proposal to design another Ansatz model, the characteristics are:
Using the same layer arbitrary rotation gate,
And employ CNOT gates with no parameters for the entangle layers.
The new ansatz it could be
We apply this ansatz how an gate like the previous part, how is only a gate we don't now use the parameter flag
We are going to make a quantum circuit with 3 layers, where each gate are the same structure.
Now we are using decompose(), it is observed that if the new structure consists of the same circuit for each layer.
Experiments
As in the previous section, we will train with simulation. In this ansatz we are using only 3 layers
The process is repeated to confirm the best optimizer using this new ansatz
Obtain the for each optimizer
It is reconfirmed that the best case is using COBYLA
Applying now the distribution obtained from the simulation result
Check how many images do not follow the expected distribution.
Exercise Redesign the methods for the noise model and the real computer and see what the results are.
Suggestion
You can use a 3x3 or 4x4 image for that consider reference [5].
Extending the problem
For this section consider this next set of images of size and generate the state representing this distribution:
We'll use the mapping:
You can use the "Mapping image to qubits" section above to help.
Use the next code to representate, only you need to identify the init_list values that represents our set of images. And consider for this problem 9 qubits.
Now we can plot the distribution using plot_histogram method.
Now you are going to design an ansatz, it is important to keep in mind with 9 qubits.
Validate with the following code that a quantum gate is being developed from your proposed ansatz.
of each gate we check that it is equivalent to our ansatz using decompose()
Based on the above examples, fill in what we are missing
Complete the code you have to run our QBM, for this consider an optimizer and the number of iterations to use, remember in the optimizer you have the variable maxiter
Now, we obtain the result in a distribution
Finally, we plot the results
If there is no problem up to this point, you will have a distribution similar to the one we have designed,congratulations!
But for the end we leave the following question, can we decrease the number of qubits?
References
Amin, Mohammad & Andriyash, Evgeny & Rolfe, Jason & Kulchytskyy, Bohdan & Melko, Roger. (2016). Quantum Boltzmann Machine. Physical Review X. 8. 10.1103/PhysRevX.8.021050 https://arxiv.org/pdf/1601.02036.pdf .
Zoufal, Christa & Lucchi, Aurelien & Woerner, Stefan. (2021). Variational quantum Boltzmann machines. Quantum Machine Intelligence. 3. 10.1007/s42484-020-00033-7 https://arxiv.org/pdf/2006.06004.pdf.
Benedetti, Marcello & Garcia-Pintos, Delfina & Nam, Yunseong & Perdomo-Ortiz, Alejandro. (2018). A generative modeling approach for benchmarking and training shallow quantum circuits. npj Quantum Information. 5. 10.1038/s41534-019-0157-8. https://arxiv.org/pdf/1801.07686.pdf paper
Rudolph, Manuel & Bashige, Ntwali & Katabarwa, Amara & Johr, Sonika & Peropadre, Borja & Perdomo-Ortiz, Alejandro. (2020). Generation of High Resolution Handwritten Digits with an Ion-Trap Quantum Computer. https://arxiv.org/pdf/2012.03924.pdf
Jinguo, Liu & Wang, Lei. (2018). Differentiable Learning of Quantum Circuit Born Machine. Physical Review A. 98. 10.1103/PhysRevA.98.062324. https://arxiv.org/pdf/1804.04168.pdf