A lightweight and fast quantum circuit simulation implementation system based on PyTorch
Through the PyTorch-based quantum circuit simulation system, the matrix multiplication calculation of the modular quantum gate circuit and the calculation backend are automatically selected, which solves the problems of inflexible quantum circuit simulation and slow calculation in the prior art, and realizes efficient and flexible quantum circuit simulation calculation.
Patent Information
- Application Number
- CN202210003901.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-04
- Publication Date
- 2025-05-30
- Estimated Expiration
- 2042-01-04
AI Technical Summary
The existing quantum circuit simulation framework has problems such as inflexibility and slow calculation, especially when the parameters of the quantum circuit are updated, the unitary matrix needs to be recalculated, which greatly slows down the computing speed.
The lightweight and fast quantum circuit simulation implementation system is adopted based on PyTorch. Through the matrix multiplication calculation process of the modular quantum gate circuit, the time complexity of the calculation is reduced, and the appropriate computing backend is automatically selected based on the user's computing device to improve the computing speed.
It realizes more efficient and flexible quantum circuit simulation and calculation, reduces the complexity of computing time, improves the computing speed, and can better cope with complex quantum circuit simulation implementation scenarios.
Smart Images

Figure CN114528995B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a technology in the field of quantum computing, specifically a lightweight and fast quantum circuit simulation implementation system based on PyTorch. Background Art
[0002] Classical computers are increasingly difficult to provide sufficient computing speed, and the birth of quantum computing brings new solutions to many computer-related problems. Existing quantum circuit calculation simulation frameworks implemented using Python mainly support the definition and modular operation of quantum circuits. After the quantum circuit is defined, the functions therein can be directly called to obtain the unitary matrix form corresponding to the quantum circuit, which greatly facilitates the implementation of quantum autoencoders and logarithmic encodings. However, since the unitary matrix needs to be recalculated every time the parameters are updated, the operation speed of the quantum circuit is greatly slowed down.
[0003] The existing quantum circuit calculation simulation framework TensorFlow Quantum developed based on Cirq can directly convert the quantum circuit defined by Cirq into a special tensor that can be used in TensorFlow. However, this framework does not support the modular operation of quantum circuit calculations well, and it is difficult to use it to implement calculation modules for some special quantum circuits and quantum encodings (such as quantum autoencoders and logarithmic encodings). Summary of the Invention
[0004] In view of the problems existing in the existing quantum circuit simulation frameworks, such as inflexibility and slow calculation, the present invention proposes a lightweight and fast quantum circuit simulation implementation system based on PyTorch. By utilizing the characteristics of quantum gate circuits, the matrix multiplication calculation process in quantum circuit simulation is modularized, reducing the time complexity of calculation; at the same time, the appropriate calculation backend is automatically selected according to the user's computing device, greatly improving the calculation speed, and being able to more efficiently and flexibly handle complex quantum circuit simulation implementation scenarios.
[0005] The present invention is implemented through the following technical solutions:
[0006] The present invention relates to a lightweight and fast quantum circuit simulation implementation system based on PyTorch, including: a quantum circuit design module, a quantum circuit conversion module, and a quantum circuit calculation module, wherein: the quantum circuit design module constructs a corresponding quantum circuit object according to the quantum circuit description input by the user; the quantum circuit conversion module converts and compiles according to the quantum circuit object to obtain a corresponding quantum circuit PyTorch object; the quantum circuit calculation module performs quantum circuit simulation calculation according to the quantum circuit PyTorch object and the quantum state input by the user to obtain the measurement result for constructing a complete quantum circuit model.
[0007] The described quantum circuit description includes all types, orders, and name information of quantum gates in the quantum circuit.
[0008] The built-in quantum circuit design module has member functions for adding gate circuits corresponding to different types of quantum gates. According to user calls, the corresponding quantum gate information is added and a quantum circuit object is constructed. This quantum circuit object stores the types, orders, and name information of the quantum gates included in the user-input quantum circuit description through member variables of the list type.
[0009] The described quantum circuit PyTorch object includes parameter information corresponding to the quantum gates included in the quantum circuit object.
[0010] The conversion refers to: according to all the quantum gate information stored in the form of a list inside the quantum circuit object, it is converted one by one into the parameter matrices corresponding to the quantum gates and stored in the sequential list of the quantum circuit PyTorch object in order. This sequential list includes an optimized parameter matrix and a non-optimized parameter matrix. Among them, the optimized parameter matrix corresponds to a quantum gate with parameters (such as U-Gate), and the non-optimized parameter matrix corresponds to a quantum gate without parameters (such as H-Gate).
[0011] The compilation refers to: traversing the sequential list of the quantum circuit PyTorch object in order, and merging two adjacent non-optimized parameter matrices to reduce the computational time overhead caused by consecutive non-optimized parameter matrices.
[0012] The described quantum circuit simulation calculation refers to: for the first parameter matrix stored in the sequential list of the quantum circuit PyTorch object, performing a matrix multiplication operation with the quantum state input by the user as the operation result corresponding to this parameter matrix; for the remaining parameter matrices stored in the sequential list of the quantum circuit PyTorch object, performing a matrix multiplication operation with the operation result corresponding to the previous parameter matrix in the sequential list as the operation result corresponding to this parameter matrix; the operation result corresponding to the last parameter matrix in the sequential list is the calculation result of the quantum circuit.
[0013] The matrix multiplication operation mentioned above refers to: automatically selecting a dense matrix or sparse matrix backend program according to the computing device used by the user when running the program of the quantum circuit simulation implementation system, and performing matrix multiplication operations on the input matrix using a specific PyTorch matrix multiplication interface. When the user uses a CPU device, the quantum circuit calculation module selects a sparse matrix calculation backend program, converts all the parameter matrices stored in the sequential list into sparse parameter matrices, and performs matrix multiplication operations using the PyTorch sparse matrix multiplication interface; when the user uses a GPU device or other devices with high-performance parallel computing capabilities, the quantum circuit calculation module selects a dense matrix calculation backend program and directly performs matrix multiplication operations using the PyTorch dense matrix multiplication interface.
[0014] The quantum circuit simulation calculation mentioned above further includes a conjugate transpose transformation operation, that is: by setting an additional conjugate transpose flag, it is determined whether to calculate after performing a conjugate transpose operation on the quantum circuit corresponding to the input quantum circuit PyTorch object. When performing the conjugate transpose operation, all the parameter matrices in the sequential list inside the quantum circuit PyTorch object are respectively subjected to matrix conjugate transpose calculations, and the sequential list is flipped front and back and stored in the quantum circuit PyTorch object.
[0015] The formation of a complete quantum circuit model mentioned above means that: the user input quantum state, parameter matrices, and operation results corresponding to the parameter matrices involved in the quantum circuit calculation module are all stored in the computer memory in the form of PyTorch tensors. By passing PyTorch tensors, multiple quantum circuit calculation modules are connected in series, or the quantum circuit calculation module is connected in series with other custom modules that can receive or output PyTorch tensors to construct a complete quantum circuit model. This model can receive different types of model inputs and calculate different types of model outputs. This model can achieve the optimization of the parameter matrices containing optimization parameters inside the quantum circuit PyTorch module through the PyTorch optimizer.
[0016] The present invention relates to a quantum circuit simulation method based on the above system. According to the quantum circuit description input by the user, a quantum circuit PyTorch object is converted and output, and the quantum circuit calculation module receives the quantum state and the quantum circuit PyTorch object input by the user to obtain a quantum circuit calculation result. This method specifically includes:
[0017] Step 1: The user uses the quantum circuit design module to define the quantum gates in the quantum circuit and generate a quantum circuit object.
[0018] Step 2: Use the quantum circuit conversion module to convert the above quantum circuit object into a quantum circuit PyTorch object. This module will perform conversion and compilation operations on the quantum circuit object.
[0019] Step 3: The user uses the generated quantum circuit PyTorch object and combines it with other user-defined modules to construct a complete quantum circuit model or a hybrid quantum circuit model.
[0020] Step 4: Use the quantum circuit calculation module to receive the quantum circuit PyTorch object and the quantum state input by the user. The quantum circuit calculation module automatically selects the backend program according to the computing device and outputs the calculation result. After obtaining the calculation result, the user can use a custom optimization scheme (such as a PyTorch optimizer) to optimize the matrix containing optimization parameters stored in the quantum circuit PyTorch object.
[0021] Technical effects
[0022] In the quantum circuit conversion module of the present invention, compilation is used to accelerate the operation process of the subsequent quantum circuit calculation module; in the quantum circuit calculation module, the sparse matrix / dense matrix backend program is selected for calculation according to the device type used by the user to optimize the operation efficiency; in the quantum circuit calculation module, the sequential operation method is used to perform matrix multiplication operations on the parameter matrix stored in the sequential list in the quantum circuit PyTorch object with the quantum state input by the user or the operation result corresponding to the previous parameter matrix in the sequential list respectively, rather than first performing matrix chain multiplication operations on the parameter matrix stored in the sequential list in the quantum circuit PyTorch object in sequence to construct the unitary matrix of the quantum circuit corresponding to the quantum circuit PyTorch object, and then performing matrix multiplication with the quantum state input by the user. This operation reduces the complexity of the single quantum gate circuit simulation calculation from O(2 3 n) to O(2 2 n), accelerating the calculation process; the quantum circuit conversion module converts and compiles the quantum circuit module into a quantum circuit PyTorch object, and the input and output matrices of the quantum circuit calculation module are stored in the computer memory in the form of PyTorch tensors, which is convenient for the user to use PyTorch and other custom modules to combine and construct a quantum circuit training model, increasing the flexibility of quantum circuit simulation calculation. Description of the drawings
[0023] Figure 1 is the flow chart of the present invention;
[0024] Figure 2 is the schematic diagram of the quantum circuit structure of the embodiment;
[0025] Figure 3 is the schematic diagram of the loss and accuracy curves of the user-defined quantum circuit model in the optimization process in the embodiment;
[0026] Figure 4Comparison chart of the optimization speed of the user-defined quantum circuit model in the embodiment and the running speed of the corresponding model of the same quantum circuit in Paddle Quantum. Detailed implementation
[0027] As Figure 1 shown, this embodiment relates to a quantum circuit model for image classification tasks implemented based on the above-mentioned quantum circuit simulation implementation system, including: the above-mentioned quantum circuit design module, quantum circuit conversion module, quantum circuit calculation module, and user-defined input image and class label, where: the quantum circuit design module obtains a quantum circuit object according to the quantum circuit description defined by the user; the quantum circuit conversion module compiles and converts the quantum circuit object to obtain a quantum circuit PyTorch module, and the quantum circuit calculation module performs calculation processing based on the quantum circuit PyTorch module and the quantum state input by the user to obtain the measurement result of the quantum circuit; the result is combined with the PyTorch optimizer defined by the user to implement the image classification task. The quantum circuit input by the user in this embodiment is a quantum classifier circuit based on amplitude quantum encoding for pneumonia image classification tasks, and is implemented and optimized using the above-mentioned quantum circuit design module, quantum circuit conversion module, quantum circuit calculation module, user-defined input and output processing module, and PyTorch optimizer.
[0028] The size of the image input by the user processed in this embodiment is 28×28 pixels. The data set includes 4,708 training images and 624 test images. The label contained in each image represents whether the image belongs to a positive / negative sample. The pixels of each image are arranged as a one-dimensional vector as the input, and the binary classification result (0 or 1) is used as the output.
[0029] In the user-defined input processing module, the above-mentioned image vector is encoded as a quantum state in an amplitude manner and input into the quantum circuit calculation module for calculation.
[0030] The quantum circuit input by the user in the quantum circuit design module consists of 10 qubits. The quantum circuit defined by the user using the quantum circuit design module is as Figure 2 shown. This circuit consists of 3 identical image processing parts. In each image processing part, the RZ, RY, and RZ gates will act on each qubit in the quantum circuit respectively, and adjacent two qubits will establish entanglement through the CNOT gate respectively.
[0031] In the last layer of the above-mentioned user-defined quantum circuit, a measurement operation is performed on the 0th qubit. The measurement result obtained is the output of the quantum circuit. In this embodiment, this output corresponds to the classification prediction result (0 or 1) of the quantum image classifier for this image.
[0032] In the user-defined output processing module, the binary cross-entropy between the measurement result of the quantum image classifier and the image label is used as the optimization objective to calculate the loss of the above-mentioned quantum circuit prediction result.
[0033] The user inputs the above-mentioned quantum circuit in the form of a quantum circuit description into the quantum circuit design module to obtain a quantum circuit object. After using the quantum circuit conversion module to convert and compile it into a corresponding quantum circuit PyTorch object, the quantum circuit calculation module is connected in series with the above-mentioned user-defined input-output module to complete the construction of the quantum circuit model. The AdamW optimizer provided by PyTorch is used to optimize the matrix containing optimization parameters in the quantum circuit PyTorch object. Figure 3 Visualize the changes in the loss function and training classification accuracy of the above-mentioned quantum circuit model during operation with the number of optimization steps.
[0034] Through specific actual experiments, on a 4-core Intel Xeon Gold 6133 processor, using the AdamW optimizer with a learning rate of 0.01 and a weight decay rate of 0.001, optimizing the above-mentioned quantum circuit model for 100 rounds for each image in the training set, the image classification accuracy that can be obtained reaches 85%. On average, 5.69 images can be trained per second, and 172.91 images can be evaluated.
[0035] Compared with the same type of quantum circuit calculation and simulation framework Paddle Quantum, this system can achieve a 20-fold improvement in the training speed and a 275-fold improvement in the inference speed for the image classification task described in this embodiment. The specific speed comparison data is as Figure 4 shown.
[0036] The above specific implementation can be locally adjusted by those skilled in the art in different ways without departing from the principles and purposes of the present invention. The protection scope of the present invention is subject to the claims and is not limited by the above specific implementation. All implementation solutions within its scope are subject to the constraints of the present invention.
Claims
1. A lightweight and fast quantum circuit simulation implementation system based on PyTorch, Characterized in that, Comprising: A quantum circuit design module, a quantum circuit conversion module, and a quantum circuit calculation module, where: The quantum circuit design module constructs a corresponding quantum circuit object according to the quantum circuit description input by the user; The quantum circuit conversion module converts and compiles to obtain a corresponding quantum circuit PyTorch object according to the quantum circuit object; The quantum circuit calculation module performs quantum circuit simulation calculations based on the quantum circuit PyTorch object and the quantum state input by the user to obtain measurement results for constructing a complete quantum circuit model; The quantum circuit description contains information on all quantum gate types, orders, and names in the quantum circuit; The quantum circuit PyTorch object includes parameter information corresponding to the quantum gates contained in the quantum circuit object; The conversion refers to: According to all quantum gate information stored in the form of a list inside the quantum circuit object, each is converted into a parameter matrix corresponding to the quantum gate and stored in the sequence table of the quantum circuit PyTorch object in order. This sequence table includes an optimized parameter matrix and a non-optimized parameter matrix, where: The optimized parameter matrix corresponds to a quantum gate containing parameters, and the non-optimized parameter matrix corresponds to a quantum gate without parameters; The compilation refers to: Traversing the sequence table of the quantum circuit PyTorch object in order, and merging two adjacent non-optimized parameter matrices to reduce the computational time overhead caused by consecutive non-optimized parameter matrices; The quantum circuit simulation calculation refers to: For the first parameter matrix stored in the sequence table of the quantum circuit PyTorch object, perform a matrix multiplication operation with the quantum state input by the user as the operation result corresponding to this parameter matrix; For the remaining parameter matrices stored in the sequence table of the quantum circuit PyTorch object, perform a matrix multiplication operation with the operation result corresponding to the previous parameter matrix in the sequence table as the operation result corresponding to this parameter matrix; The operation result corresponding to the last parameter matrix in the sequence table is the calculation result of the quantum circuit.
2. The lightweight and fast quantum circuit simulation implementation system based on PyTorch according to claim 1, Characterized in that, The quantum circuit design module has built-in member functions for adding gate circuits corresponding to different quantum gate types, adds corresponding quantum gate information according to user calls, and constructs a quantum circuit object. This quantum circuit object stores the quantum gate types, orders, and name information contained in the quantum circuit description input by the user in the form of a member variable of the list type.
3. The lightweight and fast quantum circuit simulation implementation system based on PyTorch according to claim 1, Characterized in that, The matrix multiplication operation mentioned above refers to: automatically selecting a dense matrix or sparse matrix backend program according to the computing device used by the user when running the program of the quantum circuit simulation implementation system, and performing matrix multiplication operations on the input matrix using a specific PyTorch matrix multiplication interface. When the user uses a CPU device, the quantum circuit calculation module selects a sparse matrix calculation backend program, converts all the parameter matrices stored in the sequential list into sparse parameter matrices, and performs matrix multiplication operations using the PyTorch sparse matrix multiplication interface; when the user uses a GPU device or other devices with high-performance parallel computing capabilities, the quantum circuit calculation module selects a dense matrix calculation backend program and directly performs matrix multiplication operations using the PyTorch dense matrix multiplication interface.
4. The lightweight and fast quantum circuit simulation implementation system based on PyTorch according to claim 1, characterized in that the quantum circuit simulation calculation further includes a conjugate transpose transformation operation, that is: by setting an additional conjugate transpose flag, it is determined whether to calculate after performing a conjugate transpose operation on the quantum circuit corresponding to the input quantum circuit PyTorch object. When performing the conjugate transpose operation, all the parameter matrices in the sequential list inside the quantum circuit PyTorch object are respectively subjected to matrix conjugate transpose calculations, and the sequential list is flipped front and back and stored in the quantum circuit PyTorch object.
5. The lightweight and fast quantum circuit simulation implementation system based on PyTorch according to claim 1, characterized in that the composition of the complete quantum circuit model means that: the user input quantum state, parameter matrices, and operation results corresponding to the parameter matrices involved in the quantum circuit calculation module are all stored in the computer memory in the form of PyTorch tensors. By passing PyTorch tensors, multiple quantum circuit calculation modules are connected in series, or the quantum circuit calculation module is connected in series with other custom modules that can receive or output PyTorch tensors to construct a complete quantum circuit model. This model can receive different types of model inputs and calculate different types of model outputs, and this model can implement the optimization of the parameter matrices with optimization parameters inside the quantum circuit PyTorch module through the PyTorch optimizer.
6. A quantum circuit simulation method based on the system according to any one of claims 1 to 5, characterized in that according to the quantum circuit description input by the user, convert and output a quantum circuit PyTorch object, and the quantum circuit calculation module receives the quantum state and the quantum circuit PyTorch object input by the user to obtain the quantum circuit calculation result.
7. The quantum circuit simulation method according to claim 6, characterized in that specifically includes: Step 1: The user uses the quantum circuit design module to define the quantum gates in the quantum circuit to generate a quantum circuit object; Step 2: Use the quantum circuit conversion module to convert the above quantum circuit object into a quantum circuit PyTorch object; this module will perform conversion and compilation operations on the quantum circuit object; Step 3: The user uses the generated quantum circuit PyTorch object and combines it with other user-defined modules to construct a complete quantum circuit model or a hybrid quantum circuit model; Step 4: Use the quantum circuit calculation module to receive the quantum circuit PyTorch object and the quantum state input by the user. The quantum circuit calculation module automatically selects the backend program according to the calculation device and outputs the calculation result; after obtaining the calculation result, the user uses a user-defined optimization scheme to optimize the matrix containing the optimization parameters stored in the quantum circuit PyTorch object.