Abstract: MindSpore officially open-sourced the quantum machine learning library MindQuantum on 3.28. This article introduces the key technologies of MindQuantum.
This article is shared from the HUAWEI cloud community " MindSpore Quantum Machine Learning Library MindQuantum ", author: HWCloudAI.
MindSpore officially open-sourced the quantum machine learning library MindQuantum on 3.28. This article introduces the key technologies of MindQuantum. Before introducing MindQuantum, let's briefly explain the related concepts of quantum computing.
Basic concepts of quantum computing
Quantum computing is one of the most disruptive technologies in the world today, and various countries and companies are increasing their R&D investment in this field. The concept of quantum computer was first proposed by the famous physicist Feynman in 1981. According to his idea, a quantum computer that uses the principles of quantum mechanics can simulate complex quantum systems faster than classical computers. Due to the entanglement, superposition and parallelism of quantum systems, people can implement some algorithms with polynomial or even exponential acceleration, such as the Shor algorithm for large number decomposition and the Grover algorithm for quantum search. In order for everyone to quickly enter the gate of quantum computing, we first need to understand some basic concepts, including qubits for information storage and quantum gates for logical operations on qubits.
Qubit
In classic computers, people usually use the level of potential to represent binary 0 and 1, and then use semiconductor devices such as transistors to control the current to complete general logic operations. However, at the same time, the same bit can only be at one of the low potential and the high potential. In the magical quantum world, some entities can be in two different states at the same time. For example, the spin of an electron can be in an upward state and a downward state at the same time. If we denote the upward and downward states as 0 and 1, respectively
Quantum logic gate
Quantum circuit
We can measure the quantum state after the quantum circuit has evolved, extract information from the quantum state, and perform subsequent operations.
Next, we use MindQuantum to evolve the above circuit and get the final quantum state. First, follow the installation guide complete the installation of MindQuantum, and run the following code to get the final state quantum state.
import numpy as np
from mindquantum import Circuit
from mindquantum.highlevel import StateEvolution
c = Circuit()
c.h(0)
c.x(1, 0)
c.ry('theta', 2)
state = StateEvolution(c).final_state({'theta': np.pi/2}, ket=True)
print(state)
The output is as follows:
0.5¦000⟩
0.5¦011⟩
0.5¦100⟩
0.5¦111⟩
Therefore, we get the final quantum state as:
Application of quantum in machine learning
As early as the last century, scientists put forward the concept of quantum perceptron for machine learning. In the past 20 years, more and more quantum algorithms for machine learning have been discovered, including the HHL algorithm for solving linear equations, and quantum principal component analysis and quantum support vector machines based on this. The following figure shows the acceleration effect of various quantum machine learning algorithms on the best classical algorithms.
However, the meaningful practical application of these algorithms requires tens of thousands or even millions of qubits. In the current NISQ (Noisy Intermediate-Scale Quantum) stage, the number of bits of real quantum computers can only reach tens to hundreds. And the system contains more noise. In order to demonstrate the advantages of quantum computers in the NISQ stage, people have developed a quantum classical hybrid machine learning library. Classical computers and quantum computers complement each other to complete a complex task, such as quantum chemical simulation and combinatorial optimization.
Quantum machine learning library MindQuantum
MindQuantum is a quantum machine learning library developed by combining MindSpore and HiQ, which supports the training and inference of a variety of quantum neural networks. Thanks to the quantum computing research and development capabilities of Huawei's HiQ team and the high-performance automatic differentiation capabilities of MindSpore, MindQuantum can efficiently handle quantum machine learning, quantum chemical simulation, and quantum optimization problems, and its performance reaches the industry's TOP1. It is a great service for researchers, teachers and students. Provides an efficient platform for the rapid design and verification of quantum machine learning algorithms.
The figure below is the architecture diagram of MindQuantum. At present, we use quantum simulator operators to simulate quantum systems, which can complete the functions of forward propagation and gradient calculation. On this basis, we have quantum algorithm libraries, such as quantum neural The Internet, the VQE of quantum chemical simulation and the QAOA of quantum optimization algorithm, etc., and beyond, there are quantum applications. We can use MindQuantum's quantum algorithm to perform tasks such as machine learning, chemical simulation, and operational optimization.
In MindQuantum, the structure of the quantum neural network is described in the following figure. The quantum circuit is composed of three blocks. The coding circuit will encode classical data into the quantum state, and then the circuit to be trained. We can adjust the logic gates in the circuit. Parameters to make the final measurement results meet expectations.
Through MindQuantumLayer in MindQuantum, we can easily build a quantum machine learning layer, and can seamlessly form a larger machine learning network with other operators in MindSpore. Below, we combine a simple example to experience the MindQuantum quantum machine learning library.
First experience with MindQuantum quantum neural network
1. Construction of Quantum Circuit
import numpy as np
from mindquantum.ops import QubitOperator
from mindquantum import Circuit, Hamiltonian
encoder = Circuit().rx('alpha', 0).ry('beta', 0).no_grad()
ansatz = Circuit().rx('a', 0).ry('b', 0)
circ = encoder + ansatz
ham = Hamiltonian(QubitOperator('Z0'))
alpha, beta = 0.5, 1.2
encoder_data = np.array([[alpha, beta]]).astype(np.float32)
2. Build a training network
from mindquantum.nn import MindQuantumLayer
import mindspore as ms
class Net(ms.nn.Cell):
def __init__(self, pqc):
super(Net, self).__init__()
self.pqc = pqc
def construct(self, x):
return -self.pqc(x)
pqc = MindQuantumLayer(['alpha', 'beta'], ['a', 'b'], circ, ham)
train_net = Net(pqc)
3. Training
opti = ms.nn.Adam(train_net.trainable_params(), 0.2)
net = ms.nn.TrainOneStepCell(train_net, opti)
for i in range(100):
print(net(ms.Tensor(encoder_data)))
The final convergence result is -0.993. At the same time, we can also obtain the fidelity of the final quantum state and the target state.
from mindquantum.highlevel import StateEvolution
a, b = pqc.weight.asnumpy()
pr = {'alpha': alpha, 'beta': beta, 'a': a, 'b': b}
state = StateEvolution(circ).final_state(pr)
fid = np.abs(np.vdot(state, [1, 0]))**2
Through the above training, we use the quantum neural network to offset the system error, so that the fidelity of the final quantum state reaches 99.9999%.
More examples
In addition to the simple examples above, we also give examples of using MindQuantum for natural language processing, quantum chemical simulation, combinatorial optimization solving, and handwriting recognition. For details, please refer to the following link: https://gitee.com/mindspore/ mindquantum/tree/master/tutorials
Future outlook
In the future, we will continue to enrich the quantum neural network model and support more hardware backends. We also welcome quantum machine learning enthusiasts who are interested to join us to jointly develop and maintain the MindQuantum open source community.
After understanding the key technology of MindSpore, is it very exciting? Hurry up [click on the link] and [register now], you can learn a classic case on the ModelArts platform to master deep learning based on MindSpore!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。