How To Create Quantum Multinoulli Distributions With Qiskit
如何用QISKIT创建量子多努利分布
How To Create Quantum Multinoulli Distributions With Qiskit
用Qiskit建立量子多努利分布
Quantum machine learning in practice
量子机器学习在实践中的应用
Do you want to get started with Quantum Machine Learning? Have a look at Hands-On Quantum Machine Learning With Python.
你想从量子机器学习开始吗?看看用Python进行量子机器学习。
Probability distributions are ubiquitous in machine learning. It doesn’t matter whether we aim to answer the simplest question or solve the biggest problem. Technically, the goal always is to learn a yet unknown probability distribution from data.
概率分布在机器学习中无处不在。无论我们的目标是回答最简单的问题还是解决最大的问题。从技术上讲,目标总是从数据中了解未知的概率分布。
It doesn’t matter if we do a simple regression analysis or train a deep neural network, either. The goal always is to find that certain probability distribution.
不管我们是做一个简单的回归分析还是训练一个深度神经网络。我们的目标总是找到特定的概率分布。
And, it doesn’t even matter if you use quantum machine learning algorithms. In the end, the answer you’re looking for will be probabilistic.
而且,你是否使用量子机器学习算法并不重要。最后,你所寻找的答案将是概率性的。
But not all probability distributions are alike. They can be pretty heterogeneous. And each type of distribution has its specificities you’d better know. You’re in trouble if you don’t know what kind of distribution fits your problem at hand.
但并非所有的概率分布都是一样的。它们可以是相当多样化的。每一种分布类型都有它的特性,你最好知道。如果您不知道什么样的分发适合您手头的问题,那么您就有麻烦了。
Furthermore, you are not better off if you know which distribution fits your problem but don’t know how to generate an appropriate distribution. And, guess what, generating distributions with qubits is a challenge in itself.
此外,如果您知道哪个分布适合您的问题,但不知道如何生成一个合适的分布,也不会更好。而且,你猜怎么着,用量子位生成分布本身就是一个挑战。
Classical computers have billions of bits of memory. Sparing a few to store a probability distribution is no big deal. But a quantum computer has less than a hundred qubits. We should think about how we can represent a probability distribution efficiently. Fortunately, a qubit is not just 0 or 1. Even though it is only 0 or 1 if we measure it, it is in a quantum state of superposition. And this state contains a lot more information.
经典计算机有数十亿位的内存。节省一些时间来存储概率分布并不是什么大问题。但是量子计算机只有不到一百个量子位。我们应该考虑如何有效地表示概率分布。幸运的是,量子位不仅仅是0或1。即使我们测量它,它只是0或1,但它处于叠加的量子态。这个状态包含更多的信息。
Previously, we looked at how to create a quantum system that reproduces a Bernoulli distribution. The Bernoulli distribution is the simplest of all. There are only two possible values, each occurring with a certain probability. That’s pretty much what the superposition of a single qubit denotes.
之前,我们研究了如何创建一个再现伯努利分布的量子系统。伯努利分布是最简单的。只有两个可能的值,每一个都有一定的概率发生。这几乎就是单个量子位的叠加所表示的
Let’s say our single qubit is in state |?⟩=√0.7|0⟩+√0.3|1⟩. If you measure many qubits in this state, it results in the following Bernoulli distribution.
假设我们的单个量子比特处于状态=√0.70+√0.31。如果你在这种状态下测量很多量子比特,它会得到下面的伯努利分布。
The Bernoulli distribution answers simple questions you could answer with yes or no. For instance, did a passenger survive the Titanic shipwreck? Is this a cat? Does the student pass the exam?
伯努利分布可以回答简单的问题,你可以用“是”或“否”来回答。例如,有乘客在泰坦尼克号沉船事故中生还吗?这是只猫吗?这个学生考试及格了吗?
But what if we have multiple possible answers? Let’s say we want to label handwritten digits from the famous MNIST dataset? Apparently, we need to distinguish between more than two output values.
但如果我们有多种可能的答案呢?假设我们想从著名的MNIST数据集中标记手写数字?显然,我们需要区分两个以上的输出值。
We need multiple qubits to represent all categories, for we measure a single qubit as 0 or 1. Let’s say we want to label the digits from 0 to 7. Then, we have eight different categories. We could encode these possible outcomes with three qubits. Each combination of the qubit measurements represents a number. 000 represents the digit 0. 001 represents 1. 010 represents 2. 011 represents 3, and so on. Finally, 111 represents 7.
我们需要多个量子位来表示所有类别,因为我们将单个量子位测量为0或1。假设我们想要标记从0到7的数字。然后,我们有八个不同的类别。我们可以用三个量子位对这些可能的结果进行编码。量子位测量的每一个组合代表一个数字。000代表数字0。001代表1。010代表2。011表示3,依此类推。最后,111代表7。
As a result, we would get a Multinoulli distribution, also called the categorical distribution. It covers the case where an event will have one of multiple possible outcomes. Therefore, it generalizes the Bernoulli distribution that covers events with one out of precisely two possible outcomes.
结果,我们会得到一个多oulli分布,也被称为分类分布。它涵盖了一个事件将具有多个可能结果中的一个的情况。因此,它概括了包含两个可能结果中的一个事件的伯努利分布。
So, let’s say we have the following data of such a Multinoulli distribution.
假设我们有以下关于多努利分布的数据。
The practical question is: “How can we let a quantum circuit represent such a distribution?”
实际的问题是:“我们如何让量子电路代表这样的分布?”
The easiest method is to transform these probabilities into amplitudes and pass them to the initialize function of the quantum circuit.
最简单的方法是将这些概率转换成幅度,并将它们传递给量子电路的初始化函数。
The real magic happens in line 6. We compute the initial state of our three-qubit system. The whole trick is to take the square root of each probability because the probability is the square of a quantum state amplitude.
真正的神奇发生在第6行。我们计算了我们的三量子比特系统的初始状态。整个技巧就是取每个概率的平方根,因为概率是量子态振幅的平方。
The initialize function of the Qiskit QuantumCircuit takes a list of all amplitudes as an input parameter (see the official Qiskit documentation).
Qiskit QuantumCircuit的initialize函数将所有幅度的列表作为输入参数(请参阅Qiskit官方文档)。
When we execute this circuit with the 'statevector_simulator', it reproduces the exact distribution we specified.
当我们用'StateVector_Simulator'执行这个电路时,它会重现我们指定的精确分布。
Is there another way? Could we create a Multinoulli distribution by using quantum gates?
还有别的办法吗?我们能用量子门创造一个多努利分布吗?
Of course. But, it is not as straightforward.
当然了。但是,它并不那么简单。
We can use quantum gates to transform the state of qubits. For instance, we can use the qc.ry(theta, 0) function to rotate the state vector of the qubit at position 0 by the angle theta. Moreover, we can calculate the angle theta from a probability using a prob_to_angle function. Have a look at this post for further details.
我们可以用量子门来转换量子比特的状态。例如,我们可以使用qc.ry(theta,0)函数,将位置0处的量子比特的状态矢量旋转角度theta。此外,我们还可以使用prob_to_angle函数从概率中计算角度theta。请看这篇文章以了解更多细节。
The problem is that we use the value of a single qubit for different values in our distribution. For instance, the left-digit is 1 for all the states ['100', '101', '110', '111'].
问题是,在我们的分布中,我们将单个量子比特的值用于不同的值。例如,对于所有状态['100','101','110','111'],左位数为1。
So, when we specify the state of qubit 3 (the qubit at the left-hand side), we need to take the sum of the probabilities of all these states.
所以,当我们指定量子比特3的状态(左手边的量子比特)时,我们需要取所有这些状态的概率之和。
First, we define our convenience function prob_to_angle and define a QuantumRegister. The QuantumRegister is a container for our qubits that allows us to address single qubits easily in our Python code.
首先,我们定义我们的方便函数prob_to_angle并定义一个QuantumRegister。QuantumRegister是量子位的容器,它允许我们在Python代码中轻松地寻址单个量子位。
The action starts in line 9. There, we separate the values of our distribution from four to seven, calculate the sum of these states, and split our overall probability into halves. The following figure depicts the state thus far.
动作从第9行开始。在那里,我们把我们分布的值从四分到七分,计算这些状态的和,把我们的总体概率分成一半。下图描述了到目前为止的状态。
As a result, we distinguish only two groups of states. Those where the upper qubit is 0 and those where it is 1. (Note: the upper qubit is at the left-hand, lower side of the string in the figure.)
因此,我们只区分两组国家。上面的量子比特是0,上面的量子比特是1。(注意:上方的量子比特位于图中字符串的左手下方。)
In the next step, we further halve the second half. We apply the same logic. We split the states where the upper qubit is 1 into two quarters.
下一步,我们进一步把下半场减半。我们应用同样的逻辑。我们把上量子比特为1的状态分成两个四分之一。
The only difference to the first split is the quantum gate we use. This time, we use the controlled RY gate instead of the plain RY gate. The difference is that the controlled RY gate only applies the split of probabilities if the control qubit is 1. In our case, this means that we only split the ‘100’ block and leave the ‘000’ block untouched.
唯一不同于第一个分裂的是我们使用的量子门。这一次,我们使用受控的RY门,而不是普通的RY门。不同之处在于,受控RY门仅在控制量子比特为1时才应用概率的分裂。在我们的例子中,这意味着我们只拆分了'100'块,而保留'000'块不动。
Finally, we need one more split of the upper quarters. We have to take care of each quarter separately. First, we split the upper quarter into two halves. Now, we must only touch the state where the two upper qubits are 1. Therefore, we use the multi-controlled RY gate (qc.mcry).
最后,我们需要再切开上半部。我们得把每个季度的钱分开处理。首先,我们把上面的四分之一分成两半。现在,我们必须只触及两个上量子位为1的状态。因此,我们使用多控RY门(qc.mcry)。
At this point, the QuantumRegister comes in handy because we can access the qubits as an array. The term qr[1:] selects all qubits except the first. These are the control qubits for the multi-controlled RY gate. The last argument qr[0] denotes the target qubit.
这时,QuantumRegister就派上用场了,因为我们可以以数组的形式访问量子位。术语qr[1:]选择除第一个以外的所有量子位。这些是多控RY门的控制量子位。最后一个参数qr[0]表示目标量子比特。
We also need to split the lower quarter using another multi-controlled RY gate. We encapsulate it into NOT gates that we apply on the middle qubit. This lets us select the states where the middle qubit is 0 instead of 1.
我们还需要使用另一个多控制RY门分割较低的四分之一。我们把它封装到NOT门中应用到中间的量子位上。这让我们可以选择中间量子位为0而不是1的状态。
The result so far shows that we prepared half of the states.
到目前为止的结果表明我们准备了一半的状态。
We also need to perform the corresponding steps on the lower half to produce the overall distribution. Here’s the complete source code.
我们还需要在下半部分上执行相应的步骤,以产生整体分布。这是完整的源代码。
When we look at the result, we see that it resembles our Multinoulli distribution.
当我们观察结果时,我们发现它与我们的多努利分布相似。
The approach is straightforward. We repeatedly split the states into halves and apply the corresponding qubit transformation. However, it is a lot more code than simply initializing the three qubits with the probabilities.
方法很简单。我们反复地将状态分成两半,并应用相应的量子位变换。然而,这比简单地用概率初始化三个量子位要多得多的代码。
But in some situations, you won’t be able to specify all the probabilities during initialization. For instance, if you create a Quantum Bayesian Network, the distribution values may result from other variables. Then, you inevitably want to be able to create a Multinoulli distribution programmatically.
但在某些情况下,你不能在初始化时指定所有的概率。例如,如果您创建一个量子贝叶斯网络,分布值可能来自其他变量。然后,你不可避免地希望能够以编程的方式创建一个多oulli分布。
Do you want to get started with Quantum Machine Learning? Have a look at Hands-On Quantum Machine Learning With Python.
想入门量子机器学习吗?看看用Python进行量子机器学习的实践。
Get the first three chapters for free here.
在这里免费获得前三章。