WO2023125857A1 - Model training method based on machine learning framework system and related device - Google Patents

Model training method based on machine learning framework system and related device Download PDF

Info

Publication number
WO2023125857A1
WO2023125857A1 PCT/CN2022/143597 CN2022143597W WO2023125857A1 WO 2023125857 A1 WO2023125857 A1 WO 2023125857A1 CN 2022143597 W CN2022143597 W CN 2022143597W WO 2023125857 A1 WO2023125857 A1 WO 2023125857A1
Authority
WO
WIPO (PCT)
Prior art keywords
quantum
machine learning
noise
layer
program
Prior art date
Application number
PCT/CN2022/143597
Other languages
French (fr)
Chinese (zh)
Inventor
方圆
王伟
李蕾
窦猛汉
周照辉
王汉超
孔小飞
Original Assignee
本源量子计算科技(合肥)股份有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Priority claimed from CN202111680613.0A external-priority patent/CN116415667A/en
Priority claimed from CN202111680570.6A external-priority patent/CN116432763A/en
Priority claimed from CN202111680566.XA external-priority patent/CN116415685A/en
Priority claimed from CN202111680573.XA external-priority patent/CN116432710B/en
Priority claimed from CN202111680611.1A external-priority patent/CN116432691A/en
Priority claimed from CN202210083466.7A external-priority patent/CN116542337A/en
Application filed by 本源量子计算科技(合肥)股份有限公司 filed Critical 本源量子计算科技(合肥)股份有限公司
Publication of WO2023125857A1 publication Critical patent/WO2023125857A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N3/00Computing arrangements based on biological models
    • G06N3/02Neural networks
    • G06N3/04Architecture, e.g. interconnection topology

Definitions

  • the present disclosure generally relates to the field of quantum computing technology. More specifically, the present disclosure relates to a model training method based on a machine learning framework system, a model training device, a storage medium and an electronic device.
  • Machine learning models are widely used in artificial intelligence research due to their excellent performance. By using labeled training data to train machine learning models, machine learning models that meet expectations can be obtained, and then used for speech recognition and image recognition. and other specific applications.
  • the machine learning model does not need to artificially set its standards for specific application scenarios. By training the machine learning model, it can establish corresponding working standards by itself, which has good adaptability to different application scenarios.
  • quantum computing more and more machine learning models that include quantum computing layers are beginning to appear.
  • this disclosure proposes a model training method and related equipment based on a machine learning framework, aiming at training different machine learning models or when applying machine learning models in different scenarios , reduce the change of the training method of the machine learning model, and improve the development efficiency.
  • the present disclosure provides solutions in the following aspects.
  • the present disclosure provides a model training method based on a machine learning framework, which is applied to an electronic device including a machine learning framework.
  • the machine learning framework includes a data structure module, a quantum module, and a classical module.
  • the method includes : call the data structure module to obtain input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model; input the tensor data into the the machine learning model, and call the classic module to create the training layer of the machine learning model; input the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain training After the machine learning model.
  • the training layer includes a loss function layer and an optimizer layer
  • the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model;
  • the calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer; calling the optimizer unit to create the optimizer layer.
  • the inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model includes: The output result of the machine learning model is input into the loss function layer to calculate the value of the loss function of the machine learning model; when it is determined that the value of the loss function does not meet the preset condition, the value of the loss function is input into the The optimizer layer, to update the parameters of the machine learning model based on the value of the loss function; determine the value of the loss function of the machine learning model after updating the parameters; determine that the value of the loss function satisfies In the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
  • the updating the parameters of the machine learning model based on the value of the loss function includes: calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function;
  • the gradient and gradient descent algorithms update the parameters of the machine learning model.
  • the machine learning model includes a quantum computing layer created based on the quantum module, and the loss function is calculated based on the value of the loss function relative to the
  • the gradient of the parameters of the machine learning model includes: calculating the first gradient of the loss function relative to the output result of the quantum computing layer based on the value of the loss function, and calculating the output result of the quantum computing layer relative to The second gradient of the parameters of the quantum computing layer; multiplying the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model.
  • the calculating the second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer includes: calculating the parameters of the quantum computing layer plus the first offset The first output result of the quantum computing layer, and calculate the second output result of the quantum computing layer after the parameters of the quantum computing layer minus the second offset; based on the first output result and the second The difference of the output results is to calculate the second gradient of the output results of the quantum computing layer relative to the parameters of the quantum computing layer.
  • the method further includes: using the loss function,
  • the parameters of the machine learning model and the output results of each sub-step in the process of calculating the loss function based on the parameters are calculation nodes, and the loss function, the parameters, and the output results of the sub-steps are used as calculation nodes.
  • the relationship is used as an edge to construct a directed calculation graph; the calculation of the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function includes: based on the value of the loss function and the calculation graph Gradients of the loss function are computed with respect to parameters of the machine learning model.
  • the calculation of the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function and the calculation graph includes: determining the corresponding calculation of the loss function in the calculation graph The parameters of the node to the machine learning model correspond to the path of the computing node; based on the value of the loss function, calculate the intermediate gradient of each computing node that is not a leaf node on the path relative to the predecessor node of the computing node; the calculated All the intermediate gradients are multiplied together to obtain the gradient of the loss function with respect to the parameters.
  • the present disclosure also provides a model training device based on a machine learning framework, which is applied to an electronic device including a machine learning framework.
  • the machine learning framework includes a data structure module, a quantum module, and a classical module.
  • the device Including: creating a module for calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and/or the classical module to create a machine learning model; input training module, for inputting the tensor data into the machine learning model, and calling the classic module to create the training layer of the machine learning model; a parameter update module, for inputting the output result of the machine learning model into the the training layer, and update the parameters of the machine learning model based on the output result of the training layer, so as to obtain the trained machine learning model.
  • the training layer includes a loss function layer and an optimizer layer
  • the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the input training module is also used to: call the loss function unit to create the loss A function layer; calling the optimizer unit to create the optimizer layer.
  • the parameter updating module is further configured to: input the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model; when determining the loss function When the value of the loss function does not meet the preset condition, the value of the loss function is input to the optimizer layer, so as to update the parameters of the machine learning model based on the value of the loss function; The value of the loss function of the learning model; when it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
  • the parameter update module is also used to: calculate the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function; update the machine based on the gradient and gradient descent algorithm Learn the parameters of the model.
  • the machine learning model includes a quantum computing layer created based on the quantum module, and the parameter update module is also used to: based on the value of the loss function Calculate the first gradient of the loss function relative to the output of the quantum computing layer, and calculate the second gradient of the output of the quantum computing layer relative to the parameters of the quantum computing layer; Multiplying with the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model.
  • the parameter update module is also used to: calculate the first output result of the quantum computing layer after the parameters of the quantum computing layer plus the first offset, and calculate the parameters of the quantum computing layer minus the second bias After setting the second output result of the quantum computing layer; based on the difference between the first output result and the second output result, calculate the output result of the quantum computing layer relative to the parameters of the quantum computing layer the second gradient.
  • the device further includes: a calculation graph construction module, configured to input the tensor data into the machine learning model in the input training module, and call the classic module to create the machine learning model
  • the loss function, the parameters of the machine learning model, and the output results of each sub-step in the process of calculating the loss function based on the parameters are used as calculation nodes, and the loss function, the The relationship between the parameters and the output results of this sub-step is used as an edge to construct a directed calculation graph
  • the parameter update module is also used to: calculate the loss function relative to the calculation graph based on the value of the loss function and the calculation graph The gradient of the parameters of the machine learning model.
  • the parameter update module is further configured to: determine the path from the calculation node corresponding to the loss function in the calculation graph to the calculation node corresponding to the parameter of the machine learning model; calculate based on the value of the loss function The intermediate gradient of each calculation node that is not a leaf node on the path relative to the predecessor node of the calculation node; multiplying all the calculated intermediate gradients to obtain the gradient of the loss function relative to the parameter.
  • the present disclosure also provides a storage medium, where a computer program is stored in the storage medium, wherein the computer program is configured to execute the method described in any one of the above first aspects when running step.
  • the present disclosure also provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform the above-mentioned first aspect. The steps of any one of the methods.
  • the training layer of the machine learning model can be created by calling the classic module, and then the tensor data is input into the machine learning model, and then the machine The training result of the learning model is input into the training layer, and the created machine learning model can be trained.
  • the training layer can be created by calling classic modules without creating from scratch, which reduces the steps and time required to create the training layer, thereby improving the development efficiency of training when changing machine learning models or different application scenarios.
  • tensor data corresponding to training data is created through the data structure module, and the machine learning model is created based on quantum modules and/or classical modules, so that tensor data and machine learning models can be It follows the standard based on the same machine learning framework as the training layer, reducing their mutual adaptation operations, thereby improving the development efficiency of training machine learning models.
  • this disclosure proposes a data processing method, a machine learning framework and related equipment, aiming at improving the machine learning model including quantum machine learning programs and classical machine learning programs.
  • the training development efficiency of the machine learning model is provided in the following aspects.
  • the present disclosure provides a data processing method applied to an electronic device including a machine learning framework
  • the machine learning framework includes a quantum module and a classical module
  • the method includes: calling the quantum module to create a A quantum machine learning program, a first preset training program, and a quantum computing layer of a first training interface, the first training interface is used to call the first preset training program to train the quantum machine learning program; call the classical
  • the module creates a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the classic machine learning program; call The classical module creates a machine learning model including the quantum computing layer and the classical computing layer.
  • the method further includes: calling the classic module to create a training layer of the machine learning model, the training layer is used to call the first preset training program through the first training interface, And call the second preset training program through the second training interface.
  • the machine learning framework further includes a data structure module
  • the method further includes: calling the data structure module to obtain input data and creating tensor data including the input data; converting the tensor data Input the machine learning model to obtain an output result; input the output result into the training layer to call the first preset training program through the first training interface, and train according to the first preset training program
  • the quantum machine learning program calls the second preset training program through the second training interface, trains the classical machine learning program according to the second preset training program, and obtains a trained machine learning model.
  • the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program with respect to the first dependent variable of the quantum machine learning program, the The first dependent variable is an input or parameter of the quantum machine learning program.
  • the first gradient calculation program is used to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
  • the first gradient calculation program when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for Subtract the first offset from the first dependent variable of the quantum machine learning program, and input the target data into the quantum machine learning program to obtain the first result; a parameter adding subroutine for adding the quantum The first dependent variable of the machine learning program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; a calculation subroutine is used for The second result calculates the gradient; wherein the target data is data input to the quantum machine learning program.
  • the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the The second dependent variable is an input or parameter of the classical machine learning procedure.
  • the present disclosure also provides a data processing device, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a quantum module and a classical module, and the device includes: a quantum computing layer creation module, used Invoking the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, the first training interface is used to call the first preset training program to train the quantum Machine learning program; a classic computing layer creation module, used to call the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the The second preset training program trains the classical machine learning program; the machine learning model creation module is used to call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
  • a quantum computing layer creation module used Invoking the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training
  • the device further includes: a training layer creation module, configured to call the classic module to create a training layer of the machine learning model, and the training layer is used to call the A first preset training program, and call the second preset training program through the second training interface.
  • a training layer creation module configured to call the classic module to create a training layer of the machine learning model, and the training layer is used to call the A first preset training program, and call the second preset training program through the second training interface.
  • the machine learning framework further includes a data structure module
  • the device further includes: a tensor data creation module, configured to call the data structure module to obtain input data and create a tensor including the input data data; an input module, configured to input the tensor data into the machine learning model to obtain an output result; a training module, configured to input the output result into the training layer, so as to call the first training interface through the first training interface
  • the first preset training program train the quantum machine learning program according to the first preset training program, and call the second preset training program through the second training interface, according to the second preset
  • the training program trains the classic machine learning program to obtain a trained machine learning model.
  • the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program with respect to the first dependent variable of the quantum machine learning program, the The first dependent variable is an input or parameter of the quantum machine learning program.
  • the first gradient calculation program is used to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
  • the first gradient calculation program when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for Subtracting the first offset from the first dependent variable of the quantum machine learning program, and inputting target data into the quantum machine learning program to obtain a first result; a parameter adding subroutine for adding the quantum The first dependent variable of the machine learning program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; a calculation subroutine is used for The second result calculates the gradient; wherein the target data is data input to the quantum machine learning program.
  • the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the The second dependent variable is an input or parameter of the classical machine learning procedure.
  • the present disclosure also provides a machine learning framework, including: a quantum module configured to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, the first A training interface is used to call the first preset training program to train the quantum machine learning program; a classical module configured to create a classical computing layer including a classical machine learning program, a second preset training program and a second training interface , the second training interface is used to call the second preset training program to train the classical machine learning program; the classical module is also configured to create a machine learning program including the quantum computing layer and the classical computing layer Model.
  • the present disclosure also provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any one of the methods described in the above first aspect when running .
  • the present disclosure also provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to perform any of the above-mentioned first aspects. A step of said method.
  • a quantum computing layer including the quantum machine learning program can be created, and the created quantum computing layer can directly contain a first preset training program for training the quantum machine learning program , and the first training interface for calling the first preset training program, and similarly for the classic machine learning program, you can create a second preset training interface that includes the classic machine learning program and is used to train the classic machine learning program Program, the second training interface for calling the second preset training program, and then for the created machine learning model including the quantum computing layer and the classical computing layer, during training, you can directly pass the first training interface and the second training interface
  • the two training interfaces respectively call the first preset training program and the second preset training program to realize the training of the machine learning model, which can reduce the creation of training programs for quantum machine learning programs and classical machine learning programs, and save more time.
  • the coordinated work required for training quantum machine learning programs and training classical machine learning programs improves the development efficiency of training machine learning models that include both quantum and classical computing layers.
  • this disclosure proposes a noise-containing machine learning model creation method, machine learning model framework and related equipment, aiming to create a noise-containing machine learning model, so that the The results simulated by the machine learning model on the quantum virtual machine are closer to the results calculated on the real quantum computer.
  • the present disclosure provides solutions in the following aspects.
  • the present disclosure provides a noise-containing machine learning model creation method, which is applied to an electronic device of a machine learning framework including a quantum module and a classical module, the quantum module includes a noise-containing quantum program encapsulation unit, and the method Including: creating a quantum program considering the influence of noise based on the quantum computing programming library contained in the machine learning framework; using the quantum program as a parameter of the encapsulated noise-containing quantum computing layer interface, and passing the noise-containing quantum computing layer interface; calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the classical module to create a machine learning model including the noise-containing quantum computing layer.
  • the creation of a quantum program considering the impact of noise based on the quantum computing programming library included in the machine learning framework includes: applying for a noise-containing quantum virtual machine based on the quantum computing programming library included in the machine learning framework , and setting the noise of the quantum circuit running on the noise-containing virtual machine; applying for a qubit and creating a quantum logic gate acting on the qubit to obtain the quantum circuit running on the noise-containing virtual machine;
  • the noise-containing quantum virtual machine, the noise model and the quantum circuit are packaged to obtain a quantum program considering the influence of noise.
  • the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
  • the noise is logic gate noise
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying the type of quantum logic gate, the type of noise model, and the noise model The parameters required by the type are used as the parameters of the logic gate noise interface, and are passed into the logic gate noise interface; the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the Logic gate noise acts on all qubits in the quantum circuit.
  • the noise is logic gate noise
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying qubits, quantum logic gate types, noise model types, and all The parameters required by the noise model type are used as the parameters of the logic gate noise interface, and passed into the logic gate noise interface; the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface , the logic gate noise takes effect on specified qubits in the quantum circuit.
  • the noise is the quantum state reset noise of the qubit
  • the setting the noise of the quantum circuit running on the noise-containing virtual machine includes: resetting the quantum state of the qubit in the quantum circuit
  • 1> are used as the parameters of the reset noise interface, and passed into the reset noise interface; through the reset noise interface, the virtual machine running on the noise is set Quantum state reset noise of qubits on quantum circuits.
  • the noise is the measurement noise of qubits
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying the specified noise model type and the noise model type required
  • the parameter is used as the parameter of the measurement noise interface, and is passed into the measurement noise interface; through the measurement noise interface, the measurement noise of the qubit of the quantum circuit running on the noise-containing virtual machine is set.
  • the noise is the read noise of the qubit
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: the probability that
  • the present disclosure provides a device for creating a noise-containing machine learning model, which is applied to an electronic device of a machine learning framework including a quantum module and a classical module, the quantum module includes a noise-containing quantum program encapsulation unit, the The device includes: a program creation unit, used to create a quantum program considering the influence of noise based on the quantum computing programming library contained in the machine learning framework; an interface determination unit, used to use the quantum program as a packaged noise-containing quantum computing layer The parameters of the interface, and passing in the noise-containing quantum computing layer interface; creating a unit for calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the noise-containing quantum computing layer interface
  • the classical module creates a machine learning model that includes the noisy quantum computing layer.
  • the program creation unit is specifically configured to: based on the included in the machine learning framework
  • the quantum computing programming library applies for a quantum virtual machine with noise, and sets the noise of the quantum circuit running on the virtual machine with noise; applies for qubits and creates quantum logic gates that act on the qubits, and obtains A quantum circuit on a noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model, and the quantum circuit to obtain a quantum program considering the influence of noise.
  • the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
  • the noise is logic gate noise
  • the program creation unit in terms of setting the noise of the quantum circuit running on the noise-containing virtual machine, is specifically configured to: use the specified quantum logic gate Type, noise model type, and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, it is set to run on the noise-containing virtual machine
  • the logic gate noise of the quantum circuit the logic gate noise takes effect on all qubits in the quantum circuit.
  • the noise is logic gate noise
  • the program creation unit is specifically configured to: assign the specified qubit, The quantum logic gate type, the noise model type and the parameters required by the noise model type are used as the parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, the settings run on the noise-containing Logic gate noise of the quantum circuit on the virtual machine, where the logic gate noise takes effect on specified qubits in the quantum circuit.
  • the program creation unit when the noise is the quantum state reset noise of the qubit, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: The quantum state of the qubit in the quantum circuit is reset to the probability of
  • the program creation unit is specifically configured to: use the specified noise
  • the model type and the parameters required by the noise model type are used as the parameters of the measurement noise interface, and passed into the measurement noise interface; through the measurement noise interface, the qubits of the quantum circuit running on the noise-containing virtual machine are set measurement noise.
  • the program creation unit when the noise is the read noise of qubits, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: use
  • the present disclosure provides a machine learning framework
  • the machine learning framework includes a quantum module and a classical module
  • the quantum module includes a noisy quantum program encapsulation unit configured to pass the packaged noisy quantum program
  • the calculation layer interface creates a noise-containing quantum computing layer
  • the noise-containing quantum computing layer interface is used to provide a quantum program that considers the influence of noise created based on the quantum computing programming library contained in the machine learning framework
  • the classical module is configured To create a machine learning model comprising said noisy quantum computing layer.
  • the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
  • the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
  • this disclosure creates a quantum program that considers the influence of noise based on the quantum computing programming library included in the machine learning framework; then uses the quantum program as a parameter of the encapsulated noise-containing quantum computing layer Noisy quantum computing layer interface; finally calling the noisy quantum computing layer encapsulation unit to create a noisy quantum computing layer through the noisy quantum computing layer interface; and calling the classical module to create a machine learning model including the noisy quantum computing layer.
  • This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit.
  • the machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except
  • the noisy quantum computing layer is created through the noisy quantum computing layer interface.
  • the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
  • this disclosure proposes a machine learning model construction method, a machine learning framework and related equipment, aiming to realize the construction of machine learning models across quantum machine learning frameworks.
  • the present disclosure provides solutions in the following aspects.
  • the present disclosure provides a machine learning model construction method applied to an electronic device including a first machine learning framework and not including a second machine learning framework, the first machine learning framework including a compatible quantum computing program
  • An encapsulation unit the method includes: determining a compatible quantum computing layer interface of the corresponding second machine learning framework, and the compatible quantum computing layer interface is used to provide a quantum computing programming library based on the second machine learning framework The created quantum program; calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit to construct a compatible quantum computing layer; constructing a machine learning model including the compatible quantum computing layer.
  • the method before determining the corresponding compatible quantum computing layer interface of the second machine learning framework, the method further includes: constructing a quantum circuit through the quantum computing programming library included in the second machine learning framework , and calling the initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining the quantum program.
  • the first machine learning framework further includes a classical module and a data structure module
  • the construction of a machine learning model including the compatible quantum computing layer includes: calling the classical module to construct a classical computing layer and calling The data structure module constructs the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calls the classical module to combine the classical computing layer, the compatible quantum computing layer and the classical computing layer Encapsulate the forward propagation relationship with the compatible quantum computing layer to obtain a machine learning model.
  • the classical module includes a classical neural network layer unit, and the classical neural network layer unit includes at least one of the following: a specified model classical neural network layer subunit configured to pass through the encapsulated classical neural network layer
  • the interface constructs the classical neural network layer of the specified model
  • the activation layer subunit is configured to construct an activation layer for performing nonlinear transformation on the output of the classical neural network layer
  • layer including: calling the specified model classic neural network layer subunit to construct a classic neural network layer, and using the classic neural network layer as a classic calculation layer; or calling the specified model classic neural network layer subunit and the
  • the activation layer subunit constructs a classical neural network layer and an activation layer, and uses the classical neural network layer and the activation layer as a classical calculation layer.
  • the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the classical computing layer and the compatible quantum computing layer are combined by the classical module Encapsulate the forward propagation relationship between them to obtain a machine learning model, including: calling the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialized and encapsulated The compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship; calling the abstract class The sub-module encapsulates the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class to obtain a machine learning model.
  • the present disclosure provides a machine learning model construction device applied to an electronic device including a first machine learning framework and not including a second machine learning framework, the first machine learning framework including a compatible quantum computing program
  • An encapsulation unit the device includes: a determination unit, configured to determine a corresponding compatible quantum computing layer interface of the second machine learning framework, and the compatible quantum computing layer interface is used to provide information based on the second machine learning framework.
  • a quantum program created by the quantum computing programming library a construction unit for calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit to construct a compatible quantum computing layer; constructing a machine learning model including the compatible quantum computing layer .
  • the construction unit before determining the compatible quantum computing layer interface corresponding to the second machine learning framework, is further configured to: use the quantum computing programming library included in the second machine learning framework Constructing a quantum circuit, calling an initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining a quantum program.
  • the first machine learning framework further includes a classical module and a data structure module
  • the construction includes the machine learning model compatible with the quantum computing layer
  • the construction unit is specifically used to: call the classical The module constructs the classical computing layer and calls the data structure module to construct the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calls the classical module to combine the classical computing layer, the compatible quantum computing layer and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer are encapsulated to obtain a machine learning model.
  • the classical module includes a classical neural network layer unit, and the classical neural network layer unit includes at least one of the following: a specified model classical neural network layer subunit configured to pass through the encapsulated classical neural network layer
  • the interface constructs the classical neural network layer of the specified model
  • the activation layer subunit is configured to construct an activation layer for performing nonlinear transformation on the output of the classical neural network layer
  • Layer the construction unit is specifically used to: call the specified model classic neural network layer subunit to construct a classic neural network layer, and use the classic neural network layer as a classic calculation layer; or call the specified model classic neural network
  • the layer subunit and the activation layer subunit construct a classical neural network layer and an activation layer, and use the classical neural network layer and the activation layer as a classical computing layer.
  • the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the classical computing layer and the compatible quantum computing layer are combined by the classical module Encapsulate the forward propagation relationship between them to obtain a machine learning model, and the construction unit is specifically used to: call the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, Obtaining the initialized and encapsulated compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship ; calling the abstract class submodule to encapsulate the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
  • the present disclosure provides a machine learning framework, the machine learning framework is set in an electronic device, and the machine learning framework includes: a compatible quantum computing program encapsulation unit configured to call a compatible quantum computing layer interface A compatible quantum computing layer is constructed, and the compatible quantum computing layer interface is used to receive the quantum program created based on the quantum computing programming library included in the second machine learning framework set outside the electronic device.
  • the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
  • the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
  • developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework that they are used to, and then determine in the first machine learning framework Corresponding to the compatible quantum computing layer interface corresponding to the second machine learning framework, the compatible quantum computing layer interface can be constructed by calling the compatible quantum computing layer interface included in the first machine learning framework.
  • the electronic device of the first machine learning framework but not including the second machine learning framework constructs the machine learning model, thereby realizing the construction of the machine learning model across quantum machine learning frameworks.
  • this disclosure proposes a data processing method, machine learning framework and related equipment, aiming at managing the data in the machine learning model according to the limited qubits when the machine learning model is running The way quantum programs operate to improve the efficiency of data processing.
  • the present disclosure provides solutions in the following aspects.
  • the present disclosure provides a data processing method applied to an electronic device including a machine learning framework, where the machine learning framework includes a data structure module, a quantum module, and a classical module, and the method includes: calling the quantum module and the classic module create a machine learning model including a quantum program, call the data structure module to obtain target data and create tensor data including the target data; call the quantum module based on the first required by the quantum program
  • the quantity of a qubit and the quantity of the second qubit allowed to be used in the quantum device divide the second qubit into multiple groups; call the quantum module for each group of the second qubit, based on the quantum
  • the program generates quantum circuits including the group of second qubits; based on the tensor data, the input data of each group of quantum circuits is obtained, and the input data are respectively input into each of the quantum circuits to obtain each The output result of the quantum circuit.
  • the second qubits are divided into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, including: The number of second qubits allowed to be used in the quantum device is divided by the number of first qubits required by the quantum program to obtain an intermediate number; the largest integer less than or equal to the intermediate number is used as the target number; the The second qubits are divided into groups whose number is the target number.
  • generating a quantum circuit including the group of the second qubits based on the quantum program includes: for each group of the second qubits , generating a quantum logic gate for acting on the group of the second qubits based on the quantum program; generating a quantum circuit including the group of the second qubits and the quantum logic gate.
  • the method further includes: determining a prediction result of the machine learning model for the tensor data based on the output result.
  • the classic module is also used to create a training layer of the machine learning model, and the method further includes: calling the classic module to create a training layer of the machine learning model; inputting the prediction result
  • the training layer is used to update the parameters of the quantum program to obtain the trained machine learning model.
  • the training layer includes a loss function layer and an optimizer layer
  • the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model;
  • the calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer; calling the optimizer unit to create the optimizer layer.
  • the inputting the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model includes: inputting the prediction result into the A loss function layer, to calculate the value of the loss function of the machine learning model; when it is determined that the value of the loss function does not meet a preset condition, input the value of the loss function into the optimizer layer, based on the The value of the loss function updates the parameters of the quantum program; determines the value of the loss function of the machine learning model after updating the parameters; when it is determined that the value of the loss function satisfies a preset condition, the parameters will be updated
  • the machine learning model after training is used as the machine learning model after training.
  • the updating the parameters of the quantum program based on the value of the loss function includes: calculating the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function ; updating said parameters of said quantum program based on a plurality of said gradients and a gradient descent algorithm.
  • updating the parameters of the quantum program based on multiple gradients and gradient descent algorithms includes: calculating the average value of multiple gradients; multiplying the average value by the step size to obtain an offset; subtracting the offset from the parameter of the quantum program to update the parameter of the quantum program.
  • the present disclosure provides a machine learning framework comprising: a data structure module configured to acquire target data and create tensor data including the target data; a classical module configured to create a machine learning learning model; a quantum module configured to create a machine learning model and divide the second qubits into multiples based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device group, for each group of the second qubits, generate a quantum circuit including the group of the second qubits based on the quantum program, so that when the input data of each group of the quantum circuits is determined, the input Data is respectively input into each of the quantum circuits, and an output result of each of the quantum circuits is obtained.
  • the present disclosure provides a data processing device applied to an electronic device including a machine learning framework, the machine learning framework including a data structure module, a quantum module and a classical module, and the device includes: a first creation module , for calling the quantum module and the classical module to create a machine learning model including a quantum program, calling the data structure module to obtain target data and create tensor data including the target data; the division module is used to call the The quantum module divides the second qubits into multiple groups based on the quantity of the first qubits required by the quantum program and the quantity of the second qubits allowed in the quantum device; the generation module is used to call the For each group of the second qubits, the quantum module generates a quantum circuit including the group of the second qubits based on the quantum program; an input module is used to obtain each group of the quantum circuits based on the tensor data input data, and input the input data into each of the quantum circuits respectively, to obtain the output result of each of the quantum circuits.
  • a first creation module for calling
  • the division module is also used to: divide the number of second qubits allowed in the quantum device by the number of first qubits required by the quantum program to obtain an intermediate number; The largest integer equal to the intermediate number is used as the target number; and the second qubit is divided into multiple groups whose number is the target number.
  • the generating module is further configured to: for each group of the second qubits, generate a quantum logic gate for acting on the group of the second qubits based on the quantum program; generating includes The set of quantum circuits of the second qubit and the quantum logic gate.
  • the device further includes: a determination module, configured to determine a prediction result of the machine learning model for the tensor data based on the output result.
  • the classic module is also used to create the training layer of the machine learning model
  • the device further includes: a second creation module, used to call the classic module to create the training layer of the machine learning model an update module, configured to input the prediction result into the training layer, so as to update the parameters of the quantum program, and obtain the trained machine learning model.
  • the training layer includes a loss function layer and an optimizer layer
  • the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the second creation module is also used to: call the loss function unit to create the A loss function layer; calling the optimizer unit to create the optimizer layer.
  • the update module is further configured to: input the prediction result into the loss function layer to calculate the value of the loss function of the machine learning model; When setting the condition, input the value of the loss function into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function; determine the loss of the machine learning model after updating the parameters The value of the function; when it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
  • the update module is further configured to: calculate the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function; based on multiple gradients and gradient descent algorithms The parameters of the quantum program are updated.
  • the update module is further used to: calculate the average value of multiple gradients; multiply the average value by the step size to obtain a bias; subtract the parameter of the quantum program from the the bias to update the parameters of the quantum program.
  • the present disclosure provides a storage medium in which a computer program is stored, wherein the computer program is configured to perform the steps of any one of the methods described in the above first aspect when running .
  • the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned first aspects. A step of said method.
  • the machine learning model created by calling the quantum module and the classical module before inputting the tensor data into the model, according to the number of first qubits required by the quantum program and the number of second qubits available , generate multiple sets of quantum circuits corresponding to the quantum program, and then make the tensor data input machine learning model obtain multiple input data for inputting multiple sets of quantum circuits, and then input multiple input data into multiple sets of quantum circuits to Using multiple groups of quantum circuits to perform related quantum calculations on the input data to improve the efficiency of quantum program operation.
  • this disclosure proposes a machine learning model running method, a machine learning framework and related equipment, aiming to realize seamless switching and unified calling of different computing devices, and improve computing power. Uniformity of equipment usage.
  • the present disclosure provides solutions in the following aspects.
  • the present disclosure provides a method for running a machine learning model, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a classical module and a quantum module, and the method includes: specifying a classical computing layer The first computing device and the second computing device of the quantum computing layer; passing the first computing device as a parameter into the classical computing layer interface, and passing the second computing device as a parameter into the quantum computing layer interface; calling the The classic module creates a classical computing layer through the classical computing layer interface, and calls the quantum module to create a quantum computing layer through the quantum computing layer interface; calls the classical module to build a machine that includes the classical computing layer and the quantum computing layer Learning a model, and invoking the first computing device and the second computing device to run the machine learning model.
  • the calling the first computing device and the second computing device to run the machine learning model includes: determining that the computing format supported by the second computing device does not include the quantum computing layer
  • the classical module is called to convert the format of the input data of the quantum computing layer into a computing format supported by the second computing device through the data format conversion interface; the second computing device is called to run the The quantum computing layer calculates the output data corresponding to the input data.
  • the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; the second computing The computing hardware of the device includes at least one of the following: CPU, GPU, and quantum processor QPU.
  • the method further includes: constructing the quantum virtual machine based on the quantum computing programming library included in the machine learning framework.
  • constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: passing the quantum machine type of the specified computing hardware as a parameter into the quantum computing library contained in the machine learning framework An initialization function in the quantum computing programming library, and creating the quantum virtual machine through the initialization function.
  • constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: passing the quantum machine type of the specified computing hardware as a parameter into the quantum computing library contained in the machine learning framework A quantum machine initialization interface in the quantum computing programming library, and creating the quantum virtual machine through the quantum machine initialization interface.
  • the constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: based on the quantum computing hardware specified in the quantum computing programming library contained in the machine learning framework The virtual machine class creates the quantum virtual machine.
  • the present disclosure provides a machine learning model running device, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a classical module and a quantum module, and the device includes: a computing device determining unit, It is used to specify the first computing device of the classical computing layer and the second computing device of the quantum computing layer; the interface determination unit is used to pass the first computing device as a parameter into the interface of the classical computing layer, and transfer the second computing The device is passed as a parameter to the quantum computing layer interface; the computing layer creation unit is used to call the classical module to create a classical computing layer through the classical computing layer interface, and call the quantum module to create a quantum computing layer through the quantum computing layer interface layer; a model running unit, configured to call the classical module to construct a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model .
  • the model running unit is specifically configured to: determine that the second computing device supports When the computing format does not include the format of the input data of the quantum computing layer, the classical module is called to convert the format of the input data of the quantum computing layer into a computing format supported by the second computing device through a data format conversion interface ; calling the second computing device to run the quantum computing layer to calculate the output data corresponding to the input data.
  • the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; the second computing The computing hardware of the device includes at least one of the following: CPU, GPU, and quantum processor QPU.
  • the second computing device is a quantum virtual machine; the calling the first computing device and the second computing device to run Before the machine learning model, the determining unit of the computing device is further configured to: construct the quantum virtual machine based on the quantum computing programming library included in the machine learning framework.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework, and the computing device determination unit is specifically configured to: pass the quantum machine type of the specified computing hardware as a parameter Import the initialization function in the quantum computing programming library included in the machine learning framework, and create the quantum virtual machine through the initialization function.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework, and the computing device determination unit is specifically configured to: pass the quantum machine type of the specified computing hardware as a parameter Import the quantum machine initialization interface in the quantum computing programming library included in the machine learning framework, and create the quantum virtual machine through the quantum machine initialization interface.
  • the quantum virtual machine is constructed based on the quantum computing programming library contained in the machine learning framework, and the computing device determines the unit, specifically for: based on the quantum computing contained in the machine learning framework
  • a quantum virtual machine class specifying computing hardware in a programming library creates the quantum virtual machine.
  • the present disclosure provides a machine learning framework, the machine learning framework comprising: a classic module configured to create a classic computing layer through a classic computing layer interface, the parameters of the classic computing layer interface include the The first computing device of the classical computing layer; the quantum module is configured to create a quantum computing layer through the quantum computing layer interface, and the parameters of the quantum computing layer interface include the second computing device of the quantum computing layer; the classical module, It is also configured to construct a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model.
  • the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
  • the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
  • the present disclosure builds a machine learning model under the provided machine learning framework including a classical module and a quantum module
  • the classical computing layer of the machine learning model is created by the classical module through the classical computing layer interface
  • the quantum computing layer is created by The quantum module is created through the quantum computing layer interface.
  • it specifies the first computing device of the classical computing layer as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer as the parameter of the quantum computing layer interface, so as to realize
  • the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
  • Fig. 1 is a hardware structural block diagram of a computer terminal of a model training method based on a machine learning framework system shown according to an exemplary embodiment
  • Fig. 2 is a flow chart of a model training method based on a machine learning framework system shown according to an exemplary embodiment
  • Fig. 3 is a block diagram of a machine learning framework system shown according to an exemplary embodiment
  • Fig. 4 is a block diagram of a classic module shown according to an exemplary embodiment
  • Fig. 5 is a flowchart of step S22 in a model training method based on a machine learning framework system shown according to an exemplary embodiment
  • Fig. 6 is a flowchart of step S23 in a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 7 is a flowchart of step S232 in a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 8 is a flowchart of step S2321 in a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 9 is a flowchart of step S23211 in a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 10 is another flowchart of a model training method based on a machine learning framework system shown according to an exemplary embodiment
  • Fig. 11 is another flow chart of a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 12 is another flowchart of a model training method based on a machine learning framework system shown according to an exemplary embodiment
  • Fig. 13 is a flowchart of step S125 in a model training method based on a machine learning framework system according to an exemplary embodiment
  • Fig. 14 is a calculation graph shown according to an exemplary embodiment
  • Fig. 15 is another flowchart of creating a machine learning model according to an exemplary embodiment
  • Fig. 16 is another flowchart of creating a machine learning model according to an exemplary embodiment
  • Fig. 17 is another flowchart of training a machine learning model according to an exemplary embodiment
  • Fig. 18 is a block diagram of a first gradient calculation program according to an exemplary embodiment
  • Fig. 19 is a schematic flowchart of a method for creating a noise-containing machine learning model according to an exemplary embodiment
  • Fig. 20 is another schematic flowchart of machine learning model construction according to an exemplary embodiment
  • Fig. 21 is another flow chart of training a machine learning model according to an exemplary embodiment
  • Fig. 22 is a flowchart showing dividing the second qubits into multiple groups according to an exemplary embodiment
  • Fig. 23 is a flow chart of generating a quantum circuit according to an exemplary embodiment
  • Fig. 24 is a schematic flowchart of a method for running a machine learning model according to an exemplary embodiment
  • Fig. 25 is a schematic structural diagram of a device for running a machine learning model according to an exemplary embodiment
  • Fig. 26 is a block diagram of a model training device based on a machine learning framework system according to an exemplary embodiment.
  • Embodiments of the present disclosure firstly provide a model training method based on a machine learning framework system, which can be applied to electronic devices, such as computer terminals, specifically, ordinary computers, quantum computers, and the like.
  • Fig. 1 is a block diagram showing a hardware structure of a computer terminal according to a data processing method according to an exemplary embodiment.
  • the computer terminal may include one or more (only one is shown in Figure 1) processors 102 (processors 102 may include but not limited to processing devices such as microprocessor MCU or programmable logic device FPGA, etc.) and a memory 104 for storing the quantum circuit-based data processing method.
  • the above-mentioned computer terminal may also include a transmission device 106 and an input and output device 108 for communication functions.
  • the structure shown in FIG. 1 is only for illustration, and it does not limit the structure of the above computer terminal.
  • the computer terminal may also include more or fewer components than shown in FIG. 1 , or have a different configuration than that shown in FIG. 1 .
  • the memory 104 can be used to store software programs and modules of application software, such as program instructions/modules corresponding to the data processing method in the embodiments of the present disclosure, and the processor 102 executes various programs by running the software programs and modules stored in the memory 104 Functional application and data processing are to realize the above-mentioned method.
  • the memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory.
  • the memory 104 may further include a memory that is remotely located relative to the processor 102, and these remote memories may be connected to a computer terminal through a network. Examples of the aforementioned networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
  • the transmission device 106 is used to receive or transmit data via a network.
  • the specific example of the above-mentioned network may include a wireless network provided by the communication provider of the computer terminal.
  • the transmission device 106 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station so as to communicate with the Internet.
  • the transmission device 106 may be a radio frequency (Radio Frequency, RF) module, which is used to communicate with the Internet in a wireless manner.
  • RF Radio Frequency
  • a real quantum computer has a hybrid structure, which consists of two parts: one is a classical computer, which is responsible for performing classical calculation and control; the other is a quantum device, which is responsible for running quantum programs and realizing quantum computing.
  • the quantum program is a set programming language, for example, based on the QRunes language defined by C and Python, to write a program that characterizes qubits and their evolution. It is a series of instruction sequences that can be run on a quantum computer. The support of logic gate operations, and ultimately the realization of quantum computing.
  • quantum circuit is also called quantum logic circuit. It is a general quantum computing model, which represents a circuit that operates on qubits under an abstract concept. Its composition includes qubits, circuits (timelines), As well as various quantum logic gates, the results often need to be read out through quantum measurement operations.
  • Quantum circuits can be regarded as connected by time to transmit the state of qubits evolving over time.
  • a quantum program can be composed of quantum circuits, measurement operations for qubits in quantum circuits, registers for saving measurement results, and control flow nodes (jump instructions).
  • a quantum circuit can contain tens, hundreds, or even thousands of Quantum logic gate operations.
  • the execution process of a quantum program is the process of executing all quantum logic gates according to a certain time sequence. It should be noted that timing refers to the time sequence in which a single quantum logic gate is executed.
  • Quantum logic gates can be used to evolve quantum states. Quantum logic gates are the basis of quantum circuits.
  • Quantum logic gates include single-bit quantum logic gates, such as Hadamard gates (H gates, Hadamard gates), Pauli-X gates ( X gate, Pauli X gate), Pauli-Y gate (Y gate, Pauli Y gate), Pauli-Z gate (Z gate, Pauli Z gate), RX gate (RX revolving gate), RY gate ( RY revolving gate), RZ gate (RZ revolving gate), etc.; multi-bit quantum logic gates, such as CNOT gate, CR gate, iSWAP gate, Toffoli gate, etc.
  • the function of quantum logic gates on the quantum state is calculated by multiplying the left side of the unitary matrix by the vector corresponding to the right vector of the quantum state.
  • Fig. 2 is a schematic flowchart of a model training method based on a machine learning framework system according to an exemplary embodiment.
  • the present embodiment provides a model training method based on a machine learning framework system, which can be applied to an electronic device including a first machine learning framework system 30 as shown in FIG. 3 , the first machine learning framework
  • the system 30 includes a data structure module 31, a quantum module 32 and a classical module 33, and the method includes:
  • S21 Call the data structure module to acquire input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model.
  • the first machine learning framework system integrates many function sets for creating and training machine learning models, and through the interfaces defined by it, these functions can be conveniently called to implement related operations on machine learning models.
  • the first machine learning framework system 30 may include:
  • the data structure module 31 is configured to create tensor data for input into the machine learning model, and perform operations on the tensor data;
  • a quantum module 32 configured to create a quantum computing layer for creating machine learning models
  • the classical module 33 is configured to create a classical computing layer for creating a machine learning model, an abstract class layer for encapsulating the quantum computing layer and the classical computing layer, and a machine learning machine for training and optimizing the machine learning model Model training layer.
  • the data structure module 31 defines the data structure of the tensor data. By calling the data structure module 31, the input data can be converted into tensor data for input into the machine learning model for forward calculation.
  • the data structure module 31 can also define operations between tensor data, such as mathematical operations and logical operations, etc., and then call the data structure module 31 to create a classic calculation layer of a machine learning model based on the operation relationship between tensor data, such as a classic neural network.
  • the data structure module 31 can be used to arrange the input data according to a preset data structure to create tensor data for inputting the machine learning model, and create the preset data Tensor data that is structured and numerically determined for input to the machine learning model. Furthermore, in step S21, for the input training data, the training data can be arranged according to a preset data structure to obtain tensor data, and the training data can be stored as a part of the tensor data. For example, the acquired training data is 1, 2, 3, and the input training data can be converted into a vector structure [1, 2, 3] as part of the tensor data.
  • the tensor data may also include the information of the tensor data from which the data value is calculated and the relative relation between the tensor data and the tensor data containing the data value.
  • the gradient function of the quantitative data where the information of the tensor data whose data value is calculated can include the variable of the tensor data, the storage address of the data value, and the data value, as long as it indicates that the corresponding node of the tensor data is calculated to obtain the data
  • the tensor data of the value corresponds to the predecessor node of the node.
  • the tensor data y includes the data value corresponding to y such as [1,2,3], and also includes the calculated tensor data of w, x, and b of y Information and the gradient functions of y relative to w, x, and b respectively.
  • the information may include the data value storage addresses of w, x, and b
  • the tensor data y includes the gradient of y relative to w function x, the gradient function w of y relative to x, and the gradient function 1 of y relative to b
  • the gradient values of y relative to w, x, and b are calculated through back propagation, specifically, Obtain the data value of y directly from the tensor data y, as well as the data values of w, x, and b and the corresponding gradient function, and calculate the gradient values of y relative to w, x, and b respectively through these data values and the corresponding gradient function .
  • the quantum computing layer can be created by calling the quantum module 32.
  • the quantum computing layer is a program module containing a quantum program, which can be used to realize the quantum computing corresponding to the quantum program. Standard encapsulation makes the quantum computing layer easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
  • the quantum program is a program for implementing quantum computing. The quantum program can be obtained by calling the quantum module 32 to create quantum logic gates that act on the qubits in a specific order, and the quantum program can be packaged to obtain the quantum computing layer.
  • the classic calculation layer is the classic calculation part of the machine learning model, which can be obtained by encapsulating the created classic calculation program according to certain standards through the classic module 33, so that the classic calculation layer is convenient for training the machine learning model. use.
  • the quantum computing layer and the classical computing layer can be encapsulated by the classical module 33 to create an abstract class layer that meets certain standards.
  • the abstract class layer is realized by the class method in the programming language. Quantum computing layer and classical computing layer encapsulation can create machine learning models that meet certain standards.
  • the created abstract class layer defines the way of forward computing machine learning models, which is convenient for forward computing of machine learning models when training machine learning models In order to obtain the calculation results used to calculate the loss function, it is also possible to obtain the sequence relationship of the gradient calculation during the reverse calculation.
  • the classic module 33 can also be used to create a training layer of the machine learning model to train the machine learning model.
  • step S21 in addition to calling the data structure module 31 to create tensor data, you can also call the quantum module 32 to create a quantum computing layer to obtain a pure quantum machine learning model including the quantum computing layer, or call the classical module 33 to create a classical computing layer , to obtain a purely classical machine learning model including the classical computing layer, or simultaneously call the quantum module 32 and the classical module 33 to create a quantum computing layer and a classical computing layer, and obtain a hybrid machine learning model including the quantum computing layer and the classical computing layer.
  • step S22 After creating the training data and the machine learning model, execute step S22, input the tensor data into the machine learning model, and run the machine learning model forward to obtain the output result after the machine learning model calculates the tensor data. And, in order to train the machine learning model, the classic module 33 is called to create the training layer of the machine learning model.
  • the training layer includes a loss function layer and an optimizer layer, as shown in Figure 4, the classic module 33 includes:
  • a loss function unit 331 configured to calculate a loss function of the machine learning model
  • the optimizer unit 332 is configured to update the parameters of the machine learning model based on the loss function when training the machine learning model, so as to optimize the machine learning model.
  • calling the classic module to create the training layer of the machine learning model includes:
  • the loss function unit 331 is used to calculate the loss function of the machine learning model. For example, the square difference between the forward operation result of the machine learning model and the label data can be calculated as the loss function, and the forward operation result and the label data can also be calculated.
  • the binary cross entropy (Binary Cross Entropy) as the loss function.
  • the optimizer unit 332 is used to update the parameters of the machine learning model by gradient descent algorithm according to the gradient of the loss function relative to the parameters of the machine learning model to optimize it.
  • the gradient descent algorithm used by the optimizer can be any one of Stochastic Gradient Descent (SGD), Adaptive Gradient Algorithm (Adagrad), Adaptive Moment Estimation (Adam)
  • SGD Stochastic Gradient Descent
  • Adagrad Adaptive Gradient Algorithm
  • Adagrad Adaptive Gradient Algorithm
  • Adadam Adaptive Moment Estimation
  • other algorithms can also be used to update the parameters of the machine learning model.
  • the present disclosure does not specifically limit which types of loss functions the loss function unit 331 can calculate and which method the optimizer unit 332 uses to update parameters.
  • step S221 can be executed to call the loss function unit 331 to create the loss function layer.
  • the loss function layer is a packaged calculation module, which defines the calculation method of the loss function, and then in the machine
  • the loss function of the machine learning model can be calculated according to the calculation method defined by the loss function layer.
  • the training layer After the training layer is created, enter the execution step S23, input the output result of the machine learning model to the aforementioned training layer, update the parameters of the machine learning model according to a certain method, if the machine learning model after updating the parameters meets the preset conditions, For example, if the value of the corresponding loss function is less than the threshold, then stop updating the parameters, and use the machine learning model after updating the parameters as the machine learning model after training. If the machine learning model after updating the parameters does not meet the preset conditions, for example, the value of the corresponding loss function is greater than the threshold, the parameters can be continuously updated until a machine learning model meeting the preset conditions is obtained.
  • the training layer of the machine learning model can be created by calling the classic module 33, and then the tensor data is input into the machine learning model, and then the The training result of the machine learning model is input into the training layer, and the created machine learning model can be trained.
  • the training layer can be created by calling the classic module 33, without creating from scratch, which reduces the steps and time required to create the training layer, thereby improving the development efficiency of training when changing machine learning models or different application scenarios.
  • the tensor data corresponding to the training data is created by the data structure module 31, and the machine learning model is created based on the quantum module 32 and/or the classical module 33, so that the tensor data, machine
  • the learning model and the training layer can follow the standards based on the same machine learning framework system, reducing the adaptation operations between them, thereby improving the development efficiency of training machine learning models.
  • the development efficiency of its training can be improved, and it has a wide range of applications. scope of application.
  • the output result of the machine learning model is input into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model, including:
  • the execution step S231 input the output result of the machine learning model into the loss function layer, and calculate the value of the loss function of the machine learning model.
  • the training data can include sample data and label data. After the sample data is input into the machine learning model to obtain the output result, the output result is input into the loss function layer, the square difference between the label data and the output result is calculated, and all the square differences are summed. Calculate the value of the loss function.
  • step S232 it can be judged whether the value of the loss function satisfies the preset condition by comparing the value of the loss function with a preset threshold, for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into Optimizer layer.
  • a preset threshold for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into Optimizer layer.
  • other methods can also be used to determine that the value of the loss function does not meet the preset conditions, as long as the value of the preset function can be used to judge that the current machine learning model does not meet expectations.
  • the value of the loss function is input into the optimizer layer, and the value of the loss function can be used to calculate the gradient of the loss function relative to the parameters of the machine learning model, and then update the parameters of the machine learning model based on the gradient descent algorithm.
  • step S233 after the parameters of the machine learning model are updated, the value of the corresponding loss function is recalculated. And re-judging whether the value of the loss function satisfies the preset condition, if not, return to step S232, and continue to update the parameters of the machine learning model according to the value of the loss function, and if so, proceed to step S234.
  • step S234 when it is determined that the value of the loss function satisfies the preset condition, for example, the value of the loss function is less than the threshold, it means that the gap between the output result of the machine learning model for the sample data in the training data and the label data is very small, and the machine learning model can The expected application effect is achieved, and then the machine learning model after updating the parameters is used as the machine learning model after training, and the updating of parameters is stopped.
  • updating the parameters of the machine learning model based on the value of the loss function includes:
  • step S2321 partial derivatives of the loss function with respect to its parameters may be calculated to obtain gradients of the loss function with respect to the parameters.
  • step S2322 according to the obtained gradient, it is brought into the relevant formula of the gradient descent algorithm to update the parameters of the machine learning model.
  • the gradient descent algorithm can be batch gradient descent method (Batch Gradient Descent), stochastic gradient descent method (Stochastic Gradient Descent), etc.
  • the gradient reflects the fastest changing direction of the loss function.
  • the gradient descent algorithm can quickly change the parameters, thereby increasing the speed of the value change of the loss function, so as to quickly find the parameters corresponding to the value of the loss function that meets the preset conditions, and obtain the parameters that meet the The required machine learning model.
  • the machine learning model includes a quantum computing layer created based on the quantum module 32, and the value calculation based on the loss function
  • the gradient of the loss function relative to the parameters of the machine learning model includes:
  • the machine learning model includes a quantum computing layer.
  • the machine learning model is a pure quantum machine learning model that only includes a quantum computing layer, or a hybrid machine learning model that includes both a quantum computing layer and a classical computing layer.
  • step S23211 in order to obtain the loss function relative Based on the gradient of the parameters in the quantum computing layer in the machine learning model, in step S23211, first obtain the first gradient of the loss function relative to the output result of the quantum computing layer, and the first gradient of the output result relative to the parameters of the quantum computing layer Two gradients. Then enter the execution step S23211, multiply the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the quantum computing layer, and the above process as a whole is based on the chain derivation rule.
  • a machine learning model includes a quantum computing layer and a classical computing layer, where the computing function corresponding to the quantum computing layer is f(x; ⁇ ), and the computing function corresponding to the classical computing layer is g(x; ⁇ ), where x It is the input data of the calculation function, and ⁇ and ⁇ are parameters.
  • the acquired input data x1 is first input into f(x; ⁇ ) to obtain y1, and then input y1 into g(x; ⁇ ) to obtain the output result y2 of the machine learning model.
  • the loss function is the sum of the squared differences of y2 and the labeled data.
  • calculating the second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer includes:
  • the above method is called the parameter displacement method. Since it is difficult to find the second gradient of the output result of the quantum computing layer relative to the parameters in it by solving the analytical formula, the method of parameter displacement can improve the accuracy of the obtained second gradient. degree, and whether the quantum circuit at the quantum computing layer runs on a virtual machine or a real quantum device, the gradient can be calculated by this method, which has wide adaptability.
  • the first offset and the second offset are For the quantum computing layer f(x; ⁇ ), first calculate the first output result and the second output Then the second gradient is obtained by the following formula:
  • f(x; ⁇ ) ⁇ 0
  • x is the training data
  • H is the observed quantity
  • U H (x ; ⁇ ) is the conjugate transpose of U( ⁇ )
  • U( ⁇ ) corresponds to the part of the quantum computing layer that is used to perform quantum computing of a specific algorithm.
  • other methods may also be used to obtain the gradient of the loss function relative to the parameters, such as the finite difference method.
  • the machine learning model is a neural network model, which includes a classical neural network module, a quantum computing module A and a quantum circuit module B, wherein the classical neural network module is used to realize classical computing
  • the classical computing layer, the quantum computing module A and the quantum circuit module B are the quantum computing layers for realizing quantum computing.
  • the input data is input into the classical neural network module and the quantum computing module A, and then the output is input into the quantum circuit module B, and finally the loss function is calculated according to the output of the quantum circuit B.
  • enter the reverse operation process calculate the parameter gradient of the quantum circuit module B, the parameter gradient of the classical neural network module and the parameter gradient of the quantum computing module A, and use the gradient descent method to update the parameters in these modules respectively.
  • Fig. 12 is another flowchart of a model training method based on a machine learning framework system according to an exemplary embodiment. As shown in Fig. 12, the method includes:
  • S121 Call the data structure module to acquire input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model.
  • Step S121 and step S122 may refer to step S21 and step S22 respectively.
  • Steps S124 to S127 may refer to steps S231 to S234 respectively.
  • the calculation graph is a directed acyclic graph, which represents the relationship between each data in the form of a data structure graph (graph), and is composed of computing nodes and edges.
  • a data structure graph graph
  • the calculation graph is obtained, in which the data of the predecessor node is the dependent variable of the data of the successor node.
  • step S125 according to the calculation relationship shown in the calculation diagram, the gradient of the loss function relative to the parameters is calculated based on the value of the loss function.
  • calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function and the calculation graph includes:
  • the loss function may be used as a starting point and the selected parameters may be used as an end point to determine the shortest path between the two. Furthermore, in step S1252, for each computing node on the path, the intermediate gradient of the computing node relative to its predecessor nodes is calculated, and the computing node of the predecessor node is marked as a leaf node, and the computing node without a predecessor node is marked as a non-leaf node Since non-leaf nodes have no predecessor nodes, non-leaf nodes cannot calculate the corresponding intermediate gradients, and non-leaf nodes are generally parameters, and there is no need to calculate gradients as the end of the path.
  • step S1253 After calculating the intermediate gradient, execute step S1253 to multiply all the intermediate gradients corresponding to the aforementioned paths, and obtain the gradient of the loss function relative to its parameters according to the chain derivation rule.
  • the chain derivation rule is as described above, in This will not be repeated here.
  • the corresponding machine learning model includes a quantum computing layer qvc and a classical computing layer fc.
  • the corresponding calculation node saves the information of its predecessor node input, represented by nodes[0]:prev:input, and saves the gradient calculation formula of k relative to input, represented by df:grad_fun_of_input.
  • the information of param and the gradient calculation formula are saved in the corresponding calculation node, which are represented by nodes[0]:prev:param and df:grad_fun_of_q_param respectively, and the information of k and the gradient calculation formula are saved, and the information of k and the gradient calculation formula are stored by nodes[1]:prev:k and df:grad_fun_of_q_k.
  • fc represents the classic calculation program of the classic calculation layer of the machine learning model, for example, it can be a fully connected layer, and its output y is calculated by x, W, b, W and b are classic
  • the information and gradient calculation formula are expressed by nodes[0]:prev:W and df:grad_fun_of_W(), and the information of b and the gradient calculation formula are saved, expressed by nodes[1]:prev:b and df:grad_fun_of_b(), Save the information of x and the gradient calculation formula, represented by nodes[2]:prev:x and df:grad_fun_of
  • the input training data can be obtained by calling the data structure module, creating tensor data including the training data, and calling the quantum module and/or the classical module to create a machine learning model.
  • a machine learning model can also be created by creating a quantum computing layer and a classical computing layer with a training interface, see FIG. 15 for details.
  • Fig. 15 is another flowchart of creating a machine learning model according to an exemplary embodiment.
  • another embodiment of creating a machine learning model provided by this embodiment can be applied to an electronic device including the first machine learning framework system 30 as shown in FIG. 3 , and the first machine learning framework system includes a quantum module 32 and classic module 32, and creating a machine learning model may specifically include:
  • the first machine learning framework system 30 integrates numerous function sets for creating and training machine learning models, and these functions can be conveniently called through the defined interfaces to implement related operations on the machine learning models.
  • the first machine learning framework system 30 may include:
  • the quantum module 32 is configured to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, and the first training interface is used to call the first preset training program to train the Quantum machine learning programs;
  • the classic module 33 is configured to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the Classical machine learning programs;
  • the classical module 33 is further configured to create a machine learning model including the quantum computing layer and the classical computing layer.
  • a quantum machine learning program is a program used to realize a quantum machine learning task or its subtasks, for example, it may be a program for realizing a quantum neural network, or it may be a quantum fully connected layer or quantum pooling in a quantum neural network layers, quantum convolutional layers, etc.
  • a quantum machine learning program can be created by calling the quantum module 32, and various quantum logic gates acting on qubits can be created through the quantum module 32 to form a quantum machine learning program.
  • Quantum machine learning programs can contain various parameters, such as parameters containing parametric quantum logic gates.
  • the first preset training program is a pre-created program that can be used to complete all or part of the steps of training the quantum machine learning program, for example, it can include calculating the output of the quantum machine learning program relative to the input or parameters of the quantum machine learning program gradient program.
  • the first training interface is used to call the first preset training program, and the first training interface may include a function name and one or more parameters for passing in the first preset training program. By inputting the function name and related parameters of the first training interface, the call to the first preset training interface can be realized.
  • the function name of the first training interface can be _grad_x(), and its parameters can include the number of parameters of the quantum machine learning program , the number of qubits, the number of classical bits, etc., you can enter parameters in the brackets of the function name.
  • the corresponding first preset training program can be found in the execution program corresponding to the quantum computing layer according to the function name of the first training interface, and when found, the parameters are passed into the first preset training program, and directly execute the first preset training program.
  • the quantum computing layer is a program module that encapsulates quantum machine learning programs and other programs, such as the first preset training program above.
  • the machine learning model of the machine learning program it is convenient to realize related functions.
  • a classic machine learning program is a program used to implement a classic machine learning task or its subtasks, for example, it may be a program for implementing a classic neural network, or it may be a program for implementing a fully connected layer or a pooling layer in a classic neural network, Programs for convolutional layers, etc.
  • Classical machine learning programs can be created by calling the relevant interfaces and programs of the machine learning framework system, for example, classic machine learning programs can be created through interfaces and programs of various mathematical or logical operations.
  • Classical machine learning programs can contain various parameters, such as the weight matrix that can be a classic fully connected layer.
  • the second preset training program is a pre-created program that can be used to complete all or part of the steps of training a classic machine learning program, for example, it can include computing the output of a classic machine learning program relative to the input or parameters of a classic machine learning program gradient program.
  • the second training interface is used to call the second preset training program, and the second training interface may include a function name and one or more parameters for passing in the second preset training program. By inputting the function name and related parameters of the second training interface, the call to the second preset training interface can be realized.
  • the function name of the second training interface can also be _grad_x(), and its parameters can include parameters of classic machine learning programs Quantity, etc., parameters can be entered within the parentheses of the function name.
  • the classic computing layer is a program module encapsulated with classic machine learning programs and other programs, such as the second preset training program mentioned above. By encapsulating classic machine learning programs and other programs according to certain standards, the classic When the machine learning model of the machine learning program, it is convenient to realize related functions.
  • the classical module 33 can also be used to create a machine learning model, which can obtain a hybrid quantum and classical machine learning model including the quantum computing layer and the classical computing layer by encapsulating the quantum computing layer and the classical computing layer.
  • the quantum computing layer can be created by inputting the interface for creating the quantum computing layer.
  • the interface can be QuantumLayer(qprog_with_meansure, para_num, machine_type_or_cloud_token, num_of_qubits, num_of_cbits, diff_method, delta), and the items in the brackets are parameters .
  • qprog_with_meansure is the quantum machine learning program
  • para_num is the number of parameters
  • machine_type_or_cloud_token is the type of electronic equipment that the quantum machine learning program runs, for example, it can be a real quantum computer or a virtual machine
  • num_of_qubits is the quantum required for the quantum machine learning program to run
  • diff_method is the gradient calculation method when training the quantum machine learning program, for example, it can be the finite difference method
  • delta is the step size when the aforementioned finite difference method is used to calculate the gradient.
  • quantum machine learning program as the parameter of the above interface, and input the above interface, you can search for the program corresponding to the above interface in the file library corresponding to the quantum module of the machine learning framework system, for example, when you find a program with the same interface name, confirm the search to the program corresponding to the interface, and then pass the quantum machine learning program as a parameter into the program and execute the program to create a quantum computing layer including the quantum machine learning program.
  • a quantum computing layer that includes a specific quantum machine learning program, a first preset training program, and a first training interface through other interfaces.
  • a specific quantum machine learning program can be pre-established Good quantum fully connected layers, etc.
  • the first training interface and the first preset training program corresponding to the first training interface will be automatically encapsulated in the quantum computing layer, thereby facilitating When training the machine learning model, the first preset training program is directly invoked through the first training interface to train the quantum machine learning program.
  • the classic computing layer can be created by inputting the interface for creating the classic computing layer, and the classic machine learning program can be created by calling the classic module 33, and the classic machine learning program can be used as a parameter of the aforementioned interface.
  • search for the program corresponding to the above-mentioned interface search for the program corresponding to the above-mentioned interface.
  • a learning program is passed into the program and executed, creating a classical computing layer including a classical machine learning program.
  • a classical computing layer including a specific classical machine learning program, a second preset training program, and a second training interface can also be created through other interfaces.
  • a specific classical machine learning program can be pre-established Good fully connected layers, convolutional layers, pooling layers, etc.
  • the second training interface and the second preset training program corresponding to the second training interface will be automatically encapsulated in the classic computing layer, thereby facilitating When training the machine learning model, the second preset training program is directly invoked through the second training interface to train the classic machine learning program.
  • the quantum computing layer and the classical computing layer can be packaged by calling the classical module 33, and a machine learning model including the quantum computing layer and the classical computing layer can be created. For example, you can create a machine learning model with the following code:
  • the machine learning model includes a quantum computing layer and a classical computing layer, wherein the quantum computing layer is created through the interface QuantumLayer(Qcircuit,3,"cpu",4,1), Qcircuit is a quantum machine learning program, and 3 is a parameter Quantity, "cpu” indicates that the quantum machine learning program runs on a virtual machine, 4 indicates the number of qubits required by the quantum machine learning program, and 1 indicates the number of classical bits required by the quantum machine learning program.
  • the first training interface and the first preset training program are automatically encapsulated in the quantum computing layer. Through the interface Linear(7, 2), a classic computing layer including the classic fully connected layer, the second training interface and the second preset training program is created. 7 means that the number of input feature data of the fully connected layer is 7, 2 means that the number of output feature data of the fully connected layer is 2.
  • the machine learning model also includes a forward-running program, and the subsequent program implementation is invoked through the interface forward(self,x), so as to facilitate the forward-running of the machine learning model.
  • the execution order of step S1501 and step S1502 is not limited, as long as they are executed before step S1503.
  • a quantum computing layer including the quantum machine learning program can be created, and the created quantum computing layer can directly contain a first preset training program for training the quantum machine learning program , and the first training interface for calling the first preset training program, and similarly for the classic machine learning program, you can create a second preset training interface that includes the classic machine learning program and is used to train the classic machine learning program Program, the second training interface for calling the second preset training program, and then for the created machine learning model including the quantum computing layer and the classical computing layer, during training, you can directly pass the first training interface and the second training interface
  • the two training interfaces respectively call the first preset training program and the second preset training program to realize the training of the machine learning model, which can reduce the creation of training programs for quantum machine learning programs and classical machine learning programs, and save more time.
  • the coordinated work required for training quantum machine learning programs and training classical machine learning programs improves the development efficiency of training machine learning models that include both quantum and classical computing layers.
  • Fig. 16 is another flowchart of creating a machine learning model according to an exemplary embodiment.
  • another embodiment of creating a machine learning model provided by this embodiment can be applied to an electronic device including a first machine learning framework system 30 as shown in FIG. 3, and the first machine learning framework system 30 includes a quantum Module 32 and classic module 33, and creating a machine learning model can specifically include:
  • S1602. Call the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the Classic machine learning programs.
  • S1604 call the classic module to create a training layer of the machine learning model, the training layer is used to call the first preset training program through the first training interface, and call the preset training program through the second training interface Describe the second preset training program.
  • step S1601 to step S1603 may refer to the aforementioned step S1501 to step S1503.
  • a training layer in order to train the machine learning model, can be created, and an interface for calling the first training interface and the second training interface can be preset in the training layer, and then the first training interface and the second training interface can be called through this interface.
  • the machine learning model can also be trained through an interface, see FIG. 17 for details.
  • Fig. 17 is another flow chart of training a machine learning model according to an exemplary embodiment.
  • another embodiment of the training machine learning model provided by this embodiment can be applied to an electronic device including the first machine learning framework system 30 as shown in FIG. 3 , and the first machine learning framework system 30 includes data Structure module 31, quantum module 32 and classical module 33, described method comprises:
  • S1705 Call the data structure module to acquire input data and create tensor data including the input data.
  • step S1701 to step S1703 may refer to the aforementioned step S1501 to step S1503, and step S1704 may refer to step S1604. Further, for more details about the data structure module 31 , the quantum module 32 and the classical module 33 , reference may be made to the content described above, which will not be repeated in this disclosure.
  • the data structure module 31 can be used to arrange the input data according to a preset data structure to create tensor data for inputting the machine learning model, and create the preset data Tensor data that is structured and numerically determined for input to the machine learning model. Furthermore, in step S1705, for the input data, the input data may be arranged according to a preset data structure to obtain tensor data, and the input data may be stored as a part of the tensor data. For example, the acquired input data is 1, 2, 3, and the input data can be converted into a vector structure [1, 2, 3] as part of the tensor data. It should be noted that the input data may be the data used to train the machine learning model, or the data to be predicted and classified.
  • step S1705 is not limited to be performed after step S1704, as long as it is performed before step S1706, and step S1706 is not limited to be performed after step S1704, as long as it is performed after steps S1703 and S1705 are performed.
  • step S1705 is not limited to be performed after step S1704, as long as it is performed before step S1706
  • step S1706 is not limited to be performed after step S1704, as long as it is performed after steps S1703 and S1705 are performed.
  • more details about the tensor data can refer to the content described above, and the present disclosure will not repeat them here.
  • step S1706 the tensor data is input into the machine learning model to run the machine learning model forward to obtain an output result of the machine learning model on the tensor data.
  • step S1707 input the output result into the training layer, call the first preset training program and the second preset training program through the first training interface and the second training interface, and execute these two preset training programs to directly learn the quantum machine Programs and classical machine learning programs, or combined with other training programs to train quantum machine learning programs and classical machine learning programs, and obtain expected machine learning models as the trained machine learning models.
  • the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the first dependent variable of the quantum machine learning program, the first The dependent variable is an input or parameter to the quantum machine learning program.
  • the first preset training program may simultaneously include a first input gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the input of the quantum machine learning program, and a first input gradient calculation program for A first parameter gradient calculation procedure that calculates a gradient of an output of the quantum machine learning program with respect to a parameter of the quantum machine learning program.
  • the first input gradient calculation program or the first parameter gradient calculation program can be selected to be used as required.
  • the first input gradient calculation program can be selected to calculate the output of the quantum machine learning program relative to For the gradient of its input, if the quantum machine learning program whose gradient is to be obtained is at the end of the chain derivation step, the first parameter gradient calculation program can be selected to calculate the gradient of the output of the quantum machine learning program relative to its parameters.
  • the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the second The dependent variable is the input or parameter of the classical machine learning program.
  • the gradient can be the gradient of the output z of the classic machine learning program relative to the input p of the classic machine learning, or the output y of the classic machine learning program Gradient with respect to parameter i in classical machine learning.
  • the second preset training program may simultaneously include a second input gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the input of the classic machine learning program, and a second input gradient calculation program for A second parameter gradient calculation procedure that calculates a gradient of an output of the classical machine learning program with respect to a parameter of the classical machine learning program.
  • the second input gradient calculation program or the second parameter gradient calculation program can be selected to be used as required.
  • the second input gradient calculation program can be selected to calculate the output of the classic machine learning program relative to For the gradient of its input, if the classic machine learning program whose gradient is to be calculated is at the end of the chain derivation step, the second parameter gradient calculation program can be selected to calculate the gradient of the output of the classic machine learning program relative to its parameters.
  • the loss function of the machine learning model can be calculated based on the output result, and when it is determined that the value of the loss function does not meet the preset condition, for example, when the value of the loss function is greater than a threshold, it indicates that the machine
  • the parameters of the machine learning model can be updated based on the value of the loss function, that is, the parameters of the quantum computing layer and the classical computing layer can be updated to obtain a trained machine learning model.
  • the gradient of the loss function relative to the parameters of the quantum machine learning program can be calculated based on the above-mentioned first preset training program, and the gradient of the loss function relative to the parameters of the classical machine can be calculated based on the above-mentioned second preset training program. Gradients for the parameters of the learning program.
  • the machine learning model includes a quantum computing layer and a classical computing layer
  • z is the output result of the machine learning model.
  • the loss function in order to find the gradient of the loss function relative to the parameter i, the loss function can be calculated according to z, and then the first gradient of the loss function relative to the output z of the classical machine learning program can be calculated, and the output z can be calculated based on the second preset training program relative to The second gradient of the parameter i, the gradient of the loss function relative to the output parameter i is obtained by multiplying the first gradient and the second gradient.
  • the first gradient of the loss function relative to the output z can be calculated, the third gradient of the output z relative to the output y as the dependent variable p can be calculated based on the second preset training procedure, and based on The first preset training program calculates the fourth gradient of the output y relative to the parameter ⁇ , and then multiplies the first gradient, the third gradient and the fourth gradient to obtain the gradient of the loss function relative to the parameter ⁇ .
  • the loss function can be obtained by calculating the square of the difference between the output result of the machine learning model and the label data, that is, the square loss function, or the binary cross entropy (Binary Cross Entropy) between the output result and the label data can be calculated as the loss function.
  • the gradient can be brought into the gradient descent algorithm to update the parameters, for example, the stochastic gradient descent algorithm (Stochastic Gradient Descent, SGD) can be used , Adaptive gradient algorithm (Adaptive gradient algorithm, Adagrad), adaptive moment estimation (Adaptive Moment Estimation, Adam) and other algorithms, of course, other algorithms can also be used to update the parameters of the machine learning model.
  • SGD stochastic gradient descent algorithm
  • Adagrad Adaptive gradient algorithm
  • Adagrad adaptive moment estimation
  • Adam Adaptive Moment Estimation, Adam
  • the first gradient calculation program is configured to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
  • the gradient calculation method parameters can be passed in from the first training interface, and then the gradient calculation method can be determined according to the input gradient calculation method parameters. For example, when the gradient calculation method parameter is finite_diff, select the finite difference method (finite difference method) Calculate the aforementioned gradient. When the gradient calculation method parameter is parameter_shift, select the parameter shift method to calculate the aforementioned gradient.
  • the first gradient calculation program 1800 may include:
  • the parameter subtraction subroutine 1810 is used to subtract the first bias from the first dependent variable of the quantum machine learning program, and input the target data into the quantum machine learning program to obtain the first result;
  • the parameter adding subroutine 1820 is used to add the first dependent variable of the quantum machine learning program to the second bias, and input the target data into the quantum machine learning program to obtain the second result;
  • calculation subroutine 1830 for calculating the gradient according to the first result and the second result
  • the target data is the data input into the quantum machine learning program.
  • the corresponding first gradient calculation program 1800 includes a parameter subtraction subroutine 1810, a parameter addition subroutine 1820, and a calculation subroutine 1830, and tensor data is input into the forward calculation process of the machine learning model,
  • the data used as input to the quantum machine learning program is the target data.
  • the parameter subtraction subroutine 1810 is executed to subtract the first offset from the parameter of the quantum machine learning program, such as subtracting Then input the target data into the quantum machine learning program to obtain the first result output by the quantum machine learning program, and similarly execute the parameter adding subroutine 1820 to add the parameters of the quantum machine learning program to the second offset, for example Obtain the second result output by the quantum machine learning program, and finally execute the calculation subroutine 1830, which can calculate 1/2 of the difference between the first result and the second result as the gradient of the output of the quantum machine learning program relative to the parameter, and the whole process can be Calculate using the following formula:
  • the first offset amount and the second offset amount may be different, and other values may be used, depending on specific circumstances.
  • other methods may also be used to calculate the gradient based on the first result and the second result, for example, adding a correction amount on the basis of the above calculation formula, which is not specifically limited in the present disclosure.
  • the present disclosure does not make specific limitations on the execution order of the parameter subtraction subroutine 1810 and the parameter addition subroutine 1820 , which may be determined according to specific circumstances.
  • Using the parameter shift method can calculate the corresponding gradient when the quantum machine learning program is running on a real quantum computer, which broadens its scope of use compared with some gradient calculation methods that cannot be applied to real quantum computers.
  • the present disclosure also provides a corresponding device for creating a machine learning model.
  • the device may include a quantum computing layer creation module for calling the quantum module to create a quantum machine learning program.
  • the quantum computing layer of the first preset training program and the first training interface is used to call the first preset training program to train the quantum machine learning program;
  • the classical computing layer creation module is used for Calling the classic module to create a classic computing layer including a classic machine learning program, a second preset training program and a second training interface, the second training interface is used to call the second preset training program to train the classic machine A learning program;
  • a machine learning model creation module used to call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
  • the above device further includes: a training layer creation module, configured to call the classic module to create a training layer of the machine learning model, and the training layer is used to call the first preset through the first training interface.
  • a training program is set, and the second preset training program is invoked through the second training interface.
  • the first machine learning framework system further includes a data structure module
  • the above device further includes: a tensor data creation module, configured to call the data structure module to obtain input data and create a tensor including the input data data; an input module, configured to input the tensor data into the machine learning model to obtain an output result; a training module, configured to input the output result into the training layer, so as to call the first training interface through the first training interface
  • the first preset training program train the quantum machine learning program according to the first preset training program, and call the second preset training program through the second training interface, according to the second preset
  • the training program trains the classic machine learning program to obtain a trained machine learning model.
  • the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the first dependent variable of the quantum machine learning program, the first The dependent variable is an input or parameter to the quantum machine learning program.
  • the first gradient calculation program is configured to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
  • the first gradient calculation program when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for The first dependent variable of the quantum machine learning program is subtracted from the first offset, and the target data is input into the quantum machine learning program to obtain the first result; the parameter adding subroutine is used for learning the quantum machine The first dependent variable of the program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; the calculation subroutine is used to calculate the second result based on the first result and the second As a result, the gradient is calculated; wherein the target data is the data input into the quantum machine learning program.
  • the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the second The dependent variable is the input or parameter of the classical machine learning program.
  • a machine learning model can also be created by creating a noise-containing quantum computing layer, see FIG. 19 for details.
  • Fig. 19 is a schematic flowchart of a method for creating a noise-containing machine learning model according to an exemplary embodiment, which is applied to an electronic device of a first machine learning framework system including a quantum module and a classical module, and the quantum module includes a noise-containing subprogram Encapsulated units, and methods for creating machine learning models with noise can include:
  • Step 1910 Create a quantum program considering the influence of noise based on the quantum computing programming library included in the first machine learning framework system
  • Step 1920 use the quantum program as a parameter of the encapsulated noisy quantum computing layer interface, and pass it into the noisy quantum computing layer interface;
  • Step 1930 call the noisy quantum program encapsulation unit to create a noisy quantum computing layer through the noisy quantum computing layer interface;
  • Step 1940 call the classical module to create a machine learning model including the noise-containing quantum computing layer.
  • the quantum computing programming library included in the first machine learning framework system can be, for example, Qpanda, Qsikit, Cirq, Forest, Q#, qbsolv, Blackbird, etc., which are not limited here.
  • quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of two phenomena of quantum superposition and quantum entanglement, multiple states of information can be processed simultaneously.
  • the quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
  • classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0.
  • the classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing programs according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
  • an interface is a declaration of a series of methods, and it is a collection of some method characteristics.
  • An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behavior.
  • the noise-containing quantum computing layer interface is a declaration of a series of methods corresponding to the noise-containing quantum computing layer.
  • the specific form can be, for example, NoiseQuantumLayer().
  • the included quantum computing programming library creates a quantum program that considers the influence of noise; other interfaces mentioned below can also be explained here, and will not be repeated here.
  • the first machine learning framework system also includes a data structure module, and the calling of the classical module to create a machine learning model including the noise-containing quantum computing layer includes:
  • the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
  • the classical module further includes an abstract class submodule, and the calling of the classical module combines the classical computing layer, the noisy quantum computing layer, and the classical computing layer with the noisy quantum computing layer
  • a machine learning model including:
  • the abstract class submodule is called to encapsulate the initialized and encapsulated noisy quantum computing layer and the classical computing layer, as well as the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
  • the initialization function is _init_()
  • the forward propagation function is forward()
  • the noisy quantum computing layer and the classical computing layer are initialized and packaged to obtain the initialized and packaged noisy quantum computing layer and the classical computing layer, then it can be as follows:
  • Encapsulating the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship can be as follows:
  • the initialized and encapsulated noise-containing quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship are encapsulated to obtain a machine learning model, then it can be as follows:
  • this disclosure creates a quantum program that considers the influence of noise through the quantum computing programming library based on the machine learning framework system; A noise-containing quantum computing layer interface; finally calling a noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling a classical module to create a machine learning model including a noise-containing quantum computing layer.
  • This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit.
  • the machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except
  • the noisy quantum computing layer is created through the noisy quantum computing layer interface.
  • the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
  • the creation of a quantum program considering the influence of noise based on the quantum computing programming library included in the first machine learning framework system includes:
  • the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
  • the noise model types of logic gate noise include at least the following types: qubit relaxation process noise model, qubit dephasing process noise model, decoherence noise model, depolarization noise model, bit reversal noise model, Bit phase reversal noise model, phase damping noise model.
  • the relaxation process noise model of the qubit can be represented by DAMPING_KRAUS_OPERATOR
  • the dephasing process noise model of the qubit can be represented by DEPHASING_KRAUS_OPERATOR
  • the decoherence noise model can be represented by DECOHERENCE_KRAUS_OPERATOR
  • the polarization noise model can be represented by DEPOLARIZING_KRAUS_OPERATOR
  • the bit inversion noise model can be represented by BITFLIP_KRAUS_OPERATOR
  • the bit phase inversion noise model can be represented by BIT_PHASE_FLIP_OPRATOR
  • the phase damping noise model can be represented by PHASE_DAMPING_OPRATOR.
  • the kraus operator and expression method of the relaxation process noise model of the qubit are as follows:
  • the kraus operator and expression method of the dephasing process noise model of the qubit are as follows:
  • the decoherence noise model is a combination of the above two noise models, and their relationship is as follows:
  • K 1 K 1damping K 1dephasing
  • K 2 K 1damping K 2dephasing
  • K 3 K 2damping K 1dephasing
  • K 4 K 2damping K 2dephasing
  • the kraus operator and expression method of the depolarization noise model are as follows:
  • the kraus operator and representation method of the bit-reversal noise model are as follows:
  • the kraus operator and representation method of the bit-phase-reversal noise model are as follows:
  • the kraus operator and expression method of the phase damping noise model are as follows:
  • K 1 , K 2 , K 3 , and K 4 are kraus operators
  • p is a parameter required by the noise model type
  • X, Y, Z, and I are matrices corresponding to quantum logic gates.
  • applying for a noise-containing quantum virtual machine can be realized through NoiseQVM qvm.
  • the noise-containing quantum virtual machine can also be initialized.
  • the initialization can be realized through the initialization function qvm.init(); set
  • the noise of the quantum circuit running on the noise-containing virtual machine can be realized through the interface set_moise_model(); the application of qubits can be realized through the interface qvm.qAllocMany(), and if necessary, the classical bits can also be applied through the interface qvm.cAllocMany();
  • the noise-containing quantum virtual machine, the noise model and the quantum circuit are packaged, which can be realized by the function main().
  • the above-mentioned specific method is one of them, and it can also be realized through other functions, interfaces, and classes, and no more examples are given here.
  • the noise is logic gate noise
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
  • the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the logic gate noise takes effect on all qubits in the quantum circuit.
  • the noise is logic gate noise
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
  • the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the logic gate noise takes effect on specified qubits in the quantum circuit.
  • the specified quantum logic gate type can be, for example, H gate, X gate, Y gate, RX gate, etc.
  • the noise model type is such as the above-mentioned qubit relaxation process noise model, qubit dephasing process noise model, decoherence noise model, depolarization noise model, bit-reversed noise model, bit-phase-reversed noise model, phase-damped noise model, etc.
  • the parameters required by the noise model type are p or other parameters.
  • the logic gate noise interface can be, for example, qvm.set_noise_model(); for example, qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GA TE,0.1) only specifies that the quantum logic gate type is Pauli X gate, and the noise model type is bit The parameter p required for inverting the noise model and the type of the noise model is 0.1, and if no qubit is specified, the logic gate noise takes effect on all qubits in the quantum circuit. For example, qvm.set_noise_model(); for example, qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GA TE,0.1) only specifies that the quantum logic gate type is Pauli X gate, and the noise model type is bit The parameter p required for inverting the noise model and the type of the noise model is
  • qvm.set_noise_model (NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GATE,0.1,[q[0],q[1]]) specifies that the quantum logic gate type is Pauli X gate, the noise model type is bit-reversal noise model and all The parameter p required by the above noise model type is 0.1, and the qubits q[0] and q[1] are also specified, then the logic gate noise is only effective for q[0] and q[1] in the quantum circuit.
  • the noise is the measurement noise of qubits
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
  • the measurement noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the measurement noise interface.
  • the measurement noise setting method is similar to the above logic gate noise setting method, except that there is no need to specify the quantum logic gate type, and the measurement noise interface can be qvm.set_measure_error().
  • the noise is the quantum state reset noise of the qubit
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
  • 1> are used as parameters of the reset noise interface, and passed into the reset noise interface;
  • the quantum state reset noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the reset noise interface.
  • 1> are set to 0.9 and 0.05 respectively, and it is not reset to
  • the noise is the read noise of the qubit
  • the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
  • the read noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the read noise interface.
  • the quantum module in the context of the present disclosure may further include a noisy quantum program encapsulation unit configured to create a noisy quantum computing layer through the encapsulated noisy quantum computing layer interface, the noisy quantum computing layer
  • the interface is used to provide a quantum program that considers the influence of noise created based on the quantum computing programming library included in the machine learning framework system; the classical module can also be configured to create a machine learning model including the noise-containing quantum computing layer.
  • the machine learning framework system of the present disclosure may include a quantum computing programming library, which is configured to apply for a noise-containing quantum virtual machine, and set the noise of a quantum circuit running on the noise-containing virtual machine; Quantum logic gates on qubits to obtain quantum circuits running on the noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model and the quantum circuit to obtain a quantum program that considers the influence of noise , for more details, please refer to the previous description.
  • the present disclosure also provides a corresponding device for creating a machine learning model.
  • the device may include a program creation unit for programming based on the quantum computing contained in the machine learning framework system.
  • the library creates a quantum program that considers the influence of noise; the interface determination unit is used to use the quantum program as a parameter of the encapsulated noise-containing quantum computing layer interface, and passes in the noise-containing quantum computing layer interface; the creation unit is used for calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the classical module to create a machine learning model including the noise-containing quantum computing layer.
  • the program creation unit is specifically configured to:
  • the quantum computing programming library applies for a quantum virtual machine with noise, and sets the noise of the quantum circuit running on the virtual machine with noise; applies for qubits and creates quantum logic gates that act on the qubits, and obtains A quantum circuit on a noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model, and the quantum circuit to obtain a quantum program considering the influence of noise.
  • the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
  • the noise is logic gate noise
  • the program creation unit is specifically configured to: set the specified quantum logic gate type, The noise model type and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, the quantum running on the noise-containing virtual machine is set Logic gate noise of the wire, the logic gate noise being effective for all qubits in the quantum wire.
  • the noise is logic gate noise.
  • the program creation unit is specifically used to: set the specified qubit, quantum logic
  • the gate type, the noise model type and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, it is set to run on the noise-containing virtual machine
  • the logic gate noise of the quantum circuit on the logic gate noise takes effect on a specified qubit in the quantum circuit.
  • the program creation unit is specifically configured to: convert the quantum circuit to The probability of resetting the quantum state of the qubit to
  • the program creation unit is specifically configured to: use the specified noise model type And the parameters required by the noise model type are used as the parameters of the measurement noise interface, and passed into the measurement noise interface; through the measurement noise interface, the measurement of the quantum bits of the quantum circuit running on the noise-containing virtual machine is set noise.
  • the program creation unit is specifically configured to: set
  • this disclosure creates a quantum program that considers the influence of noise through the quantum computing programming library based on the machine learning framework system; A noise-containing quantum computing layer interface; finally calling a noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling a classical module to create a machine learning model including a noise-containing quantum computing layer.
  • This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit.
  • the machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except
  • the noisy quantum computing layer is created through the noisy quantum computing layer interface.
  • the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
  • the present disclosure also proposes to construct a compatible quantum computing layer by calling the compatible quantum computing layer, so as to create a machine learning model including a compatible quantum computing layer, see FIG. 20 for details.
  • Fig. 20 is another schematic flowchart of machine learning model building according to an exemplary embodiment.
  • the machine learning model construction method is applied to an electronic device that includes a first machine learning framework system and does not include a second machine learning framework system.
  • the first machine learning framework system includes a compatible quantum computing program encapsulation unit, and the machine learning model construction can be include:
  • Step 2010 Determine the compatible quantum computing layer interface of the corresponding second machine learning framework system, and the compatible quantum computing layer interface is used to provide the quantum computer created based on the quantum computing programming library contained in the second machine learning framework system. program;
  • Step 2020 constructing a compatible quantum computing layer by calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit;
  • Step 2030 Construct a machine learning model including the compatible quantum computing layer.
  • quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of two quantum phenomena called superposition and entanglement, multiple states of information can be processed simultaneously.
  • the quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
  • the compatible quantum computing program encapsulation unit includes a Compatiblelayer interface
  • the inheritance interface of this interface may have, for example, a compatible quantum computing layer interface QiskitLayer, which is used for quantum programs created based on the quantum computing programming library contained in IBM's second machine learning framework system;
  • a compatible quantum computing layer interface CirqLayer for quantum programs created based on the quantum computing programming library contained in Google's second machine learning framework system;
  • a compatible quantum computing layer interface QuantumLayer for Microsoft's second Quantum programs created by the quantum computing programming library included in the machine learning framework system;
  • the second machine learning framework system has a one-to-one correspondence with the compatible quantum computing layer interface, and the corresponding compatible quantum computing layer interface of the second machine learning framework system can be determined specifically through the corresponding relationship.
  • the quantum program created based on the quantum computing programming library contained in the second machine learning framework system can be used as a compatible
  • the parameters of the quantum computing layer interface can realize the construction of the quantum computing layer by calling the compatible quantum computing layer interface.
  • the method before determining the compatible quantum computing layer interface of the corresponding second machine learning framework system, the method further includes:
  • the quantum circuit is constructed through the quantum computing programming library included in the second machine learning framework system, and the quantum circuit is initialized by calling an initialization function, and the operation and measurement mode of the quantum circuit is defined through the operation function to obtain a quantum program.
  • the initialization function may be, for example, _init_()
  • the running function may be, for example, run().
  • the first machine learning framework system also includes a classical module and a data structure module, and the building includes the machine learning model compatible with the quantum computing layer, including:
  • classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0.
  • the classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing program according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
  • the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
  • the classic module includes a classic neural network layer unit, and the classic neural network layer unit includes at least one of the following: a specified model classic neural network layer subunit configured to be constructed through an encapsulated classic neural network layer interface The classical neural network layer of the specified model; the activation layer subunit configured to construct an activation layer for nonlinearly transforming the output of the classical neural network layer;
  • Said calling said classic module to build a classic computing layer including:
  • the classic neural network layer interface includes at least one of the following: one-dimensional convolutional layer interface Conv1D(), two-dimensional convolutional layer interface Conv2D, two-dimensional transposed convolutional layer interface ConvT2D(), average pooling layer interface AvgPool2D( ), maximum pooling layer interface MaxPool2D(), embedding layer interface Embedding2D(), one-dimensional batch normalization layer interface BatchNorm1D(), two-dimensional batch normalization layer interface BatchNorm2D, fully connected layer interface Linear(), random loss Layer interface Dropout().
  • the activation layer interface includes at least one of the following: Sigmoid(), Softplus(), Softsign(), Softmax(), HardSigmoid(), Relu(), LeakyRelu(), Elu(), Tanh().
  • the created classical computing layer and compatible quantum computing layer are as follows:
  • a, b, c, d, e, f, g, h are interface parameters.
  • the classical module includes an assignment function, and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer can be constructed through the assignment function.
  • the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the connection between the classical computing layer and the compatible quantum computing layer are connected through the classical module.
  • the forward propagation relationship is encapsulated to obtain a machine learning model, including:
  • the abstract class submodule is called to encapsulate the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class to obtain a machine learning model.
  • developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework system that they are used to, and then use the first machine learning framework system Determine the compatible quantum computing layer interface corresponding to the second machine learning framework system, and call the compatible quantum computing layer interface through the compatible quantum computing program encapsulation unit included in the first machine learning framework system to build a compatible quantum computing layer, thereby
  • the construction of the machine learning model is realized through the electronic device including the first machine learning framework system but not including the second machine learning framework system, thereby realizing the construction of the machine learning model across the quantum machine learning framework system.
  • the present disclosure also provides a corresponding device for creating a machine learning model.
  • the device may include a determination unit for determining the corresponding quantum of the second machine learning framework system Computing layer interface, the compatible quantum computing layer interface is used to provide the quantum program created based on the quantum computing programming library contained in the second machine learning framework system; the construction unit is used to call through the compatible quantum computing program encapsulation unit
  • the compatible quantum computing layer interface constructs a compatible quantum computing layer; constructs a machine learning model including the compatible quantum computing layer.
  • the construction unit is further configured to: use the quantum computing programming library included in the second machine learning framework system Constructing a quantum circuit, calling an initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining a quantum program.
  • the first machine learning framework system further includes a classical module and a data structure module
  • the construction includes the machine learning model compatible with the quantum computing layer
  • the construction unit is specifically used to: call the classical module Constructing the classical computing layer and calling the data structure module to construct the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calling the classical computing layer to combine the classical computing layer and the compatible quantum computing layer and encapsulating the forward propagation relationship between the classical computing layer and the compatible quantum computing layer to obtain a machine learning model.
  • the classic module includes a classic neural network layer unit
  • the classic neural network layer unit includes at least one of the following: a specified model classic neural network layer subunit configured to be constructed through an encapsulated classic neural network layer interface
  • the classical neural network layer of the specified model the activation layer subunit is configured to construct an activation layer for nonlinear transformation of the output of the classical neural network layer
  • the classical calculation layer is constructed by calling the classic module
  • the construction unit is specifically used to: call the specified model classic neural network layer subunit to construct a classic neural network layer, and use the classic neural network layer as a classic calculation layer; or call the specified model classic neural network layer subunit
  • the unit and the activation layer subunit construct a classical neural network layer and an activation layer, and use the classical neural network layer and the activation layer as a classical calculation layer.
  • the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the connection between the classical computing layer and the compatible quantum computing layer are connected through the classical module.
  • the forward propagation relationship is encapsulated to obtain a machine learning model, and the construction unit is specifically used to: call the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialization and the encapsulated compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship; calling The abstract class sub-module encapsulates the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on a module class to obtain a machine learning model.
  • developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework system that they are used to, and then use the first machine learning framework system Determine the compatible quantum computing layer interface corresponding to the second machine learning framework system, and call the compatible quantum computing layer interface through the compatible quantum computing program encapsulation unit included in the first machine learning framework system to build a compatible quantum computing layer, thereby
  • the construction of the machine learning model is realized through the electronic device including the first machine learning framework system but not including the second machine learning framework system, thereby realizing the construction of the machine learning model across the quantum machine learning framework system.
  • Fig. 21 is another flow chart of training a machine learning model according to an exemplary embodiment.
  • another embodiment of the training machine learning model provided in this embodiment can be applied to an electronic device including the first machine learning framework system 30 shown in FIG. 3 , and the first machine learning framework system 30 includes data Structure module 31, quantum module 32 and classical module 33, and the training machine learning model can include:
  • S2110 call the quantum module and the classical module to create a machine learning model including a quantum program, call the data structure module to acquire target data and create tensor data including the target data.
  • S2120 Call the quantum module to divide the second qubits into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device.
  • the first machine learning framework system 30 integrates numerous function sets for creating and training machine learning models, and these functions can be conveniently called through the defined interfaces to implement related operations on the machine learning models.
  • the first machine learning framework system 30 may include:
  • a data structure module 31 configured to acquire target data and create tensor data including the target data
  • a classical module 33 configured to create a machine learning model
  • a quantum module 32 configured to create a machine learning model, and divide said second qubits into groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, For each group of the second qubits, generate a quantum circuit including the group of the second qubits based on the quantum program, so that when the input data of each group of the quantum circuits is determined, the input data are respectively Input each quantum circuit, and obtain the output result of each quantum circuit.
  • the data structure module 31 defines the data structure of tensor data.
  • the input target data can be converted into tensor data, which can be used for inputting into the machine learning model for forward calculation.
  • the quantum computing layer of the machine learning model can be created by calling the quantum module 32.
  • the quantum computing layer is a program module containing a quantum program, which can be used to realize the quantum computing of the corresponding quantum program. Standard encapsulation makes the quantum computing layer easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
  • the quantum program is a program for implementing quantum computing.
  • the quantum program can be obtained by calling the quantum module 32 to create quantum logic gates that act on the qubits in a specific order, and the quantum program can be packaged to obtain the quantum computing layer.
  • the quantum module 32 can also group the second qubits according to the number of the above-mentioned first qubits and the number of the second qubits, and then generate quantum circuits corresponding to multiple groups of second qubits based on the quantum program, so as to obtain Multiple groups of quantum circuits are used to process data, and the specific execution method can refer to the subsequent description of the relevant steps of the data classification method.
  • the classic calculation layer of the machine learning model can be created by calling the classic module 33.
  • the classic calculation layer is the classic calculation part in the machine learning model, which can be the classic calculation program created by the classic module 33 according to Encapsulation of certain standards makes the classical computing layer easy to use when training machine learning models.
  • After the quantum computing layer and the classical computing layer are created they can be encapsulated through the classical module 33 to create an abstract class layer that meets certain standards.
  • the abstract class layer is realized by the class method in the programming language.
  • Computing layer and classic computing layer encapsulation can create machine learning models that meet certain standards.
  • the created abstract class layer defines the way of forward computing machine learning models, which is convenient for forward computing of machine learning models when training machine learning models.
  • the calculation result used to calculate the loss function can be obtained, and the sequence relationship of gradient calculation during reverse calculation can also be obtained.
  • the classic module 33 can also be used to create a training layer of the machine learning model to train the machine learning model.
  • the quantum module 32 can be called to create a quantum computing layer
  • the classical module 33 can be called to create a classical computing layer
  • the classical module 33 can be used to encapsulate the quantum computing layer and the classical computing layer to obtain a machine learning model that mixes quantum computing and classical computing .
  • the data structure module 31 is invoked to create tensor data containing the target data for input into the machine learning model.
  • the quantum device may contain a certain number of qubits for performing quantum calculations, for example, it may contain a quantum chip, the quantum chip contains a certain number of qubits, the second qubit is that the quantum device is not currently performing quantum computing tasks, Qubits that are idle and can be used at any time.
  • the first qubit is the qubit required to realize the quantum program or construct the quantum circuit corresponding to the quantum program.
  • the quantum module 32 can be called to obtain the number of first qubits required by the input quantum program through a related interface, or the number of first qubits required by the quantum program can be directly obtained automatically from the quantum computing layer.
  • the number of second qubits allowed to be used can also be obtained by passing parameters through the interface, or the state information of the quantum device can be automatically obtained, and then the number of second qubits allowed to be used by the quantum device can be obtained from the state information.
  • the present disclosure does not specifically limit how to obtain the two quantities.
  • the second qubits are divided into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, including :
  • step S2210 perform the above division operation to obtain the intermediate number. For example, there are 64 qubits that are currently idle and can be used normally in the quantum device, and the number of second qubits allowed to be used is 64. Run A quantum program requires at least 10 qubits, and the number of first qubits required by a quantum program is 10. Divide 64 by 10 to get the middle number 6.4.
  • step S2220 is executed.
  • the maximum integer is 6, so 6 is taken as the target number.
  • enter the execution step S2230 divide the second qubits into multiple groups, the number of groups is the target number, and the number of the second qubits in each group is greater than or equal to the first qubits required by the quantum program.
  • the number of the number of qubits following the aforementioned example, divides 64 second qubits into 6 groups, and the number of second qubits in each group is 10.
  • the second qubits since there are 4 second qubits that are allowed to be used, it can also be divided into Among the 6 groups of second qubits, 4 groups of second qubits have 11, and the remaining 2 groups of second qubits have 10. In a possible implementation manner, the second qubits may also be divided into 1 group.
  • the quantum program includes a series of quantum logic gates acting on the first qubit in sequence
  • the generated quantum circuit can also include the same quantum logic gates as the quantum logic gates of the quantum program, of course , it is also possible to optimize the quantum logic gates of the quantum program to generate quantum logic gates that perform the same quantum calculation for constructing quantum circuits.
  • the quantum logic gates contained in the quantum program are H gate, RZ gate, and Pauli X gate in sequence
  • the corresponding generated quantum circuit can also contain H gate, RZ gate, and Pauli X gate in sequence.
  • the generated quantum circuit can also be used for
  • the quantum logic gates are optimized to contain different quantum logic gates, but the quantum computation performed is still the same as the quantum program. For each generated quantum circuit, they may be the same or different, as long as the quantum calculations performed by them are the same as the quantum program, which is not specifically limited in the present disclosure.
  • generating a quantum circuit including the group of the second qubits based on the quantum program includes:
  • step S2310 referring to the foregoing description, the same quantum logic gates as the quantum logic gates included in the quantum program can be generated according to the quantum program, or the quantum logic gates of the quantum program can be optimized to generate different quantum logic gates for acting on The second qubit, as long as it performs the same quantum computation.
  • the quantum circuit includes the second qubit and the aforementioned quantum logic gate, and the quantum logic gate acts on the second qubit in a certain order, and the order may be the same as the quantum logic gate of the quantum program.
  • the quantum circuit may also include measurement operations on some or all of the second qubits.
  • generating quantum circuits can be performed before running the machine learning model, or it can be executed simultaneously with running the machine learning model.
  • step S2140 is executed to input the tensor data into the machine learning model, and then in the process of running the machine learning model forward, the input data for inputting the quantum circuit is obtained.
  • a machine learning model that includes a classical computing layer and a quantum computing layer
  • multiple tensor data are correspondingly created after obtaining multiple target data, which needs to be calculated in turn by the classical computing layer and the quantum computing layer to obtain the output result of the machine learning model .
  • multiple tensor data are first input into the classical computing layer for calculation, and the obtained multiple output data of the classical computing layer can be used as the input of multiple quantum circuits
  • Data is input into each quantum circuit, and the output result of each quantum circuit is obtained.
  • multiple quantum circuits can run in parallel, or run according to a certain time difference according to the schedule.
  • the input data to each quantum circuit can be different.
  • the machine learning model created by calling the quantum module and the classical module before inputting the tensor data into the model, according to the number of first qubits required by the quantum program and the number of second qubits available , generate multiple sets of quantum circuits corresponding to the quantum program, and then make the tensor data input machine learning model obtain multiple input data for inputting multiple sets of quantum circuits, and then input multiple input data into multiple sets of quantum circuits to Using multiple groups of quantum circuits to perform related quantum calculations on the input data to improve the efficiency of quantum program operation.
  • the machine learning model After obtaining the output result of each quantum circuit, the machine learning model continues to operate on the output result to obtain the prediction result of the tensor data.
  • the machine learning model includes a quantum computing layer and a classical computing layer in turn. After the quantum program based on the quantum computing layer generates the corresponding quantum circuit, tensor data is input into the quantum circuit to obtain the output result. Then, the output result is input into the classical computing layer to obtain the calculation result, which is used as the prediction result of the machine learning model for the tensor data, so that the machine learning model can complete the prediction task of the target data. Next, create the training layer of the machine learning model by calling the classical module, and input the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model .
  • the training layer includes a loss function layer and an optimizer layer
  • the classic module includes: a loss function unit configured to calculate the loss function of the machine learning model; an optimizer unit configured to train The machine learning model updates parameters of the machine learning model based on the loss function, so as to optimize the machine learning model.
  • calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer, and calling the optimizer unit to create the optimizer layer.
  • the loss function unit is used to calculate the loss function of the machine learning model. For example, the square difference between the prediction result of the machine learning model for tensor data and the label data can be calculated as the loss function, and the difference between the prediction result and the label data can also be calculated.
  • Binary Cross Entropy (Binary Cross Entropy) as a loss function.
  • the optimizer unit can be used to update the parameters of the machine learning model by using the gradient descent algorithm according to the gradient of the loss function relative to the parameters of the machine learning model to optimize it.
  • the gradient descent algorithm used by the optimizer can be any one of Stochastic Gradient Descent (SGD), Adaptive Gradient Algorithm (Adagrad), Adaptive Moment Estimation (Adam)
  • SGD Stochastic Gradient Descent
  • Adagrad Adaptive Gradient Algorithm
  • Adadam Adaptive Moment Estimation
  • other algorithms can also be used to update the parameters of the machine learning model.
  • the present disclosure does not specifically limit which types of loss functions the loss function unit can calculate and which method the optimizer unit uses to update the parameters.
  • the loss function unit is called to create the loss function layer.
  • the loss function layer is a packaged calculation module, which defines the calculation method of the loss function, and then input the prediction result of the machine learning model When reaching the loss function layer, the loss function of the machine learning model can be calculated according to the calculation method defined by the loss function layer.
  • the optimizer unit can be called to create the optimizer layer, so that after the prediction result is input to the loss function layer and the loss function is calculated, the parameters of the machine learning model are updated according to the loss function until a suitable The parameters enable the machine learning model to achieve the desired effect and complete the optimization of the machine learning model.
  • the training layer After creating the training layer, input the prediction results of the machine learning model to the aforementioned training layer, and update the parameters of the machine learning model according to a certain method. If the machine learning model after updating the parameters meets the preset conditions, such as its corresponding loss If the value of the function is less than the threshold, the update of the parameters is stopped, and the machine learning model after updating the parameters is used as the machine learning model after training. If the machine learning model after updating the parameters does not meet the preset conditions, for example, the value of the corresponding loss function is greater than the threshold, you can continue to update the parameters until a machine learning model that meets the preset conditions is obtained.
  • the preset conditions such as its corresponding loss
  • the same set of tensor data will be repeatedly input into the machine learning model for training.
  • the order of the tensor data can be randomly changed to prevent the machine learning model from being repeatedly trained.
  • the resulting overtraining prevents the loss function from converging to a local minimum when using the gradient descent algorithm.
  • the sequence of the input data may be randomly changed. It is also possible to randomly change the order of the tensor data before obtaining the input data of each set of quantum circuits based on the tensor data, and then change the order of inputting the tensor data into the machine learning model.
  • inputting the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model includes: inputting the prediction result into the loss function layer, to calculate the value of the loss function of the machine learning model.
  • the value of the loss function is input into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function.
  • the machine learning model after updating the parameters is used as the machine learning model after training.
  • the prediction result of the machine learning model on tensor data is input into the loss function layer, and the value of the loss function of the machine learning model is calculated.
  • the loss function represents the prediction result of the machine learning model and the actual calculation situation
  • the target data can include sample data and label data. After the sample data is input into the machine learning model to obtain the prediction result, the prediction result is input into the loss function layer, and the square of the difference between the label data and the prediction result is calculated, and the square of all differences is calculated. The sum calculates the value of the loss function.
  • the value of the loss function satisfies the preset condition by comparing the value of the loss function with a preset threshold, for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into the optimizer layer.
  • a preset threshold for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into the optimizer layer.
  • other methods can also be used to determine that the value of the loss function does not meet the preset conditions, as long as the value of the preset function can be used to judge that the current machine learning model does not meet expectations.
  • the value of the loss function is input into the optimizer layer, and the value of the loss function can be used to calculate the gradient of the loss function relative to the parameters of the machine learning model, and then update the parameters of the machine learning model based on the gradient descent algorithm.
  • the machine learning model After updating the parameters of the machine learning model, recalculate the value of the corresponding loss function. And re-judging whether the value of the loss function meets the preset condition, if not, the value of the loss function can be input into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function, and continue according to The value of the loss function updates the parameters of the machine learning model, and if satisfied, the machine learning model after updating the parameters can be used as the machine learning model after training.
  • the machine learning model predicts the sample data in the target data.
  • the application effect and then use the machine learning model after updating the parameters as the machine learning model after training, and stop updating the parameters.
  • updating the parameters of the quantum program based on the value of the loss function includes: calculating the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function.
  • the parameters of the quantum program are updated based on a plurality of the gradient and gradient descent algorithms. For example, the partial derivative of the loss function with respect to its parameters can be calculated to obtain the gradient of the loss function with respect to the parameters of the quantum program. Since multiple quantum circuits are generated above, multiple tensor data are processed to obtain multiple output results. For multiple output results, the machine learning model processes them to obtain multiple prediction results, and multiple prediction results can obtain the loss function. For multiple values, for multiple values of the loss function, the gradient of the loss function relative to the same parameter is calculated to obtain multiple gradients.
  • the gradient reflects the fastest changing direction of the loss function.
  • the gradient descent algorithm can quickly change the parameters, thereby increasing the speed of the value change of the loss function, so as to quickly find the parameters corresponding to the value of the loss function that meets the preset conditions, and obtain the parameters that meet the The required machine learning model.
  • updating the parameters of the quantum program based on multiple gradients and gradient descent algorithms includes: calculating an average value of multiple gradients.
  • the mean is multiplied by the step size to get the bias.
  • the offset is subtracted from the parameter of the quantum program to update the parameter of the quantum program.
  • the arithmetic mean of multiple gradients can be calculated. For example, if there are 10 gradients in total, the 10 gradients can be added and then divided by 10 to obtain the average value. After obtaining the average value, multiply the average value by the step size.
  • the step size can be a preset value, indicating the size of the change in each parameter update. The product of the two multiplied is used as the offset value, and the quantum program Subtract the offset from the parameter to obtain a new parameter value, and replace the previous parameter value with the new parameter value to complete the update of the parameters of the quantum program.
  • the present disclosure also provides a method for running the machine learning model, see FIG. 24 for details.
  • Fig. 24 is a schematic flowchart of a method for running a machine learning model according to an exemplary embodiment, which is applied to an electronic device including a first machine learning framework system, and the first machine learning framework system includes a classical module and a quantum module,
  • the methods include:
  • Step 2410 designate the first computing device of the classical computing layer and the second computing device of the quantum computing layer;
  • Step 2420 Pass the first computing device as a parameter into the classical computing layer interface, and pass the second computing device as a parameter into the quantum computing layer interface;
  • Step 2430 calling the classical module to create a classical computing layer through the classical computing layer interface, and calling the quantum module to create a quantum computing layer through the quantum computing layer interface;
  • Step 2440 calling the classical module to build a machine learning model including the classical computing layer and the quantum computing layer;
  • Step 2450 Call the first computing device and the second computing device to run the machine learning model.
  • quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of quantum superposition and quantum entanglement, multiple states of information can be processed simultaneously.
  • the quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
  • classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0.
  • the classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing program according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
  • an interface is a declaration of a series of methods, and it is a collection of some method characteristics.
  • An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behavior.
  • the classical computing layer interface is the declaration of a series of methods corresponding to the classical computing layer
  • the quantum computing layer interface is the declaration of a series of methods corresponding to the quantum computing layer. The other interfaces mentioned below can also be explained by referring to this section, no longer repeat.
  • the classic calculation layer includes a classic neural network layer and/or an activation layer, and the type of the classic neural network layer interface corresponding to the classic neural network layer can be as described above for the classic neural network layer interface, and will not be repeated here.
  • the type of the activation layer interface may be as described above in the introduction of the activation layer interface, and will not be repeated here.
  • the quantum computing layer includes general quantum computing layer interfaces (such as QuantumLayer(), QuantumLayerV2()), compatible quantum computing layer interfaces (such as Compatiblelayer()), noisy quantum computing layer interfaces (such as NoiseQuantumLayer()), quantum convolution Layer interface (such as QConv ()), quantum fully connected layer interface (such as Qlinear ()); the general quantum computing layer interface is used to provide the quantum computing programming library created based on the second machine learning framework system.
  • general quantum computing layer interfaces such as QuantumLayer(), QuantumLayerV2()
  • compatible quantum computing layer interfaces such as Compatiblelayer()
  • noisy quantum computing layer interfaces such as NoiseQuantumLayer()
  • quantum convolution Layer interface such as QConv ()
  • quantum fully connected layer interface such as Qlinear ()
  • the general quantum computing layer interface is used to provide the quantum computing programming library created based on the second machine learning framework system.
  • the compatible quantum computing layer interface is used to provide a quantum program created based on a quantum computing programming library not included in the second machine learning framework system
  • the noisy quantum computing layer interface is used to provide a quantum program based on the second machine learning framework system A quantum program that considers the influence of noise created by the quantum computing programming library included in the machine learning framework system
  • each classical computing layer interface or quantum computing layer interface includes a parameter machine_type_or_cloud_token, which can be used to specify the first computing device or the second computing device.
  • the first computing device is a classical computing device
  • the second computing device is a quantum computing device.
  • the computing hardware of the first computing device includes at least one of the following: a central processing unit (central processing unit, CPU), a graphics processing unit (graphics processing unit, GPU), a tensor processing unit (tensor processing unit, TPU), an on-site A programmable logic gate array (Field Programmable Gate Array, FPGA);
  • the computing hardware of the second computing device includes at least one of the following: CPU, GPU, and quantum processor (Quantum Processing Unit, QPU).
  • the first machine learning framework system also includes a data structure module, and the calling of the classical module to construct a machine learning model including the classical computing layer and the quantum computing layer includes:
  • the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
  • the classical module further includes an abstract class submodule, and the calling of the classical module integrates the classical computing layer, the quantum computing layer, and the preceding layer between the classical computing layer and the quantum computing layer Encapsulate the propagation relationship to obtain a machine learning model, including:
  • the abstract class submodule is called to encapsulate the initialized and encapsulated quantum computing layer and the classical computing layer, as well as the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
  • this disclosure builds a machine learning model under the provided machine learning framework system including classical modules and quantum modules.
  • the classical computing layer of the machine learning model is created by the classical module through the interface of the classical computing layer, and the quantum computing layer Created by the quantum module through the quantum computing layer interface, when it is created, the first computing device of the classical computing layer is designated as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer is designated as the parameter of the quantum computing layer interface, thus
  • the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
  • the calling the first computing device and the second computing device to run the machine learning model includes:
  • the classical module is called to convert the format of the input data of the quantum computing layer into the format of the input data through the data format conversion interface.
  • the computing format supported by the second computing device
  • the calling the first computing device and the second computing device to run the machine learning model also includes:
  • the classical module is called to convert the format of the input data of the classical computing layer into the format of the input data through the data format conversion interface The computing format supported by the first computing device;
  • the machine learning model includes multiple computing layers.
  • the computing layer includes the classical computing layer and the quantum computing layer.
  • Each computing layer can be operated by different computing devices. Therefore, the computing format of the output data of the previous computing layer It is not necessarily the computing format supported by the input data of the next computing layer, so data format conversion is required.
  • the data format conversion can be realized through the data format conversion interface.
  • the method also includes:
  • the quantum computing programming library included in the second machine learning framework system can be, for example, Qpanda, Qsikit, Cirq, Forest, Q#, qbsolv, Blackbird, etc., which are not limited here.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the second machine learning framework system.
  • a virtual machine refers to a complete computer system that is simulated by software and has complete hardware system functions and runs in a completely isolated environment.
  • a quantum virtual machine is a complete computer system that simulates a quantum computer through a classical computer.
  • constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
  • the quantum machine type of the specified computing hardware is passed as a parameter to the initialization function in the quantum computing programming library included in the second machine learning framework system, and the quantum virtual machine is created through the initialization function.
  • the initialization function is init()
  • the specific creation method of the virtual machine is init(QMachineType::CPU), which will generate a global quantum virtual machine qvm in the code.
  • constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
  • the quantum machine type of the specified computing hardware is passed as a parameter to the quantum machine initialization interface in the quantum computing programming library contained in the second machine learning framework system, and the quantum virtual machine is created through the quantum machine initialization interface.
  • the quantum machine initialization interface is initQuantumMachine()
  • constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
  • the quantum virtual machine is created based on the quantum virtual machine class of the specified computing hardware in the quantum computing programming library included in the second machine learning framework system.
  • the quantum virtual machine class of the specified computing hardware can be, for example, CPUQVM, GPUQVM or others
  • Fig. 25 is a schematic structural diagram of a machine learning model running device according to an exemplary embodiment, which is applied to an electronic device including a machine learning framework system, and the machine learning framework system includes a classical module and a quantum module,
  • the operating device 2500 includes:
  • a computing device determining unit 2510 configured to specify the first computing device of the classical computing layer and the second computing device of the quantum computing layer;
  • An interface determining unit 2520 configured to pass the first computing device into the classical computing layer interface as a parameter, and pass the second computing device into the quantum computing layer interface as a parameter;
  • a computing layer creation unit 2530 configured to call the classical module to create a classical computing layer through the classical computing layer interface, and call the quantum module to create a quantum computing layer through the quantum computing layer interface;
  • the model running unit 2540 is configured to call the classical module to build a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model.
  • the model running unit 2540 is specifically configured to:
  • the classical module is called to convert the format of the input data of the quantum computing layer into the format of the input data through the data format conversion interface.
  • the computing format supported by the second computing device
  • the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; Computing hardware includes at least one of the following: CPU, GPU, quantum processor QPU.
  • the computing hardware of the second computing device is CPU or GPU, then the second computing device is a quantum virtual machine; the calling of the first computing device and the second computing device to run machine learning Before the model, the computing device determination unit 2510 is also used to:
  • the quantum virtual machine is constructed based on the quantum computing programming library contained in the machine learning framework system.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
  • the quantum machine type of the specified computing hardware is passed as a parameter to the initialization function in the quantum computing programming library included in the machine learning framework system, and the quantum virtual machine is created through the initialization function.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
  • the quantum machine type of the specified computing hardware is passed as a parameter to the quantum machine initialization interface in the quantum computing programming library contained in the machine learning framework system, and the quantum virtual machine is created through the quantum machine initialization interface.
  • the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
  • the quantum virtual machine is created based on the quantum virtual machine class of the specified computing hardware in the quantum computing programming library included in the machine learning framework system.
  • this disclosure builds a machine learning model under the provided machine learning framework system including classical modules and quantum modules.
  • the classical computing layer of the machine learning model is created by the classical module through the interface of the classical computing layer, and the quantum computing layer Created by the quantum module through the quantum computing layer interface, when it is created, the first computing device of the classical computing layer is designated as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer is designated as the parameter of the quantum computing layer interface, thus
  • the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
  • the classic module in the machine learning framework system of the present disclosure is further configured to create a classic computing layer through the classic computing layer interface, and the parameters of the classic computing layer interface include the first computing device of the classic computing layer
  • the quantum module is also configured to create a quantum computing layer through the quantum computing layer interface, and the parameters of the quantum computing layer interface include the second computing device of the quantum computing layer
  • the classical module is also configured to construct the classical The machine learning model of the computing layer and the quantum computing layer, and calling the first computing device and the second computing device to run the machine learning model.
  • the classical module is also configured to convert the input data format of the quantum computing layer to Convert to a computing format supported by the second computing device.
  • FIG. 26 is a block diagram of a model training device based on a machine learning framework system according to an exemplary embodiment, which can be applied to an electronic device including the first machine learning framework system 30 shown in FIG. 3,
  • the first machine learning framework system 30 includes a data structure module 31, a quantum module 32 and a classical module 33, as shown in Figure 15, the training device 2600 includes:
  • Creation module 2610 used to call the data structure module 31 to obtain input training data, create tensor data including the training data, and call the quantum module 32 and/or the classical module 33 to create a machine learning model;
  • the input training module 2620 is used for inputting the tensor data into the machine learning model, and calling the classic module 33 to create a training layer of the machine learning model;
  • a parameter update module 2630 configured to input the output result of the machine learning model into the training layer, and update the parameters of the machine learning model based on the output result of the training layer, so as to obtain the trained machine learning model.
  • An embodiment of the present disclosure also provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the model training method described in FIG. 1 above when running.
  • An embodiment of the present disclosure also provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to perform the model training described in the above-mentioned accompanying drawing 1 method.
  • the term “if” may be interpreted as “when” or “once” or “in response to determining” or “in response to detecting” depending on the context.
  • the phrase “if determined” or “if [the described condition or event] is detected” may be construed, depending on the context, to mean “once determined” or “in response to the determination” or “once detected [the described condition or event] ]” or “in response to detection of [described condition or event]”.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Health & Medical Sciences (AREA)
  • Computing Systems (AREA)
  • Biomedical Technology (AREA)
  • Biophysics (AREA)
  • Computational Linguistics (AREA)
  • Data Mining & Analysis (AREA)
  • Evolutionary Computation (AREA)
  • Life Sciences & Earth Sciences (AREA)
  • Molecular Biology (AREA)
  • Artificial Intelligence (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Mathematical Physics (AREA)
  • Software Systems (AREA)
  • Health & Medical Sciences (AREA)
  • Electrically Operated Instructional Devices (AREA)
  • Machine Translation (AREA)

Abstract

A model training method based on a machine learning framework system and a related device. The method comprises: calling a data structure module to obtain input training data, creating tensor data comprising training data, and calling a quantum module and/or a classic module to create a machine learning model (S21); inputting the tensor data into the machine learning model, and calling the classic module to create a training layer of the machine learning model (S22); and inputting an output result of the machine learning model into the training layer so as to update parameters of the machine learning model to obtain a trained machine learning model (S23).

Description

基于机器学习框架***的模型训练方法及相关设备Model training method and related equipment based on machine learning framework system
相关申请的交叉引用Cross References to Related Applications
本公开要求于2021年12月30日申请的,申请号为2021116806111,名称为“基于机器学习框架的模型训练方法及相关设备”;于2022年01月24日申请的,申请号为2022100834667,名称为“数据处理方法、机器学习框架及相关设备”;于2021年12月30日申请的,申请号为202111680566X,名称为“含噪声机器学习模型创建方法、机器学习框架及相关设备”;于2022年12月30日申请的,申请号为202111680573X,名称为“机器学习模型构建方法、机器学习框架及相关设备”;于2021年12月30日申请的,申请号为2021116806130,名称为“数据处理方法、机器学习框架及相关设备”;于2021年12月30日申请的,申请号为2021116805706,名称为“机器学习模型运行方法、机器学习框架及相关设备”的中国专利申请的优先权。This disclosure requires application on December 30, 2021, with application number 2021116806111, titled "Model Training Method and Related Equipment Based on Machine Learning Framework"; application on January 24, 2022, application number 2022100834667, title It is "data processing method, machine learning framework and related equipment"; applied on December 30, 2021, the application number is 202111680566X, and the name is "noise-containing machine learning model creation method, machine learning framework and related equipment"; in 2022 For applications filed on December 30, 2021, the application number is 202111680573X, and the name is "Machine Learning Model Construction Method, Machine Learning Framework, and Related Equipment"; for applications on December 30, 2021, the application number is 2021116806130, and the name is "Data Processing method, machine learning framework and related equipment”; the priority of the Chinese patent application filed on December 30, 2021 with the application number 2021116805706 titled “Machine Learning Model Operation Method, Machine Learning Framework and Related Equipment”.
技术领域technical field
本公开一般地涉及量子计算技术领域。更具体地,本公开涉及一种基于机器学习框架***的模型训练方法、模型训练装置、存储介质和电子装置。The present disclosure generally relates to the field of quantum computing technology. More specifically, the present disclosure relates to a model training method based on a machine learning framework system, a model training device, a storage medium and an electronic device.
背景技术Background technique
机器学习模型由于其优异的性能而被广泛应用于人工智能研究,通过利用标注好的训练数据对机器学习模型进行训练,可以得到符合预期的机器学习模型,进而将其用于语音识别、图像识别等具体应用工作。机器学习模型无须人为设立其用于具体应用场景的标准,通过训练机器学习模型可以自己建立相应的工作标准,对于不同应用场景具有较好的适应性。随着量子计算的发展,越来越多的包含量子计算层的机器学习模型开始出现。Machine learning models are widely used in artificial intelligence research due to their excellent performance. By using labeled training data to train machine learning models, machine learning models that meet expectations can be obtained, and then used for speech recognition and image recognition. and other specific applications. The machine learning model does not need to artificially set its standards for specific application scenarios. By training the machine learning model, it can establish corresponding working standards by itself, which has good adaptability to different application scenarios. With the development of quantum computing, more and more machine learning models that include quantum computing layers are beginning to appear.
相关技术中,对于不同的机器学习模型尤其是包含量子计算层的机器学习模型,或机器学习模型应用于不同场景时,需要对训练方法作较多变更以得到合适的训练方法,开发效率低。In related technologies, for different machine learning models, especially machine learning models including quantum computing layers, or when machine learning models are applied to different scenarios, it is necessary to make many changes to the training method to obtain a suitable training method, and the development efficiency is low.
发明内容Contents of the invention
2021116806111为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种基于机器学习框架的模型训练方法及相关设备,旨在训练不同的机器学习模型或应用于不同场景的机器学习模型时,减少机器学习模型的训练方法的变更,提高开发效率。鉴于此,本公开在如下的多个方面提供解决方案。2021116806111 In order to at least solve the technical problems described in the background technology section above, this disclosure proposes a model training method and related equipment based on a machine learning framework, aiming at training different machine learning models or when applying machine learning models in different scenarios , reduce the change of the training method of the machine learning model, and improve the development efficiency. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供一种基于机器学习框架的模型训练方法,应用于包括机器学习框架的电子设备,所述机器学习框架包括数据结构模块、量子模块和经典模块,所述方法包括:调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型;将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层;将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型。In a first aspect, the present disclosure provides a model training method based on a machine learning framework, which is applied to an electronic device including a machine learning framework. The machine learning framework includes a data structure module, a quantum module, and a classical module. The method includes : call the data structure module to obtain input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model; input the tensor data into the the machine learning model, and call the classic module to create the training layer of the machine learning model; input the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain training After the machine learning model.
在一个实施例中,所述训练层包括损失函数层和优化器层,所述经典模块包括:损失函数单元,被配置为计算所述机器学习模型的损失函数;优化器单元,被配置为在训练所 述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化;所述调用所述经典模块创建所述机器学习模型的训练层,包括:调用所述损失函数单元创建所述损失函数层;调用所述优化器单元创建所述优化器层。In one embodiment, the training layer includes a loss function layer and an optimizer layer, and the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer; calling the optimizer unit to create the optimizer layer.
在一个实施例中,所述将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型,包括:将所述机器学习模型的输出结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值;在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述机器学习模型的参数;确定更新所述参数后的所述机器学习模型的所述损失函数的值;在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。In one embodiment, the inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model includes: The output result of the machine learning model is input into the loss function layer to calculate the value of the loss function of the machine learning model; when it is determined that the value of the loss function does not meet the preset condition, the value of the loss function is input into the The optimizer layer, to update the parameters of the machine learning model based on the value of the loss function; determine the value of the loss function of the machine learning model after updating the parameters; determine that the value of the loss function satisfies In the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
在一个实施例中,所述基于所述损失函数的值更新所述机器学习模型的参数,包括:基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度;基于所述梯度和梯度下降算法更新所述机器学习模型的参数。In one embodiment, the updating the parameters of the machine learning model based on the value of the loss function includes: calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function; The gradient and gradient descent algorithms update the parameters of the machine learning model.
在一个实施例中,若调用所述量子模块创建机器学习模型,所述机器学习模型包括基于所述量子模块创建的量子计算层,所述基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度,包括:基于所述损失函数的值计算所述损失函数相对于所述量子计算层的输出结果的第一梯度,并计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度;将所述第一梯度与所述第二梯度相乘得到所述损失函数相对于所述机器学习模型的参数的梯度。In one embodiment, if the quantum module is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module, and the loss function is calculated based on the value of the loss function relative to the The gradient of the parameters of the machine learning model includes: calculating the first gradient of the loss function relative to the output result of the quantum computing layer based on the value of the loss function, and calculating the output result of the quantum computing layer relative to The second gradient of the parameters of the quantum computing layer; multiplying the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model.
在一个实施例中,所述计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度,包括:计算所述量子计算层的参数加上第一偏置量后所述量子计算层的第一输出结果,并计算所述量子计算层的参数减去第二偏置量后所述量子计算层的第二输出结果;基于所述第一输出结果与所述第二输出结果的差值,计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度。In one embodiment, the calculating the second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer includes: calculating the parameters of the quantum computing layer plus the first offset The first output result of the quantum computing layer, and calculate the second output result of the quantum computing layer after the parameters of the quantum computing layer minus the second offset; based on the first output result and the second The difference of the output results is to calculate the second gradient of the output results of the quantum computing layer relative to the parameters of the quantum computing layer.
在一个实施例中,所述将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层之后,所述方法还包括:以所述损失函数、所述机器学习模型的参数、以及基于该参数计算得到所述损失函数过程中的每一子步骤的输出结果为计算节点,并以该损失函数、该参数、该子步骤的输出结果之间的关系作为边,构建有向的计算图;所述基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度,包括:基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度。In one embodiment, after inputting the tensor data into the machine learning model and calling the classic module to create the training layer of the machine learning model, the method further includes: using the loss function, The parameters of the machine learning model and the output results of each sub-step in the process of calculating the loss function based on the parameters are calculation nodes, and the loss function, the parameters, and the output results of the sub-steps are used as calculation nodes. The relationship is used as an edge to construct a directed calculation graph; the calculation of the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function includes: based on the value of the loss function and the calculation graph Gradients of the loss function are computed with respect to parameters of the machine learning model.
在一个实施例中,所述基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度,包括:确定所述计算图中所述损失函数对应计算节点至所述机器学习模型的参数对应计算节点的路径;基于所述损失函数的值计算该路径上非叶子节点的每一计算节点相对于该计算节点的前驱节点的中间梯度;将计算得到的所有所述中间梯度相乘得到所述损失函数相对于所述参数的梯度。In one embodiment, the calculation of the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function and the calculation graph includes: determining the corresponding calculation of the loss function in the calculation graph The parameters of the node to the machine learning model correspond to the path of the computing node; based on the value of the loss function, calculate the intermediate gradient of each computing node that is not a leaf node on the path relative to the predecessor node of the computing node; the calculated All the intermediate gradients are multiplied together to obtain the gradient of the loss function with respect to the parameters.
在第二方面中,本公开还提供一种基于机器学习框架的模型训练装置,应用于包括机器学习框架的电子设备,所述机器学习框架包括数据结构模块、量子模块和经典模块,所述装置包括:创建模块,用于调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型;输入 训练模块,用于将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层;参数更新模块,用于将所述机器学习模型的输出结果输入所述训练层,并基于所述训练层的输出结果对所述机器学习模型的参数进行更新,以得到训练后的所述机器学习模型。In the second aspect, the present disclosure also provides a model training device based on a machine learning framework, which is applied to an electronic device including a machine learning framework. The machine learning framework includes a data structure module, a quantum module, and a classical module. The device Including: creating a module for calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and/or the classical module to create a machine learning model; input training module, for inputting the tensor data into the machine learning model, and calling the classic module to create the training layer of the machine learning model; a parameter update module, for inputting the output result of the machine learning model into the the training layer, and update the parameters of the machine learning model based on the output result of the training layer, so as to obtain the trained machine learning model.
在一个实施例中,所述训练层包括损失函数层和优化器层,所述经典模块包括:损失函数单元,被配置为计算所述机器学习模型的损失函数;优化器单元,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化;所述输入训练模块还用于:调用所述损失函数单元创建所述损失函数层;调用所述优化器单元创建所述优化器层。In one embodiment, the training layer includes a loss function layer and an optimizer layer, and the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the input training module is also used to: call the loss function unit to create the loss A function layer; calling the optimizer unit to create the optimizer layer.
在一个实施例中,所述参数更新模块还用于:将所述机器学习模型的输出结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值;在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述机器学习模型的参数;确定更新所述参数后的所述机器学习模型的所述损失函数的值;在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。In one embodiment, the parameter updating module is further configured to: input the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model; when determining the loss function When the value of the loss function does not meet the preset condition, the value of the loss function is input to the optimizer layer, so as to update the parameters of the machine learning model based on the value of the loss function; The value of the loss function of the learning model; when it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
在一个实施例中,所述参数更新模块还用于:基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度;基于所述梯度和梯度下降算法更新所述机器学习模型的参数。In one embodiment, the parameter update module is also used to: calculate the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function; update the machine based on the gradient and gradient descent algorithm Learn the parameters of the model.
在一个实施例中,若调用所述量子模块创建机器学习模型,所述机器学习模型包括基于所述量子模块创建的量子计算层,所述参数更新模块还用于:基于所述损失函数的值计算所述损失函数相对于所述量子计算层的输出结果的第一梯度,并计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度;将所述第一梯度与所述第二梯度相乘得到所述损失函数相对于所述机器学习模型的参数的梯度。所述参数更新模块还用于:计算所述量子计算层的参数加上第一偏置量后所述量子计算层的第一输出结果,并计算所述量子计算层的参数减去第二偏置量后所述量子计算层的第二输出结果;基于所述第一输出结果与所述第二输出结果的差值,计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度。In one embodiment, if the quantum module is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module, and the parameter update module is also used to: based on the value of the loss function Calculate the first gradient of the loss function relative to the output of the quantum computing layer, and calculate the second gradient of the output of the quantum computing layer relative to the parameters of the quantum computing layer; Multiplying with the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model. The parameter update module is also used to: calculate the first output result of the quantum computing layer after the parameters of the quantum computing layer plus the first offset, and calculate the parameters of the quantum computing layer minus the second bias After setting the second output result of the quantum computing layer; based on the difference between the first output result and the second output result, calculate the output result of the quantum computing layer relative to the parameters of the quantum computing layer the second gradient.
在一个实施例中,所述装置还包括:计算图构建模块,用于在所述输入训练模块将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层之后,以所述损失函数、所述机器学习模型的参数、以及基于该参数计算得到所述损失函数过程中的每一子步骤的输出结果为计算节点,并以该损失函数、该参数、该子步骤的输出结果之间的关系作为边,构建有向的计算图;所述参数更新模块还用于:基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度。In one embodiment, the device further includes: a calculation graph construction module, configured to input the tensor data into the machine learning model in the input training module, and call the classic module to create the machine learning model After the training layer, the loss function, the parameters of the machine learning model, and the output results of each sub-step in the process of calculating the loss function based on the parameters are used as calculation nodes, and the loss function, the The relationship between the parameters and the output results of this sub-step is used as an edge to construct a directed calculation graph; the parameter update module is also used to: calculate the loss function relative to the calculation graph based on the value of the loss function and the calculation graph The gradient of the parameters of the machine learning model.
在一个实施例中,所述参数更新模块还用于:确定所述计算图中所述损失函数对应计算节点至所述机器学习模型的参数对应计算节点的路径;基于所述损失函数的值计算该路径上非叶子节点的每一计算节点相对于该计算节点的前驱节点的中间梯度;将计算得到的所有所述中间梯度相乘得到所述损失函数相对于所述参数的梯度。In one embodiment, the parameter update module is further configured to: determine the path from the calculation node corresponding to the loss function in the calculation graph to the calculation node corresponding to the parameter of the machine learning model; calculate based on the value of the loss function The intermediate gradient of each calculation node that is not a leaf node on the path relative to the predecessor node of the calculation node; multiplying all the calculated intermediate gradients to obtain the gradient of the loss function relative to the parameter.
在第三方面中,本公开还提供了一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述第一方面中任一项所述方法的步骤。In the third aspect, the present disclosure also provides a storage medium, where a computer program is stored in the storage medium, wherein the computer program is configured to execute the method described in any one of the above first aspects when running step.
在第四方面中,本公开还提供了一种电子装置,包括存储器和处理器,所述存储器中 存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述第一方面中任一项所述方法的步骤。In the fourth aspect, the present disclosure also provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform the above-mentioned first aspect. The steps of any one of the methods.
基于上述技术方案,无论对于不同的机器学习模型,还是机器学习模型应用于不同场景时,均可以通过调用经典模块创建机器学习模型的训练层,进而将张量数据输入机器学习模型,再将机器学习模型的训练结果输入训练层,便可以对创建的机器学习模型进行训练。训练层通过调用经典模块即可实现创建,无需从零开始创建,减少了创建训练层所需的步骤和时间,进而在变更机器学习模型或不同应用场景时提高了训练的开发效率。同时对于不同的机器学习模型或其不同的应用场景,通过数据结构模块创建对应训练数据的张量数据,且机器学习模型基于量子模块和/或经典模块创建,使得张量数据、机器学习模型可以与训练层遵循基于同一机器学习框架的标准,减少其相互之前的适配操作,进而提高训练机器学习模型的开发效率。Based on the above technical solution, no matter for different machine learning models or when the machine learning model is applied to different scenarios, the training layer of the machine learning model can be created by calling the classic module, and then the tensor data is input into the machine learning model, and then the machine The training result of the learning model is input into the training layer, and the created machine learning model can be trained. The training layer can be created by calling classic modules without creating from scratch, which reduces the steps and time required to create the training layer, thereby improving the development efficiency of training when changing machine learning models or different application scenarios. At the same time, for different machine learning models or their different application scenarios, tensor data corresponding to training data is created through the data structure module, and the machine learning model is created based on quantum modules and/or classical modules, so that tensor data and machine learning models can be It follows the standard based on the same machine learning framework as the training layer, reducing their mutual adaptation operations, thereby improving the development efficiency of training machine learning models.
20211168061112021116806111
2022100834667为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种数据处理方法、机器学习框架及相关设备,旨在针对包含量子机器学习程序和经典机器学习程序的机器学习模型,提高该机器学习模型的训练开发效率。鉴于此,本公开在如下的多个方面提供解决方案。2022100834667 In order to at least solve the technical problems described in the background technology section above, this disclosure proposes a data processing method, a machine learning framework and related equipment, aiming at improving the machine learning model including quantum machine learning programs and classical machine learning programs. The training development efficiency of the machine learning model. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供了一种数据处理方法,应用于包括机器学习框架的电子设备,所述机器学习框架包括量子模块和经典模块,所述方法包括:调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。In a first aspect, the present disclosure provides a data processing method applied to an electronic device including a machine learning framework, the machine learning framework includes a quantum module and a classical module, and the method includes: calling the quantum module to create a A quantum machine learning program, a first preset training program, and a quantum computing layer of a first training interface, the first training interface is used to call the first preset training program to train the quantum machine learning program; call the classical The module creates a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the classic machine learning program; call The classical module creates a machine learning model including the quantum computing layer and the classical computing layer.
在一个实施例中,所述方法还包括:调用所述经典模块创建所述机器学习模型的训练层,所述训练层用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。In one embodiment, the method further includes: calling the classic module to create a training layer of the machine learning model, the training layer is used to call the first preset training program through the first training interface, And call the second preset training program through the second training interface.
在一个实施例中,所述机器学习框架还包括数据结构模块,所述方法还包括:调用所述数据结构模块获取输入数据并创建包括所述输入数据的张量数据;将所述张量数据输入所述机器学习模型,得到输出结果;将所述输出结果输入所述训练层,以通过所述第一训练接口调用所述第一预设训练程序,根据所述第一预设训练程序训练所述量子机器学习程序,并通过所述第二训练接口调用所述第二预设训练程序,根据所述第二预设训练程序训练所述经典机器学习程序,得到训练后的机器学习模型。In one embodiment, the machine learning framework further includes a data structure module, and the method further includes: calling the data structure module to obtain input data and creating tensor data including the input data; converting the tensor data Input the machine learning model to obtain an output result; input the output result into the training layer to call the first preset training program through the first training interface, and train according to the first preset training program The quantum machine learning program calls the second preset training program through the second training interface, trains the classical machine learning program according to the second preset training program, and obtains a trained machine learning model.
在一个实施例中,所述第一预设训练程序包括用于计算所述量子机器学习程序的输出相对于所述量子机器学习程序的第一因变量的梯度的第一梯度计算程序,所述第一因变量为所述量子机器学习程序的输入或参数。In one embodiment, the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program with respect to the first dependent variable of the quantum machine learning program, the The first dependent variable is an input or parameter of the quantum machine learning program.
在一个实施例中,所述第一梯度计算程序用于根据从所述第一训练接口传入的梯度计算方法参数,选择有限差分法或参数位移法计算所述梯度。In one embodiment, the first gradient calculation program is used to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
在一个实施例中,在所述根据所述第一训练接口传入的梯度计算方法参数,选择参数位移法计算所述梯度时,所述第一梯度计算程序包括:参数减子程序,用于将所述量子机器学习程序的所述第一因变量减去第一偏置量,并将目标数据输入所述量子机器学习程序, 得到第一结果;参数加子程序,用于将所述量子机器学习程序的所述第一因变量加上第二偏置量,并将目标数据输入所述量子机器学习程序,得到第二结果;计算子程序,用于根据所述第一结果和所述第二结果计算所述梯度;其中,所述目标数据为输入所述量子机器学习程序的数据。In one embodiment, when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for Subtract the first offset from the first dependent variable of the quantum machine learning program, and input the target data into the quantum machine learning program to obtain the first result; a parameter adding subroutine for adding the quantum The first dependent variable of the machine learning program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; a calculation subroutine is used for The second result calculates the gradient; wherein the target data is data input to the quantum machine learning program.
在一个实施例中,所述第二预设训练程序包括用于计算所述经典机器学习程序的输出相对于所述经典机器学习程序的第二因变量的梯度的第二梯度计算程序,所述第二因变量为所述经典机器学习程序的输入或参数。In one embodiment, the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the The second dependent variable is an input or parameter of the classical machine learning procedure.
在第二方面中,本公开还提供一种数据处理装置,应用于包括机器学习框架的电子设备,所述机器学习框架包括量子模块和经典模块,所述装置包括:量子计算层创建模块,用于调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;经典计算层创建模块,用于调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;机器学习模型创建模块,用于调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。In the second aspect, the present disclosure also provides a data processing device, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a quantum module and a classical module, and the device includes: a quantum computing layer creation module, used Invoking the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, the first training interface is used to call the first preset training program to train the quantum Machine learning program; a classic computing layer creation module, used to call the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the The second preset training program trains the classical machine learning program; the machine learning model creation module is used to call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
在一个实施例中,所述装置还包括:训练层创建模块,用于调用所述经典模块创建所述机器学习模型的训练层,所述训练层用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。In one embodiment, the device further includes: a training layer creation module, configured to call the classic module to create a training layer of the machine learning model, and the training layer is used to call the A first preset training program, and call the second preset training program through the second training interface.
在一个实施例中,所述机器学习框架还包括数据结构模块,所述装置还包括:张量数据创建模块,用于调用所述数据结构模块获取输入数据并创建包括所述输入数据的张量数据;输入模块,用于将所述张量数据输入所述机器学习模型,得到输出结果;训练模块,用于将所述输出结果输入所述训练层,以通过所述第一训练接口调用所述第一预设训练程序,根据所述第一预设训练程序训练所述量子机器学习程序,并通过所述第二训练接口调用所述第二预设训练程序,根据所述第二预设训练程序训练所述经典机器学习程序,得到训练后的机器学习模型。In one embodiment, the machine learning framework further includes a data structure module, and the device further includes: a tensor data creation module, configured to call the data structure module to obtain input data and create a tensor including the input data data; an input module, configured to input the tensor data into the machine learning model to obtain an output result; a training module, configured to input the output result into the training layer, so as to call the first training interface through the first training interface The first preset training program, train the quantum machine learning program according to the first preset training program, and call the second preset training program through the second training interface, according to the second preset The training program trains the classic machine learning program to obtain a trained machine learning model.
在一个实施例中,所述第一预设训练程序包括用于计算所述量子机器学习程序的输出相对于所述量子机器学习程序的第一因变量的梯度的第一梯度计算程序,所述第一因变量为所述量子机器学习程序的输入或参数。In one embodiment, the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program with respect to the first dependent variable of the quantum machine learning program, the The first dependent variable is an input or parameter of the quantum machine learning program.
在一个实施例中,所述第一梯度计算程序用于根据从所述第一训练接口传入的梯度计算方法参数,选择有限差分法或参数位移法计算所述梯度。In one embodiment, the first gradient calculation program is used to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
在一个实施例中,在所述根据所述第一训练接口传入的梯度计算方法参数,选择参数位移法计算所述梯度时,所述第一梯度计算程序包括:参数减子程序,用于将所述量子机器学习程序的所述第一因变量减去第一偏置量,并将目标数据输入所述量子机器学习程序,得到第一结果;参数加子程序,用于将所述量子机器学习程序的所述第一因变量加上第二偏置量,并将目标数据输入所述量子机器学习程序,得到第二结果;计算子程序,用于根据所述第一结果和所述第二结果计算所述梯度;其中,所述目标数据为输入所述量子机器学习程序的数据。In one embodiment, when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for Subtracting the first offset from the first dependent variable of the quantum machine learning program, and inputting target data into the quantum machine learning program to obtain a first result; a parameter adding subroutine for adding the quantum The first dependent variable of the machine learning program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; a calculation subroutine is used for The second result calculates the gradient; wherein the target data is data input to the quantum machine learning program.
在一个实施例中,所述第二预设训练程序包括用于计算所述经典机器学习程序的输出相对于所述经典机器学习程序的第二因变量的梯度的第二梯度计算程序,所述第二因变量为所述经典机器学习程序的输入或参数。In one embodiment, the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the The second dependent variable is an input or parameter of the classical machine learning procedure.
在第三方面中,本公开还提供一种机器学习框架,包括:量子模块,被配置为创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;经典模块,被配置为创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;所述经典模块还被配置为创建包括所述量子计算层和所述经典计算层的机器学习模型。In a third aspect, the present disclosure also provides a machine learning framework, including: a quantum module configured to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, the first A training interface is used to call the first preset training program to train the quantum machine learning program; a classical module configured to create a classical computing layer including a classical machine learning program, a second preset training program and a second training interface , the second training interface is used to call the second preset training program to train the classical machine learning program; the classical module is also configured to create a machine learning program including the quantum computing layer and the classical computing layer Model.
在第四方面中,本公开还提供一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述第一方面中任一项所述方法的步骤。In the fourth aspect, the present disclosure also provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any one of the methods described in the above first aspect when running .
在第五方面中,本公开还提供一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述第一方面中任一项所述方法的步骤。In the fifth aspect, the present disclosure also provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to perform any of the above-mentioned first aspects. A step of said method.
基于上述技术方案,对于量子机器学习程序,可以创建包括该量子机器学习程序的量子计算层,并且创建好的该量子计算层可以直接包含有用于训练该量子机器学习程序的第一预设训练程序,以及用于调用该第一预设训练程序的第一训练接口,并同理对于经典机器学习程序,可以创建包括该经典机器学习程序、用于训练该经典机器学习程序的第二预设训练程序、用于调用该第二预设训练程序的第二训练接口,进而对于创建好的包括该量子计算层和经典计算层的机器学习模型,在训练时,可以直接通过第一训练接口和第二训练接口分别调用第一预设训练程序和第二预设训练程序,以实现对于机器学习模型的训练,可以减少针对量子机器学习程序和经典机器学习程序的训练程序的创建工作,更节省了为训练量子机器学习程序和训练经典机器学习程序所需的配合工作,提高了对包含量子计算层和经典计算层的机器学习模型的训练的开发效率。Based on the above technical solution, for a quantum machine learning program, a quantum computing layer including the quantum machine learning program can be created, and the created quantum computing layer can directly contain a first preset training program for training the quantum machine learning program , and the first training interface for calling the first preset training program, and similarly for the classic machine learning program, you can create a second preset training interface that includes the classic machine learning program and is used to train the classic machine learning program Program, the second training interface for calling the second preset training program, and then for the created machine learning model including the quantum computing layer and the classical computing layer, during training, you can directly pass the first training interface and the second training interface The two training interfaces respectively call the first preset training program and the second preset training program to realize the training of the machine learning model, which can reduce the creation of training programs for quantum machine learning programs and classical machine learning programs, and save more time. The coordinated work required for training quantum machine learning programs and training classical machine learning programs improves the development efficiency of training machine learning models that include both quantum and classical computing layers.
20221008346672022100834667
202111680566X为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种含噪声机器学习模型创建方法、机器学习模型框架及相关设备,旨在创建一个含噪声的机器学习模型,以使得该机器学习模型在量子虚拟机上模拟出的结果更加贴近真实量子计算机上计算出的结果。鉴于此,本公开在如下的多个方面提供解决方案。202111680566X In order to at least solve the technical problems described in the background technology section above, this disclosure proposes a noise-containing machine learning model creation method, machine learning model framework and related equipment, aiming to create a noise-containing machine learning model, so that the The results simulated by the machine learning model on the quantum virtual machine are closer to the results calculated on the real quantum computer. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供一种含噪声机器学习模型创建方法,应用于包括量子模块和经典模块的机器学习框架的电子装置,所述量子模块包括含噪量子程序封装单元,所述方法包括:基于所述机器学习框架内含的量子计算编程库创建考虑噪声影响的量子程序;将所述量子程序作为已封装的含噪量子计算层接口的参数,以及传入所述含噪量子计算层接口;调用所述含噪量子程序封装单元通过所述含噪量子计算层接口创建含噪量子计算层;以及调用所述经典模块创建包括所述含噪量子计算层的机器学习模型。In a first aspect, the present disclosure provides a noise-containing machine learning model creation method, which is applied to an electronic device of a machine learning framework including a quantum module and a classical module, the quantum module includes a noise-containing quantum program encapsulation unit, and the method Including: creating a quantum program considering the influence of noise based on the quantum computing programming library contained in the machine learning framework; using the quantum program as a parameter of the encapsulated noise-containing quantum computing layer interface, and passing the noise-containing quantum computing layer interface; calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the classical module to create a machine learning model including the noise-containing quantum computing layer.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库创建考虑噪声影响的量子程序,包括:基于所述机器学习框架内含的量子计算编程库申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序。In one embodiment, the creation of a quantum program considering the impact of noise based on the quantum computing programming library included in the machine learning framework includes: applying for a noise-containing quantum virtual machine based on the quantum computing programming library included in the machine learning framework , and setting the noise of the quantum circuit running on the noise-containing virtual machine; applying for a qubit and creating a quantum logic gate acting on the qubit to obtain the quantum circuit running on the noise-containing virtual machine; The noise-containing quantum virtual machine, the noise model and the quantum circuit are packaged to obtain a quantum program considering the influence of noise.
在一个实施例中,所述噪声包括以下至少一种:逻辑门噪声、量子比特的量子态重置噪声、量子比特的测量噪声、量子比特的读取噪声。In one embodiment, the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
在一个实施例中,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量 子线路的噪声,包括:将指定的量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的所有量子比特生效。In one embodiment, the noise is logic gate noise, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying the type of quantum logic gate, the type of noise model, and the noise model The parameters required by the type are used as the parameters of the logic gate noise interface, and are passed into the logic gate noise interface; the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the Logic gate noise acts on all qubits in the quantum circuit.
在一个实施例中,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:将指定的量子比特、量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的指定的量子比特生效。In one embodiment, the noise is logic gate noise, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying qubits, quantum logic gate types, noise model types, and all The parameters required by the noise model type are used as the parameters of the logic gate noise interface, and passed into the logic gate noise interface; the logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface , the logic gate noise takes effect on specified qubits in the quantum circuit.
在一个实施例中,所述噪声为量子比特的量子态重置噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:将量子线路中的量子比特的量子态重置到|0>的概率和重置到|1>的概率作为重置噪声接口的参数,以及传入所述重置噪声接口;通过所述重置噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声。In one embodiment, the noise is the quantum state reset noise of the qubit, and the setting the noise of the quantum circuit running on the noise-containing virtual machine includes: resetting the quantum state of the qubit in the quantum circuit The probability of setting to |0> and the probability of resetting to |1> are used as the parameters of the reset noise interface, and passed into the reset noise interface; through the reset noise interface, the virtual machine running on the noise is set Quantum state reset noise of qubits on quantum circuits.
在一个实施例中,所述噪声为量子比特的测量噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:将指定的噪声模型类型以及所述噪声模型类型所需的参数作为测量噪声接口的参数,以及传入所述测量噪声接口;通过所述测量噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的测量噪声。In one embodiment, the noise is the measurement noise of qubits, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: specifying the specified noise model type and the noise model type required The parameter is used as the parameter of the measurement noise interface, and is passed into the measurement noise interface; through the measurement noise interface, the measurement noise of the qubit of the quantum circuit running on the noise-containing virtual machine is set.
在一个实施例中,所述噪声为量子比特的读取噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:将|0>被读取为|0>的概率以及被读取为|1>的概率、|1>被读取为|0>的概率以及被读取为|1>的概率作为读取噪声接口的参数,以及传入所述读取噪声接口;通过所述读取噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的读取噪声。In one embodiment, the noise is the read noise of the qubit, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes: the probability that |0> is read as |0> and the probability of being read as |1>, the probability of |1> being read as |0>, and the probability of being read as |1> as the parameters of the read noise interface, and passed to the read noise interface ; Set the read noise of the qubits of the quantum circuit running on the noise-containing virtual machine through the read noise interface.
在第二方面中,本公开提供了一种含噪声机器学习模型创建装置,应用于包括量子模块和经典模块的机器学习框架的电子装置,所述量子模块包括含噪量子程序封装单元,所述装置包括:程序创建单元,用于基于所述机器学习框架内含的量子计算编程库创建考虑噪声影响的量子程序;接口确定单元,用于将所述量子程序作为已封装的含噪量子计算层接口的参数,以及传入所述含噪量子计算层接口;创建单元,用于调用所述含噪量子程序封装单元通过所述含噪量子计算层接口创建含噪量子计算层;以及调用所述经典模块创建包括所述含噪量子计算层的机器学习模型。In a second aspect, the present disclosure provides a device for creating a noise-containing machine learning model, which is applied to an electronic device of a machine learning framework including a quantum module and a classical module, the quantum module includes a noise-containing quantum program encapsulation unit, the The device includes: a program creation unit, used to create a quantum program considering the influence of noise based on the quantum computing programming library contained in the machine learning framework; an interface determination unit, used to use the quantum program as a packaged noise-containing quantum computing layer The parameters of the interface, and passing in the noise-containing quantum computing layer interface; creating a unit for calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the noise-containing quantum computing layer interface The classical module creates a machine learning model that includes the noisy quantum computing layer.
在一个实施例中,在所述基于所述机器学习框架内含的量子计算编程库创建考虑噪声影响的量子程序方面,所述程序创建单元,具体用于:基于所述机器学习框架内含的量子计算编程库申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序。In one embodiment, in terms of creating a quantum program that considers the influence of noise based on the quantum computing programming library included in the machine learning framework, the program creation unit is specifically configured to: based on the included in the machine learning framework The quantum computing programming library applies for a quantum virtual machine with noise, and sets the noise of the quantum circuit running on the virtual machine with noise; applies for qubits and creates quantum logic gates that act on the qubits, and obtains A quantum circuit on a noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model, and the quantum circuit to obtain a quantum program considering the influence of noise.
在一个实施例中,所述噪声包括以下至少一种:逻辑门噪声、量子比特的量子态重置噪声、量子比特的测量噪声、量子比特的读取噪声。In one embodiment, the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
在一个实施例中,所述噪声为逻辑门噪声,在所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑 门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的所有量子比特生效。In one embodiment, the noise is logic gate noise, and in terms of setting the noise of the quantum circuit running on the noise-containing virtual machine, the program creation unit is specifically configured to: use the specified quantum logic gate Type, noise model type, and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, it is set to run on the noise-containing virtual machine The logic gate noise of the quantum circuit, the logic gate noise takes effect on all qubits in the quantum circuit.
在一个实施例中,所述噪声为逻辑门噪声,在所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的量子比特、量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的指定的量子比特生效。In one embodiment, the noise is logic gate noise, and in terms of setting the noise of the quantum circuit running on the noise-containing virtual machine, the program creation unit is specifically configured to: assign the specified qubit, The quantum logic gate type, the noise model type and the parameters required by the noise model type are used as the parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, the settings run on the noise-containing Logic gate noise of the quantum circuit on the virtual machine, where the logic gate noise takes effect on specified qubits in the quantum circuit.
在一个实施例中,在所述噪声为量子比特的量子态重置噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将量子线路中的量子比特的量子态重置到|0>的概率和重置到|1>的概率作为重置噪声接口的参数,以及传入所述重置噪声接口;通过所述重置噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声。In one embodiment, when the noise is the quantum state reset noise of the qubit, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: The quantum state of the qubit in the quantum circuit is reset to the probability of |0> and the probability of being reset to |1> as the parameters of the reset noise interface, and the reset noise interface is passed in; through the reset noise The interface sets the quantum state reset noise of the qubit of the quantum circuit running on the noise-containing virtual machine.
在一个实施例中,在所述噪声为量子比特的测量噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的噪声模型类型以及所述噪声模型类型所需的参数作为测量噪声接口的参数,以及传入所述测量噪声接口;通过所述测量噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的测量噪声。In one embodiment, when the noise is the measurement noise of the qubit, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: use the specified noise The model type and the parameters required by the noise model type are used as the parameters of the measurement noise interface, and passed into the measurement noise interface; through the measurement noise interface, the qubits of the quantum circuit running on the noise-containing virtual machine are set measurement noise.
在一个实施例中,在所述噪声为量子比特的读取噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将|0>被读取为|0>的概率以及被读取为|1>的概率、|1>被读取为|0>的概率以及被读取为|1>的概率作为读取噪声接口的参数,以及传入所述读取噪声接口;通过所述读取噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的读取噪声。In one embodiment, when the noise is the read noise of qubits, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: use |0 > probability of being read as |0> and probability of being read as |1>, probability of |1> being read as |0> and probability of being read as |1> as parameters of the read noise interface , and passing in the read noise interface; setting the read noise of the qubits of the quantum circuit running on the noise-containing virtual machine through the read noise interface.
在第三方面中,本公开提供了一种机器学习框架,所述机器学习框架包括量子模块和经典模块,所述量子模块包括含噪量子程序封装单元,被配置为通过已封装的含噪量子计算层接口创建含噪量子计算层,所述含噪量子计算层接口用于提供基于所述机器学习框架内含的量子计算编程库创建的考虑噪声影响的量子程序;所述经典模块,被配置为创建包括所述含噪量子计算层的机器学习模型。In a third aspect, the present disclosure provides a machine learning framework, the machine learning framework includes a quantum module and a classical module, the quantum module includes a noisy quantum program encapsulation unit configured to pass the packaged noisy quantum program The calculation layer interface creates a noise-containing quantum computing layer, and the noise-containing quantum computing layer interface is used to provide a quantum program that considers the influence of noise created based on the quantum computing programming library contained in the machine learning framework; the classical module is configured To create a machine learning model comprising said noisy quantum computing layer.
在第四方面中,本公开提供了一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述任一项中所述的方法。In a fourth aspect, the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
在第五方面中,本公开提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述任一项中所述的方法。In a fifth aspect, the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
与现有技术相比,本公开通过基于机器学习框架内含的量子计算编程库创建考虑噪声影响的量子程序;然后将量子程序作为已封装的含噪量子计算层接口的参数,以及传入含噪量子计算层接口;最后调用含噪量子程序封装单元通过含噪量子计算层接口创建含噪量子计算层;以及调用经典模块创建包括含噪量子计算层的机器学习模型。本公开通过调用含噪量子程序封装单元即实现了含噪声机器学习模型的创建,该机器学习模型含有噪声,因此在量子虚拟机上模拟出的结果更加贴近真实量子计算机上计算出的结果;除此之外,通过该含噪量子计算层接口创建含噪量子计算层,当模拟的真实量子计算机不同时,可以通过改变该含噪量子计算层接口的参数——考虑噪声影响的量子程序即可,不需要改变机 器学习模型的其他部分,使得该含噪声的机器学习模型易于移植和复制,也进一步提高了该含噪声机器学习模型的易用性。Compared with the prior art, this disclosure creates a quantum program that considers the influence of noise based on the quantum computing programming library included in the machine learning framework; then uses the quantum program as a parameter of the encapsulated noise-containing quantum computing layer Noisy quantum computing layer interface; finally calling the noisy quantum computing layer encapsulation unit to create a noisy quantum computing layer through the noisy quantum computing layer interface; and calling the classical module to create a machine learning model including the noisy quantum computing layer. This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit. The machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except In addition, the noisy quantum computing layer is created through the noisy quantum computing layer interface. When the simulated real quantum computer is different, the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
202111680566X202111680566X
202111680573X为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种机器学习模型构建方法、机器学习框架及相关设备,旨在实现跨量子机器学习框架构建机器学习模型。鉴于此,本公开在如下的多个方面提供解决方案。202111680573X In order to at least solve the technical problems described in the background technology section above, this disclosure proposes a machine learning model construction method, a machine learning framework and related equipment, aiming to realize the construction of machine learning models across quantum machine learning frameworks. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供了一种机器学习模型构建方法,应用于包括第一机器学习框架且不包括第二机器学习框架的电子装置,所述第一机器学习框架包括兼容量子计算程序封装单元,所述方法包括:确定对应的所述第二机器学习框架的兼容量子计算层接口,所述兼容量子计算层接口用于提供基于所述第二机器学习框架内含的量子计算编程库创建的量子程序;通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层;构建包括所述兼容量子计算层的机器学习模型。In a first aspect, the present disclosure provides a machine learning model construction method applied to an electronic device including a first machine learning framework and not including a second machine learning framework, the first machine learning framework including a compatible quantum computing program An encapsulation unit, the method includes: determining a compatible quantum computing layer interface of the corresponding second machine learning framework, and the compatible quantum computing layer interface is used to provide a quantum computing programming library based on the second machine learning framework The created quantum program; calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit to construct a compatible quantum computing layer; constructing a machine learning model including the compatible quantum computing layer.
在一个实施例中,所述确定对应的所述第二机器学习框架的兼容量子计算层接口之前,所述方法还包括:通过所述第二机器学习框架内含的量子计算编程库构建量子线路,以及调用初始化函数对所述量子线路进行初始化,通过运行函数定义所述量子线路的运行和测量方式,得到量子程序。In one embodiment, before determining the corresponding compatible quantum computing layer interface of the second machine learning framework, the method further includes: constructing a quantum circuit through the quantum computing programming library included in the second machine learning framework , and calling the initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining the quantum program.
在一个实施例中,所述第一机器学习框架还包括经典模块和数据结构模块,所述构建包括所述兼容量子计算层的机器学习模型,包括:调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述兼容量子计算层之间的前向传播关系;调用所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型。In one embodiment, the first machine learning framework further includes a classical module and a data structure module, and the construction of a machine learning model including the compatible quantum computing layer includes: calling the classical module to construct a classical computing layer and calling The data structure module constructs the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calls the classical module to combine the classical computing layer, the compatible quantum computing layer and the classical computing layer Encapsulate the forward propagation relationship with the compatible quantum computing layer to obtain a machine learning model.
在一个实施例中,所述经典模块包括经典神经网络层单元,所述经典神经网络层单元包括以下至少一者:指定模型经典神经网络层子单元,被配置为通过已封装的经典神经网络层接口构建指定模型的所述经典神经网络层;激活层子单元,被配置为构建用于对所述经典神经网络层的输出进行非线性变换的激活层;所述调用所述经典模块构建经典计算层,包括:调用所述指定模型经典神经网络层子单元构建经典神经网络层,以及将所述经典神经网络层作为经典计算层;或者,调用所述指定模型经典神经网络层子单元和所述激活层子单元构建经典神经网络层和激活层,以及将所述经典神经网络层和所述激活层作为经典计算层。In one embodiment, the classical module includes a classical neural network layer unit, and the classical neural network layer unit includes at least one of the following: a specified model classical neural network layer subunit configured to pass through the encapsulated classical neural network layer The interface constructs the classical neural network layer of the specified model; the activation layer subunit is configured to construct an activation layer for performing nonlinear transformation on the output of the classical neural network layer; layer, including: calling the specified model classic neural network layer subunit to construct a classic neural network layer, and using the classic neural network layer as a classic calculation layer; or calling the specified model classic neural network layer subunit and the The activation layer subunit constructs a classical neural network layer and an activation layer, and uses the classical neural network layer and the activation layer as a classical calculation layer.
在一个实施例中,所述经典模块还包括抽象类子模块,所述通过所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型,包括:调用所述抽象类子模块基于初始化函数对所述兼容量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述兼容量子计算层和所述经典计算层;调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;调用所述抽象类子模块基于模块类对所述初始化和封装后的所述兼容量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。In one embodiment, the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the classical computing layer and the compatible quantum computing layer are combined by the classical module Encapsulate the forward propagation relationship between them to obtain a machine learning model, including: calling the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialized and encapsulated The compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship; calling the abstract class The sub-module encapsulates the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class to obtain a machine learning model.
在第二方面中,本公开提供了一种机器学习模型构建装置,应用于包括第一机器学习框架且不包括第二机器学习框架的电子装置,所述第一机器学习框架包括兼容量子计算程 序封装单元,所述装置包括:确定单元,用于确定对应的所述第二机器学习框架的兼容量子计算层接口,所述兼容量子计算层接口用于提供基于所述第二机器学习框架内含的量子计算编程库创建的量子程序;构建单元,用于通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层;构建包括所述兼容量子计算层的机器学习模型。In a second aspect, the present disclosure provides a machine learning model construction device applied to an electronic device including a first machine learning framework and not including a second machine learning framework, the first machine learning framework including a compatible quantum computing program An encapsulation unit, the device includes: a determination unit, configured to determine a corresponding compatible quantum computing layer interface of the second machine learning framework, and the compatible quantum computing layer interface is used to provide information based on the second machine learning framework. A quantum program created by the quantum computing programming library; a construction unit for calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit to construct a compatible quantum computing layer; constructing a machine learning model including the compatible quantum computing layer .
在一个实施例中,所述确定对应的所述第二机器学习框架对应的兼容量子计算层接口之前,所述构建单元还用于:通过所述第二机器学习框架内含的量子计算编程库构建量子线路,以及调用初始化函数对所述量子线路进行初始化,通过运行函数定义所述量子线路的运行和测量方式,得到量子程序。In one embodiment, before determining the compatible quantum computing layer interface corresponding to the second machine learning framework, the construction unit is further configured to: use the quantum computing programming library included in the second machine learning framework Constructing a quantum circuit, calling an initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining a quantum program.
在一个实施例中,所述第一机器学习框架还包括经典模块和数据结构模块,所述构建包括所述兼容量子计算层的机器学习模型,所述构建单元,具体用于:调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述兼容量子计算层之间的前向传播关系;调用所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型。In one embodiment, the first machine learning framework further includes a classical module and a data structure module, the construction includes the machine learning model compatible with the quantum computing layer, and the construction unit is specifically used to: call the classical The module constructs the classical computing layer and calls the data structure module to construct the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calls the classical module to combine the classical computing layer, the compatible quantum computing layer and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer are encapsulated to obtain a machine learning model.
在一个实施例中,所述经典模块包括经典神经网络层单元,所述经典神经网络层单元包括以下至少一者:指定模型经典神经网络层子单元,被配置为通过已封装的经典神经网络层接口构建指定模型的所述经典神经网络层;激活层子单元,被配置为构建用于对所述经典神经网络层的输出进行非线性变换的激活层;所述调用所述经典模块构建经典计算层,所述构建单元具体用于:调用所述指定模型经典神经网络层子单元构建经典神经网络层,以及将所述经典神经网络层作为经典计算层;或者,调用所述指定模型经典神经网络层子单元和所述激活层子单元构建经典神经网络层和激活层,以及将所述经典神经网络层和所述激活层作为经典计算层。In one embodiment, the classical module includes a classical neural network layer unit, and the classical neural network layer unit includes at least one of the following: a specified model classical neural network layer subunit configured to pass through the encapsulated classical neural network layer The interface constructs the classical neural network layer of the specified model; the activation layer subunit is configured to construct an activation layer for performing nonlinear transformation on the output of the classical neural network layer; Layer, the construction unit is specifically used to: call the specified model classic neural network layer subunit to construct a classic neural network layer, and use the classic neural network layer as a classic calculation layer; or call the specified model classic neural network The layer subunit and the activation layer subunit construct a classical neural network layer and an activation layer, and use the classical neural network layer and the activation layer as a classical computing layer.
在一个实施例中,所述经典模块还包括抽象类子模块,所述通过所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型,所述构建单元具体用于:调用所述抽象类子模块基于初始化函数对所述兼容量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述兼容量子计算层和所述经典计算层;调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;调用所述抽象类子模块基于模块类对所述初始化和封装后的所述兼容量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。In one embodiment, the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the classical computing layer and the compatible quantum computing layer are combined by the classical module Encapsulate the forward propagation relationship between them to obtain a machine learning model, and the construction unit is specifically used to: call the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, Obtaining the initialized and encapsulated compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship ; calling the abstract class submodule to encapsulate the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
在第三方面中,本公开提供了一种机器学习框架,所述机器学习框架设置在电子装置内,所述机器学习框架包括:兼容量子计算程序封装单元,被配置为调用兼容量子计算层接口构建兼容量子计算层,所述兼容量子计算层接口用于接收基于设置在所述电子装置外的第二机器学习框架内含的量子计算编程库创建的量子程序。In a third aspect, the present disclosure provides a machine learning framework, the machine learning framework is set in an electronic device, and the machine learning framework includes: a compatible quantum computing program encapsulation unit configured to call a compatible quantum computing layer interface A compatible quantum computing layer is constructed, and the compatible quantum computing layer interface is used to receive the quantum program created based on the quantum computing programming library included in the second machine learning framework set outside the electronic device.
在第四方面中,本公开提供了一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述任一项中所述的方法。In a fourth aspect, the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
在第五方面中,本公开提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述任一项中所述的方法。In a fifth aspect, the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
与现有技术相比,本公开提供的机器学习模型构建方法,开发者可以基于自己习惯的 第二机器学习框架内含的量子计算编程库创建的量子程序,然后在第一机器学习框架中确定对应的该第二机器学习框架对应的兼容量子计算层接口,通过第一机器学习框架包括的兼容量子计算程序封装单元调用该兼容量子计算层接口即可构建兼容量子计算层,从而实现了通过包括第一机器学习框架但不包括第二机器学习框架的电子设备进行机器学习模型的构建,进而实现跨量子机器学习框架构建机器学习模型。Compared with the prior art, in the machine learning model construction method provided by this disclosure, developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework that they are used to, and then determine in the first machine learning framework Corresponding to the compatible quantum computing layer interface corresponding to the second machine learning framework, the compatible quantum computing layer interface can be constructed by calling the compatible quantum computing layer interface included in the first machine learning framework. The electronic device of the first machine learning framework but not including the second machine learning framework constructs the machine learning model, thereby realizing the construction of the machine learning model across quantum machine learning frameworks.
202111680573X202111680573X
2021116806130为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种数据处理方法、机器学习框架及相关设备,旨在机器学习模型运行时,根据有限的量子比特管理机器学习模型中的量子程序的运行方式以提高对数据的处理效率。鉴于此,本公开在如下的多个方面提供解决方案。2021116806130 In order to at least solve the technical problems described in the above background technology section, this disclosure proposes a data processing method, machine learning framework and related equipment, aiming at managing the data in the machine learning model according to the limited qubits when the machine learning model is running The way quantum programs operate to improve the efficiency of data processing. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供一种数据处理方法,应用于包括机器学习框架的电子设备,所述机器学习框架包括数据结构模块、量子模块和经典模块,所述方法包括:调用所述量子模块和所述经典模块创建包括量子程序的机器学习模型,调用所述数据结构模块获取目标数据并创建包括所述目标数据的张量数据;调用所述量子模块基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组;调用所述量子模块针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路;基于所述张量数据得到每组所述量子线路的输入数据,并将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果。In a first aspect, the present disclosure provides a data processing method applied to an electronic device including a machine learning framework, where the machine learning framework includes a data structure module, a quantum module, and a classical module, and the method includes: calling the quantum module and the classic module create a machine learning model including a quantum program, call the data structure module to obtain target data and create tensor data including the target data; call the quantum module based on the first required by the quantum program The quantity of a qubit and the quantity of the second qubit allowed to be used in the quantum device divide the second qubit into multiple groups; call the quantum module for each group of the second qubit, based on the quantum The program generates quantum circuits including the group of second qubits; based on the tensor data, the input data of each group of quantum circuits is obtained, and the input data are respectively input into each of the quantum circuits to obtain each The output result of the quantum circuit.
在一个实施例中,所述基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组,包括:将所述量子设备中允许使用的第二量子比特的数量除以所述量子程序所需的第一量子比特的数量得到中间数;将小于等于所述中间数的最大整数作为目标数;将所述第二量子比特划分为数量为所述目标数的多组。In one embodiment, the second qubits are divided into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, including: The number of second qubits allowed to be used in the quantum device is divided by the number of first qubits required by the quantum program to obtain an intermediate number; the largest integer less than or equal to the intermediate number is used as the target number; the The second qubits are divided into groups whose number is the target number.
在一个实施例中,所述针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路,包括:针对每一组所述第二量子比特,基于所述量子程序生成用于作用于该组所述第二量子比特的量子逻辑门;生成包括该组所述第二量子比特和所述量子逻辑门的量子线路。In one embodiment, for each group of the second qubits, generating a quantum circuit including the group of the second qubits based on the quantum program includes: for each group of the second qubits , generating a quantum logic gate for acting on the group of the second qubits based on the quantum program; generating a quantum circuit including the group of the second qubits and the quantum logic gate.
在一个实施例中,所述方法还包括:基于所述输出结果确定所述机器学习模型对所述张量数据的预测结果。In one embodiment, the method further includes: determining a prediction result of the machine learning model for the tensor data based on the output result.
在一个实施例中,所述经典模块还用于创建所述机器学习模型的训练层,所述方法还包括:调用所述经典模块创建所述机器学习模型的训练层;将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型。In one embodiment, the classic module is also used to create a training layer of the machine learning model, and the method further includes: calling the classic module to create a training layer of the machine learning model; inputting the prediction result The training layer is used to update the parameters of the quantum program to obtain the trained machine learning model.
在一个实施例中,所述训练层包括损失函数层和优化器层,所述经典模块包括:损失函数单元,被配置为计算所述机器学习模型的损失函数;优化器单元,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化;所述调用所述经典模块创建所述机器学习模型的训练层,包括:调用所述损失函数单元创建所述损失函数层;调用所述优化器单元创建所述优化器层。In one embodiment, the training layer includes a loss function layer and an optimizer layer, and the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer; calling the optimizer unit to create the optimizer layer.
在一个实施例中,所述将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型,包括:将所述预测结果输入所述损失函数层, 以计算所述机器学习模型的损失函数的值;在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述量子程序的参数;确定更新所述参数后的所述机器学习模型的所述损失函数的值;在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。In one embodiment, the inputting the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model includes: inputting the prediction result into the A loss function layer, to calculate the value of the loss function of the machine learning model; when it is determined that the value of the loss function does not meet a preset condition, input the value of the loss function into the optimizer layer, based on the The value of the loss function updates the parameters of the quantum program; determines the value of the loss function of the machine learning model after updating the parameters; when it is determined that the value of the loss function satisfies a preset condition, the parameters will be updated The machine learning model after training is used as the machine learning model after training.
在一个实施例中,所述基于所述损失函数的值更新所述量子程序的参数,包括:基于所述损失函数的值计算所述损失函数相对于所述量子程序的同一所述参数的梯度;基于多个所述梯度和梯度下降算法更新所述量子程序的所述参数。In one embodiment, the updating the parameters of the quantum program based on the value of the loss function includes: calculating the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function ; updating said parameters of said quantum program based on a plurality of said gradients and a gradient descent algorithm.
在一个实施例中,所述基于多个所述梯度和梯度下降算法更新所述量子程序的所述参数,包括:计算多个所述梯度的平均值;将所述平均值乘以步长得到偏置量;将所述量子程序的所述参数减去所述偏置量以更新所述量子程序的所述参数。In one embodiment, updating the parameters of the quantum program based on multiple gradients and gradient descent algorithms includes: calculating the average value of multiple gradients; multiplying the average value by the step size to obtain an offset; subtracting the offset from the parameter of the quantum program to update the parameter of the quantum program.
在第二方面中,本公开提供一种机器学习框架,所述框架包括:数据结构模块,被配置为获取目标数据并创建包括所述目标数据的张量数据;经典模块,被配置为创建机器学习模型;量子模块,被配置为创建机器学习模型,以及基于量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组,针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路,以使得每组所述量子线路的输入数据确定时,将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果。In a second aspect, the present disclosure provides a machine learning framework comprising: a data structure module configured to acquire target data and create tensor data including the target data; a classical module configured to create a machine learning learning model; a quantum module configured to create a machine learning model and divide the second qubits into multiples based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device group, for each group of the second qubits, generate a quantum circuit including the group of the second qubits based on the quantum program, so that when the input data of each group of the quantum circuits is determined, the input Data is respectively input into each of the quantum circuits, and an output result of each of the quantum circuits is obtained.
在第三方面中,本公开提供一种数据处理装置,应用于包括机器学习框架的电子设备,所述机器学习框架包括数据结构模块、量子模块和经典模块,所述装置包括:第一创建模块,用于调用所述量子模块和所述经典模块创建包括量子程序的机器学习模型,调用所述数据结构模块获取目标数据并创建包括所述目标数据的张量数据;划分模块,用于调用所述量子模块基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组;生成模块,用于调用所述量子模块针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路;输入模块,用于基于所述张量数据得到每组所述量子线路的输入数据,并将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果。In a third aspect, the present disclosure provides a data processing device applied to an electronic device including a machine learning framework, the machine learning framework including a data structure module, a quantum module and a classical module, and the device includes: a first creation module , for calling the quantum module and the classical module to create a machine learning model including a quantum program, calling the data structure module to obtain target data and create tensor data including the target data; the division module is used to call the The quantum module divides the second qubits into multiple groups based on the quantity of the first qubits required by the quantum program and the quantity of the second qubits allowed in the quantum device; the generation module is used to call the For each group of the second qubits, the quantum module generates a quantum circuit including the group of the second qubits based on the quantum program; an input module is used to obtain each group of the quantum circuits based on the tensor data input data, and input the input data into each of the quantum circuits respectively, to obtain the output result of each of the quantum circuits.
在一个实施例中,所述划分模块还用于:将所述量子设备中允许使用的第二量子比特的数量除以所述量子程序所需的第一量子比特的数量得到中间数;将小于等于所述中间数的最大整数作为目标数;将所述第二量子比特划分为数量为所述目标数的多组。In one embodiment, the division module is also used to: divide the number of second qubits allowed in the quantum device by the number of first qubits required by the quantum program to obtain an intermediate number; The largest integer equal to the intermediate number is used as the target number; and the second qubit is divided into multiple groups whose number is the target number.
在一个实施例中,所述生成模块还用于:针对每一组所述第二量子比特,基于所述量子程序生成用于作用于该组所述第二量子比特的量子逻辑门;生成包括该组所述第二量子比特和所述量子逻辑门的量子线路。In one embodiment, the generating module is further configured to: for each group of the second qubits, generate a quantum logic gate for acting on the group of the second qubits based on the quantum program; generating includes The set of quantum circuits of the second qubit and the quantum logic gate.
在一个实施例中,所述装置还包括:确定模块,用于基于所述输出结果确定所述机器学习模型对所述张量数据的预测结果。In one embodiment, the device further includes: a determination module, configured to determine a prediction result of the machine learning model for the tensor data based on the output result.
在一个实施例中,所述经典模块还用于创建所述机器学习模型的训练层,所述装置还包括:第二创建模块,用于调用所述经典模块创建所述机器学习模型的训练层;更新模块,用于将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型。In one embodiment, the classic module is also used to create the training layer of the machine learning model, and the device further includes: a second creation module, used to call the classic module to create the training layer of the machine learning model an update module, configured to input the prediction result into the training layer, so as to update the parameters of the quantum program, and obtain the trained machine learning model.
在一个实施例中,所述训练层包括损失函数层和优化器层,所述经典模块包括:损失 函数单元,被配置为计算所述机器学习模型的损失函数;优化器单元,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化;所述第二创建模块还用于:调用所述损失函数单元创建所述损失函数层;调用所述优化器单元创建所述优化器层。In one embodiment, the training layer includes a loss function layer and an optimizer layer, and the classical module includes: a loss function unit configured to calculate a loss function of the machine learning model; an optimizer unit configured to When training the machine learning model, update the parameters of the machine learning model based on the loss function to optimize the machine learning model; the second creation module is also used to: call the loss function unit to create the A loss function layer; calling the optimizer unit to create the optimizer layer.
在一个实施例中,所述更新模块还用于:将所述预测结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值;在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述量子程序的参数;确定更新所述参数后的所述机器学习模型的所述损失函数的值;在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。In one embodiment, the update module is further configured to: input the prediction result into the loss function layer to calculate the value of the loss function of the machine learning model; When setting the condition, input the value of the loss function into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function; determine the loss of the machine learning model after updating the parameters The value of the function; when it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
在一个实施例中,所述更新模块还用于:基于所述损失函数的值计算所述损失函数相对于所述量子程序的同一所述参数的梯度;基于多个所述梯度和梯度下降算法更新所述量子程序的所述参数。In one embodiment, the update module is further configured to: calculate the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function; based on multiple gradients and gradient descent algorithms The parameters of the quantum program are updated.
在一个实施例中,所述更新模块还用于:计算多个所述梯度的平均值;将所述平均值乘以步长得到偏置量;将所述量子程序的所述参数减去所述偏置量以更新所述量子程序的所述参数。In one embodiment, the update module is further used to: calculate the average value of multiple gradients; multiply the average value by the step size to obtain a bias; subtract the parameter of the quantum program from the the bias to update the parameters of the quantum program.
在第四面中,本公开提供了一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述第一方面中任一项所述方法的步骤。In the fourth aspect, the present disclosure provides a storage medium in which a computer program is stored, wherein the computer program is configured to perform the steps of any one of the methods described in the above first aspect when running .
在第五面中,本公开提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述第一方面中任一项所述方法的步骤。In the fifth aspect, the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned first aspects. A step of said method.
基于上述技术方案,对于调用量子模块和经典模块创建的机器学习模型,在将张量数据输入该模型之前,根据量子程序所需的第一量子比特的数量,以及可用的第二量子比特的数量,生成多组对应于量子程序的量子线路,进而使得张量数据输入机器学习模型得到用于输入多组量子线路的多个输入数据后,可以将多个输入数据输入到多组量子线路,以利用多组量子线路对输入数据进行相关的量子计算,提高量子程序运行的效率。Based on the above technical solution, for the machine learning model created by calling the quantum module and the classical module, before inputting the tensor data into the model, according to the number of first qubits required by the quantum program and the number of second qubits available , generate multiple sets of quantum circuits corresponding to the quantum program, and then make the tensor data input machine learning model obtain multiple input data for inputting multiple sets of quantum circuits, and then input multiple input data into multiple sets of quantum circuits to Using multiple groups of quantum circuits to perform related quantum calculations on the input data to improve the efficiency of quantum program operation.
20211168061302021116806130
2021116805706为了至少解决上述背景技术部分所描述的技术问题,本公开提出了一种机器学习模型运行方法、机器学习框架及相关设备,旨在实现对不同运算设备的无缝切换和统一调用,提高运算设备使用的统一性。鉴于此,本公开在如下的多个方面提供解决方案。2021116805706 In order to at least solve the technical problems described in the background technology section above, this disclosure proposes a machine learning model running method, a machine learning framework and related equipment, aiming to realize seamless switching and unified calling of different computing devices, and improve computing power. Uniformity of equipment usage. In view of this, the present disclosure provides solutions in the following aspects.
在第一方面中,本公开提供了一种机器学习模型运行方法,应用于包括机器学习框架的电子装置,所述机器学习框架包括经典模块和量子模块,所述方法包括:指定经典计算层的第一运算设备和量子计算层的第二运算设备;将所述第一运算设备作为参数传入经典计算层接口,以及将所述第二运算设备作为参数传入量子计算层接口;调用所述经典模块通过所述经典计算层接口创建经典计算层,以及调用所述量子模块通过所述量子计算层接口创建量子计算层;调用所述经典模块构建包括所述经典计算层和量子计算层的机器学习模型,以及调用所述第一运算设备和所述第二运算设备运行所述机器学习模型。In a first aspect, the present disclosure provides a method for running a machine learning model, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a classical module and a quantum module, and the method includes: specifying a classical computing layer The first computing device and the second computing device of the quantum computing layer; passing the first computing device as a parameter into the classical computing layer interface, and passing the second computing device as a parameter into the quantum computing layer interface; calling the The classic module creates a classical computing layer through the classical computing layer interface, and calls the quantum module to create a quantum computing layer through the quantum computing layer interface; calls the classical module to build a machine that includes the classical computing layer and the quantum computing layer Learning a model, and invoking the first computing device and the second computing device to run the machine learning model.
在一个实施例中,所述调用所述第一运算设备和所述第二运算设备运行所述机器学习模型,包括:在确定所述第二运算设备支持的计算格式不包括所述量子计算层的输入数据的格式时,调用所述经典模块通过数据格式转换接口将所述量子计算层的输入数据的格式 转换为所述第二运算设备支持的计算格式;调用所述第二运算设备运行所述量子计算层计算所述输入数据对应的输出数据。In one embodiment, the calling the first computing device and the second computing device to run the machine learning model includes: determining that the computing format supported by the second computing device does not include the quantum computing layer When the format of the input data is used, the classical module is called to convert the format of the input data of the quantum computing layer into a computing format supported by the second computing device through the data format conversion interface; the second computing device is called to run the The quantum computing layer calculates the output data corresponding to the input data.
在一个实施例中,所述第一运算设备的计算硬件包括以下至少一种:中央处理器CPU、图形处理器GPU、张量处理器TPU、现场可编程逻辑门阵列FPGA;所述第二运算设备的计算硬件包括以下至少一种:CPU、GPU、量子处理器QPU。In one embodiment, the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; the second computing The computing hardware of the device includes at least one of the following: CPU, GPU, and quantum processor QPU.
在一个实施例中,若所述第二运算设备的计算硬件为CPU或GPU,则所述第二运算设备为量子虚拟机;所述调用所述第一运算设备和所述第二运算设备运行机器学习模型之前,所述方法还包括:基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机。In one embodiment, if the computing hardware of the second computing device is CPU or GPU, then the second computing device is a quantum virtual machine; the calling the first computing device and the second computing device to run Before the machine learning model, the method further includes: constructing the quantum virtual machine based on the quantum computing programming library included in the machine learning framework.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,包括:将指定计算硬件的量子机器类型作为参数传入所述机器学习框架内含的量子计算编程库中的初始化函数,以及通过所述初始化函数创建所述量子虚拟机。In one embodiment, constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: passing the quantum machine type of the specified computing hardware as a parameter into the quantum computing library contained in the machine learning framework An initialization function in the quantum computing programming library, and creating the quantum virtual machine through the initialization function.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,包括:将指定计算硬件的量子机器类型作为参数传入所述机器学习框架内含的量子计算编程库中的量子机器初始化接口,以及通过所述量子机器初始化接口创建所述量子虚拟机。In one embodiment, constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: passing the quantum machine type of the specified computing hardware as a parameter into the quantum computing library contained in the machine learning framework A quantum machine initialization interface in the quantum computing programming library, and creating the quantum virtual machine through the quantum machine initialization interface.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,包括:基于所述机器学习框架内含的量子计算编程库中的指定计算硬件的量子虚拟机类创建所述量子虚拟机。In one embodiment, the constructing the quantum virtual machine based on the quantum computing programming library contained in the machine learning framework includes: based on the quantum computing hardware specified in the quantum computing programming library contained in the machine learning framework The virtual machine class creates the quantum virtual machine.
在第二方面中,本公开提供了一种机器学习模型运行装置,应用于包括机器学习框架的电子装置,所述机器学习框架包括经典模块和量子模块,所述装置包括:运算设备确定单元,用于指定经典计算层的第一运算设备和量子计算层的第二运算设备;接口确定单元,用于将所述第一运算设备作为参数传入经典计算层接口,以及将所述第二运算设备作为参数传入量子计算层接口;计算层创建单元,用于调用所述经典模块通过所述经典计算层接口创建经典计算层,以及调用所述量子模块通过所述量子计算层接口创建量子计算层;模型运行单元,用于调用所述经典模块构建包括所述经典计算层和量子计算层的机器学习模型,以及调用所述第一运算设备和所述第二运算设备所述运行机器学习模型。In a second aspect, the present disclosure provides a machine learning model running device, which is applied to an electronic device including a machine learning framework, the machine learning framework includes a classical module and a quantum module, and the device includes: a computing device determining unit, It is used to specify the first computing device of the classical computing layer and the second computing device of the quantum computing layer; the interface determination unit is used to pass the first computing device as a parameter into the interface of the classical computing layer, and transfer the second computing The device is passed as a parameter to the quantum computing layer interface; the computing layer creation unit is used to call the classical module to create a classical computing layer through the classical computing layer interface, and call the quantum module to create a quantum computing layer through the quantum computing layer interface layer; a model running unit, configured to call the classical module to construct a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model .
在一个实施例中,在所述调用所述第一运算设备和所述第二运算设备运行所述机器学习模型方面,所述模型运行单元,具体用于:在确定所述第二运算设备支持的计算格式不包括所述量子计算层的输入数据的格式时,调用所述经典模块通过数据格式转换接口将所述量子计算层的输入数据的格式转换为所述第二运算设备支持的计算格式;调用所述第二运算设备运行所述量子计算层计算所述输入数据对应的输出数据。In one embodiment, in terms of invoking the first computing device and the second computing device to run the machine learning model, the model running unit is specifically configured to: determine that the second computing device supports When the computing format does not include the format of the input data of the quantum computing layer, the classical module is called to convert the format of the input data of the quantum computing layer into a computing format supported by the second computing device through a data format conversion interface ; calling the second computing device to run the quantum computing layer to calculate the output data corresponding to the input data.
在一个实施例中,所述第一运算设备的计算硬件包括以下至少一种:中央处理器CPU、图形处理器GPU、张量处理器TPU、现场可编程逻辑门阵列FPGA;所述第二运算设备的计算硬件包括以下至少一种:CPU、GPU、量子处理器QPU。In one embodiment, the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; the second computing The computing hardware of the device includes at least one of the following: CPU, GPU, and quantum processor QPU.
在一个实施例中,若所述第二运算设备的计算硬件为CPU或GPU,则所述第二运算设备为量子虚拟机;所述调用所述第一运算设备和所述第二运算设备运行机器学习模型之前,所述运算设备确定单元,还用于:基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机。In one embodiment, if the computing hardware of the second computing device is CPU or GPU, then the second computing device is a quantum virtual machine; the calling the first computing device and the second computing device to run Before the machine learning model, the determining unit of the computing device is further configured to: construct the quantum virtual machine based on the quantum computing programming library included in the machine learning framework.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元,具体用于:将指定计算硬件的量子机器类型作为参数传入 所述机器学习框架内含的量子计算编程库中的初始化函数,以及通过所述初始化函数创建所述量子虚拟机。In one embodiment, the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework, and the computing device determination unit is specifically configured to: pass the quantum machine type of the specified computing hardware as a parameter Import the initialization function in the quantum computing programming library included in the machine learning framework, and create the quantum virtual machine through the initialization function.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元,具体用于:将指定计算硬件的量子机器类型作为参数传入所述机器学习框架内含的量子计算编程库中的量子机器初始化接口,以及通过所述量子机器初始化接口创建所述量子虚拟机。In one embodiment, the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework, and the computing device determination unit is specifically configured to: pass the quantum machine type of the specified computing hardware as a parameter Import the quantum machine initialization interface in the quantum computing programming library included in the machine learning framework, and create the quantum virtual machine through the quantum machine initialization interface.
在一个实施例中,所述基于所述机器学习框架内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元,具体用于:基于所述机器学习框架内含的量子计算编程库中的指定计算硬件的量子虚拟机类创建所述量子虚拟机。In one embodiment, the quantum virtual machine is constructed based on the quantum computing programming library contained in the machine learning framework, and the computing device determines the unit, specifically for: based on the quantum computing contained in the machine learning framework A quantum virtual machine class specifying computing hardware in a programming library creates the quantum virtual machine.
在第三方面中,本公开提供了一种机器学习框架,所述机器学习框架包括:经典模块,被配置为通过经典计算层接口创建经典计算层,所述经典计算层接口的参数包括所述经典计算层的第一运算设备;量子模块,被配置为通过量子计算层接口创建量子计算层,所述量子计算层接口的参数包括所述量子计算层的第二运算设备;所述经典模块,还被配置为构建包括所述经典计算层和量子计算层的机器学习模型,以及调用所述第一运算设备和所述第二运算设备运行所述机器学习模型。In a third aspect, the present disclosure provides a machine learning framework, the machine learning framework comprising: a classic module configured to create a classic computing layer through a classic computing layer interface, the parameters of the classic computing layer interface include the The first computing device of the classical computing layer; the quantum module is configured to create a quantum computing layer through the quantum computing layer interface, and the parameters of the quantum computing layer interface include the second computing device of the quantum computing layer; the classical module, It is also configured to construct a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model.
在第四方面中,本公开提供了一种存储介质,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行上述任一项中所述的方法。In a fourth aspect, the present disclosure provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method described in any one of the above when running.
在第五方面中,本公开提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述任一项中所述的方法。In a fifth aspect, the present disclosure provides an electronic device, including a memory and a processor, the memory stores a computer program, and the processor is configured to run the computer program to perform any of the above-mentioned described method.
与现有技术相比,本公开在提供的包括经典模块和量子模块的机器学习框架下构建机器学习模型,该机器学习模型的经典计算层由经典模块通过经典计算层接口创建,量子计算层由量子模块通过量子计算层接口创建,在创建之时即指定经典计算层的第一运算设备作为经典计算层接口的参数和指定量子计算层的第二运算设备作为量子计算层接口的参数,从而实现了在运行时,可以通过第一运算设备运行经典计算层和第二运算设备运行量子计算层,实现了对不同运算设备的无缝切换和统一调用,提高运算设备使用的统一性。Compared with the prior art, the present disclosure builds a machine learning model under the provided machine learning framework including a classical module and a quantum module, the classical computing layer of the machine learning model is created by the classical module through the classical computing layer interface, and the quantum computing layer is created by The quantum module is created through the quantum computing layer interface. When it is created, it specifies the first computing device of the classical computing layer as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer as the parameter of the quantum computing layer interface, so as to realize During operation, the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
20211168057062021116805706
附图说明Description of drawings
通过参考附图阅读下文的详细描述,本公开示例性实施方式的说明以及其他目的、特征和优点将变得易于理解。在附图中,以示例性而非限制性的方式示出了本公开的若干实施方式,并且相同或对应的标号表示相同或对应的部分,其中:The description of the exemplary embodiments of the present disclosure, as well as other objects, features and advantages will become readily understood by reading the following detailed description with reference to the accompanying drawings. In the drawings, several embodiments of the present disclosure are shown by way of illustration and not limitation, and the same or corresponding reference numerals indicate the same or corresponding parts, wherein:
图1是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的计算机终端的硬件结构框图;Fig. 1 is a hardware structural block diagram of a computer terminal of a model training method based on a machine learning framework system shown according to an exemplary embodiment;
图2是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的流程图;Fig. 2 is a flow chart of a model training method based on a machine learning framework system shown according to an exemplary embodiment;
图3是根据一示例性实施例示出的一种机器学习框架***的框图;Fig. 3 is a block diagram of a machine learning framework system shown according to an exemplary embodiment;
图4是根据一示例性实施例示出的一种经典模块的框图;Fig. 4 is a block diagram of a classic module shown according to an exemplary embodiment;
图5是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S22的流程图;Fig. 5 is a flowchart of step S22 in a model training method based on a machine learning framework system shown according to an exemplary embodiment;
图6是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S23的流程图;Fig. 6 is a flowchart of step S23 in a model training method based on a machine learning framework system according to an exemplary embodiment;
图7是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S232的流程图;Fig. 7 is a flowchart of step S232 in a model training method based on a machine learning framework system according to an exemplary embodiment;
图8是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S2321的流程图;Fig. 8 is a flowchart of step S2321 in a model training method based on a machine learning framework system according to an exemplary embodiment;
图9是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S23211的流程图;Fig. 9 is a flowchart of step S23211 in a model training method based on a machine learning framework system according to an exemplary embodiment;
图10是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的另一流程图;Fig. 10 is another flowchart of a model training method based on a machine learning framework system shown according to an exemplary embodiment;
图11是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的另一流程图;Fig. 11 is another flow chart of a model training method based on a machine learning framework system according to an exemplary embodiment;
图12是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的另一流程图;Fig. 12 is another flowchart of a model training method based on a machine learning framework system shown according to an exemplary embodiment;
图13是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法中步骤S125的流程图;Fig. 13 is a flowchart of step S125 in a model training method based on a machine learning framework system according to an exemplary embodiment;
图14是根据一示例性实施例示出的一种计算图;Fig. 14 is a calculation graph shown according to an exemplary embodiment;
图15是根据一示例性实施例示出的创建机器学习模型的又一流程图;Fig. 15 is another flowchart of creating a machine learning model according to an exemplary embodiment;
图16是根据一示例性实施例示出的创建机器学习模型的又一流程图;Fig. 16 is another flowchart of creating a machine learning model according to an exemplary embodiment;
图17是根据一示例性实施例示出的训练机器学习模型的又一流程图;Fig. 17 is another flowchart of training a machine learning model according to an exemplary embodiment;
图18是根据一示例性实施例示出的一种第一梯度计算程序的框图;Fig. 18 is a block diagram of a first gradient calculation program according to an exemplary embodiment;
图19是根据一示例性实施例示出的含噪声机器学习模型创建方法的流程示意图;Fig. 19 is a schematic flowchart of a method for creating a noise-containing machine learning model according to an exemplary embodiment;
图20是根据一示例性实施例示出的机器学习模型构建的又一流程示意图;Fig. 20 is another schematic flowchart of machine learning model construction according to an exemplary embodiment;
图21是根据一示例性实施例示出的训练机器学习模型的又一流程图;Fig. 21 is another flow chart of training a machine learning model according to an exemplary embodiment;
图22是根据一示例性实施例示出的将所述第二量子比特划分为多组的流程图;Fig. 22 is a flowchart showing dividing the second qubits into multiple groups according to an exemplary embodiment;
图23是根据一示例性实施例示出的生成量子线路的流程图;Fig. 23 is a flow chart of generating a quantum circuit according to an exemplary embodiment;
图24是根据一示例性实施例示出的一种机器学习模型运行方法的流程示意图;Fig. 24 is a schematic flowchart of a method for running a machine learning model according to an exemplary embodiment;
图25是根据一示例性实施例示出的一种机器学习模型运行装置的结构示意图;Fig. 25 is a schematic structural diagram of a device for running a machine learning model according to an exemplary embodiment;
图26是根据一示例性实施例示出的一种基于机器学习框架***的模型训练装置的框图。Fig. 26 is a block diagram of a model training device based on a machine learning framework system according to an exemplary embodiment.
具体实施方式Detailed ways
下面将结合本公开实施例中的附图,对本公开实施例中的技术方案进行清楚、完整地描述。显然,所描述的实施例是本公开一部分实施例,而不是全部的实施例。基于本公开中的实施例,本领域技术人员在没有做出创造性劳动前提下所获得的所有其他实施例,都属于本公开保护的范围。The following will clearly and completely describe the technical solutions in the embodiments of the present disclosure with reference to the drawings in the embodiments of the present disclosure. Apparently, the described embodiments are some of the embodiments of the present disclosure, but not all of them. Based on the embodiments in the present disclosure, all other embodiments obtained by those skilled in the art without making creative efforts belong to the protection scope of the present disclosure.
应当理解,本公开的权利要求、说明书及附图中的术语“第一”、“第二”、“第三”和“第四”等是用于区别不同对象,而不是用于描述特定顺序。本公开的说明书和权利要求书中使用的术语“包括”和“包含”指示所描述特征、整体、步骤、操作、元素和/或组件的存在,但并不排除一个或多个其它特征、整体、步骤、操作、元素、组件和/或其 集合的存在或添加。It should be understood that the terms "first", "second", "third" and "fourth" in the claims, specification and drawings of the present disclosure are used to distinguish different objects, rather than to describe a specific order . The terms "comprising" and "comprises" used in the specification and claims of the present disclosure indicate the presence of described features, integers, steps, operations, elements and/or components, but do not exclude one or more other features, integers , steps, operations, elements, components, and/or the presence or addition of collections thereof.
还应当理解,在此本公开说明书中所使用的术语仅仅是出于描述特定实施例的目的,而并不意在限定本公开。如在本公开说明书和权利要求书中所使用的那样,除非上下文清楚地指明其它情况,否则单数形式的“一”、“一个”及“该”意在包括复数形式。还应当进一步理解,在本公开说明书和权利要求书中使用的术语“和/或”是指相关联列出的项中的一个或多个的任何组合以及所有可能组合,并且包括这些组合。It should also be understood that the terminology used in the present disclosure is for the purpose of describing particular embodiments only and is not intended to be limiting of the present disclosure. As used in this disclosure and the claims, the singular forms "a", "an" and "the" are intended to include plural referents unless the context clearly dictates otherwise. It should be further understood that the term "and/or" used in the present disclosure and claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.
本公开实施例首先提供了一种基于机器学习框架***的模型训练方法,该方法可以应用于电子设备,如计算机终端,具体如普通电脑、量子计算机等。Embodiments of the present disclosure firstly provide a model training method based on a machine learning framework system, which can be applied to electronic devices, such as computer terminals, specifically, ordinary computers, quantum computers, and the like.
下面以运行在计算机终端上为例对其进行详细说明。图1是根据一示例性实施例示出的一种数据处理方法的计算机终端的硬件结构框图。如图1所示,计算机终端可以包括一个或多个(图1中仅示出一个)处理器102(处理器102可以包括但不限于微处理器MCU或可编程逻辑器件FPGA等的处理装置)和用于存储基于量子线路的数据处理方法的存储器104,可选地,上述计算机终端还可以包括用于通信功能的传输装置106以及输入输出设备108。本领域普通技术人员可以理解,图1所示的结构仅为示意,其并不对上述计算机终端的结构造成限定。例如,计算机终端还可包括比图1中所示更多或者更少的组件,或者具有与图1所示不同的配置。The following will describe it in detail by taking it running on a computer terminal as an example. Fig. 1 is a block diagram showing a hardware structure of a computer terminal according to a data processing method according to an exemplary embodiment. As shown in Figure 1, the computer terminal may include one or more (only one is shown in Figure 1) processors 102 (processors 102 may include but not limited to processing devices such as microprocessor MCU or programmable logic device FPGA, etc.) and a memory 104 for storing the quantum circuit-based data processing method. Optionally, the above-mentioned computer terminal may also include a transmission device 106 and an input and output device 108 for communication functions. Those skilled in the art can understand that the structure shown in FIG. 1 is only for illustration, and it does not limit the structure of the above computer terminal. For example, the computer terminal may also include more or fewer components than shown in FIG. 1 , or have a different configuration than that shown in FIG. 1 .
存储器104可用于存储应用软件的软件程序以及模块,如本公开实施例中的数据处理方法对应的程序指令/模块,处理器102通过运行存储在存储器104内的软件程序以及模块,从而执行各种功能应用以及数据处理,即实现上述的方法。存储器104可包括高速随机存储器,还可包括非易失性存储器,如一个或者多个磁性存储装置、闪存、或者其他非易失性固态存储器。在一些实例中,存储器104可进一步包括相对于处理器102远程设置的存储器,这些远程存储器可以通过网络连接至计算机终端。上述网络的实例包括但不限于互联网、企业内部网、局域网、移动通信网及其组合。The memory 104 can be used to store software programs and modules of application software, such as program instructions/modules corresponding to the data processing method in the embodiments of the present disclosure, and the processor 102 executes various programs by running the software programs and modules stored in the memory 104 Functional application and data processing are to realize the above-mentioned method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include a memory that is remotely located relative to the processor 102, and these remote memories may be connected to a computer terminal through a network. Examples of the aforementioned networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
传输装置106用于经由一个网络接收或者发送数据。上述的网络具体实例可包括计算机终端的通信供应商提供的无线网络。在一个实例中,传输装置106包括一个网络适配器(Network Interface Controller,NIC),其可通过基站与其他网络设备相连从而可与互联网进行通讯。在一个实例中,传输装置106可以为射频(Radio Frequency,RF)模块,其用于通过无线方式与互联网进行通讯。The transmission device 106 is used to receive or transmit data via a network. The specific example of the above-mentioned network may include a wireless network provided by the communication provider of the computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 may be a radio frequency (Radio Frequency, RF) module, which is used to communicate with the Internet in a wireless manner.
需要说明的是,真正的量子计算机是混合结构的,它包含两大部分:一部分是经典计算机,负责执行经典计算与控制;另一部分是量子设备,负责运行量子程序进而实现量子计算。而量子程序是由设定编程语言,示例性的,基于C、Python定义的QRunes语言,编写表征量子比特及其演化的程序,是一串能够在量子计算机上运行的指令序列,实现了对量子逻辑门操作的支持,并最终实现量子计算。It should be noted that a real quantum computer has a hybrid structure, which consists of two parts: one is a classical computer, which is responsible for performing classical calculation and control; the other is a quantum device, which is responsible for running quantum programs and realizing quantum computing. The quantum program is a set programming language, for example, based on the QRunes language defined by C and Python, to write a program that characterizes qubits and their evolution. It is a series of instruction sequences that can be run on a quantum computer. The support of logic gate operations, and ultimately the realization of quantum computing.
量子线路作为量子程序的一种体现方式,也称量子逻辑电路,是一种通用量子计算模型,表示在抽象概念下对于量子比特进行操作的线路,其组成包括量子比特、线路(时间线)、以及各种量子逻辑门,最后常需要通过量子测量操作将结果读取出来。As an embodiment of quantum program, quantum circuit is also called quantum logic circuit. It is a general quantum computing model, which represents a circuit that operates on qubits under an abstract concept. Its composition includes qubits, circuits (timelines), As well as various quantum logic gates, the results often need to be read out through quantum measurement operations.
传统电路是用金属线所连接以传递电压信号或电流信号,在量子线路中这一点是不同的,量子线路可看成是由时间所连接,以传递量子比特随着时间演化的状态。Traditional circuits are connected by metal wires to transmit voltage signals or current signals. This is different in quantum circuits. Quantum circuits can be regarded as connected by time to transmit the state of qubits evolving over time.
一个量子程序可以由量子线路、针对量子线路中量子比特的测量操作、保存测量结果的寄存器及控制流节点(跳转指令)组成,一条量子线路可以包含几十上百个甚至成千上 万个量子逻辑门操作。量子程序的执行过程,就是对所有的量子逻辑门按照一定时序执行的过程。需要说明的是,时序即单个量子逻辑门被执行的时间顺序。A quantum program can be composed of quantum circuits, measurement operations for qubits in quantum circuits, registers for saving measurement results, and control flow nodes (jump instructions). A quantum circuit can contain tens, hundreds, or even thousands of Quantum logic gate operations. The execution process of a quantum program is the process of executing all quantum logic gates according to a certain time sequence. It should be noted that timing refers to the time sequence in which a single quantum logic gate is executed.
需要说明的是,经典计算中,最基本的单元是比特,而最基本的控制模式是逻辑门,可以通过逻辑门的组合来达到控制电路的目的。类似地,处理量子比特的方式就是量子逻辑门。使用量子逻辑门,能够使量子态发生演化,量子逻辑门是构成量子线路的基础,量子逻辑门包括单比特量子逻辑门,如Hadamard门(H门,阿达马门)、泡利-X门(X门,泡利X门)、泡利-Y门(Y门,泡利Y门)、泡利-Z门(Z门,泡利Z门)、RX门(RX旋转门)、RY门(RY旋转门)、RZ门(RZ旋转门)等等;多比特量子逻辑门,如CNOT门、CR门、iSWAP门、Toffoli门等等。一般量子逻辑门在量子态上的作用是通过酉矩阵左乘以量子态右矢对应的向量进行计算。It should be noted that in classical computing, the most basic unit is a bit, and the most basic control mode is a logic gate. The purpose of controlling a circuit can be achieved through the combination of logic gates. Similarly, the way to handle qubits is quantum logic gates. Quantum logic gates can be used to evolve quantum states. Quantum logic gates are the basis of quantum circuits. Quantum logic gates include single-bit quantum logic gates, such as Hadamard gates (H gates, Hadamard gates), Pauli-X gates ( X gate, Pauli X gate), Pauli-Y gate (Y gate, Pauli Y gate), Pauli-Z gate (Z gate, Pauli Z gate), RX gate (RX revolving gate), RY gate ( RY revolving gate), RZ gate (RZ revolving gate), etc.; multi-bit quantum logic gates, such as CNOT gate, CR gate, iSWAP gate, Toffoli gate, etc. Generally, the function of quantum logic gates on the quantum state is calculated by multiplying the left side of the unitary matrix by the vector corresponding to the right vector of the quantum state.
图2是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的流程示意图。参见图2,本实施例提供一种基于机器学习框架***的模型训练方法,该方法可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***30包括数据结构模块31、量子模块32和经典模块33,所述方法包括:Fig. 2 is a schematic flowchart of a model training method based on a machine learning framework system according to an exemplary embodiment. Referring to FIG. 2 , the present embodiment provides a model training method based on a machine learning framework system, which can be applied to an electronic device including a first machine learning framework system 30 as shown in FIG. 3 , the first machine learning framework The system 30 includes a data structure module 31, a quantum module 32 and a classical module 33, and the method includes:
S21,调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型。S21. Call the data structure module to acquire input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model.
S22,将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层。S22. Input the tensor data into the machine learning model, and call the classic module to create a training layer of the machine learning model.
S23,将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型。S23. Input the output result of the machine learning model into the training layer, so as to update the parameters of the machine learning model, and obtain the trained machine learning model.
具体来讲,第一机器学习框架***集成了众多用于创建和训练机器学习模型的函数集,通过其定义的接口可以方便的调用这些函数实现对机器学习模型的相关操作。如图3所示,所述第一机器学习框架***30可以包括:Specifically, the first machine learning framework system integrates many function sets for creating and training machine learning models, and through the interfaces defined by it, these functions can be conveniently called to implement related operations on machine learning models. As shown in Figure 3, the first machine learning framework system 30 may include:
数据结构模块31,被配置为创建用于输入机器学习模型的张量数据,以及对所述张量数据执行运算;The data structure module 31 is configured to create tensor data for input into the machine learning model, and perform operations on the tensor data;
量子模块32,被配置为创建用于创建机器学习模型的量子计算层;a quantum module 32 configured to create a quantum computing layer for creating machine learning models;
经典模块33,被配置为创建用于创建机器学习模型的经典计算层、用于封装所述量子计算层和所述经典计算层的抽象类层、用于训练优化所述机器学习模型的机器学习模型训练层。The classical module 33 is configured to create a classical computing layer for creating a machine learning model, an abstract class layer for encapsulating the quantum computing layer and the classical computing layer, and a machine learning machine for training and optimizing the machine learning model Model training layer.
具体来讲,数据结构模块31定义了张量数据的数据结构,通过调用数据结构模块31,可以将输入的数据转化为张量数据,以用于输入机器学习模型进行正向计算。数据结构模块31还可以定义张量数据之间的运算例如数学运算和逻辑运算等,进而可以调用数据结构模块31基于张量数据之间的运算关系创建机器学习模型的经典计算层,例如经典神经网络的全连接层通过函数y=wx+b定义了输入数据x和输出数据y之间的关系,其中w和b为参数,通过将输入数据x、参数w、参数b转化为张量数据,并调用数据结构模块31对这些张量数据执行与该函数对应的运算,可以构建全连接层。Specifically, the data structure module 31 defines the data structure of the tensor data. By calling the data structure module 31, the input data can be converted into tensor data for input into the machine learning model for forward calculation. The data structure module 31 can also define operations between tensor data, such as mathematical operations and logical operations, etc., and then call the data structure module 31 to create a classic calculation layer of a machine learning model based on the operation relationship between tensor data, such as a classic neural network. The fully connected layer of the network defines the relationship between the input data x and the output data y through the function y=wx+b, where w and b are parameters, by converting the input data x, parameter w, and parameter b into tensor data, And calling the data structure module 31 to perform operations corresponding to the function on these tensor data, a fully connected layer can be constructed.
在一种可能的实施方式中,数据结构模块31可以用于将输入数据按照预设的数据结构排列以创建用于输入所述机器学习模型的张量数据,以及创建以所述预设的数据结构排列且数值确定的用于输入所述机器学习模型的张量数据。进而在步骤S21中,对于输入的训练数据,可以将训练数据按照预设的数据结构排列以得到张量数据,训练数据可以作为 张量数据的一部分存储。例如获取的训练数据为1,2,3,可以将输入的该训练数据转化为向量结构[1,2,3]作为张量数据的一部分。In a possible implementation, the data structure module 31 can be used to arrange the input data according to a preset data structure to create tensor data for inputting the machine learning model, and create the preset data Tensor data that is structured and numerically determined for input to the machine learning model. Furthermore, in step S21, for the input training data, the training data can be arranged according to a preset data structure to obtain tensor data, and the training data can be stored as a part of the tensor data. For example, the acquired training data is 1, 2, 3, and the input training data can be converted into a vector structure [1, 2, 3] as part of the tensor data.
需要说明的是,张量数据除包括按照预设的数据结构排列的数据值外,还可以包括计算得到该数据值的张量数据的信息以及该张量数据相对于包含所述数据值的张量数据的梯度函数,其中计算得到该数据值的张量数据的信息可以包括该张量数据的变量、数据值存储地址以及数据值等,只要其表明该张量数据对应节点是计算得到该数据值的张量数据对应节点的前驱节点即可。以上述函数关系y=wx+b为例,对于张量数据y,其包括y对应的数据值如[1,2,3],还包括计算得到y的w、x、b的张量数据的信息以及y分别相对于w、x、b的梯度函数,在一种可能的实施方式中,该信息可以包括w、x和b的数据值存储地址,张量数据y包括y相对于w的梯度函数x,y相对于x的梯度函数w,以及y相对于b的梯度函数1,进而在训练机器学习模型时,通过反向传播计算y分别相对于w、x、b的梯度值,具体可以直接从张量数据y中获取y的数据值,以及w、x、b的数据值和对应的梯度函数,通过这些数据值和对应的梯度函数计算y分别相对于w、x、b的梯度值。It should be noted that besides the data values arranged according to the preset data structure, the tensor data may also include the information of the tensor data from which the data value is calculated and the relative relation between the tensor data and the tensor data containing the data value. The gradient function of the quantitative data, where the information of the tensor data whose data value is calculated can include the variable of the tensor data, the storage address of the data value, and the data value, as long as it indicates that the corresponding node of the tensor data is calculated to obtain the data The tensor data of the value corresponds to the predecessor node of the node. Taking the above functional relationship y=wx+b as an example, for the tensor data y, it includes the data value corresponding to y such as [1,2,3], and also includes the calculated tensor data of w, x, and b of y Information and the gradient functions of y relative to w, x, and b respectively. In a possible implementation, the information may include the data value storage addresses of w, x, and b, and the tensor data y includes the gradient of y relative to w function x, the gradient function w of y relative to x, and the gradient function 1 of y relative to b, and then when training the machine learning model, the gradient values of y relative to w, x, and b are calculated through back propagation, specifically, Obtain the data value of y directly from the tensor data y, as well as the data values of w, x, and b and the corresponding gradient function, and calculate the gradient values of y relative to w, x, and b respectively through these data values and the corresponding gradient function .
具体来讲,对于量子模块32,可以通过调用该量子模块32创建量子计算层,量子计算层为包含量子程序的程序模块,可以用于实现对应量子程序的量子计算,通过对量子程序按照一定的标准进行封装,使得量子计算层便于在创建和训练机器学习模型时进行使用。对于机器学习模型通过量子计算实现的部分,均可以理解为对应的量子计算层。量子程序为实现量子计算的程序,可以通过调用量子模块32创建按特定顺序作用于量子比特的量子逻辑门得到量子程序,并对量子程序进行封装得到量子计算层。Specifically, for the quantum module 32, the quantum computing layer can be created by calling the quantum module 32. The quantum computing layer is a program module containing a quantum program, which can be used to realize the quantum computing corresponding to the quantum program. Standard encapsulation makes the quantum computing layer easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer. The quantum program is a program for implementing quantum computing. The quantum program can be obtained by calling the quantum module 32 to create quantum logic gates that act on the qubits in a specific order, and the quantum program can be packaged to obtain the quantum computing layer.
具体来讲,经典计算层为机器学习模型中的经典计算部分,其可以是通过经典模块33对创建好的经典计算程序按照一定标准进行封装得到,使得经典计算层便于在训练机器学习模型时进行使用。在创建好量子计算层和经典计算层后,可以通过经典模块33对其进行封装,以创建符合一定标准的抽象类层,抽象类层通过编程语言中的类(class)的方法实现,通过对量子计算层和经典计算层封装可以创建符合一定标准的机器学习模型,例如创建的抽象类层定义了正向运算机器学习模型的方式,便于在训练机器学习模型时对机器学习模型进行正向运算以得到用于计算损失函数的计算结果,同时也可以得到反向计算时进行梯度计算的顺序关系。经典模块33还可以用于创建机器学习模型的训练层对机器学习模型进行训练。Specifically, the classic calculation layer is the classic calculation part of the machine learning model, which can be obtained by encapsulating the created classic calculation program according to certain standards through the classic module 33, so that the classic calculation layer is convenient for training the machine learning model. use. After the quantum computing layer and the classical computing layer are created, they can be encapsulated by the classical module 33 to create an abstract class layer that meets certain standards. The abstract class layer is realized by the class method in the programming language. Quantum computing layer and classical computing layer encapsulation can create machine learning models that meet certain standards. For example, the created abstract class layer defines the way of forward computing machine learning models, which is convenient for forward computing of machine learning models when training machine learning models In order to obtain the calculation results used to calculate the loss function, it is also possible to obtain the sequence relationship of the gradient calculation during the reverse calculation. The classic module 33 can also be used to create a training layer of the machine learning model to train the machine learning model.
在步骤S21中,除调用数据结构模块31创建张量数据外,还可以调用量子模块32创建量子计算层,得到包括该量子计算层的纯量子机器学习模型,或调用经典模块33创建经典计算层,得到包括该经典计算层的纯经典机器学习模型,也可以同时调用量子模块32和经典模块33创建量子计算层和经典计算层,得到包括该量子计算层和经典计算层的混合机器学习模型。In step S21, in addition to calling the data structure module 31 to create tensor data, you can also call the quantum module 32 to create a quantum computing layer to obtain a pure quantum machine learning model including the quantum computing layer, or call the classical module 33 to create a classical computing layer , to obtain a purely classical machine learning model including the classical computing layer, or simultaneously call the quantum module 32 and the classical module 33 to create a quantum computing layer and a classical computing layer, and obtain a hybrid machine learning model including the quantum computing layer and the classical computing layer.
创建训练数据和机器学习模型后,执行步骤S22,将张量数据输入机器学习模型,并正向运行机器学习模型得到机器学习模型对张量数据进行计算后的输出结果。并且,为训练机器学习模型,调用经典模块33创建机器学习模型的训练层。After creating the training data and the machine learning model, execute step S22, input the tensor data into the machine learning model, and run the machine learning model forward to obtain the output result after the machine learning model calculates the tensor data. And, in order to train the machine learning model, the classic module 33 is called to create the training layer of the machine learning model.
可选地,所述训练层包括损失函数层和优化器层,如图4所示,所述经典模块33包括:Optionally, the training layer includes a loss function layer and an optimizer layer, as shown in Figure 4, the classic module 33 includes:
损失函数单元331,被配置为计算所述机器学习模型的损失函数;A loss function unit 331 configured to calculate a loss function of the machine learning model;
优化器单元332,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机 器学习模型的参数,以对所述机器学习模型进行优化。The optimizer unit 332 is configured to update the parameters of the machine learning model based on the loss function when training the machine learning model, so as to optimize the machine learning model.
可选地,如图5所示,调用所述经典模块创建所述机器学习模型的训练层,包括:Optionally, as shown in Figure 5, calling the classic module to create the training layer of the machine learning model includes:
S221,调用所述损失函数单元创建所述损失函数层;S221. Call the loss function unit to create the loss function layer;
S222,调用所述优化器单元创建所述优化器层。S222. Call the optimizer unit to create the optimizer layer.
具体来讲,损失函数单元331用于计算机器学***方差作为损失函数,也可以计算该正向运算结果与标签数据的二元交叉熵(Binary Cross Entropy)作为损失函数。优化器单元332则用于根据损失函数相对于机器学习模型的参数的梯度,利用梯度下降算法更新机器学习模型的参数对其进行优化。例如优化器采用的梯度下降算法可以为随机梯度下降算法(Stochastic Gradient Descent,SGD),自适应梯度算法(Adaptive gradient algorithm,Adagrad)、自适应矩估计(Adaptive Moment Estimation,Adam)中的任意一种,当然还可以采用其它的算法更新机器学习模型的参数,本公开对于损失函数单元331可以计算哪些种类的损失函数以及优化器单元332采用何种方法更新参数不作具体限制。Specifically, the loss function unit 331 is used to calculate the loss function of the machine learning model. For example, the square difference between the forward operation result of the machine learning model and the label data can be calculated as the loss function, and the forward operation result and the label data can also be calculated. The binary cross entropy (Binary Cross Entropy) as the loss function. The optimizer unit 332 is used to update the parameters of the machine learning model by gradient descent algorithm according to the gradient of the loss function relative to the parameters of the machine learning model to optimize it. For example, the gradient descent algorithm used by the optimizer can be any one of Stochastic Gradient Descent (SGD), Adaptive Gradient Algorithm (Adagrad), Adaptive Moment Estimation (Adam) Of course, other algorithms can also be used to update the parameters of the machine learning model. The present disclosure does not specifically limit which types of loss functions the loss function unit 331 can calculate and which method the optimizer unit 332 uses to update parameters.
为实现对机器学习模型的训练,可以执行步骤S221,调用所述损失函数单元331创建所述损失函数层,损失函数层为封装好的计算模块,其定义了损失函数的计算方式,进而在机器学习模型的输出结果输入到损失函数层时,可以根据损失函数层定义的计算方式计算机器学习模型的损失函数。创建好损失函数层之后可以进入执行步骤S222,调用所述优化器单元332创建所述优化器层,以在输出结果输入到损失函数层,计算出损失函数之后,根据损失函数更新机器学习模型的参数,直至得到合适的参数使得机器学习模型能达到预期的效果,完成对机器学习模型的优化。In order to realize the training of the machine learning model, step S221 can be executed to call the loss function unit 331 to create the loss function layer. The loss function layer is a packaged calculation module, which defines the calculation method of the loss function, and then in the machine When the output of the learning model is input to the loss function layer, the loss function of the machine learning model can be calculated according to the calculation method defined by the loss function layer. After creating the loss function layer, you can enter the execution step S222, and call the optimizer unit 332 to create the optimizer layer, so that after the output result is input to the loss function layer and the loss function is calculated, update the machine learning model according to the loss function parameters, until the appropriate parameters are obtained so that the machine learning model can achieve the expected effect, and the optimization of the machine learning model is completed.
创建好训练层后,进入执行步骤S23,将机器学习模型的输出结果输入到前述训练层,按照一定的方法对机器学习模型的参数进行更新,如果更新参数后的机器学习模型满足预设条件,例如其对应的损失函数的值小于阈值,则停止更新参数,并将更新参数后的机器学习模型作为训练后的机器学习模型。如果更新参数后的机器学习模型不满足预设条件,例如其对应的损失函数的值大于阈值,则可以继续更新参数,直至得到满足预设条件的机器学习模型为止。After the training layer is created, enter the execution step S23, input the output result of the machine learning model to the aforementioned training layer, update the parameters of the machine learning model according to a certain method, if the machine learning model after updating the parameters meets the preset conditions, For example, if the value of the corresponding loss function is less than the threshold, then stop updating the parameters, and use the machine learning model after updating the parameters as the machine learning model after training. If the machine learning model after updating the parameters does not meet the preset conditions, for example, the value of the corresponding loss function is greater than the threshold, the parameters can be continuously updated until a machine learning model meeting the preset conditions is obtained.
基于上述技术方案,无论对于不同的机器学习模型,还是机器学习模型应用于不同场景时,均可以通过调用经典模块33创建机器学习模型的训练层,进而将张量数据输入机器学习模型,再将机器学习模型的训练结果输入训练层,便可以对创建的机器学习模型进行训练。训练层通过调用经典模块33即可实现创建,无需从零开始创建,减少了创建训练层所需的步骤和时间,进而在变更机器学习模型或不同应用场景时提高了训练的开发效率。同时对于不同的机器学习模型或其不同的应用场景,通过数据结构模块31创建对应训练数据的张量数据,且机器学习模型基于量子模块32和/或经典模块33创建,使得张量数据、机器学习模型可以与训练层遵循基于同一机器学习框架***的标准,减少其相互之间的适配操作,进而提高训练机器学习模型的开发效率。并且,通过上述方案,无论对于纯量子机器学习模型还是纯经典机器学习模型,或者同时包括经典计算层和量子计算层的混合机器学习模型,均可以提高对其训练的开发效率,具有较广的适用范围。Based on the above technical solution, no matter for different machine learning models or when the machine learning model is applied to different scenarios, the training layer of the machine learning model can be created by calling the classic module 33, and then the tensor data is input into the machine learning model, and then the The training result of the machine learning model is input into the training layer, and the created machine learning model can be trained. The training layer can be created by calling the classic module 33, without creating from scratch, which reduces the steps and time required to create the training layer, thereby improving the development efficiency of training when changing machine learning models or different application scenarios. At the same time, for different machine learning models or their different application scenarios, the tensor data corresponding to the training data is created by the data structure module 31, and the machine learning model is created based on the quantum module 32 and/or the classical module 33, so that the tensor data, machine The learning model and the training layer can follow the standards based on the same machine learning framework system, reducing the adaptation operations between them, thereby improving the development efficiency of training machine learning models. Moreover, through the above scheme, whether it is a pure quantum machine learning model or a pure classical machine learning model, or a hybrid machine learning model that includes both classical computing layers and quantum computing layers, the development efficiency of its training can be improved, and it has a wide range of applications. scope of application.
可选地,如图6所示,将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型,包括:Optionally, as shown in Figure 6, the output result of the machine learning model is input into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model, including:
S231,将所述机器学习模型的输出结果输入所述损失函数层,以计算所述机器学习模 型的损失函数的值。S231. Input the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model.
S232,在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述机器学习模型的参数。S232. When it is determined that the value of the loss function does not meet the preset condition, input the value of the loss function into the optimizer layer, so as to update the parameters of the machine learning model based on the value of the loss function.
S233,确定更新所述参数后的所述机器学习模型的所述损失函数的值。S233. Determine a value of the loss function of the machine learning model after the parameter is updated.
S234,在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。S234. When it is determined that the value of the loss function satisfies the preset condition, use the machine learning model after updating the parameters as the machine learning model after training.
为对创建的机器学***方差,并对所有的平方差求和计算损失函数的值。In order to train the created machine learning model, enter the execution step S231, input the output result of the machine learning model into the loss function layer, and calculate the value of the loss function of the machine learning model. The gap between the label data of the case. For example, the training data can include sample data and label data. After the sample data is input into the machine learning model to obtain the output result, the output result is input into the loss function layer, the square difference between the label data and the output result is calculated, and all the square differences are summed. Calculate the value of the loss function.
在步骤S232中,可以通过将损失函数的值与预设的阈值比较,来判断损失函数的值是否满足预设条件,例如在确定损失函数的值大于或等于阈值时,将损失函数的值输入优化器层。当然也可以通过其它方法确定损失函数的值不满足预设条件,只要能通过预设函数的值判断当前的机器学习模型不符合预期即可。不满足预设条件时,将损失函数的值输入优化器层,可以利用损失函数的值求损失函数相对于机器学习模型的参数的梯度,进而基于梯度下降算法更新机器学习模型的参数。In step S232, it can be judged whether the value of the loss function satisfies the preset condition by comparing the value of the loss function with a preset threshold, for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into Optimizer layer. Of course, other methods can also be used to determine that the value of the loss function does not meet the preset conditions, as long as the value of the preset function can be used to judge that the current machine learning model does not meet expectations. When the preset conditions are not met, the value of the loss function is input into the optimizer layer, and the value of the loss function can be used to calculate the gradient of the loss function relative to the parameters of the machine learning model, and then update the parameters of the machine learning model based on the gradient descent algorithm.
在步骤S233中,在对机器学习模型更新参数后,重新计算其对应的损失函数的值。并重新判断损失函数的值是否满足预设条件,如果不满足,可以返回执行步骤S232,继续根据损失函数的值更新机器学习模型的参数,如果满足,则可以进入执行步骤S234。In step S233, after the parameters of the machine learning model are updated, the value of the corresponding loss function is recalculated. And re-judging whether the value of the loss function satisfies the preset condition, if not, return to step S232, and continue to update the parameters of the machine learning model according to the value of the loss function, and if so, proceed to step S234.
在步骤S234中,确定损失函数的值满足预设条件时,例如损失函数的值小于阈值,则表示机器学习模型对训练数据中样本数据的输出结果与标签数据的差距很小,机器学习模型能达到预期的应用效果,进而将更新参数后的机器学习模型作为训练后的机器学习模型,停止更新参数。In step S234, when it is determined that the value of the loss function satisfies the preset condition, for example, the value of the loss function is less than the threshold, it means that the gap between the output result of the machine learning model for the sample data in the training data and the label data is very small, and the machine learning model can The expected application effect is achieved, and then the machine learning model after updating the parameters is used as the machine learning model after training, and the updating of parameters is stopped.
可选地,如图7所示,基于所述损失函数的值更新所述机器学习模型的参数,包括:Optionally, as shown in Figure 7, updating the parameters of the machine learning model based on the value of the loss function includes:
S2321,基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度。S2321. Calculate the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function.
S2322,基于所述梯度和梯度下降算法更新所述机器学习模型的参数。S2322. Update parameters of the machine learning model based on the gradient and the gradient descent algorithm.
在步骤S2321中,例如可以求损失函数相对于其参数的偏导数以得到损失函数相对参数的梯度。在步骤S2322中,根据求得的梯度,带入梯度下降算法的相关公式中,更新机器学习模型的参数。例如梯度下降算法可以为批量梯度下降法(Batch Gradient Descent)、随机梯度下降法(Stochastic Gradient Descent)等。梯度反映了损失函数变化最快的方向,通过梯度下降算法可以快速的变更参数,进而提高损失函数的值变化的速度,以快速的找到满足预设条件的损失函数的值对应的参数,得到符合要求的机器学习模型。In step S2321, for example, partial derivatives of the loss function with respect to its parameters may be calculated to obtain gradients of the loss function with respect to the parameters. In step S2322, according to the obtained gradient, it is brought into the relevant formula of the gradient descent algorithm to update the parameters of the machine learning model. For example, the gradient descent algorithm can be batch gradient descent method (Batch Gradient Descent), stochastic gradient descent method (Stochastic Gradient Descent), etc. The gradient reflects the fastest changing direction of the loss function. The gradient descent algorithm can quickly change the parameters, thereby increasing the speed of the value change of the loss function, so as to quickly find the parameters corresponding to the value of the loss function that meets the preset conditions, and obtain the parameters that meet the The required machine learning model.
可选地,如图8所示,若调用所述量子模块32创建机器学习模型,所述机器学习模型包括基于所述量子模块32创建的量子计算层,所述基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度,包括:Optionally, as shown in FIG. 8, if the quantum module 32 is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module 32, and the value calculation based on the loss function The gradient of the loss function relative to the parameters of the machine learning model includes:
S23211,基于所述损失函数的值计算所述损失函数相对于所述量子计算层的输出结果的第一梯度,并计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度。S23211. Calculate the first gradient of the loss function relative to the output result of the quantum computing layer based on the value of the loss function, and calculate the first gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer Two gradients.
S23212,将所述第一梯度与所述第二梯度相乘得到所述损失函数相对于所述机器学习 模型的参数的梯度。S23212. Multiply the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model.
机器学习模型包括量子计算层,例如机器学习模型为仅包括量子计算层的纯量子机器学习模型,或同时包括量子计算层和经典计算层的混合机器学习模型,此时,为求得损失函数相对于机器学习模型中量子计算层中的参数的梯度,在步骤S23211中,先求出损失函数相对于量子计算层的输出结果的第一梯度,以及该输出结果相对于量子计算层的参数的第二梯度。再进入执行步骤S23211,将第一梯度与第二梯度相乘得到损失函数相对于量子计算层的参数的梯度,以上过程整体是基于链式求导法则进行的。The machine learning model includes a quantum computing layer. For example, the machine learning model is a pure quantum machine learning model that only includes a quantum computing layer, or a hybrid machine learning model that includes both a quantum computing layer and a classical computing layer. At this time, in order to obtain the loss function relative Based on the gradient of the parameters in the quantum computing layer in the machine learning model, in step S23211, first obtain the first gradient of the loss function relative to the output result of the quantum computing layer, and the first gradient of the output result relative to the parameters of the quantum computing layer Two gradients. Then enter the execution step S23211, multiply the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the quantum computing layer, and the above process as a whole is based on the chain derivation rule.
举例来讲,机器学***方差之和。为计算损失函数相对于参数α的梯度,先计算损失函数相对于y1的第一梯度,以及计算y1相对于α的第二梯度,然后将第一梯度与第二梯度相乘即可得到损失函数相对于参数α的梯度。For example, a machine learning model includes a quantum computing layer and a classical computing layer, where the computing function corresponding to the quantum computing layer is f(x; α), and the computing function corresponding to the classical computing layer is g(x; β), where x It is the input data of the calculation function, and α and β are parameters. The acquired input data x1 is first input into f(x; α) to obtain y1, and then input y1 into g(x; β) to obtain the output result y2 of the machine learning model. The loss function is the sum of the squared differences of y2 and the labeled data. In order to calculate the gradient of the loss function relative to the parameter α, first calculate the first gradient of the loss function relative to y1, and calculate the second gradient of y1 relative to α, and then multiply the first gradient and the second gradient to obtain the loss function Gradient with respect to parameter α.
可选地,如图9所示,计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度,包括:Optionally, as shown in FIG. 9, calculating the second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer includes:
S23211a,计算所述量子计算层的参数加上第一偏置量后所述量子计算层的第一输出结果,并计算所述量子计算层的参数减去第二偏置量后所述量子计算层的第二输出结果。S23211a, calculating the first output result of the quantum computing layer after the parameters of the quantum computing layer plus the first offset, and calculating the quantum computing after the parameters of the quantum computing layer minus the second bias The second output result of the layer.
S23211b,基于所述第一输出结果与所述第二输出结果的差值,计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度。S23211b. Based on the difference between the first output result and the second output result, calculate a second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer.
上述方法称为参数位移法,由于难以通过求解析式的方式求量子计算层的输出结果相对于其中的参数的第二梯度,因此采用参数位移的方法,可以提高求得的第二梯度的准确度,并且无论在量子计算层的量子线路运行于虚拟机还是真实量子设备,均可以通过该方法求梯度,具有较广的适配性。The above method is called the parameter displacement method. Since it is difficult to find the second gradient of the output result of the quantum computing layer relative to the parameters in it by solving the analytical formula, the method of parameter displacement can improve the accuracy of the obtained second gradient. degree, and whether the quantum circuit at the quantum computing layer runs on a virtual machine or a real quantum device, the gradient can be calculated by this method, which has wide adaptability.
沿用前述例子,第一偏置量和第二偏置量为
Figure PCTCN2022143597-appb-000001
对于量子计算层f(x;α),先计算第一输出结果
Figure PCTCN2022143597-appb-000002
以及第二输出结果
Figure PCTCN2022143597-appb-000003
然后通过以下公式求得第二梯度:
Following the previous example, the first offset and the second offset are
Figure PCTCN2022143597-appb-000001
For the quantum computing layer f(x; α), first calculate the first output result
Figure PCTCN2022143597-appb-000002
and the second output
Figure PCTCN2022143597-appb-000003
Then the second gradient is obtained by the following formula:
Figure PCTCN2022143597-appb-000004
Figure PCTCN2022143597-appb-000004
在一种可能的实施方式中,f(x;α)=<0|U H(x;α)HU(x;α)|0>,x是训练数据,H为观测量,U H(x;α)为U(α)的共轭转置,U(α)对应量子计算层中用于执行特定算法的量子计算的部分。当然在其它可能的实施方式中,也可以采用其它方法求损失函数相对于参数的梯度,例如有限差分法。 In a possible implementation, f(x; α)=<0|U H (x; α)HU(x; α)|0>, x is the training data, H is the observed quantity, U H (x ; α) is the conjugate transpose of U(α), and U(α) corresponds to the part of the quantum computing layer that is used to perform quantum computing of a specific algorithm. Of course, in other possible implementation manners, other methods may also be used to obtain the gradient of the loss function relative to the parameters, such as the finite difference method.
在一种可能的实施方式中,参见图10,机器学习模型的量子计算层的量子线路运行于真实的量子芯片时,对于仅包含量子线路的纯量子机器学习模型,为训练该模型,先输入数据,以使得量子芯片所在的量子计算机获取训练数据,然后在真实芯片上运行量子线路进行正向计算,也即正向运算机器学习模型,对量子计算层也即量子层的结果量子态进行测量得到量子层输出的测量值,然后根据测量值进行损失函数的计算,之后为计算梯度,可以采用上述参数位移的方式,在真实芯片上运行量子线路进行梯度计算,根据量子线路分别在参数加上和减去偏置量的计算结果计算梯度,然后根据梯度函数优化参数值,最终得到量子线路的可变参数的参数值,若不满足预设条件,例如此时损失函数的值大于阈值,则可以重复上述过程继续对可变参数进行优化,直至预设条件满足得到符合要求的机器学习模型为止。In a possible implementation, referring to Figure 10, when the quantum circuits of the quantum computing layer of the machine learning model run on a real quantum chip, for a pure quantum machine learning model that only includes quantum circuits, in order to train the model, first input Data, so that the quantum computer where the quantum chip is located can obtain training data, and then run the quantum circuit on the real chip for forward calculation, that is, the forward calculation machine learning model, and measure the quantum state of the quantum computing layer, that is, the result of the quantum layer Get the measured value output by the quantum layer, and then calculate the loss function according to the measured value, and then calculate the gradient. You can use the above parameter displacement method to run the quantum circuit on the real chip for gradient calculation. Add Calculate the gradient by summing and subtracting the calculation result of the offset, and then optimize the parameter value according to the gradient function, and finally obtain the parameter value of the variable parameter of the quantum circuit. If the preset condition is not satisfied, for example, the value of the loss function is greater than the threshold at this time, then The above process can be repeated to continue optimizing the variable parameters until the preset conditions are met and a machine learning model meeting the requirements is obtained.
在一种可能的实施方式中,参见图11,机器学习模型为神经网络模型,该模型包括经典神经网络模块,量子计算模块A和量子线路模块B,其中经典神经网络模块为用于实现经典计算的经典计算层,量子计算模块A和量子线路模块B为用于实现量子计算的量子计算层。在正向运算时,将输入数据输入经典神经网络模块和量子计算模块A,再将其输出输入量子线路模块B,最后根据量子线路B的输出计算损失函数。之后为训练模型,进入反向运算流程,计算量子线路模块B的参数梯度、经典神经网络模块的参数梯度以及量子计算模块A的参数梯度,并用梯度下降法分别更新这些模块中的参数。In a possible implementation, referring to Fig. 11, the machine learning model is a neural network model, which includes a classical neural network module, a quantum computing module A and a quantum circuit module B, wherein the classical neural network module is used to realize classical computing The classical computing layer, the quantum computing module A and the quantum circuit module B are the quantum computing layers for realizing quantum computing. In the forward operation, the input data is input into the classical neural network module and the quantum computing module A, and then the output is input into the quantum circuit module B, and finally the loss function is calculated according to the output of the quantum circuit B. Afterwards, for the training model, enter the reverse operation process, calculate the parameter gradient of the quantum circuit module B, the parameter gradient of the classical neural network module and the parameter gradient of the quantum computing module A, and use the gradient descent method to update the parameters in these modules respectively.
图12是根据一示例性实施例示出的一种基于机器学习框架***的模型训练方法的另一流程图,如图12所示,该方法包括:Fig. 12 is another flowchart of a model training method based on a machine learning framework system according to an exemplary embodiment. As shown in Fig. 12, the method includes:
S121,调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型。S121. Call the data structure module to acquire input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model.
S122,将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层。S122. Input the tensor data into the machine learning model, and call the classic module to create a training layer of the machine learning model.
S123,以所述损失函数、所述机器学习模型的参数、以及基于该参数计算得到所述损失函数过程中的每一子步骤的输出结果为计算节点,并以该损失函数、该参数、该子步骤的输出结果之间的关系作为边,构建有向的计算图。S123, using the loss function, the parameters of the machine learning model, and the output results of each sub-step in the process of calculating the loss function based on the parameters as calculation nodes, and using the loss function, the parameters, and the The relationship between the output results of sub-steps is used as edges to construct a directed calculation graph.
S124,将所述机器学习模型的输出结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值。S124. Input the output result of the machine learning model into the loss function layer, so as to calculate the value of the loss function of the machine learning model.
S125,在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度;基于所述梯度和梯度下降算法更新所述机器学习模型的参数。S125. When it is determined that the value of the loss function does not meet the preset condition, input the value of the loss function into the optimizer layer, so as to calculate the relative value of the loss function based on the value of the loss function and the calculation graph. Gradients of parameters of the machine learning model; updating parameters of the machine learning model based on the gradient and a gradient descent algorithm.
S126,确定更新所述参数后的所述机器学习模型的所述损失函数的值。S126. Determine a value of the loss function of the machine learning model after the parameter is updated.
S127,在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。S127. When it is determined that the value of the loss function satisfies the preset condition, use the machine learning model after updating the parameters as the machine learning model after training.
步骤S121和步骤S122可以分别参考步骤S21和步骤S22。步骤S124至步骤S127可以分别参见步骤S231至步骤S234。Step S121 and step S122 may refer to step S21 and step S22 respectively. Steps S124 to S127 may refer to steps S231 to S234 respectively.
在步骤S123中,计算图是一种有向无环图,以数据结构的图(graph)的形式表示各数据之间的关系,由计算节点和边组成,在构建上述计算图时,按照机器学习模型正向计算的顺序,将其损失函数、参数、以及机器学习模型正向计算过程中通过参数计算得到输出结果及损失函数的每一子步骤的输出结果作为计算节点,并将其计算关系作为边,得到上述计算图,该计算图中,前驱节点的数据为其后继节点的数据的因变量。In step S123, the calculation graph is a directed acyclic graph, which represents the relationship between each data in the form of a data structure graph (graph), and is composed of computing nodes and edges. When constructing the above computing graph, according to the machine The order of the forward calculation of the learning model, the loss function, parameters, and the output results obtained through parameter calculation during the forward calculation of the machine learning model and the output results of each sub-step of the loss function are used as calculation nodes, and the calculation relationship As an edge, the above-mentioned computation graph is obtained, in which the data of the predecessor node is the dependent variable of the data of the successor node.
进而在步骤S125中,根据计算图示出的计算关系,基于损失函数的值计算损失函数相对于参数的梯度。Furthermore, in step S125, according to the calculation relationship shown in the calculation diagram, the gradient of the loss function relative to the parameters is calculated based on the value of the loss function.
可选地,如图13所示,基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度,包括:Optionally, as shown in FIG. 13 , calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function and the calculation graph includes:
S1251,确定所述计算图中所述损失函数对应计算节点至所述机器学习模型的参数对应计算节点的路径。S1251. Determine a path from the calculation node corresponding to the loss function in the calculation graph to the calculation node corresponding to the parameter of the machine learning model.
S1252,基于所述损失函数的值计算该路径上非叶子节点的每一计算节点相对于该计算节点的前驱节点的中间梯度。S1252. Calculate, based on the value of the loss function, the intermediate gradient of each calculation node that is not a leaf node on the path relative to the predecessor node of the calculation node.
S1253,将计算得到的所有所述中间梯度相乘得到所述损失函数相对于所述参数的梯 度。S1253. Multiply all the calculated intermediate gradients to obtain the gradient of the loss function relative to the parameter.
在步骤S1251中,可以以损失函数为起点,选择的参数为终点,确定两者之间的最短路径。进而在步骤S1252中,对于该路径上的每一个计算节点,计算该计算节点相对于其前驱节点的中间梯度,由前驱节点的计算节点记为叶子节点,没有前驱节点的计算节点记为非叶子节点,由于非叶子节点没有前驱节点,故非叶子节点无法计算相应的中间梯度,且非叶子节点一般为参数,作为路径的终点也无需计算梯度。In step S1251, the loss function may be used as a starting point and the selected parameters may be used as an end point to determine the shortest path between the two. Furthermore, in step S1252, for each computing node on the path, the intermediate gradient of the computing node relative to its predecessor nodes is calculated, and the computing node of the predecessor node is marked as a leaf node, and the computing node without a predecessor node is marked as a non-leaf node Since non-leaf nodes have no predecessor nodes, non-leaf nodes cannot calculate the corresponding intermediate gradients, and non-leaf nodes are generally parameters, and there is no need to calculate gradients as the end of the path.
计算得到中间梯度后,执行步骤S1253,将前述路径对应的所有中间梯度相乘,根据链式求导法则即可得到损失函数相对于其参数的梯度,链式求导法则如上文所述,在此不再展开赘述。After calculating the intermediate gradient, execute step S1253 to multiply all the intermediate gradients corresponding to the aforementioned paths, and obtain the gradient of the loss function relative to its parameters according to the chain derivation rule. The chain derivation rule is as described above, in This will not be repeated here.
举例来讲,参见图14,其对应机器学***方差之和,将y作为loss的前驱节点,同时在loss对应计算节点中保存y的信息和梯度计算公式,通过nodes[0]:prev:y和df:grad_fn_of_y()表示。由于不需要计算loss相对于标签数据label的梯度,故label可以不作为计算节点加入计算图中。具体地,可以通过链表存储前驱节点和后继节点的关系,通过数组存储一个计算节点的多个前驱节点的信息。For example, referring to FIG. 14, the corresponding machine learning model includes a quantum computing layer qvc and a classical computing layer fc. In this calculation graph, there are multiple tensor data Qtensors, where the tensor data corresponding to the training data is input, and the tensor data k is obtained by the formula k=input/255, so input is used as the precursor node of k, and at k The corresponding calculation node saves the information of its predecessor node input, represented by nodes[0]:prev:input, and saves the gradient calculation formula of k relative to input, represented by df:grad_fun_of_input. According to param and k, x=qvc(k) can be calculated, qvc represents the quantum circuit of the quantum computing layer of the machine learning model, and param is a parameter in the quantum computing layer, so k and param are used as the precursor nodes of x, and at x The information of param and the gradient calculation formula are saved in the corresponding calculation node, which are represented by nodes[0]:prev:param and df:grad_fun_of_q_param respectively, and the information of k and the gradient calculation formula are saved, and the information of k and the gradient calculation formula are stored by nodes[1]:prev:k and df:grad_fun_of_q_k. According to x, calculate y=fc(x), fc represents the classic calculation program of the classic calculation layer of the machine learning model, for example, it can be a fully connected layer, and its output y is calculated by x, W, b, W and b are classic The parameters of the calculation layer, for example, the calculation formula corresponding to fc() can be y=W*x+b, so W, b and x in the form of tensor data are used as the precursor nodes of y, and at the same time, W is saved in the calculation node corresponding to y The information and gradient calculation formula are expressed by nodes[0]:prev:W and df:grad_fun_of_W(), and the information of b and the gradient calculation formula are saved, expressed by nodes[1]:prev:b and df:grad_fun_of_b(), Save the information of x and the gradient calculation formula, represented by nodes[2]:prev:x and df:grad_fun_of_x(). The value of the loss function loss=LossFunc(y, label) can be calculated through y, and LossFunc is the loss function, for example, it can be the sum of the square difference between y and label, and y is used as the precursor node of loss, and at the same time, the corresponding calculation node of loss Save the information of y and the gradient calculation formula in , represented by nodes[0]:prev:y and df:grad_fn_of_y(). Since there is no need to calculate the gradient of the loss relative to the label data label, the label may not be added to the calculation graph as a calculation node. Specifically, a linked list may be used to store the relationship between the predecessor node and the successor node, and an array may be used to store information of multiple predecessor nodes of a computing node.
在需要计算梯度时,以计算loss相对于param的梯度为例,首先确定loss至param的路径,依次遍历loss、y、x、param对应计算节点,根据其保存的梯度函数和前驱节点的信息,计算loss相对于y的中间梯度,y相对于x的中间梯度,以及x相对于param的中间梯度,最后将这3个中间梯度相乘即可得到loss相对于param的梯度。When it is necessary to calculate the gradient, take the calculation of the gradient of loss relative to param as an example, first determine the path from loss to param, and traverse the calculation nodes corresponding to loss, y, x, and param in turn, and according to the stored gradient function and the information of the predecessor node, Calculate the intermediate gradient of loss relative to y, the intermediate gradient of y relative to x, and the intermediate gradient of x relative to param, and finally multiply these three intermediate gradients to obtain the gradient of loss relative to param.
如前所述,可以通过调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型。在一个实施例中,还可以通过创建有训练接口的量子计算层和经典计算层来创建机器学习模型,具体可参见图15。As mentioned above, the input training data can be obtained by calling the data structure module, creating tensor data including the training data, and calling the quantum module and/or the classical module to create a machine learning model. In one embodiment, a machine learning model can also be created by creating a quantum computing layer and a classical computing layer with a training interface, see FIG. 15 for details.
图15是根据一示例性实施例示出的创建机器学习模型的又一流程图。参见图15,本实施例提供的创建机器学习模型的又一实施例可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***包括量子模块32和经典模块32, 并且创建机器学习模型具体可以包括:Fig. 15 is another flowchart of creating a machine learning model according to an exemplary embodiment. Referring to FIG. 15 , another embodiment of creating a machine learning model provided by this embodiment can be applied to an electronic device including the first machine learning framework system 30 as shown in FIG. 3 , and the first machine learning framework system includes a quantum module 32 and classic module 32, and creating a machine learning model may specifically include:
S1501,调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序。S1501. Call the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, and the first training interface is used to call the first preset training program to train the Quantum machine learning programs.
S1502,调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序。S1502, calling the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, the second training interface is used to call the second preset training program to train the Classic machine learning programs.
S1503,调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。S1503. Call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
具体来讲,第一机器学习框架***30集成了众多用于创建和训练机器学习模型的函数集,通过其定义的接口可以方便的调用这些函数实现对机器学习模型的相关操作。如图3所示,所述第一机器学习框架***30可以包括:Specifically, the first machine learning framework system 30 integrates numerous function sets for creating and training machine learning models, and these functions can be conveniently called through the defined interfaces to implement related operations on the machine learning models. As shown in Figure 3, the first machine learning framework system 30 may include:
量子模块32,被配置为创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;The quantum module 32 is configured to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, and the first training interface is used to call the first preset training program to train the Quantum machine learning programs;
经典模块33,被配置为创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;The classic module 33 is configured to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the Classical machine learning programs;
所述经典模块33还被配置为创建包括所述量子计算层和所述经典计算层的机器学习模型。The classical module 33 is further configured to create a machine learning model including the quantum computing layer and the classical computing layer.
具体来讲,量子机器学习程序是用于实现量子机器学习任务或其子任务的程序,例如可以是实现量子神经网络的程序,也可以是实现量子神经网络中的量子全连接层或量子池化层、量子卷积层等的程序。量子机器学习程序可以通过调用量子模块32创建,可以通过量子模块32创建各种作用于量子比特的量子逻辑门构成量子机器学习程序。量子机器学习程序可以包含各种参数,例如可以是含参量子逻辑门的参数。Specifically, a quantum machine learning program is a program used to realize a quantum machine learning task or its subtasks, for example, it may be a program for realizing a quantum neural network, or it may be a quantum fully connected layer or quantum pooling in a quantum neural network layers, quantum convolutional layers, etc. A quantum machine learning program can be created by calling the quantum module 32, and various quantum logic gates acting on qubits can be created through the quantum module 32 to form a quantum machine learning program. Quantum machine learning programs can contain various parameters, such as parameters containing parametric quantum logic gates.
第一预设训练程序是预先创建好的程序,可以用于完成训练量子机器学习程序的所有或部分步骤,例如可以包括用于计算量子机器学习程序的输出相对于量子机器学习程序的输入或参数的梯度的程序。第一训练接口用于调用第一预设训练程序,第一训练接口可以包括一个函数名,以及用于传入第一预设训练程序的一个或多个参数。通过输入第一训练接口的函数名和相关参数,可以实现对第一预设训练接口的调用,例如第一训练接口的函数名可以为_grad_x(),其参数可以包括量子机器学习程序的参数数量、量子比特数量、经典比特数量等,可以在函数名的括号内输入参数。通过输入第一训练接口的函数名,可以根据第一训练接口的函数名在量子计算层对应的执行程序中寻找对应的第一预设训练程序,当找到时,将参数传入第一预设训练程序,并直接执行第一预设训练程序即可。The first preset training program is a pre-created program that can be used to complete all or part of the steps of training the quantum machine learning program, for example, it can include calculating the output of the quantum machine learning program relative to the input or parameters of the quantum machine learning program gradient program. The first training interface is used to call the first preset training program, and the first training interface may include a function name and one or more parameters for passing in the first preset training program. By inputting the function name and related parameters of the first training interface, the call to the first preset training interface can be realized. For example, the function name of the first training interface can be _grad_x(), and its parameters can include the number of parameters of the quantum machine learning program , the number of qubits, the number of classical bits, etc., you can enter parameters in the brackets of the function name. By inputting the function name of the first training interface, the corresponding first preset training program can be found in the execution program corresponding to the quantum computing layer according to the function name of the first training interface, and when found, the parameters are passed into the first preset training program, and directly execute the first preset training program.
量子计算层是封装有量子机器学习程序和其它程序,如上述第一预设训练程序,的程序模块,其通过按照一定标准对量子机器学习程序和其它程序进行封装,使得在创建和训练包含量子机器学习程序的机器学习模型时,便于实现相关功能。The quantum computing layer is a program module that encapsulates quantum machine learning programs and other programs, such as the first preset training program above. When the machine learning model of the machine learning program, it is convenient to realize related functions.
具体来讲,经典机器学习程序是用于实现经典机器学习任务或其子任务的程序,例如可以是实现经典神经网络的程序,也可以是实现经典神经网络中的全连接层或池化层、卷积层等的程序。经典机器学习程序可以通过调用机器学习框架***的相关接口和程序进行 创建,例如可以通过各种数学运算或逻辑运算的接口和程序等创建经典机器学习程序。经典机器学习程序可以包含各种参数,例如可以是经典的全连接层的权重矩阵。Specifically, a classic machine learning program is a program used to implement a classic machine learning task or its subtasks, for example, it may be a program for implementing a classic neural network, or it may be a program for implementing a fully connected layer or a pooling layer in a classic neural network, Programs for convolutional layers, etc. Classical machine learning programs can be created by calling the relevant interfaces and programs of the machine learning framework system, for example, classic machine learning programs can be created through interfaces and programs of various mathematical or logical operations. Classical machine learning programs can contain various parameters, such as the weight matrix that can be a classic fully connected layer.
第二预设训练程序是预先创建好的程序,可以用于完成训练经典机器学习程序的所有或部分步骤,例如可以包括用于计算经典机器学习程序的输出相对于经典机器学习程序的输入或参数的梯度的程序。第二训练接口用于调用第二预设训练程序,第二训练接口可以包括一个函数名,以及用于传入第二预设训练程序的一个或多个参数。通过输入第二训练接口的函数名和相关参数,可以实现对于第二预设训练接口的调用,例如第二训练接口的函数名也可以为_grad_x(),其参数可以包括经典机器学习程序的参数数量等,可以在函数名的括号内输入参数。通过输入第二训练接口的函数名,可以根据第二训练接口的函数名在经典计算层对应的执行程序中寻找对应的第二预设训练程序,当找到时,将参数传入第二预设训练程序,并直接执行第二预设训练程序即可。需要说明的是,第一训练接口和第二训练接口的函数名相同时,为在调用时进行区分,可以结合经典计算层或量子计算层的名称进行调用,例如量子计算层名称为Quantumlayer,则调用第一训练接口可以输入Quantumlayer._grad_x()。The second preset training program is a pre-created program that can be used to complete all or part of the steps of training a classic machine learning program, for example, it can include computing the output of a classic machine learning program relative to the input or parameters of a classic machine learning program gradient program. The second training interface is used to call the second preset training program, and the second training interface may include a function name and one or more parameters for passing in the second preset training program. By inputting the function name and related parameters of the second training interface, the call to the second preset training interface can be realized. For example, the function name of the second training interface can also be _grad_x(), and its parameters can include parameters of classic machine learning programs Quantity, etc., parameters can be entered within the parentheses of the function name. By inputting the function name of the second training interface, you can search for the corresponding second preset training program in the execution program corresponding to the classic computing layer according to the function name of the second training interface, and when found, pass the parameters into the second preset training program, and directly execute the second preset training program. It should be noted that when the function names of the first training interface and the second training interface are the same, in order to distinguish when calling, they can be called in combination with the name of the classical computing layer or the quantum computing layer. For example, the name of the quantum computing layer is Quantumlayer, then Calling the first training interface may input Quantumlayer._grad_x().
经典计算层是封装有经典机器学习程序和其它程序,如上述第二预设训练程序,的程序模块,其通过按照一定标准对经典机器学习程序和其它程序进行封装,使得在创建和训练包含经典机器学习程序的机器学习模型时,便于实现相关功能。The classic computing layer is a program module encapsulated with classic machine learning programs and other programs, such as the second preset training program mentioned above. By encapsulating classic machine learning programs and other programs according to certain standards, the classic When the machine learning model of the machine learning program, it is convenient to realize related functions.
此外,经典模块33还可以用于创建机器学习模型,其可以通过对量子计算层和经典计算层进行封装得到包括量子计算层和经典计算层的混合量子和经典的机器学习模型。In addition, the classical module 33 can also be used to create a machine learning model, which can obtain a hybrid quantum and classical machine learning model including the quantum computing layer and the classical computing layer by encapsulating the quantum computing layer and the classical computing layer.
在步骤S1501中,可以通过输入创建量子计算层的接口,来创建量子计算层,例如其接口可以为QuantumLayer(qprog_with_meansure,para_num,machine_type_or_cloud_token,num_of_qubits,num_of_cbits,diff_method,delta),其括号内各项为参数。qprog_with_meansure为量子机器学习程序,para_num为参数数量,machine_type_or_cloud_token为量子机器学习程序所运行的电子设备类型,例如可以为真实的量子计算机或虚拟机,num_of_qubits,num_of_cbits分别为量子机器学习程序运行所需的量子比特数量和经典比特数量,diff_method为训练量子机器学习程序时的梯度计算方法,例如可以为有限差分法,delta为计算梯度采用前述有限差分法时的步长。将量子机器学习程序作为上述接口的参数,并输入上述接口,可以在机器学习框架***的量子模块对应的文件库中查找上述接口对应的程序,例如在查找到接口名称一样的程序时,确定查找到该接口对应的程序,进而将作为参数的量子机器学习程序传入该程序并执行该程序,创建包括量子机器学习程序的量子计算层。在其他可能的实施方式中,也可以通过其它的接口创建包含特定的量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,例如特定的量子机器学习程序可以是预先建立好的量子全连接层等。In step S1501, the quantum computing layer can be created by inputting the interface for creating the quantum computing layer. For example, the interface can be QuantumLayer(qprog_with_meansure, para_num, machine_type_or_cloud_token, num_of_qubits, num_of_cbits, diff_method, delta), and the items in the brackets are parameters . qprog_with_meansure is the quantum machine learning program, para_num is the number of parameters, machine_type_or_cloud_token is the type of electronic equipment that the quantum machine learning program runs, for example, it can be a real quantum computer or a virtual machine, num_of_qubits, num_of_cbits are the quantum required for the quantum machine learning program to run The number of bits and the number of classical bits, diff_method is the gradient calculation method when training the quantum machine learning program, for example, it can be the finite difference method, and delta is the step size when the aforementioned finite difference method is used to calculate the gradient. Use the quantum machine learning program as the parameter of the above interface, and input the above interface, you can search for the program corresponding to the above interface in the file library corresponding to the quantum module of the machine learning framework system, for example, when you find a program with the same interface name, confirm the search to the program corresponding to the interface, and then pass the quantum machine learning program as a parameter into the program and execute the program to create a quantum computing layer including the quantum machine learning program. In other possible implementations, it is also possible to create a quantum computing layer that includes a specific quantum machine learning program, a first preset training program, and a first training interface through other interfaces. For example, a specific quantum machine learning program can be pre-established Good quantum fully connected layers, etc.
当然,在执行创建量子计算层的接口对应的程序创建量子计算层时,还会自动在量子计算层中封装第一训练接口和与该第一训练接口对应的第一预设训练程序,进而便于在训练机器学习模型时直接通过第一训练接口调用第一预设训练程序对量子机器学习程序进行训练。Certainly, when executing the program corresponding to the interface for creating the quantum computing layer to create the quantum computing layer, the first training interface and the first preset training program corresponding to the first training interface will be automatically encapsulated in the quantum computing layer, thereby facilitating When training the machine learning model, the first preset training program is directly invoked through the first training interface to train the quantum machine learning program.
在步骤S1502,可以通过输入创建经典计算层的接口,来创建经典计算层,可以通过调用经典模块33创建经典机器学习程序,并将经典机器学习程序作为前述接口的参数,输入该接口,可以在第一机器学习框架***30的经典模块33对应的文件库中查找上述接 口对应的程序,例如在查找到接口名称一样的程序时,确定查找到该接口对应的程序,进而将作为参数的经典机器学习程序传入该程序并执行该程序,创建包括经典机器学习程序的经典计算层。在其他可能的实施方式中,也可以通过其它的接口创建包含特定的经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,例如特定的经典机器学习程序可以是预先建立好的全连接层、卷积层、池化层等。In step S1502, the classic computing layer can be created by inputting the interface for creating the classic computing layer, and the classic machine learning program can be created by calling the classic module 33, and the classic machine learning program can be used as a parameter of the aforementioned interface. In the file library corresponding to the classic module 33 of the first machine learning framework system 30, search for the program corresponding to the above-mentioned interface. A learning program is passed into the program and executed, creating a classical computing layer including a classical machine learning program. In other possible implementations, a classical computing layer including a specific classical machine learning program, a second preset training program, and a second training interface can also be created through other interfaces. For example, a specific classical machine learning program can be pre-established Good fully connected layers, convolutional layers, pooling layers, etc.
当然,在执行创建经典计算层的接口对应的程序创建经典计算层时,还会自动在经典计算层中封装第二训练接口和与该第二训练接口对应的第二预设训练程序,进而便于在训练机器学习模型时直接通过第二训练接口调用第二预设训练程序对经典机器学习程序进行训练。Of course, when the program corresponding to the interface for creating the classic computing layer is executed to create the classic computing layer, the second training interface and the second preset training program corresponding to the second training interface will be automatically encapsulated in the classic computing layer, thereby facilitating When training the machine learning model, the second preset training program is directly invoked through the second training interface to train the classic machine learning program.
创建好量子计算层和经典计算层后,进入执行步骤S1503,可以通过调用经典模块33对量子计算层和经典计算层进行封装,创建包括量子计算层和经典计算层的机器学习模型。例如可以通过如下代码创建机器学习模型:After the quantum computing layer and the classical computing layer are created, enter the execution step S1503, the quantum computing layer and the classical computing layer can be packaged by calling the classical module 33, and a machine learning model including the quantum computing layer and the classical computing layer can be created. For example, you can create a machine learning model with the following code:
class Model(Module):class Model(Module):
def__init__(self):def__init__(self):
super(Model,self).__init__()super(Model, self).__init__()
self.qvc=QuantumLayer(Qcircuit,3,"cpu",4,1)self.qvc=QuantumLayer(Qcircuit,3,"cpu",4,1)
self.fc=Linear(7,2)self.fc = Linear(7, 2)
def forward(self,x):def forward(self,x):
x=self.qvc(x)x = self.qvc(x)
x=self.fc(x)x = self.fc(x)
return xreturn x
该机器学习模型包括了一个量子计算层和一个经典计算层,其中,量子计算层通过接口QuantumLayer(Qcircuit,3,"cpu",4,1)创建,Qcircuit为量子机器学习程序,3为参数的数量,"cpu"表示量子机器学习程序运行于虚拟机,4表示量子机器学习程序所需的量子比特的数量,1表示量子机器学习程序所需的经典比特的数量,创建该量子计算层时,自动在量子计算层中封装有第一训练接口和第一预设训练程序。通过接口Linear(7,2)创建了一个包含经典的全连接层、第二训练接口和第二预设训练程序的经典计算层,7表示该全连接层的输入特征数据的数量为7个,2表示该全连接层的输出特征数据的数量为2个。The machine learning model includes a quantum computing layer and a classical computing layer, wherein the quantum computing layer is created through the interface QuantumLayer(Qcircuit,3,"cpu",4,1), Qcircuit is a quantum machine learning program, and 3 is a parameter Quantity, "cpu" indicates that the quantum machine learning program runs on a virtual machine, 4 indicates the number of qubits required by the quantum machine learning program, and 1 indicates the number of classical bits required by the quantum machine learning program. When creating this quantum computing layer, The first training interface and the first preset training program are automatically encapsulated in the quantum computing layer. Through the interface Linear(7, 2), a classic computing layer including the classic fully connected layer, the second training interface and the second preset training program is created. 7 means that the number of input feature data of the fully connected layer is 7, 2 means that the number of output feature data of the fully connected layer is 2.
此外,该机器学习模型还包括了正向运行的程序,通过接口forward(self,x)调用后续的程序实现,便于正向运行该机器学习模型。需要说明的是,步骤S1501和步骤S1502相互的执行先后顺序并不限定,只要其在步骤S1503之前执行即可。In addition, the machine learning model also includes a forward-running program, and the subsequent program implementation is invoked through the interface forward(self,x), so as to facilitate the forward-running of the machine learning model. It should be noted that, the execution order of step S1501 and step S1502 is not limited, as long as they are executed before step S1503.
基于上述技术方案,对于量子机器学习程序,可以创建包括该量子机器学习程序的量子计算层,并且创建好的该量子计算层可以直接包含有用于训练该量子机器学习程序的第一预设训练程序,以及用于调用该第一预设训练程序的第一训练接口,并同理对于经典机器学习程序,可以创建包括该经典机器学习程序、用于训练该经典机器学习程序的第二预设训练程序、用于调用该第二预设训练程序的第二训练接口,进而对于创建好的包括该量子计算层和经典计算层的机器学习模型,在训练时,可以直接通过第一训练接口和第二训练接口分别调用第一预设训练程序和第二预设训练程序,以实现对于机器学习模型的训练,可以减少针对量子机器学习程序和经典机器学习程序的训练程序的创建工作,更节省了为训练量子机器学习程序和训练经典机器学习程序所需的配合工作,提高了对包含量子计算层和经典计算层的机器学习模型的训练的开发效率。Based on the above technical solution, for a quantum machine learning program, a quantum computing layer including the quantum machine learning program can be created, and the created quantum computing layer can directly contain a first preset training program for training the quantum machine learning program , and the first training interface for calling the first preset training program, and similarly for the classic machine learning program, you can create a second preset training interface that includes the classic machine learning program and is used to train the classic machine learning program Program, the second training interface for calling the second preset training program, and then for the created machine learning model including the quantum computing layer and the classical computing layer, during training, you can directly pass the first training interface and the second training interface The two training interfaces respectively call the first preset training program and the second preset training program to realize the training of the machine learning model, which can reduce the creation of training programs for quantum machine learning programs and classical machine learning programs, and save more time. The coordinated work required for training quantum machine learning programs and training classical machine learning programs improves the development efficiency of training machine learning models that include both quantum and classical computing layers.
图16是根据一示例性实施例示出的创建机器学习模型的又一流程图。参见图16,本实施例提供的创建机器学习模型的又一实施例可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***30包括量子模块32和经典模块33,并且创建机器学习模型具体可以包括:Fig. 16 is another flowchart of creating a machine learning model according to an exemplary embodiment. Referring to FIG. 16, another embodiment of creating a machine learning model provided by this embodiment can be applied to an electronic device including a first machine learning framework system 30 as shown in FIG. 3, and the first machine learning framework system 30 includes a quantum Module 32 and classic module 33, and creating a machine learning model can specifically include:
S1601,调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序。S1601. Call the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, and the first training interface is used to call the first preset training program to train the Quantum machine learning programs.
S1602,调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序。S1602. Call the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, and the second training interface is used to call the second preset training program to train the Classic machine learning programs.
S1603,调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。S1603. Call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
S1604,调用所述经典模块创建所述机器学习模型的训练层,所述训练层用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。S1604, call the classic module to create a training layer of the machine learning model, the training layer is used to call the first preset training program through the first training interface, and call the preset training program through the second training interface Describe the second preset training program.
其中,步骤S1601至步骤S1603可以参见前述步骤S1501至步骤S1503。Wherein, step S1601 to step S1603 may refer to the aforementioned step S1501 to step S1503.
在步骤S1604中,为训练机器学习模型,可以创建训练层,训练层中可以预设有用于调用第一训练接口和第二训练接口的接口,进而通过该接口可以调用第一训练接口和第二训练接口,并通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。而无需在训练层中创建相关的训练程序,减少对于包含量子计算层和经典计算层的机器学习模型的训练的开发量。在一个实施例中,还可以通过含有接口的训练机器学习模型,具体可以参见图17。In step S1604, in order to train the machine learning model, a training layer can be created, and an interface for calling the first training interface and the second training interface can be preset in the training layer, and then the first training interface and the second training interface can be called through this interface. A training interface, calling the first preset training program through the first training interface, and calling the second preset training program through the second training interface. There is no need to create related training programs in the training layer, reducing the amount of development for the training of machine learning models including quantum computing layers and classical computing layers. In one embodiment, the machine learning model can also be trained through an interface, see FIG. 17 for details.
图17是根据一示例性实施例示出的训练机器学习模型的又一流程图。参见图17,本实施例提供的训练机器学习模型的又一实施例可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***30包括数据结构模块31、量子模块32和经典模块33,所述方法包括:Fig. 17 is another flow chart of training a machine learning model according to an exemplary embodiment. Referring to FIG. 17 , another embodiment of the training machine learning model provided by this embodiment can be applied to an electronic device including the first machine learning framework system 30 as shown in FIG. 3 , and the first machine learning framework system 30 includes data Structure module 31, quantum module 32 and classical module 33, described method comprises:
S1701,调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序。S1701. Call the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program, and a first training interface, and the first training interface is used to call the first preset training program to train the Quantum machine learning programs.
S1702,调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序。S1702, calling the classic module to create a classic computing layer including a classic machine learning program, a second preset training program, and a second training interface, the second training interface is used to call the second preset training program to train the Classic machine learning programs.
S1703,调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。S1703. Call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
S1704,调用所述经典模块创建所述机器学习模型的训练层,所述训练层用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。S1704, call the classic module to create a training layer of the machine learning model, the training layer is used to call the first preset training program through the first training interface, and call the preset training program through the second training interface Describe the second preset training program.
S1705,调用所述数据结构模块获取输入数据并创建包括所述输入数据的张量数据。S1705. Call the data structure module to acquire input data and create tensor data including the input data.
S1706,将所述张量数据输入所述机器学习模型,得到输出结果。S1706. Input the tensor data into the machine learning model to obtain an output result.
S1707,将所述输出结果输入所述训练层,以通过所述第一训练接口调用所述第一预 设训练程序,根据所述第一预设训练程序训练所述量子机器学习程序,并通过所述第二训练接口调用所述第二预设训练程序,根据所述第二预设训练程序训练所述经典机器学习程序,得到训练后的机器学习模型。S1707. Input the output result into the training layer, so as to call the first preset training program through the first training interface, train the quantum machine learning program according to the first preset training program, and pass The second training interface invokes the second preset training program, and trains the classic machine learning program according to the second preset training program to obtain a trained machine learning model.
其中,步骤S1701至步骤S1703可以参见前述步骤S1501至步骤S1503,步骤S1704可以参见步骤S1604。进一步地,关于数据结构模块31、量子模块32和经典模块33的更多细节可以参考前文描述的内容,本公开在此不再赘述。Wherein, step S1701 to step S1703 may refer to the aforementioned step S1501 to step S1503, and step S1704 may refer to step S1604. Further, for more details about the data structure module 31 , the quantum module 32 and the classical module 33 , reference may be made to the content described above, which will not be repeated in this disclosure.
在一种可能的实施方式中,数据结构模块31可以用于将输入数据按照预设的数据结构排列以创建用于输入所述机器学习模型的张量数据,以及创建以所述预设的数据结构排列且数值确定的用于输入所述机器学习模型的张量数据。进而在步骤S1705中,对于输入的输入数据,可以将输入数据按照预设的数据结构排列以得到张量数据,输入数据可以作为张量数据的一部分存储。例如获取的输入数据为1,2,3,可以将输入的该输入数据转化为向量结构[1,2,3]作为张量数据的一部分。需要说明的是,输入数据可以是用于训练机器学习模型的数据,也可以是待预测分类的数据。需要说明的是,步骤S1705并不限制在步骤S1704之后,只要在步骤S1706前执行即可,步骤S1706也并不限制在步骤S1704之后,只要步骤S1703和步骤S1705执行后,即可执行步骤S1706。其中,关于张量数据的更多细节可以参见前文描述的内容,本公开在此不再赘述。In a possible implementation, the data structure module 31 can be used to arrange the input data according to a preset data structure to create tensor data for inputting the machine learning model, and create the preset data Tensor data that is structured and numerically determined for input to the machine learning model. Furthermore, in step S1705, for the input data, the input data may be arranged according to a preset data structure to obtain tensor data, and the input data may be stored as a part of the tensor data. For example, the acquired input data is 1, 2, 3, and the input data can be converted into a vector structure [1, 2, 3] as part of the tensor data. It should be noted that the input data may be the data used to train the machine learning model, or the data to be predicted and classified. It should be noted that step S1705 is not limited to be performed after step S1704, as long as it is performed before step S1706, and step S1706 is not limited to be performed after step S1704, as long as it is performed after steps S1703 and S1705 are performed. Wherein, more details about the tensor data can refer to the content described above, and the present disclosure will not repeat them here.
在步骤S1706中,将所述张量数据输入所述机器学习模型,以正向运行所述机器学习模型,得到机器学习模型对张量数据的输出结果。然后执行步骤S1707,将输出结果输入训练层,通过第一训练接口和第二训练接口调用第一预设训练程序和第二预设训练程序,执行这两个预设训练程序直接对量子机器学习程序和经典机器学习程序进行训练,或者结合其它训练程序对量子机器学习程序和经典机器学习程序进行训练,得到符合预期的机器学习模型作为训练后的机器学习模型。In step S1706, the tensor data is input into the machine learning model to run the machine learning model forward to obtain an output result of the machine learning model on the tensor data. Then execute step S1707, input the output result into the training layer, call the first preset training program and the second preset training program through the first training interface and the second training interface, and execute these two preset training programs to directly learn the quantum machine Programs and classical machine learning programs, or combined with other training programs to train quantum machine learning programs and classical machine learning programs, and obtain expected machine learning models as the trained machine learning models.
可选地,所述第一预设训练程序包括用于计算所述量子机器学习程序的输出相对于所述量子机器学习程序的第一因变量的梯度的第一梯度计算程序,所述第一因变量为所述量子机器学习程序的输入或参数。Optionally, the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the first dependent variable of the quantum machine learning program, the first The dependent variable is an input or parameter to the quantum machine learning program.
具体来讲,第一预设训练程序可以包括用于计算梯度的第一梯度计算程序,例如量子机器学习程序为y=f(x;θ),其中y为输出,x为输入,θ为参数,f()表示量子机器学习程序的映射关系,则该梯度可以是量子机器学习程序的输出y相对于量子机器学习的输入x的梯度,也可以是量子机器机器学习程序的输出y相对于量子机器学习的参数θ的梯度。当然,在一种可能的实施方式中,第一预设训练程序可以同时包括用于计算量子机器学习程序的输出相对于量子机器学习程序的输入的梯度的第一输入梯度计算程序,和用于计算量子机器学习程序的输出相对于量子机器学习程序的参数的梯度的第一参数梯度计算程序。进而可以根据需要选择使用第一输入梯度计算程序或第一参数梯度计算程序。例如采用自动微分基于链式求导法则计算梯度时,若待求梯度的量子机器学习程序处于链式求导步骤的中间,则可以选用第一输入梯度计算程序计算量子机器学习程序的输出相对于其输入的梯度,若待求梯度的量子机器学习程序处于链式求导步骤的最后,则可以选用第一参数梯度计算程序计算量子机器学习程序的输出相对于其参数的梯度。Specifically, the first preset training program may include a first gradient calculation program for calculating gradients, for example, a quantum machine learning program is y=f(x; θ), where y is the output, x is the input, and θ is the parameter , f() represents the mapping relationship of the quantum machine learning program, then the gradient can be the gradient of the output y of the quantum machine learning program relative to the input x of the quantum machine learning, or the output y of the quantum machine learning program relative to the quantum The gradient of the parameter θ for machine learning. Of course, in a possible implementation manner, the first preset training program may simultaneously include a first input gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the input of the quantum machine learning program, and a first input gradient calculation program for A first parameter gradient calculation procedure that calculates a gradient of an output of the quantum machine learning program with respect to a parameter of the quantum machine learning program. Furthermore, the first input gradient calculation program or the first parameter gradient calculation program can be selected to be used as required. For example, when automatic differentiation is used to calculate the gradient based on the chain derivation rule, if the quantum machine learning program to be gradient is in the middle of the chain derivation step, the first input gradient calculation program can be selected to calculate the output of the quantum machine learning program relative to For the gradient of its input, if the quantum machine learning program whose gradient is to be obtained is at the end of the chain derivation step, the first parameter gradient calculation program can be selected to calculate the gradient of the output of the quantum machine learning program relative to its parameters.
可选地,所述第二预设训练程序包括用于计算所述经典机器学习程序的输出相对于所述经典机器学习程序的第二因变量的梯度的第二梯度计算程序,所述第二因变量为所述经典机器学习程序的输入或参数。Optionally, the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the second The dependent variable is the input or parameter of the classical machine learning program.
具体来讲,第二预设训练程序可以包括用于计算梯度的第二梯度计算程序,例如经典机器学习程序对应映射关系为z=g(p;i),其中z为输出,p为输入,i为参数,g()表示经典机器学习程序的映射关系,则该梯度可以是经典机器学习程序的输出z相对于经典机器学习的输入p的梯度,也可以是经典机器机器学习程序的输出y相对于经典机器学习的参数i的梯度。当然,在一种可能的实施方式中,第二预设训练程序可以同时包括用于计算经典机器学习程序的输出相对于经典机器学习程序的输入的梯度的第二输入梯度计算程序,和用于计算经典机器学习程序的输出相对于经典机器学习程序的参数的梯度的第二参数梯度计算程序。进而可以根据需要选择使用第二输入梯度计算程序或第二参数梯度计算程序。例如采用自动微分基于链式求导法则计算梯度时,若待求梯度的经典机器学习程序处于链式求导步骤的中间,则可以选用第二输入梯度计算程序计算经典机器学习程序的输出相对于其输入的梯度,若待求梯度的经典机器学习程序处于链式求导步骤的最后,则可以选用第二参数梯度计算程序计算经典机器学习程序的输出相对于其参数的梯度。Specifically, the second preset training program may include a second gradient calculation program for calculating gradients, for example, the corresponding mapping relationship of a classical machine learning program is z=g(p; i), where z is the output, p is the input, i is a parameter, g() represents the mapping relationship of a classic machine learning program, then the gradient can be the gradient of the output z of the classic machine learning program relative to the input p of the classic machine learning, or the output y of the classic machine learning program Gradient with respect to parameter i in classical machine learning. Of course, in a possible implementation manner, the second preset training program may simultaneously include a second input gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the input of the classic machine learning program, and a second input gradient calculation program for A second parameter gradient calculation procedure that calculates a gradient of an output of the classical machine learning program with respect to a parameter of the classical machine learning program. Furthermore, the second input gradient calculation program or the second parameter gradient calculation program can be selected to be used as required. For example, when automatic differentiation is used to calculate the gradient based on the chain derivation rule, if the classic machine learning program to be gradient is in the middle of the chain derivation step, the second input gradient calculation program can be selected to calculate the output of the classic machine learning program relative to For the gradient of its input, if the classic machine learning program whose gradient is to be calculated is at the end of the chain derivation step, the second parameter gradient calculation program can be selected to calculate the gradient of the output of the classic machine learning program relative to its parameters.
可选地,输出结果输入训练层后,可以先基于输出结果计算机器学习模型的损失函数,在确定所述损失函数的值不满足预设条件时,例如损失函数的值大于阈值时,表示机器学习模型的输出结果与符合预期的标签数据的差别较大,因此需要对机器学习模型进行优化。进而可以基于损失函数的值更新机器学习模型的参数,也即更新量子计算层和经典计算层的参数,得到训练后的机器学习模型。Optionally, after the output result is input into the training layer, the loss function of the machine learning model can be calculated based on the output result, and when it is determined that the value of the loss function does not meet the preset condition, for example, when the value of the loss function is greater than a threshold, it indicates that the machine The output of the learning model is quite different from the expected label data, so the machine learning model needs to be optimized. Furthermore, the parameters of the machine learning model can be updated based on the value of the loss function, that is, the parameters of the quantum computing layer and the classical computing layer can be updated to obtain a trained machine learning model.
具体来讲,为对机器学***方即平方损失函数得到,也可以计算该输出结果与标签数据的二元交叉熵(Binary Cross Entropy)作为损失函数。Specifically, in order to optimize the machine learning model, the gradient of the loss function relative to the parameters of the quantum machine learning program can be calculated based on the above-mentioned first preset training program, and the gradient of the loss function relative to the parameters of the classical machine can be calculated based on the above-mentioned second preset training program. Gradients for the parameters of the learning program. For example, the machine learning model includes a quantum computing layer and a classical computing layer, the quantum machine learning program of the quantum computing layer is the above-mentioned y=f(x; θ), and the classical machine learning program of the classical computing layer is the above-mentioned z=g(p; i), and y is used as p to input the classical machine learning program z=g(p; i), and z is the output result of the machine learning model. Then, in order to find the gradient of the loss function relative to the parameter i, the loss function can be calculated according to z, and then the first gradient of the loss function relative to the output z of the classical machine learning program can be calculated, and the output z can be calculated based on the second preset training program relative to The second gradient of the parameter i, the gradient of the loss function relative to the output parameter i is obtained by multiplying the first gradient and the second gradient. In order to find the gradient of the loss function relative to the parameter θ, the first gradient of the loss function relative to the output z can be calculated, the third gradient of the output z relative to the output y as the dependent variable p can be calculated based on the second preset training procedure, and based on The first preset training program calculates the fourth gradient of the output y relative to the parameter θ, and then multiplies the first gradient, the third gradient and the fourth gradient to obtain the gradient of the loss function relative to the parameter θ. The loss function can be obtained by calculating the square of the difference between the output result of the machine learning model and the label data, that is, the square loss function, or the binary cross entropy (Binary Cross Entropy) between the output result and the label data can be calculated as the loss function.
计算出损失函数相对于量子机器学习程序和经典机器学习程序的参数的梯度后,可以将该梯度带入梯度下降算法以对参数进行更新,例如可以采用随机梯度下降算法(Stochastic Gradient Descent,SGD),自适应梯度算法(Adaptive gradient algorithm,Adagrad)、自适应矩估计(Adaptive Moment Estimation,Adam)等算法中的任意一种,当然还可以采用其它的算法更新机器学习模型的参数。计算更新所述参数后的所述机器学习模型的所述损失函数的值,在确定损失函数的值满足预设条件时例如损失函数的值小于阈值时,将更新参数后的机器学习模型作为训练后的机器学习模型。若更新参数后,机器学习模型的损失函数的值不满足预设条件,则可以返回执行前述计算损失函数等用于更新机器学习模型参数的步骤,直至更新参数后得到的损失函数的值满足预设条件。After calculating the gradient of the loss function relative to the parameters of the quantum machine learning program and the classical machine learning program, the gradient can be brought into the gradient descent algorithm to update the parameters, for example, the stochastic gradient descent algorithm (Stochastic Gradient Descent, SGD) can be used , Adaptive gradient algorithm (Adaptive gradient algorithm, Adagrad), adaptive moment estimation (Adaptive Moment Estimation, Adam) and other algorithms, of course, other algorithms can also be used to update the parameters of the machine learning model. Calculating the value of the loss function of the machine learning model after updating the parameters, and using the machine learning model after updating the parameters as training subsequent machine learning models. If after updating the parameters, the value of the loss function of the machine learning model does not meet the preset conditions, you can return to the aforementioned steps for updating the parameters of the machine learning model, such as calculating the loss function, until the value of the loss function obtained after updating the parameters meets the preset conditions. set conditions.
可选地,所述第一梯度计算程序用于根据从所述第一训练接口传入的梯度计算方法参数,选择有限差分法或参数位移法计算所述梯度。Optionally, the first gradient calculation program is configured to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
具体来讲,可以将梯度计算方法参数从第一训练接口传入,进而根据输入的梯度计算方法参数确定计算梯度的方法,例如梯度计算方法参数为finite_diff时,选择有限差分法(finite difference method)计算前述梯度,梯度计算方法参数为parameter_shift时,选择参数位移法计算前述梯度。Specifically, the gradient calculation method parameters can be passed in from the first training interface, and then the gradient calculation method can be determined according to the input gradient calculation method parameters. For example, when the gradient calculation method parameter is finite_diff, select the finite difference method (finite difference method) Calculate the aforementioned gradient. When the gradient calculation method parameter is parameter_shift, select the parameter shift method to calculate the aforementioned gradient.
可选地,如图18所示,在所述根据所述第一训练接口传入的梯度计算方法参数,选择参数位移法计算所述梯度时,所述第一梯度计算程序1800可以包括:Optionally, as shown in FIG. 18 , when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method passed in from the first training interface, the first gradient calculation program 1800 may include:
参数减子程序1810,用于将所述量子机器学习程序的所述第一因变量减去第一偏置量,并将目标数据输入所述量子机器学习程序,得到第一结果;The parameter subtraction subroutine 1810 is used to subtract the first bias from the first dependent variable of the quantum machine learning program, and input the target data into the quantum machine learning program to obtain the first result;
参数加子程序1820,用于将所述量子机器学习程序的所述第一因变量加上第二偏置量,并将目标数据输入所述量子机器学习程序,得到第二结果;The parameter adding subroutine 1820 is used to add the first dependent variable of the quantum machine learning program to the second bias, and input the target data into the quantum machine learning program to obtain the second result;
计算子程序1830,用于根据所述第一结果和所述第二结果计算所述梯度;calculation subroutine 1830, for calculating the gradient according to the first result and the second result;
其中,所述目标数据为输入所述量子机器学习程序的数据。Wherein, the target data is the data input into the quantum machine learning program.
具体来讲,选择参数位移法时,对应的第一梯度计算程序1800包括参数减子程序1810、参数加子程序1820和计算子程序1830,将张量数据输入机器学习模型前向计算过程中,用于输入量子机器学习程序的数据为目标数据。以第一因变量为量子机器学习程序的参数为例,在执行第一梯度计算程序70时,执行参数减子程序1810将量子机器学习程序的参数减去第一偏置量,例如减去
Figure PCTCN2022143597-appb-000005
然后将目标数据输入量子机器学习程序,得到量子机器学习程序输出的第一结果,同理执行参数加子程序1820将量子机器学习程序的参数加上第二偏置量例如
Figure PCTCN2022143597-appb-000006
得到量子机器学习程序输出的第二结果,最后执行计算子程序1830,可以计算第一结果和第二结果之差的二分之一作为量子机器学习程序的输出相对于参数的梯度,整个过程可以利用如下公式计算:
Specifically, when the parameter displacement method is selected, the corresponding first gradient calculation program 1800 includes a parameter subtraction subroutine 1810, a parameter addition subroutine 1820, and a calculation subroutine 1830, and tensor data is input into the forward calculation process of the machine learning model, The data used as input to the quantum machine learning program is the target data. Taking the first dependent variable as the parameter of the quantum machine learning program as an example, when the first gradient calculation program 70 is executed, the parameter subtraction subroutine 1810 is executed to subtract the first offset from the parameter of the quantum machine learning program, such as subtracting
Figure PCTCN2022143597-appb-000005
Then input the target data into the quantum machine learning program to obtain the first result output by the quantum machine learning program, and similarly execute the parameter adding subroutine 1820 to add the parameters of the quantum machine learning program to the second offset, for example
Figure PCTCN2022143597-appb-000006
Obtain the second result output by the quantum machine learning program, and finally execute the calculation subroutine 1830, which can calculate 1/2 of the difference between the first result and the second result as the gradient of the output of the quantum machine learning program relative to the parameter, and the whole process can be Calculate using the following formula:
Figure PCTCN2022143597-appb-000007
Figure PCTCN2022143597-appb-000007
其中,
Figure PCTCN2022143597-appb-000008
为量子机器学习程序的输出相对于参数的梯度,f(x;θ i)为量子机器学习程序,x为目标数据,θ i为参数。当然,在其它可能的实施方式中,第一偏置量和第二偏置量可以不同,且可以采用其它数值,视具体情况而定。并且基于第一结果和第二结果计算梯度也可以采用其它方法,例如在上述计算公式的基础上加上修正量,本公开对此不做具体限制。对于参数减子程序1810和参数加子程序1820的执行顺序,本公开也不做具体限制,可以根据具体情况而定。采用参数位移法可以在量子机器学习程序运行于真实的量子计算机时计算相应的梯度,相对于部分不能应用于真实量子计算机的梯度计算方法,拓宽其使用范围。
in,
Figure PCTCN2022143597-appb-000008
is the gradient of the output of the quantum machine learning program relative to the parameters, f(x; θ i ) is the quantum machine learning program, x is the target data, and θ i is the parameter. Certainly, in other possible implementation manners, the first offset amount and the second offset amount may be different, and other values may be used, depending on specific circumstances. Moreover, other methods may also be used to calculate the gradient based on the first result and the second result, for example, adding a correction amount on the basis of the above calculation formula, which is not specifically limited in the present disclosure. The present disclosure does not make specific limitations on the execution order of the parameter subtraction subroutine 1810 and the parameter addition subroutine 1820 , which may be determined according to specific circumstances. Using the parameter shift method can calculate the corresponding gradient when the quantum machine learning program is running on a real quantum computer, which broadens its scope of use compared with some gradient calculation methods that cannot be applied to real quantum computers.
基于上述图15描述的创建机器学习模型的实施例,本公开还提供相应的创建机器学习模型的装置,该装置可以包括量子计算层创建模块,用于调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;经典计算层创建模块,用于调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;机器学习模型创建模块,用于调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。Based on the embodiment of creating a machine learning model described in FIG. 15 above, the present disclosure also provides a corresponding device for creating a machine learning model. The device may include a quantum computing layer creation module for calling the quantum module to create a quantum machine learning program. , the quantum computing layer of the first preset training program and the first training interface, the first training interface is used to call the first preset training program to train the quantum machine learning program; the classical computing layer creation module is used for Calling the classic module to create a classic computing layer including a classic machine learning program, a second preset training program and a second training interface, the second training interface is used to call the second preset training program to train the classic machine A learning program; a machine learning model creation module, used to call the classical module to create a machine learning model including the quantum computing layer and the classical computing layer.
可选地,上述装置还包括:训练层创建模块,用于调用所述经典模块创建所述机器学习模型的训练层,所述训练层用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。Optionally, the above device further includes: a training layer creation module, configured to call the classic module to create a training layer of the machine learning model, and the training layer is used to call the first preset through the first training interface. A training program is set, and the second preset training program is invoked through the second training interface.
可选地,所述第一机器学习框架***还包括数据结构模块,上述装置还包括:张量数据创建模块,用于调用所述数据结构模块获取输入数据并创建包括所述输入数据的张量数据;输入模块,用于将所述张量数据输入所述机器学习模型,得到输出结果;训练模块,用于将所述输出结果输入所述训练层,以通过所述第一训练接口调用所述第一预设训练程序,根据所述第一预设训练程序训练所述量子机器学习程序,并通过所述第二训练接口调用所述第二预设训练程序,根据所述第二预设训练程序训练所述经典机器学习程序,得到训练后的机器学习模型。Optionally, the first machine learning framework system further includes a data structure module, and the above device further includes: a tensor data creation module, configured to call the data structure module to obtain input data and create a tensor including the input data data; an input module, configured to input the tensor data into the machine learning model to obtain an output result; a training module, configured to input the output result into the training layer, so as to call the first training interface through the first training interface The first preset training program, train the quantum machine learning program according to the first preset training program, and call the second preset training program through the second training interface, according to the second preset The training program trains the classic machine learning program to obtain a trained machine learning model.
可选地,所述第一预设训练程序包括用于计算所述量子机器学习程序的输出相对于所述量子机器学习程序的第一因变量的梯度的第一梯度计算程序,所述第一因变量为所述量子机器学习程序的输入或参数。Optionally, the first preset training program includes a first gradient calculation program for calculating the gradient of the output of the quantum machine learning program relative to the first dependent variable of the quantum machine learning program, the first The dependent variable is an input or parameter to the quantum machine learning program.
可选地,所述第一梯度计算程序用于根据从所述第一训练接口传入的梯度计算方法参数,选择有限差分法或参数位移法计算所述梯度。Optionally, the first gradient calculation program is configured to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method input from the first training interface.
可选地,在所述根据所述第一训练接口传入的梯度计算方法参数,选择参数位移法计算所述梯度时,所述第一梯度计算程序包括:参数减子程序,用于将所述量子机器学习程序的所述第一因变量减去第一偏置量,并将目标数据输入所述量子机器学习程序,得到第一结果;参数加子程序,用于将所述量子机器学习程序的所述第一因变量加上第二偏置量,并将目标数据输入所述量子机器学习程序,得到第二结果;计算子程序,用于根据所述第一结果和所述第二结果计算所述梯度;其中,所述目标数据为输入所述量子机器学习程序的数据。Optionally, when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes: a parameter subtraction subroutine for The first dependent variable of the quantum machine learning program is subtracted from the first offset, and the target data is input into the quantum machine learning program to obtain the first result; the parameter adding subroutine is used for learning the quantum machine The first dependent variable of the program is added with a second bias, and the target data is input into the quantum machine learning program to obtain a second result; the calculation subroutine is used to calculate the second result based on the first result and the second As a result, the gradient is calculated; wherein the target data is the data input into the quantum machine learning program.
可选地,所述第二预设训练程序包括用于计算所述经典机器学习程序的输出相对于所述经典机器学习程序的第二因变量的梯度的第二梯度计算程序,所述第二因变量为所述经典机器学习程序的输入或参数。Optionally, the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the second The dependent variable is the input or parameter of the classical machine learning program.
关于上述实施例中的装置,其中各个模块执行操作的具体方式已经在有关该方法的实施例中进行了详细描述,此处将不做详细阐述说明。Regarding the apparatus in the foregoing embodiments, the specific manner in which each module executes operations has been described in detail in the embodiments related to the method, and will not be described in detail here.
在一个实施例中,还可以通过创建含噪量子计算层,以创建机器学习模型,具体可参见图19。In one embodiment, a machine learning model can also be created by creating a noise-containing quantum computing layer, see FIG. 19 for details.
图19是根据一示例性实施例示出的含噪声机器学习模型创建方法的流程示意图,应用于包括量子模块和经典模块的第一机器学习框架***的电子装置,所述量子模块包括含噪量子程序封装单元,并且含噪声机器学习模型创建方法可以包括:Fig. 19 is a schematic flowchart of a method for creating a noise-containing machine learning model according to an exemplary embodiment, which is applied to an electronic device of a first machine learning framework system including a quantum module and a classical module, and the quantum module includes a noise-containing subprogram Encapsulated units, and methods for creating machine learning models with noise can include:
步骤1910:基于所述第一机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序;Step 1910: Create a quantum program considering the influence of noise based on the quantum computing programming library included in the first machine learning framework system;
步骤1920:将所述量子程序作为已封装的含噪量子计算层接口的参数,以及传入所述含噪量子计算层接口;Step 1920: use the quantum program as a parameter of the encapsulated noisy quantum computing layer interface, and pass it into the noisy quantum computing layer interface;
步骤1930:调用所述含噪量子程序封装单元通过所述含噪量子计算层接口创建含噪量子计算层;Step 1930: call the noisy quantum program encapsulation unit to create a noisy quantum computing layer through the noisy quantum computing layer interface;
步骤1940:调用所述经典模块创建包括所述含噪量子计算层的机器学习模型。Step 1940: call the classical module to create a machine learning model including the noise-containing quantum computing layer.
其中,第一机器学习框架***内含的量子计算编程库例如可以为Qpanda、Qsikit、Cirq、 Forest、Q#、qbsolv、Blackbird等,在此不做限定。Wherein, the quantum computing programming library included in the first machine learning framework system can be, for example, Qpanda, Qsikit, Cirq, Forest, Q#, qbsolv, Blackbird, etc., which are not limited here.
其中,量子计算是一种遵循量子力学规律调控量子信息单元进行计算的新型计算模式,借助量子叠加和量子纠缠的两个现象,能够同时处理信息的多个状态。量子计算层为包含量子线路的程序模块,可以用于实现对应量子线路的量子计算,通过对量子线路按照一定的标准进行封装,使得量子计算层便于在创建和训练机器学习模型时进行使用。对于机器学习模型通过量子计算实现的部分,均可以理解为对应的量子计算层。Among them, quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of two phenomena of quantum superposition and quantum entanglement, multiple states of information can be processed simultaneously. The quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
其中,经典计算是一种遵循经典物理学规律调控经典信息单元进行计算的传统计算模式,它通过一个二进制***工作,即信息使用1或0来存储。经典计算层与量子计算层相对应,其可以对创建好的经典计算程序按照一定标准进行封装,使得经典计算层便于在创建和训练机器学习模型时进行使用。Among them, classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0. The classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing programs according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
其中,接口(interface)是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为。含噪量子计算层接口即为含噪声量子计算层对应的一系列方法的声明,具体形式例如可以为NoiseQuantumLayer(),所述含噪量子计算层接口用于提供基于所述第一机器学习框架***内含的量子计算编程库创建的考虑噪声影响的量子程序;下述提及的其他接口也可参照本处解释,不再赘述。Among them, an interface is a declaration of a series of methods, and it is a collection of some method characteristics. An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behavior. The noise-containing quantum computing layer interface is a declaration of a series of methods corresponding to the noise-containing quantum computing layer. The specific form can be, for example, NoiseQuantumLayer(). The included quantum computing programming library creates a quantum program that considers the influence of noise; other interfaces mentioned below can also be explained here, and will not be repeated here.
具体的,所述第一机器学习框架***还包括数据结构模块,所述调用所述经典模块创建包括所述含噪量子计算层的机器学习模型,包括:Specifically, the first machine learning framework system also includes a data structure module, and the calling of the classical module to create a machine learning model including the noise-containing quantum computing layer includes:
调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述含噪量子计算层之间的前向传播关系;Calling the classical module to construct a classical computing layer and calling the data structure module to construct a forward propagation relationship between the classical computing layer and the noisy quantum computing layer;
调用所述经典模块将所述经典计算层、所述含噪量子计算层以及所述经典计算层与所述含噪量子计算层之间的前向传播关系进行封装,得到机器学习模型。Invoking the classical module to encapsulate the classical computing layer, the noisy quantum computing layer, and the forward propagation relationship between the classical computing layer and the noisy quantum computing layer to obtain a machine learning model.
其中,前向传播为将上一计算层的输出作为下一计算层的输入,并计算下一计算层的输出,一直运算到没有下一计算层为止,其中计算层可以为上述的经典计算层也可以为上述的量子计算层。Among them, the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
可选的,所述经典模块还包括抽象类子模块,所述调用所述经典模块将所述经典计算层、所述含噪量子计算层以及所述经典计算层与所述含噪量子计算层之间的前向传播关系进行封装,得到机器学习模型,包括:Optionally, the classical module further includes an abstract class submodule, and the calling of the classical module combines the classical computing layer, the noisy quantum computing layer, and the classical computing layer with the noisy quantum computing layer The forward propagation relationship between is encapsulated to obtain a machine learning model, including:
调用所述抽象类子模块基于初始化函数对所述含噪量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述含噪量子计算层和所述经典计算层;calling the abstract class submodule to initialize and encapsulate the noisy quantum computing layer and the classical computing layer based on an initialization function, and obtain the initialized and encapsulated noisy quantum computing layer and the classical computing layer;
调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;Calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship;
调用所述抽象类子模块基于模块类对所述初始化和封装后的所述含噪量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。The abstract class submodule is called to encapsulate the initialized and encapsulated noisy quantum computing layer and the classical computing layer, as well as the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
其中,初始化函数为_init_(),前向传播函数为forward()。Among them, the initialization function is _init_(), and the forward propagation function is forward().
基于初始化函数对所述含噪量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述含噪量子计算层和所述经典计算层,那么可以如下:Based on the initialization function, the noisy quantum computing layer and the classical computing layer are initialized and packaged to obtain the initialized and packaged noisy quantum computing layer and the classical computing layer, then it can be as follows:
def_init_(self):def_init_(self):
super(Net,self)._init_()super(Net, self)._init_()
self.conv1=Conv2D(a)self.conv1 = Conv2D(a)
self.maxpool=Maxpool2D(b)self.maxpool = Maxpool2D(b)
self.conv2=Conv2D(c)self.conv2 = Conv2D(c)
self.maxpool2=Maxpool2D(d)self.maxpool2 = Maxpool2D(d)
self.fc1=Linear(e)self.fc1 = Linear(e)
self.fc2=Linear(f)self.fc2 = Linear(f)
self.hybrid=NoiseQuantumLayer(g)self.hybrid = NoiseQuantumLayer(g)
self.fc3=Linear(h)self.fc3 = Linear(h)
基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系,那么可以如下:Encapsulating the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship can be as follows:
def forward(self,x):def forward(self, x):
x=self.conv1(x)x = self.conv1(x)
x=self.maxpool1(x)x = self.maxpool1(x)
x=self.conv2(x)x = self.conv2(x)
x=maxpool2(x)x = maxpool2(x)
x=self.fc1(x)x = self.fc1(x)
x=self.fc2(x)x = self.fc2(x)
x=self.hybrid(x)x = self. hybrid(x)
x=self.fc3(x)x = self.fc3(x)
return xreturn x
基于模块类对所述初始化和封装后的所述含噪量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型,那么可以如下:Based on the module class, the initialized and encapsulated noise-containing quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship are encapsulated to obtain a machine learning model, then it can be as follows:
class Net(Module):class Net(Module):
def_init_(self):def_init_(self):
super(Net,self)._init_()super(Net, self)._init_()
self.conv1=Conv2D(a)self.conv1 = Conv2D(a)
self.maxpool=Maxpool2D(b)self.maxpool = Maxpool2D(b)
self.conv2=Conv2D(c)self.conv2 = Conv2D(c)
self.maxpool2=Maxpool2D(d)self.maxpool2 = Maxpool2D(d)
self.fc1=Linear(e)self.fc1 = Linear(e)
self.fc2=Linear(f)self.fc2 = Linear(f)
self.hybrid=NoiseQuantumLayer(g)self.hybrid = NoiseQuantumLayer(g)
self.fc3=Linear(h)self.fc3 = Linear(h)
def forward(self,x):def forward(self, x):
x=self.conv1(x)x = self.conv1(x)
x=self.maxpool1(x)x = self.maxpool1(x)
x=self.conv2(x)x = self.conv2(x)
x=maxpool2(x)x = maxpool2(x)
x=self.fc1(x)x = self.fc1(x)
x=self.fc2(x)x = self.fc2(x)
x=self.hybrid(x)x = self. hybrid(x)
x=self.fc3(x)x = self.fc3(x)
return xreturn x
与现有技术相比,本公开通过基于机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序;然后将量子程序作为已封装的含噪量子计算层接口的参数,以及传入含噪量子计算层接口;最后调用含噪量子程序封装单元通过含噪量子计算层接口创建含噪量子计算层;以及调用经典模块创建包括含噪量子计算层的机器学习模型。本公开通过 调用含噪量子程序封装单元即实现了含噪声机器学习模型的创建,该机器学习模型含有噪声,因此在量子虚拟机上模拟出的结果更加贴近真实量子计算机上计算出的结果;除此之外,通过该含噪量子计算层接口创建含噪量子计算层,当模拟的真实量子计算机不同时,可以通过改变该含噪量子计算层接口的参数——考虑噪声影响的量子程序即可,不需要改变机器学习模型的其他部分,使得该含噪声的机器学习模型易于移植和复制,也进一步提高了该含噪声机器学习模型的易用性。Compared with the prior art, this disclosure creates a quantum program that considers the influence of noise through the quantum computing programming library based on the machine learning framework system; A noise-containing quantum computing layer interface; finally calling a noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling a classical module to create a machine learning model including a noise-containing quantum computing layer. This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit. The machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except In addition, the noisy quantum computing layer is created through the noisy quantum computing layer interface. When the simulated real quantum computer is different, the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
在本公开提供的一实施例中,所述基于所述第一机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序,包括:In an embodiment provided by the present disclosure, the creation of a quantum program considering the influence of noise based on the quantum computing programming library included in the first machine learning framework system includes:
基于所述第一机器学习框架***内含的量子计算编程库申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;Applying for a noise-containing quantum virtual machine based on the quantum computing programming library contained in the first machine learning framework system, and setting the noise of the quantum circuit running on the noise-containing virtual machine;
申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;Applying for qubits and creating quantum logic gates acting on the qubits to obtain quantum circuits running on the noise-containing virtual machine;
将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序。Encapsulate the noise-containing quantum virtual machine, the noise model and the quantum circuit to obtain a quantum program considering the influence of noise.
其中,所述噪声包括以下至少一种:逻辑门噪声、量子比特的量子态重置噪声、量子比特的测量噪声、量子比特的读取噪声。Wherein, the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
其中,逻辑门噪声的噪声模型类型至少包括以下几种:量子比特的弛豫过程噪声模型、量子比特的退相位过程噪声模型、退相干噪声模型、去极化噪声模型、比特反转噪声模型、比特相位反转噪声模型、相位阻尼噪声模型。Among them, the noise model types of logic gate noise include at least the following types: qubit relaxation process noise model, qubit dephasing process noise model, decoherence noise model, depolarization noise model, bit reversal noise model, Bit phase reversal noise model, phase damping noise model.
在机器学习框架***内含的量子计算编程库中,量子比特的弛豫过程噪声模型可以用DAMPING_KRAUS_OPERATOR表示,量子比特的退相位过程噪声模型可以用DEPHASING_KRAUS_OPERATOR表示,退相干噪声模型可以用DECOHERENCE_KRAUS_OPERATOR表示,去极化噪声模型可以用DEPOLARIZING_KRAUS_OPERATOR表示,比特反转噪声模型可以用BITFLIP_KRAUS_OPERATOR表示,比特相位反转噪声模型可以用BIT_PHASE_FLIP_OPRATOR表示,相位阻尼噪声模型可以用PHASE_DAMPING_OPRATOR表示。In the quantum computing programming library included in the machine learning framework system, the relaxation process noise model of the qubit can be represented by DAMPING_KRAUS_OPERATOR, the dephasing process noise model of the qubit can be represented by DEPHASING_KRAUS_OPERATOR, and the decoherence noise model can be represented by DECOHERENCE_KRAUS_OPERATOR, to The polarization noise model can be represented by DEPOLARIZING_KRAUS_OPERATOR, the bit inversion noise model can be represented by BITFLIP_KRAUS_OPERATOR, the bit phase inversion noise model can be represented by BIT_PHASE_FLIP_OPRATOR, and the phase damping noise model can be represented by PHASE_DAMPING_OPRATOR.
其中,量子比特的弛豫过程噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and expression method of the relaxation process noise model of the qubit are as follows:
Figure PCTCN2022143597-appb-000009
Figure PCTCN2022143597-appb-000009
其中,量子比特的退相位过程噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and expression method of the dephasing process noise model of the qubit are as follows:
Figure PCTCN2022143597-appb-000010
Figure PCTCN2022143597-appb-000010
其中,退相干噪声模型为上述两种噪声模型的综合,他们的关系如下:Among them, the decoherence noise model is a combination of the above two noise models, and their relationship is as follows:
Figure PCTCN2022143597-appb-000011
Figure PCTCN2022143597-appb-000011
K 1=K 1dampingK 1dephasing,K 2=K 1dampingK 2dephasing K 1 =K 1damping K 1dephasing ,K 2 =K 1damping K 2dephasing
K 3=K 2dampingK 1dephasing,K 4=K 2dampingK 2dephasing K 3 =K 2damping K 1dephasing ,K 4 =K 2damping K 2dephasing
其中,去极化噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and expression method of the depolarization noise model are as follows:
Figure PCTCN2022143597-appb-000012
Figure PCTCN2022143597-appb-000012
Figure PCTCN2022143597-appb-000013
Figure PCTCN2022143597-appb-000013
其中,比特反转噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and representation method of the bit-reversal noise model are as follows:
Figure PCTCN2022143597-appb-000014
Figure PCTCN2022143597-appb-000014
其中,比特-相位反转噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and representation method of the bit-phase-reversal noise model are as follows:
Figure PCTCN2022143597-appb-000015
Figure PCTCN2022143597-appb-000015
其中,相位阻尼噪声模型的kraus算符和表示方法如下:Among them, the kraus operator and expression method of the phase damping noise model are as follows:
Figure PCTCN2022143597-appb-000016
Figure PCTCN2022143597-appb-000016
上述各噪声模型类型中K 1、K 2、K 3、K 4为kraus算符,p为该噪声模型类型所需的参数,X、Y、Z、I为其量子逻辑门对应的矩阵。 Among the above noise model types, K 1 , K 2 , K 3 , and K 4 are kraus operators, p is a parameter required by the noise model type, and X, Y, Z, and I are matrices corresponding to quantum logic gates.
具体来讲,申请含噪声量子虚拟机可以通过NoiseQVM qvm实现,在申请含噪声量子虚拟机之后,还可以对该含噪声量子虚拟机进行初始化,初始化可以通过初始化函数qvm.init()实现;设置运行于所述含噪声虚拟机上的量子线路的噪声可以通过接口set_moise_model()实现;申请量子比特可以通过接口qvm.qAllocMany()实现,若需要还可以通过接口qvm.cAllocMany()申请经典比特;最后将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,可以用过函数main()实现。上述的具体方式为其中一种,还可以通过其他函数、接口、类去实现,在此不再举例。Specifically, applying for a noise-containing quantum virtual machine can be realized through NoiseQVM qvm. After applying for a noise-containing quantum virtual machine, the noise-containing quantum virtual machine can also be initialized. The initialization can be realized through the initialization function qvm.init(); set The noise of the quantum circuit running on the noise-containing virtual machine can be realized through the interface set_moise_model(); the application of qubits can be realized through the interface qvm.qAllocMany(), and if necessary, the classical bits can also be applied through the interface qvm.cAllocMany(); Finally, the noise-containing quantum virtual machine, the noise model and the quantum circuit are packaged, which can be realized by the function main(). The above-mentioned specific method is one of them, and it can also be realized through other functions, interfaces, and classes, and no more examples are given here.
在本公开提供的一实施例中,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:In an embodiment provided by the present disclosure, the noise is logic gate noise, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
将指定的量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;Using the specified quantum logic gate type, noise model type, and parameters required by the noise model type as parameters of the logic gate noise interface, and passing in the logic gate noise interface;
通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的所有量子比特生效。The logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the logic gate noise takes effect on all qubits in the quantum circuit.
在本公开提供的一实施例中,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:In an embodiment provided by the present disclosure, the noise is logic gate noise, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
将指定的量子比特、量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;Using the specified qubit, quantum logic gate type, noise model type, and parameters required by the noise model type as parameters of the logic gate noise interface, and passing in the logic gate noise interface;
通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的指定的量子比特生效。The logic gate noise of the quantum circuit running on the noise-containing virtual machine is set through the logic gate noise interface, and the logic gate noise takes effect on specified qubits in the quantum circuit.
其中,指定的量子逻辑门类型例如可以是H门、X门、Y门、RX门等,噪声模型类型如上述量子比特的弛豫过程噪声模型、量子比特的退相位过程噪声模型、退相干噪声模型、去极化噪声模型、比特反转噪声模型、比特相位反转噪声模型、相位阻尼噪声模型等等,噪声模型类型所需的参数即p或其他参数。Among them, the specified quantum logic gate type can be, for example, H gate, X gate, Y gate, RX gate, etc., and the noise model type is such as the above-mentioned qubit relaxation process noise model, qubit dephasing process noise model, decoherence noise model, depolarization noise model, bit-reversed noise model, bit-phase-reversed noise model, phase-damped noise model, etc., the parameters required by the noise model type are p or other parameters.
其中,逻辑门噪声接口例如可以为qvm.set_noise_model();例如qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GA TE,0.1)则只指定了量子逻辑门类型为泡利X门,噪声模型类型为比特反转噪声模型以及所述噪声模型类型所需的参数p为0.1,未指定量子比特,则逻辑门噪声对所述量子线路中的所有量子比特生效。例如Among them, the logic gate noise interface can be, for example, qvm.set_noise_model(); for example, qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GA TE,0.1) only specifies that the quantum logic gate type is Pauli X gate, and the noise model type is bit The parameter p required for inverting the noise model and the type of the noise model is 0.1, and if no qubit is specified, the logic gate noise takes effect on all qubits in the quantum circuit. For example
qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GATE,0.1,[q[0],q[1]])则指定了量子逻辑门类型为泡利X门,噪声模型类型为比特反转 噪声模型以及所述噪声模型类型所需的参数p为0.1,还指定量子比特q[0]和q[1],则逻辑门噪声只对所述量子线路中的q[0]和q[1]生效。qvm.set_noise_model(NoiseModel.BITFLIP_KRAUS_OPERATOR,GateType.PAULI_X_GATE,0.1,[q[0],q[1]]) specifies that the quantum logic gate type is Pauli X gate, the noise model type is bit-reversal noise model and all The parameter p required by the above noise model type is 0.1, and the qubits q[0] and q[1] are also specified, then the logic gate noise is only effective for q[0] and q[1] in the quantum circuit.
在本公开提供的一实施例中,所述噪声为量子比特的测量噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:In an embodiment provided by the present disclosure, the noise is the measurement noise of qubits, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
将指定的噪声模型类型以及所述噪声模型类型所需的参数作为测量噪声接口的参数,以及传入所述测量噪声接口;Using the specified noise model type and the parameters required by the noise model type as parameters of the measurement noise interface, and passing it into the measurement noise interface;
通过所述测量噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的测量噪声。The measurement noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the measurement noise interface.
需要说明的是,测量噪声设置方式与上述逻辑门噪声的设置方法类似,只不过不需要指定量子逻辑门类型,其测量噪声接口可以为qvm.set_measure_error()。It should be noted that the measurement noise setting method is similar to the above logic gate noise setting method, except that there is no need to specify the quantum logic gate type, and the measurement noise interface can be qvm.set_measure_error().
在本公开提供的一实施例中,所述噪声为量子比特的量子态重置噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:In an embodiment provided by the present disclosure, the noise is the quantum state reset noise of the qubit, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
将量子线路中的量子比特的量子态重置到|0>的概率和重置到|1>的概率作为重置噪声接口的参数,以及传入所述重置噪声接口;The probability of resetting the quantum state of the qubit in the quantum circuit to |0> and the probability of resetting to |1> are used as parameters of the reset noise interface, and passed into the reset noise interface;
通过所述重置噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声。The quantum state reset noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the reset noise interface.
举例说明,例如有如下代码:For example, for example, the following code:
p0=0.9p0=0.9
p1=0.05p1=0.05
qvm.set_reset_error(p0,p1)qvm.set_reset_error(p0,p1)
表示将量子线路中的量子比特的量子态重置到|0>的概率p0和重置到|1>的概率p1分别设置为0.9和0.05,未被重置到|0>和重置到|1>的概率则为1-p0-p1=0.05,然后将p0和p1作为重置噪声接口qvm.set_reset_error()的参数传入该接口,通过该重置噪声接口qvm.set_reset_error(p0,p1)即可设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声。Indicates that the probability p0 of resetting the quantum state of the qubit in the quantum circuit to |0> and the probability p1 of resetting to |1> are set to 0.9 and 0.05 respectively, and it is not reset to |0> and reset to | The probability of 1> is 1-p0-p1=0.05, and then pass p0 and p1 as parameters of the reset noise interface qvm.set_reset_error() to this interface, through the reset noise interface qvm.set_reset_error(p0,p1) That is, the quantum state reset noise of the qubits of the quantum circuit running on the noise-containing virtual machine can be set.
在本公开提供的一实施例中,所述噪声为量子比特的读取噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:In an embodiment provided by the present disclosure, the noise is the read noise of the qubit, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
将|0>被读取为|0>的概率以及被读取为|1>的概率、|1>被读取为|0>的概率以及被读取为|1>的概率作为读取噪声接口的参数,以及传入所述读取噪声接口;The probability of |0> being read as |0> and the probability of being read as |1>, the probability of |1> being read as |0> and the probability of being read as |1> as the read noise The parameters of the interface, and the interface for passing in the read noise;
通过所述读取噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的读取噪声。The read noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the read noise interface.
举例说明,例如有如下代码:For example, for example, the following code:
double f0=0.9double f0=0.9
double f1=0.85double f1=0.85
qvm.set_readout_error([[f0,1-f0],[1-f1,f1]],[q[0]])qvm.set_readout_error([[f0,1-f0],[1-f1,f1]],[q[0]])
表示将|0>被读取为|0>的概率f0以及被读取为|1>的概率1-f0分别设置为0.9和0.1;|1>被读取为|0>的概率f1以及被读取为|1>的概率1-f1分贝设置为0.85和0.15,然后将f0、1-f0、f1、1-f1作为读取噪声接口set_readout_error()的参数传入该接口,通过该读取噪声接口qvm.set_readout_error([[f0,1-f0],[1-f1,f1]],[q[0]])即可设置运行于该含噪声虚拟机上的量子线路的量子比特的读取噪声。Indicates that the probability f0 of |0> being read as |0> and the probability 1-f0 of being read as |1> are set to 0.9 and 0.1 respectively; the probability f1 of |1> being read as |0> and The probability of reading |1> 1-f1 is set to 0.85 and 0.15, and then f0, 1-f0, f1, 1-f1 are passed to the interface as parameters of the read noise interface set_readout_error(), through which the read The noise interface qvm.set_readout_error([[f0,1-f0],[1-f1,f1]],[q[0]]) can set the readout of qubits of the quantum circuit running on the noise-containing virtual machine Take the noise.
在上述实施例中,本公开上下文中的量子模块还可以包括含噪量子程序封装单元,被 配置为通过已封装的含噪量子计算层接口创建含噪量子计算层,所述含噪量子计算层接口用于提供基于所述机器学习框架***内含的量子计算编程库创建的考虑噪声影响的量子程序;经典模块还可以被配置为创建包括所述含噪量子计算层的机器学习模型。本公开的机器学习框架***可以包括量子计算编程库,被配置为申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序,其中更多细节可以参考前文的描述。In the above embodiments, the quantum module in the context of the present disclosure may further include a noisy quantum program encapsulation unit configured to create a noisy quantum computing layer through the encapsulated noisy quantum computing layer interface, the noisy quantum computing layer The interface is used to provide a quantum program that considers the influence of noise created based on the quantum computing programming library included in the machine learning framework system; the classical module can also be configured to create a machine learning model including the noise-containing quantum computing layer. The machine learning framework system of the present disclosure may include a quantum computing programming library, which is configured to apply for a noise-containing quantum virtual machine, and set the noise of a quantum circuit running on the noise-containing virtual machine; Quantum logic gates on qubits to obtain quantum circuits running on the noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model and the quantum circuit to obtain a quantum program that considers the influence of noise , for more details, please refer to the previous description.
基于上述图19描述的创建机器学习模型的实施例,本公开还提供相应的创建机器学习模型的装置,该装置可以包括程序创建单元,用于基于所述机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序;接口确定单元,用于将所述量子程序作为已封装的含噪量子计算层接口的参数,以及传入所述含噪量子计算层接口;创建单元,用于调用所述含噪量子程序封装单元通过所述含噪量子计算层接口创建含噪量子计算层;以及调用所述经典模块创建包括所述含噪量子计算层的机器学习模型。Based on the embodiment of creating a machine learning model described in FIG. 19 above, the present disclosure also provides a corresponding device for creating a machine learning model. The device may include a program creation unit for programming based on the quantum computing contained in the machine learning framework system. The library creates a quantum program that considers the influence of noise; the interface determination unit is used to use the quantum program as a parameter of the encapsulated noise-containing quantum computing layer interface, and passes in the noise-containing quantum computing layer interface; the creation unit is used for calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the classical module to create a machine learning model including the noise-containing quantum computing layer.
可选的,在所述基于所述机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序方面,所述程序创建单元,具体用于:基于所述机器学习框架***内含的量子计算编程库申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序。Optionally, in terms of creating a quantum program that considers the influence of noise based on the quantum computing programming library contained in the machine learning framework system, the program creation unit is specifically configured to: The quantum computing programming library applies for a quantum virtual machine with noise, and sets the noise of the quantum circuit running on the virtual machine with noise; applies for qubits and creates quantum logic gates that act on the qubits, and obtains A quantum circuit on a noise-containing virtual machine; encapsulating the noise-containing quantum virtual machine, the noise model, and the quantum circuit to obtain a quantum program considering the influence of noise.
可选的,所述噪声包括以下至少一种:逻辑门噪声、量子比特的量子态重置噪声、量子比特的测量噪声、量子比特的读取噪声。Optionally, the noise includes at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
可选的,所述噪声为逻辑门噪声,在所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的所有量子比特生效。Optionally, the noise is logic gate noise, and in terms of setting the noise of the quantum circuit running on the noise-containing virtual machine, the program creation unit is specifically configured to: set the specified quantum logic gate type, The noise model type and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, the quantum running on the noise-containing virtual machine is set Logic gate noise of the wire, the logic gate noise being effective for all qubits in the quantum wire.
可选的,所述噪声为逻辑门噪声,在所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的量子比特、量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的指定的量子比特生效。Optionally, the noise is logic gate noise. In terms of setting the noise of the quantum circuit running on the noise-containing virtual machine, the program creation unit is specifically used to: set the specified qubit, quantum logic The gate type, the noise model type and the parameters required by the noise model type are used as parameters of the logic gate noise interface, and are passed into the logic gate noise interface; through the logic gate noise interface, it is set to run on the noise-containing virtual machine The logic gate noise of the quantum circuit on , the logic gate noise takes effect on a specified qubit in the quantum circuit.
可选的,在所述噪声为量子比特的量子态重置噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将量子线路中的量子比特的量子态重置到|0>的概率和重置到|1>的概率作为重置噪声接口的参数,以及传入所述重置噪声接口;通过所述重置噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声。Optionally, in the aspect that the noise is the quantum state reset noise of the qubit, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: convert the quantum circuit to The probability of resetting the quantum state of the qubit to |0> and the probability of resetting to |1> are used as the parameters of the reset noise interface, and are passed into the reset noise interface; set by the reset noise interface Quantum state reset noise of qubits of quantum circuits running on the noise-containing virtual machine.
可选的,在所述噪声为量子比特的测量噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将指定的噪声模型类型以及所述噪声模型类型所需的参数作为测量噪声接口的参数,以及传入所述测量噪声接口;通过所述 测量噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的测量噪声。Optionally, in the aspect that the noise is the measurement noise of qubits, and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: use the specified noise model type And the parameters required by the noise model type are used as the parameters of the measurement noise interface, and passed into the measurement noise interface; through the measurement noise interface, the measurement of the quantum bits of the quantum circuit running on the noise-containing virtual machine is set noise.
可选的,在所述噪声为量子比特的读取噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声方面,所述程序创建单元,具体用于:将|0>被读取为|0>的概率以及被读取为|1>的概率、|1>被读取为|0>的概率以及被读取为|1>的概率作为读取噪声接口的参数,以及传入所述读取噪声接口;通过所述读取噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的读取噪声。Optionally, in the aspect that the noise is the read noise of qubits and the noise of the quantum circuit running on the noise-containing virtual machine is set, the program creation unit is specifically configured to: set |0> to be The probability of reading as |0> and the probability of being read as |1>, the probability of |1> being read as |0> and the probability of being read as |1> as parameters of the read noise interface, and Passing in the read noise interface; setting the read noise of the qubits of the quantum circuit running on the noise-containing virtual machine through the read noise interface.
与现有技术相比,本公开通过基于机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序;然后将量子程序作为已封装的含噪量子计算层接口的参数,以及传入含噪量子计算层接口;最后调用含噪量子程序封装单元通过含噪量子计算层接口创建含噪量子计算层;以及调用经典模块创建包括含噪量子计算层的机器学习模型。本公开通过调用含噪量子程序封装单元即实现了含噪声机器学习模型的创建,该机器学习模型含有噪声,因此在量子虚拟机上模拟出的结果更加贴近真实量子计算机上计算出的结果;除此之外,通过该含噪量子计算层接口创建含噪量子计算层,当模拟的真实量子计算机不同时,可以通过改变该含噪量子计算层接口的参数——考虑噪声影响的量子程序即可,不需要改变机器学习模型的其他部分,使得该含噪声的机器学习模型易于移植和复制,也进一步提高了该含噪声机器学习模型的易用性。Compared with the prior art, this disclosure creates a quantum program that considers the influence of noise through the quantum computing programming library based on the machine learning framework system; A noise-containing quantum computing layer interface; finally calling a noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling a classical module to create a machine learning model including a noise-containing quantum computing layer. This disclosure realizes the creation of a noise-containing machine learning model by calling the noise-containing quantum program packaging unit. The machine learning model contains noise, so the simulated results on the quantum virtual machine are closer to the results calculated on the real quantum computer; except In addition, the noisy quantum computing layer is created through the noisy quantum computing layer interface. When the simulated real quantum computer is different, the parameters of the noisy quantum computing layer interface can be changed - the quantum program that considers the influence of noise is enough , without changing other parts of the machine learning model, making the noisy machine learning model easy to transplant and replicate, and further improving the usability of the noisy machine learning model.
在一个实施例中,本公开还提出通过调用兼容量子计算层构建兼容量子计算层,以创建包含兼容量子计算层的机器学习模型,具体参见图20。In one embodiment, the present disclosure also proposes to construct a compatible quantum computing layer by calling the compatible quantum computing layer, so as to create a machine learning model including a compatible quantum computing layer, see FIG. 20 for details.
图20是根据一示例性实施例示出的机器学习模型构建的又一流程示意图。该机器学习模型构建方法应用于包括第一机器学习框架***且不包括第二机器学习框架***的电子装置,所述第一机器学习框架***包括兼容量子计算程序封装单元,并且机器学习模型构建可以包括:Fig. 20 is another schematic flowchart of machine learning model building according to an exemplary embodiment. The machine learning model construction method is applied to an electronic device that includes a first machine learning framework system and does not include a second machine learning framework system. The first machine learning framework system includes a compatible quantum computing program encapsulation unit, and the machine learning model construction can be include:
步骤2010:确定对应的所述第二机器学习框架***的兼容量子计算层接口,所述兼容量子计算层接口用于提供基于所述第二机器学习框架***内含的量子计算编程库创建的量子程序;Step 2010: Determine the compatible quantum computing layer interface of the corresponding second machine learning framework system, and the compatible quantum computing layer interface is used to provide the quantum computer created based on the quantum computing programming library contained in the second machine learning framework system. program;
步骤2020:通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层;Step 2020: constructing a compatible quantum computing layer by calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit;
步骤2030:构建包括所述兼容量子计算层的机器学习模型。Step 2030: Construct a machine learning model including the compatible quantum computing layer.
其中,量子计算是一种遵循量子力学规律调控量子信息单元进行计算的新型计算模式,借助量子的名为叠加和纠缠的两个现象,能够同时处理信息的多个状态。量子计算层为包含量子线路的程序模块,可以用于实现对应量子线路的量子计算,通过对量子线路按照一定的标准进行封装,使得量子计算层便于在创建和训练机器学习模型时进行使用。对于机器学习模型通过量子计算实现的部分,均可以理解为对应的量子计算层。Among them, quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of two quantum phenomena called superposition and entanglement, multiple states of information can be processed simultaneously. The quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
其中,兼容量子计算程序封装单元包括Compatiblelayer接口,该接口的继承接口例如可以有兼容量子计算层接口QiskitLayer,用于基于IBM的第二机器学习框架***内含的量子计算编程库创建的量子程序;还可以有兼容量子计算层接口CirqLayer,用于基于谷歌的第二机器学习框架***内含的量子计算编程库创建的量子程序;还可以有兼容量子计算层接口QuantumLayer,用于基于微软的第二机器学习框架***内含的量子计算编程库创建的量子程序;还可以有其他兼容量子计算层接口,用于基于其他公司的第二机器学习框架***内含的量子计算编程库创建的量子程序,在此不再一一举例。Wherein, the compatible quantum computing program encapsulation unit includes a Compatiblelayer interface, and the inheritance interface of this interface may have, for example, a compatible quantum computing layer interface QiskitLayer, which is used for quantum programs created based on the quantum computing programming library contained in IBM's second machine learning framework system; There can also be a compatible quantum computing layer interface CirqLayer for quantum programs created based on the quantum computing programming library contained in Google's second machine learning framework system; there can also be a compatible quantum computing layer interface QuantumLayer for Microsoft's second Quantum programs created by the quantum computing programming library included in the machine learning framework system; there can also be other compatible quantum computing layer interfaces for quantum programs created based on the quantum computing programming library included in the second machine learning framework system of other companies, No more examples here.
具体的,第二机器学习框架***与兼容量子计算层接口具有一一对应的关系,确定对应的所述第二机器学习框架***的兼容量子计算层接口,具体可以通过该对应关系进行确定。Specifically, the second machine learning framework system has a one-to-one correspondence with the compatible quantum computing layer interface, and the corresponding compatible quantum computing layer interface of the second machine learning framework system can be determined specifically through the corresponding relationship.
具体的,通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层,可以将基于所述第二机器学习框架***内含的量子计算编程库创建的量子程序作为兼容量子计算层接口的参数,通过调用该兼容量子计算层接口即可实现量子计算层的构建。Specifically, by calling the compatible quantum computing layer interface through the compatible quantum computing program packaging unit to construct a compatible quantum computing layer, the quantum program created based on the quantum computing programming library contained in the second machine learning framework system can be used as a compatible The parameters of the quantum computing layer interface can realize the construction of the quantum computing layer by calling the compatible quantum computing layer interface.
可选的,所述确定对应的所述第二机器学习框架***的兼容量子计算层接口之前,所述方法还包括:Optionally, before determining the compatible quantum computing layer interface of the corresponding second machine learning framework system, the method further includes:
通过所述第二机器学习框架***内含的量子计算编程库构建量子线路,以及调用初始化函数对所述量子线路进行初始化,通过运行函数定义所述量子线路的运行和测量方式,得到量子程序。The quantum circuit is constructed through the quantum computing programming library included in the second machine learning framework system, and the quantum circuit is initialized by calling an initialization function, and the operation and measurement mode of the quantum circuit is defined through the operation function to obtain a quantum program.
其中,初始化函数例如可以为_init_(),运行函数例如可以为run()。Wherein, the initialization function may be, for example, _init_(), and the running function may be, for example, run().
可选的,所述第一机器学习框架***还包括经典模块和数据结构模块,所述构建包括所述兼容量子计算层的机器学习模型,包括:Optionally, the first machine learning framework system also includes a classical module and a data structure module, and the building includes the machine learning model compatible with the quantum computing layer, including:
调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述兼容量子计算层之间的前向传播关系;Calling the classical module to construct a classical computing layer and calling the data structure module to construct a forward propagation relationship between the classical computing layer and the compatible quantum computing layer;
调用所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型。Invoking the classical module to encapsulate the classical computing layer, the compatible quantum computing layer, and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer, to obtain a machine learning model.
其中,经典计算是一种遵循经典物理学规律调控经典信息单元进行计算的传统计算模式,它通过一个二进制***工作,即信息使用1或0来存储。经典计算层与量子计算层相对应,其可以是对创建好的经典计算程序按照一定标准进行封装,使得经典计算层便于在创建和训练机器学习模型时进行使用。Among them, classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0. The classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing program according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
其中,前向传播为将上一计算层的输出作为下一计算层的输入,并计算下一计算层的输出,一直运算到没有下一计算层为止,其中计算层可以为上述的经典计算层也可以为上述的量子计算层。Among them, the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
可选的,所述经典模块包括经典神经网络层单元,所述经典神经网络层单元包括以下至少一者:指定模型经典神经网络层子单元,被配置为通过已封装的经典神经网络层接口构建指定模型的所述经典神经网络层;激活层子单元,被配置为构建用于对所述经典神经网络层的输出进行非线性变换的激活层;Optionally, the classic module includes a classic neural network layer unit, and the classic neural network layer unit includes at least one of the following: a specified model classic neural network layer subunit configured to be constructed through an encapsulated classic neural network layer interface The classical neural network layer of the specified model; the activation layer subunit configured to construct an activation layer for nonlinearly transforming the output of the classical neural network layer;
所述调用所述经典模块构建经典计算层,包括:Said calling said classic module to build a classic computing layer, including:
调用所述指定模型经典神经网络层子单元构建经典神经网络层,以及将所述经典神经网络层作为经典计算层;或者,calling the designated model classical neural network layer subunit to construct a classical neural network layer, and using the classical neural network layer as a classical computing layer; or,
调用所述指定模型经典神经网络层子单元和所述激活层子单元构建经典神经网络层和激活层,以及将所述经典神经网络层和所述激活层作为经典计算层。Calling the specified model classical neural network layer subunit and the activation layer subunit to construct a classical neural network layer and activation layer, and using the classical neural network layer and the activation layer as a classical computing layer.
其中,经典神经网络层接口包括以下至少一者:一维卷积层接口Conv1D()、二维卷积层接口Conv2D、二维转置卷积层接口ConvT2D()、平均池化层接口AvgPool2D()、最大池化层接口MaxPool2D()、嵌入层接口Embedding2D()、一维批量归一化层接口BatchNorm1D()、二维批量归一化层接口BatchNorm2D、全连接层接口Linear()、随机丢失层接口Dropout()。Wherein, the classic neural network layer interface includes at least one of the following: one-dimensional convolutional layer interface Conv1D(), two-dimensional convolutional layer interface Conv2D, two-dimensional transposed convolutional layer interface ConvT2D(), average pooling layer interface AvgPool2D( ), maximum pooling layer interface MaxPool2D(), embedding layer interface Embedding2D(), one-dimensional batch normalization layer interface BatchNorm1D(), two-dimensional batch normalization layer interface BatchNorm2D, fully connected layer interface Linear(), random loss Layer interface Dropout().
其中,激活层接口包括以下至少一者:Sigmoid()、Softplus()、Softsign()、Softmax()、HardSigmoid()、Relu()、LeakyRelu()、Elu()、Tanh()。Wherein, the activation layer interface includes at least one of the following: Sigmoid(), Softplus(), Softsign(), Softmax(), HardSigmoid(), Relu(), LeakyRelu(), Elu(), Tanh().
其中,Sigmoid()对应的激活函数为
Figure PCTCN2022143597-appb-000017
Softplus()对应的激活函数为Softplus(x)=log(1+e x),Softsign()对应的激活函数为
Figure PCTCN2022143597-appb-000018
Softmax()对应的激活函数为
Figure PCTCN2022143597-appb-000019
Relu()对应的激活函数为Relu(x)=max(0,x),LeakyRelu()对应的激活函数为
Figure PCTCN2022143597-appb-000020
Figure PCTCN2022143597-appb-000021
Elu()对应的激活函数为
Figure PCTCN2022143597-appb-000022
Tanh()对应的激活函数为
Figure PCTCN2022143597-appb-000023
HardSigmoid()对应的激活函数为
Among them, the activation function corresponding to Sigmoid() is
Figure PCTCN2022143597-appb-000017
The activation function corresponding to Softplus() is Softplus(x)=log(1+e x ), and the activation function corresponding to Softsign() is
Figure PCTCN2022143597-appb-000018
The activation function corresponding to Softmax() is
Figure PCTCN2022143597-appb-000019
The activation function corresponding to Relu() is Relu(x)=max(0,x), and the activation function corresponding to LeakyRelu() is
Figure PCTCN2022143597-appb-000020
Figure PCTCN2022143597-appb-000021
The activation function corresponding to Elu() is
Figure PCTCN2022143597-appb-000022
The activation function corresponding to Tanh() is
Figure PCTCN2022143597-appb-000023
The activation function corresponding to HardSigmoid() is
Figure PCTCN2022143597-appb-000024
Figure PCTCN2022143597-appb-000024
举例说明,创建的经典计算层和兼容量子计算层如下:For example, the created classical computing layer and compatible quantum computing layer are as follows:
self.conv1=Conv2D(a)self.conv1 = Conv2D(a)
self.maxpool=Maxpool2D(b)self.maxpool = Maxpool2D(b)
self.conv2=Conv2D(c)self.conv2 = Conv2D(c)
self.maxpool2=Maxpool2D(d)self.maxpool2 = Maxpool2D(d)
self.fc1=Linear(e)self.fc1 = Linear(e)
self.fc2=Linear(f)self.fc2 = Linear(f)
self.hybrid=Qiskitlayer(g)self.hybrid = Qiskitlayer(g)
self.fc3=Linear(h)self.fc3 = Linear(h)
其中,a、b、c、d、e、f、g、h为接口参数。Among them, a, b, c, d, e, f, g, h are interface parameters.
其中,经典模块包括赋值函数,可以通过赋值函数构建所述经典计算层与所述兼容量子计算层之间的前向传播关系。Wherein, the classical module includes an assignment function, and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer can be constructed through the assignment function.
举例说明,通过赋值函数构建的前向传播关系如下:For example, the forward propagation relationship constructed by the assignment function is as follows:
x=self.conv1(x)x = self.conv1(x)
x=self.maxpool1(x)x = self.maxpool1(x)
x=self.conv2(x)x = self.conv2(x)
x=maxpool2(x)x = maxpool2(x)
x=self.fc1(x)x = self.fc1(x)
x=self.fc2(x)x = self.fc2(x)
x=self.hybrid(x)x = self. hybrid(x)
x=self.fc3(x)x = self.fc3(x)
return xreturn x
可选的,所述经典模块还包括抽象类子模块,所述通过所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型,包括:Optionally, the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the connection between the classical computing layer and the compatible quantum computing layer are connected through the classical module. The forward propagation relationship is encapsulated to obtain a machine learning model, including:
调用所述抽象类子模块基于初始化函数对所述兼容量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述兼容量子计算层和所述经典计算层;calling the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialized and encapsulated compatible quantum computing layer and the classical computing layer;
调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;Calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship;
调用所述抽象类子模块基于模块类对所述初始化和封装后的所述兼容量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。The abstract class submodule is called to encapsulate the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class to obtain a machine learning model.
以上步骤的详细解释参与上述实施例介绍,在此不再赘述。The detailed explanation of the above steps is involved in the introduction of the above embodiments, and will not be repeated here.
与现有技术相比,本公开提供的机器学习模型构建方法,开发者可以基于自己习惯的第二机器学习框架***内含的量子计算编程库创建的量子程序,然后在第一机器学习框架***中确定对应的该第二机器学习框架***对应的兼容量子计算层接口,通过第一机器学习框架***包括的兼容量子计算程序封装单元调用该兼容量子计算层接口即可构建兼容量子计算层,从而实现了通过包括第一机器学习框架***但不包括第二机器学习框架***的电子设备进行机器学习模型的构建,进而实现跨量子机器学习框架***构建机器学习模型。Compared with the prior art, in the machine learning model construction method provided by this disclosure, developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework system that they are used to, and then use the first machine learning framework system Determine the compatible quantum computing layer interface corresponding to the second machine learning framework system, and call the compatible quantum computing layer interface through the compatible quantum computing program encapsulation unit included in the first machine learning framework system to build a compatible quantum computing layer, thereby The construction of the machine learning model is realized through the electronic device including the first machine learning framework system but not including the second machine learning framework system, thereby realizing the construction of the machine learning model across the quantum machine learning framework system.
基于上述图20描述的创建机器学习模型的实施例,本公开还提供相应的创建机器学习模型的装置,该装置可以包括确定单元,用于确定对应的所述第二机器学习框架***的兼容量子计算层接口,所述兼容量子计算层接口用于提供基于所述第二机器学习框架***内含的量子计算编程库创建的量子程序;构建单元,用于通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层;构建包括所述兼容量子计算层的机器学习模型。Based on the embodiment of creating a machine learning model described in FIG. 20 above, the present disclosure also provides a corresponding device for creating a machine learning model. The device may include a determination unit for determining the corresponding quantum of the second machine learning framework system Computing layer interface, the compatible quantum computing layer interface is used to provide the quantum program created based on the quantum computing programming library contained in the second machine learning framework system; the construction unit is used to call through the compatible quantum computing program encapsulation unit The compatible quantum computing layer interface constructs a compatible quantum computing layer; constructs a machine learning model including the compatible quantum computing layer.
可选的,所述确定对应的所述第二机器学习框架***对应的兼容量子计算层接口之前,所述构建单元还用于:通过所述第二机器学习框架***内含的量子计算编程库构建量子线路,以及调用初始化函数对所述量子线路进行初始化,通过运行函数定义所述量子线路的运行和测量方式,得到量子程序。Optionally, before determining the compatible quantum computing layer interface corresponding to the second machine learning framework system, the construction unit is further configured to: use the quantum computing programming library included in the second machine learning framework system Constructing a quantum circuit, calling an initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the running function, and obtaining a quantum program.
可选的,所述第一机器学习框架***还包括经典模块和数据结构模块,所述构建包括所述兼容量子计算层的机器学习模型,所述构建单元,具体用于:调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述兼容量子计算层之间的前向传播关系;调用所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型。Optionally, the first machine learning framework system further includes a classical module and a data structure module, the construction includes the machine learning model compatible with the quantum computing layer, and the construction unit is specifically used to: call the classical module Constructing the classical computing layer and calling the data structure module to construct the forward propagation relationship between the classical computing layer and the compatible quantum computing layer; calling the classical computing layer to combine the classical computing layer and the compatible quantum computing layer and encapsulating the forward propagation relationship between the classical computing layer and the compatible quantum computing layer to obtain a machine learning model.
可选的,所述经典模块包括经典神经网络层单元,所述经典神经网络层单元包括以下至少一者:指定模型经典神经网络层子单元,被配置为通过已封装的经典神经网络层接口构建指定模型的所述经典神经网络层;激活层子单元,被配置为构建用于对所述经典神经网络层的输出进行非线性变换的激活层;所述调用所述经典模块构建经典计算层,所述构建单元具体用于:调用所述指定模型经典神经网络层子单元构建经典神经网络层,以及将所述经典神经网络层作为经典计算层;或者,调用所述指定模型经典神经网络层子单元和所述激活层子单元构建经典神经网络层和激活层,以及将所述经典神经网络层和所述激活层作为经典计算层。Optionally, the classic module includes a classic neural network layer unit, and the classic neural network layer unit includes at least one of the following: a specified model classic neural network layer subunit configured to be constructed through an encapsulated classic neural network layer interface The classical neural network layer of the specified model; the activation layer subunit is configured to construct an activation layer for nonlinear transformation of the output of the classical neural network layer; the classical calculation layer is constructed by calling the classic module, The construction unit is specifically used to: call the specified model classic neural network layer subunit to construct a classic neural network layer, and use the classic neural network layer as a classic calculation layer; or call the specified model classic neural network layer subunit The unit and the activation layer subunit construct a classical neural network layer and an activation layer, and use the classical neural network layer and the activation layer as a classical calculation layer.
可选的,所述经典模块还包括抽象类子模块,所述通过所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型,所述构建单元具体用于:调用所述抽象类子模块基于初始化函数对所述兼容量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述兼容量子计算层和所述经典计算层;调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;调用所述抽象类子模块基于模块类对所述初始化和封装后的所述兼容量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。Optionally, the classical module further includes an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the connection between the classical computing layer and the compatible quantum computing layer are connected through the classical module. The forward propagation relationship is encapsulated to obtain a machine learning model, and the construction unit is specifically used to: call the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialization and the encapsulated compatible quantum computing layer and the classical computing layer; calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship; calling The abstract class sub-module encapsulates the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on a module class to obtain a machine learning model.
与现有技术相比,本公开提供的机器学习模型构建方法,开发者可以基于自己习惯的 第二机器学习框架***内含的量子计算编程库创建的量子程序,然后在第一机器学习框架***中确定对应的该第二机器学习框架***对应的兼容量子计算层接口,通过第一机器学习框架***包括的兼容量子计算程序封装单元调用该兼容量子计算层接口即可构建兼容量子计算层,从而实现了通过包括第一机器学习框架***但不包括第二机器学习框架***的电子设备进行机器学习模型的构建,进而实现跨量子机器学习框架***构建机器学习模型。Compared with the prior art, in the machine learning model construction method provided by this disclosure, developers can create quantum programs based on the quantum computing programming library included in the second machine learning framework system that they are used to, and then use the first machine learning framework system Determine the compatible quantum computing layer interface corresponding to the second machine learning framework system, and call the compatible quantum computing layer interface through the compatible quantum computing program encapsulation unit included in the first machine learning framework system to build a compatible quantum computing layer, thereby The construction of the machine learning model is realized through the electronic device including the first machine learning framework system but not including the second machine learning framework system, thereby realizing the construction of the machine learning model across the quantum machine learning framework system.
图21是根据一示例性实施例示出的训练机器学习模型的又一流程图。参见图21,本实施例提供的训练机器学习模型的又一实施例可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***30包括数据结构模块31、量子模块32和经典模块33,并且训练机器学习模型可以包括:Fig. 21 is another flow chart of training a machine learning model according to an exemplary embodiment. Referring to FIG. 21 , another embodiment of the training machine learning model provided in this embodiment can be applied to an electronic device including the first machine learning framework system 30 shown in FIG. 3 , and the first machine learning framework system 30 includes data Structure module 31, quantum module 32 and classical module 33, and the training machine learning model can include:
S2110,调用所述量子模块和所述经典模块创建包括量子程序的机器学习模型,调用所述数据结构模块获取目标数据并创建包括所述目标数据的张量数据。S2110, call the quantum module and the classical module to create a machine learning model including a quantum program, call the data structure module to acquire target data and create tensor data including the target data.
S2120,调用所述量子模块基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组。S2120. Call the quantum module to divide the second qubits into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device.
S2130,调用所述量子模块针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路。S2130. Invoke the quantum module to generate, for each group of the second qubits, a quantum circuit including the group of the second qubits based on the quantum program.
S2140,基于所述张量数据得到每组所述量子线路的输入数据,并将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果。S2140. Obtain input data of each group of quantum circuits based on the tensor data, and respectively input the input data into each of the quantum circuits to obtain an output result of each of the quantum circuits.
S2150,基于所述输出结果确定所述机器学习模型对所述张量数据的预测结果。S2150. Determine a prediction result of the machine learning model for the tensor data based on the output result.
S2160,调用所述经典模块创建所述机器学习模型的训练层。S2160, calling the classic module to create a training layer of the machine learning model.
S2170,将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型。S2170. Input the prediction result into the training layer, so as to update the parameters of the quantum program, and obtain the trained machine learning model.
具体来讲,第一机器学习框架***30集成了众多用于创建和训练机器学习模型的函数集,通过其定义的接口可以方便的调用这些函数实现对机器学习模型的相关操作。如图3所示,所述第一机器学习框架***30可以包括:Specifically, the first machine learning framework system 30 integrates numerous function sets for creating and training machine learning models, and these functions can be conveniently called through the defined interfaces to implement related operations on the machine learning models. As shown in Figure 3, the first machine learning framework system 30 may include:
数据结构模块31,被配置为获取目标数据并创建包括所述目标数据的张量数据;A data structure module 31 configured to acquire target data and create tensor data including the target data;
经典模块33,被配置为创建机器学习模型;a classical module 33 configured to create a machine learning model;
量子模块32,被配置为创建机器学习模型,以及基于量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组,针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路,以使得每组所述量子线路的输入数据确定时,将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果。a quantum module 32 configured to create a machine learning model, and divide said second qubits into groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, For each group of the second qubits, generate a quantum circuit including the group of the second qubits based on the quantum program, so that when the input data of each group of the quantum circuits is determined, the input data are respectively Input each quantum circuit, and obtain the output result of each quantum circuit.
具体来讲,数据结构模块31定义了张量数据的数据结构,通过调用数据结构模块31,可以将输入的目标数据转化为张量数据,以用于输入机器学习模型进行正向计算。对于量子模块32,可以通过调用该量子模块32创建机器学习模型的量子计算层,量子计算层为包含量子程序的程序模块,可以用于实现对应量子程序的量子计算,通过对量子程序按照一定的标准进行封装,使得量子计算层便于在创建和训练机器学习模型时进行使用。对于机器学习模型通过量子计算实现的部分,均可以理解为对应的量子计算层。量子程序为实现量子计算的程序,可以通过调用量子模块32创建按特定顺序作用于量子比特的量子逻辑门得到量子程序,并对量子程序进行封装得到量子计算层。此外,量子模块32还可以 根据上述第一量子比特的数量及第二量子比特的数量对第二量子比特进行分组,进而再基于量子程序生成对应于多组第二量子比特的量子线路,以得到多组量子线路用于处理数据,其具体执行方式可以参见后续对于数据分类方法相关步骤的描述。Specifically, the data structure module 31 defines the data structure of tensor data. By calling the data structure module 31, the input target data can be converted into tensor data, which can be used for inputting into the machine learning model for forward calculation. For the quantum module 32, the quantum computing layer of the machine learning model can be created by calling the quantum module 32. The quantum computing layer is a program module containing a quantum program, which can be used to realize the quantum computing of the corresponding quantum program. Standard encapsulation makes the quantum computing layer easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer. The quantum program is a program for implementing quantum computing. The quantum program can be obtained by calling the quantum module 32 to create quantum logic gates that act on the qubits in a specific order, and the quantum program can be packaged to obtain the quantum computing layer. In addition, the quantum module 32 can also group the second qubits according to the number of the above-mentioned first qubits and the number of the second qubits, and then generate quantum circuits corresponding to multiple groups of second qubits based on the quantum program, so as to obtain Multiple groups of quantum circuits are used to process data, and the specific execution method can refer to the subsequent description of the relevant steps of the data classification method.
对于经典模块33,可以用过调用该经典模块33创建机器学习模型的经典计算层,经典计算层为机器学习模型中的经典计算部分,其可以是通过经典模块33对创建好的经典计算程序按照一定标准进行封装得到,使得经典计算层便于在训练机器学习模型时进行使用。在创建好量子计算层和经典计算层后,可以通过经典模块33对其进行封装,创建符合一定标准的抽象类层,抽象类层通过编程语言中的类(class)的方法实现,通过对量子计算层和经典计算层封装可以创建符合一定标准的机器学习模型,例如创建的抽象类层定义了正向运算机器学习模型的方式,便于在训练机器学习模型时对机器学习模型进行正向运算以得到用于计算损失函数的计算结果,同时也可以得到反向计算时进行梯度计算的顺序关系。经典模块33还可以用于创建机器学习模型的训练层对机器学习模型进行训练。For the classic module 33, the classic calculation layer of the machine learning model can be created by calling the classic module 33. The classic calculation layer is the classic calculation part in the machine learning model, which can be the classic calculation program created by the classic module 33 according to Encapsulation of certain standards makes the classical computing layer easy to use when training machine learning models. After the quantum computing layer and the classical computing layer are created, they can be encapsulated through the classical module 33 to create an abstract class layer that meets certain standards. The abstract class layer is realized by the class method in the programming language. Computing layer and classic computing layer encapsulation can create machine learning models that meet certain standards. For example, the created abstract class layer defines the way of forward computing machine learning models, which is convenient for forward computing of machine learning models when training machine learning models. The calculation result used to calculate the loss function can be obtained, and the sequence relationship of gradient calculation during reverse calculation can also be obtained. The classic module 33 can also be used to create a training layer of the machine learning model to train the machine learning model.
在步骤S2110中,可以调用量子模块32创建量子计算层,调用经典模块33创建经典计算层,再利用经典模块33对量子计算层和经典计算层进行封装得到混合量子计算和经典计算的机器学习模型。当然也可以调用量子模块32创建量子计算层后,直接利用经典模块33对量子计算层进行封装得到纯量子机器学习模型。同时对于输入的目标数据,调用数据结构模块31创建包含目标数据的张量数据,以用于输入机器学习模型。In step S2110, the quantum module 32 can be called to create a quantum computing layer, the classical module 33 can be called to create a classical computing layer, and then the classical module 33 can be used to encapsulate the quantum computing layer and the classical computing layer to obtain a machine learning model that mixes quantum computing and classical computing . Of course, it is also possible to call the quantum module 32 to create a quantum computing layer, and then directly use the classical module 33 to encapsulate the quantum computing layer to obtain a pure quantum machine learning model. At the same time, for the input target data, the data structure module 31 is invoked to create tensor data containing the target data for input into the machine learning model.
在步骤S2120中,量子设备可以包含一定数量的量子比特用于执行量子计算,例如可以包含量子芯片,量子芯片包含一定数量的量子比特,第二量子比特即量子设备中当前没有执行量子计算任务,处于空闲状态可以被随时利用的量子比特。第一量子比特即实现量子程序或构建量子程序对应的量子线路所需的量子比特。可以调用量子模块32,通过相关的接口获取输入的量子程序所需的第一量子比特的数量,也可以直接从量子计算层中自动获取量子程序所需的第一量子比特的数量。对于允许使用的第二量子比特的数量,同样可以通过接口传参获取,也可以自动获取量子设备的状态信息,进而从状态信息中获取量子设备允许使用的第二量子比特的数量。本公开对于如何获取这两者数量不作具体限制。In step S2120, the quantum device may contain a certain number of qubits for performing quantum calculations, for example, it may contain a quantum chip, the quantum chip contains a certain number of qubits, the second qubit is that the quantum device is not currently performing quantum computing tasks, Qubits that are idle and can be used at any time. The first qubit is the qubit required to realize the quantum program or construct the quantum circuit corresponding to the quantum program. The quantum module 32 can be called to obtain the number of first qubits required by the input quantum program through a related interface, or the number of first qubits required by the quantum program can be directly obtained automatically from the quantum computing layer. The number of second qubits allowed to be used can also be obtained by passing parameters through the interface, or the state information of the quantum device can be automatically obtained, and then the number of second qubits allowed to be used by the quantum device can be obtained from the state information. The present disclosure does not specifically limit how to obtain the two quantities.
获取所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量之后,根据这两个数量对可用的第二量子比特进行分组,得到多组第二量子比特数量。After obtaining the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, group the available second qubits according to these two quantities to obtain multiple groups of second qubits number of bits.
可选地,如图22所示,基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组,包括:Optionally, as shown in Figure 22, the second qubits are divided into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, including :
S2210,将所述量子设备中允许使用的第二量子比特的数量除以所述量子程序所需的第一量子比特的数量得到中间数。S2210. Divide the number of second qubits allowed in the quantum device by the number of first qubits required by the quantum program to obtain an intermediate number.
S2220,将小于等于所述中间数的最大整数作为目标数。S2220. Taking the largest integer less than or equal to the intermediate number as the target number.
S2230,将所述第二量子比特划分为数量为所述目标数的多组。S2230. Divide the second qubits into multiple groups whose quantity is the target number.
在步骤S2210中,做上述除法运算得到中间数,举例来讲,量子设备中当前处于空闲状态且能被正常使用的量子比特有64个,进而允许使用的第二量子比特的数量为64,运行量子程序最少需要10个量子比特,进而量子程序所需的第一量子比特的数量为10,将64除以10得到中间数6.4。In step S2210, perform the above division operation to obtain the intermediate number. For example, there are 64 qubits that are currently idle and can be used normally in the quantum device, and the number of second qubits allowed to be used is 64. Run A quantum program requires at least 10 qubits, and the number of first qubits required by a quantum program is 10. Divide 64 by 10 to get the middle number 6.4.
得到中间数后,执行步骤S2220,沿用前述例子,在小于等于6.4的数中,最大的整数为6,故将6作为目标数。得到目标数后,进入执行步骤S2230,将第二量子比特划分 为多组,组数为目标数,每组所述第二量子比特的数量大于或等于所述量子程序所需的第一量子比特的数量,沿用前述例子,将64个第二量子比特划分为6组,每组第二量子比特的数量均为10,当然,由于还有4个允许使用的第二量子比特,也可以在划分的6组第二量子比特中,4组第二量子比特均为11个,其余2组第二量子比特均为10个。在一种可能的实施方式中,第二量子比特也可以被划分为1组。After the intermediate number is obtained, step S2220 is executed. Using the previous example, among the numbers less than or equal to 6.4, the largest integer is 6, so 6 is taken as the target number. After obtaining the target number, enter the execution step S2230, divide the second qubits into multiple groups, the number of groups is the target number, and the number of the second qubits in each group is greater than or equal to the first qubits required by the quantum program The number of the number of qubits, following the aforementioned example, divides 64 second qubits into 6 groups, and the number of second qubits in each group is 10. Of course, since there are 4 second qubits that are allowed to be used, it can also be divided into Among the 6 groups of second qubits, 4 groups of second qubits have 11, and the remaining 2 groups of second qubits have 10. In a possible implementation manner, the second qubits may also be divided into 1 group.
当然在其它可能的实施方式中,也可以基于量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量执行其它运算,以对第二量子比特分组,例如第二量子比特的数量为64,第一量子比特的数量为10,可以先将64减去一个固定数如20留存部分第二量子比特,再将剩下的数量44除以10进行分组。本公开对于采用何种方式对第二量子比特分组不作具体限制。Of course, in other possible implementations, other calculations can also be performed based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device, so as to group the second qubits, for example, The number of second qubits is 64, and the number of first qubits is 10. You can first subtract a fixed number such as 20 from 64 to save some second qubits, and then divide the remaining number of 44 by 10 for grouping. The present disclosure does not specifically limit the method used to group the second qubits.
对第二量子比特分组后,执行步骤S2130,对于每一组第二量子比特,根据量子程序在基于组第二量子比特上生成量子线路,量子线路对应的量子计算与量子程序对应的量子计算相同,例如量子程序用于实现运算y=x*w+b,其中,x为量子程序的输入数据,y为量子程序的输出数据,w和b为参数,则量子线路也用于实现运算y=x*w+b。在一种可能的实施方式中,量子程序包括了一系列按顺序作用于第一量子比特的量子逻辑门,则生成的量子线路也可以包括与量子程序的量子逻辑门相同的量子逻辑门,当然,也可以对量子程序的量子逻辑门进行优化后生成执行相同量子计算的量子逻辑门用于构建量子线路。例如量子程序包含的量子逻辑门依次为H门、RZ门、泡利X门,则对应生成的量子线路也可以依次包含H门、RZ门、泡利X门,当然生成的量子线路也可以对量子逻辑门进行优化,进而包含不同的量子逻辑门,但执行的量子计算仍然与量子程序相同。对于生成的每一个量子线路,其可以相同,也可以不同,只要其执行的量子计算均与量子程序相同即可,本公开对此不做具体限制。After grouping the second qubits, perform step S2130, for each group of second qubits, generate quantum circuits based on the group of second qubits according to the quantum program, the quantum calculation corresponding to the quantum circuit is the same as the quantum calculation corresponding to the quantum program , for example, the quantum program is used to realize the operation y=x*w+b, wherein, x is the input data of the quantum program, y is the output data of the quantum program, w and b are parameters, then the quantum circuit is also used to realize the operation y= x*w+b. In a possible implementation, the quantum program includes a series of quantum logic gates acting on the first qubit in sequence, then the generated quantum circuit can also include the same quantum logic gates as the quantum logic gates of the quantum program, of course , it is also possible to optimize the quantum logic gates of the quantum program to generate quantum logic gates that perform the same quantum calculation for constructing quantum circuits. For example, the quantum logic gates contained in the quantum program are H gate, RZ gate, and Pauli X gate in sequence, and the corresponding generated quantum circuit can also contain H gate, RZ gate, and Pauli X gate in sequence. Of course, the generated quantum circuit can also be used for The quantum logic gates are optimized to contain different quantum logic gates, but the quantum computation performed is still the same as the quantum program. For each generated quantum circuit, they may be the same or different, as long as the quantum calculations performed by them are the same as the quantum program, which is not specifically limited in the present disclosure.
可选地,如图23所示,针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路,包括:Optionally, as shown in FIG. 23, for each group of the second qubits, generating a quantum circuit including the group of the second qubits based on the quantum program includes:
S2310,针对每一组所述第二量子比特,基于所述量子程序生成用于作用于该组所述第二量子比特的量子逻辑门。S2310. For each group of the second qubits, generate a quantum logic gate for acting on the group of the second qubits based on the quantum program.
S2320,生成包括该组所述第二量子比特和所述量子逻辑门的量子线路。S2320. Generate a quantum circuit including the group of the second qubits and the quantum logic gate.
在步骤S2310中,参见前述描述,可以根据量子程序生成与量子程序包含的量子逻辑门相同的量子逻辑门,也可以对量子程序的量子逻辑门进行优化,生成不同的量子逻辑门用于作用于第二量子比特,只要其执行的量子计算相同即可。In step S2310, referring to the foregoing description, the same quantum logic gates as the quantum logic gates included in the quantum program can be generated according to the quantum program, or the quantum logic gates of the quantum program can be optimized to generate different quantum logic gates for acting on The second qubit, as long as it performs the same quantum computation.
在步骤S2320中,量子线路包括第二量子比特和前述量子逻辑门,量子逻辑门按照一定顺序作用于第二量子比特,其顺序可以与量子程序的量子逻辑门相同。当然,量子线路还可以包括针对部分的第二量子比特或所有第二量子比特的测量操作。In step S2320, the quantum circuit includes the second qubit and the aforementioned quantum logic gate, and the quantum logic gate acts on the second qubit in a certain order, and the order may be the same as the quantum logic gate of the quantum program. Of course, the quantum circuit may also include measurement operations on some or all of the second qubits.
创建机器学习模型后,生成量子线路可以是在运行机器学习模型之前,也可以与机器学习模型运行同步执行。After the machine learning model is created, generating quantum circuits can be performed before running the machine learning model, or it can be executed simultaneously with running the machine learning model.
生成量子线路后,执行步骤S2140,可以将张量数据输入机器学习模型,进而在正向运行机器学习模型的过程中,得到用于输入量子线路的输入数据。例如对于包含一经典计算层和量子计算层的机器学习模型,获取多个目标数据后对应创建多个张量数据,其需要依次经过经典计算层和量子计算层计算后得到机器学习模型的输出结果。对于量子计算层中的量子程序,生成对应的多组量子线路后,将多个张量数据先输入经典计算层进行计算, 得到的经典计算层的多个输出数据可以作为多个量子线路的输入数据,以输入每一量子线路,得到每一量子线路的输出结果。当然,多个量子线路可以并行运行,也可以根据调度按照一定的时间差运行。输入每个量子线路的输入数据可以不同。After the quantum circuit is generated, step S2140 is executed to input the tensor data into the machine learning model, and then in the process of running the machine learning model forward, the input data for inputting the quantum circuit is obtained. For example, for a machine learning model that includes a classical computing layer and a quantum computing layer, multiple tensor data are correspondingly created after obtaining multiple target data, which needs to be calculated in turn by the classical computing layer and the quantum computing layer to obtain the output result of the machine learning model . For the quantum program in the quantum computing layer, after generating corresponding sets of quantum circuits, multiple tensor data are first input into the classical computing layer for calculation, and the obtained multiple output data of the classical computing layer can be used as the input of multiple quantum circuits Data is input into each quantum circuit, and the output result of each quantum circuit is obtained. Of course, multiple quantum circuits can run in parallel, or run according to a certain time difference according to the schedule. The input data to each quantum circuit can be different.
基于上述技术方案,对于调用量子模块和经典模块创建的机器学习模型,在将张量数据输入该模型之前,根据量子程序所需的第一量子比特的数量,以及可用的第二量子比特的数量,生成多组对应于量子程序的量子线路,进而使得张量数据输入机器学习模型得到用于输入多组量子线路的多个输入数据后,可以将多个输入数据输入到多组量子线路,以利用多组量子线路对输入数据进行相关的量子计算,提高量子程序运行的效率。Based on the above technical solution, for the machine learning model created by calling the quantum module and the classical module, before inputting the tensor data into the model, according to the number of first qubits required by the quantum program and the number of second qubits available , generate multiple sets of quantum circuits corresponding to the quantum program, and then make the tensor data input machine learning model obtain multiple input data for inputting multiple sets of quantum circuits, and then input multiple input data into multiple sets of quantum circuits to Using multiple groups of quantum circuits to perform related quantum calculations on the input data to improve the efficiency of quantum program operation.
得到每一量子线路的输出结果后,机器学习模型继续对该输出结果进行运算得到对张量数据的预测结果。举例来讲,机器学习模型依次包括一量子计算层和经典计算层,基于量子计算层的量子程序生成对应的量子线路后,将张量数据输入量子线路得到输出结果。进而将输出结果再输入经典计算层,得到计算结果,该计算结果作为机器学习模型对张量数据的预测结果,以使得机器学习模型完成对目标数据的预测任务。接着,通过调用所述经典模块创建所述机器学习模型的训练层,并将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型。After obtaining the output result of each quantum circuit, the machine learning model continues to operate on the output result to obtain the prediction result of the tensor data. For example, the machine learning model includes a quantum computing layer and a classical computing layer in turn. After the quantum program based on the quantum computing layer generates the corresponding quantum circuit, tensor data is input into the quantum circuit to obtain the output result. Then, the output result is input into the classical computing layer to obtain the calculation result, which is used as the prediction result of the machine learning model for the tensor data, so that the machine learning model can complete the prediction task of the target data. Next, create the training layer of the machine learning model by calling the classical module, and input the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model .
参见上述训练层,所述训练层包括损失函数层和优化器层,所述经典模块包括:损失函数单元,被配置为计算所述机器学习模型的损失函数;优化器单元,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化。Referring to the above-mentioned training layer, the training layer includes a loss function layer and an optimizer layer, and the classic module includes: a loss function unit configured to calculate the loss function of the machine learning model; an optimizer unit configured to train The machine learning model updates parameters of the machine learning model based on the loss function, so as to optimize the machine learning model.
可选地,调用所述经典模块创建所述机器学***方差作为损失函数,也可以计算该预测结果与标签数据的二元交叉熵(Binary Cross Entropy)作为损失函数。优化器单元则可以用于根据损失函数相对于机器学习模型的参数的梯度,利用梯度下降算法更新机器学习模型的参数对其进行优化。例如优化器采用的梯度下降算法可以为随机梯度下降算法(Stochastic Gradient Descent,SGD),自适应梯度算法(Adaptive gradient algorithm,Adagrad)、自适应矩估计(Adaptive Moment Estimation,Adam)中的任意一种,当然还可以采用其它的算法更新机器学习模型的参数,本公开对于损失函数单元可以计算哪些种类的损失函数以及优化器单元采用何种方法更新参数不作具体限制。Optionally, calling the classic module to create the training layer of the machine learning model includes: calling the loss function unit to create the loss function layer, and calling the optimizer unit to create the optimizer layer. Specifically, the loss function unit is used to calculate the loss function of the machine learning model. For example, the square difference between the prediction result of the machine learning model for tensor data and the label data can be calculated as the loss function, and the difference between the prediction result and the label data can also be calculated. Binary Cross Entropy (Binary Cross Entropy) as a loss function. The optimizer unit can be used to update the parameters of the machine learning model by using the gradient descent algorithm according to the gradient of the loss function relative to the parameters of the machine learning model to optimize it. For example, the gradient descent algorithm used by the optimizer can be any one of Stochastic Gradient Descent (SGD), Adaptive Gradient Algorithm (Adagrad), Adaptive Moment Estimation (Adam) Of course, other algorithms can also be used to update the parameters of the machine learning model. The present disclosure does not specifically limit which types of loss functions the loss function unit can calculate and which method the optimizer unit uses to update the parameters.
为实现对机器学习模型的训练,调用所述损失函数单元创建所述损失函数层,损失函数层为封装好的计算模块,其定义了损失函数的计算方式,进而在机器学习模型的预测结果输入到损失函数层时,可以根据损失函数层定义的计算方式计算机器学习模型的损失函数。创建好损失函数层之后可以调用所述优化器单元创建所述优化器层,以在预测结果输入到损失函数层,计算出损失函数之后,根据损失函数更新机器学习模型的参数,直至得到合适的参数使得机器学习模型能达到预期的效果,完成对机器学习模型的优化。In order to realize the training of the machine learning model, the loss function unit is called to create the loss function layer. The loss function layer is a packaged calculation module, which defines the calculation method of the loss function, and then input the prediction result of the machine learning model When reaching the loss function layer, the loss function of the machine learning model can be calculated according to the calculation method defined by the loss function layer. After the loss function layer is created, the optimizer unit can be called to create the optimizer layer, so that after the prediction result is input to the loss function layer and the loss function is calculated, the parameters of the machine learning model are updated according to the loss function until a suitable The parameters enable the machine learning model to achieve the desired effect and complete the optimization of the machine learning model.
创建好训练层后,将机器学习模型的预测结果输入到前述训练层,按照一定的方法对机器学习模型的参数进行更新,如果更新参数后的机器学习模型满足预设条件,例如其对应的损失函数的值小于阈值,则停止更新参数,并将更新参数后的机器学习模型作为训练后的机器学习模型。如果更新参数后的机器学习模型不满足预设条件,例如其对应的损失 函数的值大于阈值,则可以继续更新参数,直至得到满足预设条件的机器学习模型为止。After creating the training layer, input the prediction results of the machine learning model to the aforementioned training layer, and update the parameters of the machine learning model according to a certain method. If the machine learning model after updating the parameters meets the preset conditions, such as its corresponding loss If the value of the function is less than the threshold, the update of the parameters is stopped, and the machine learning model after updating the parameters is used as the machine learning model after training. If the machine learning model after updating the parameters does not meet the preset conditions, for example, the value of the corresponding loss function is greater than the threshold, you can continue to update the parameters until a machine learning model that meets the preset conditions is obtained.
在一种可能的实施方式中,同一组张量数据会重复多次输入机器学习模型进行训练,每次输入张量数据之前,可以随机变换张量数据的顺序,以防止机器学习模型由于重复训练导致的过度训练,利用梯度下降算法时,防止损失函数收敛至局部最小值。具体地,在将所述输入数据分别输入每一所述量子线路前,可以随机变换输入数据的顺序。也可以在基于所述张量数据得到每组所述量子线路的输入数据之前,随机变换张量数据顺序,进而变换张量数据输入机器学习模型的顺序。In a possible implementation, the same set of tensor data will be repeatedly input into the machine learning model for training. Before each input of tensor data, the order of the tensor data can be randomly changed to prevent the machine learning model from being repeatedly trained. The resulting overtraining prevents the loss function from converging to a local minimum when using the gradient descent algorithm. Specifically, before the input data is respectively input into each of the quantum circuits, the sequence of the input data may be randomly changed. It is also possible to randomly change the order of the tensor data before obtaining the input data of each set of quantum circuits based on the tensor data, and then change the order of inputting the tensor data into the machine learning model.
可选地,将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型,包括:将所述预测结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值。在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述量子程序的参数。确定更新所述参数后的所述机器学习模型的所述损失函数的值。在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。Optionally, inputting the prediction result into the training layer to update the parameters of the quantum program to obtain the trained machine learning model includes: inputting the prediction result into the loss function layer, to calculate the value of the loss function of the machine learning model. When it is determined that the value of the loss function does not meet the preset condition, the value of the loss function is input into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function. Determine the value of the loss function of the machine learning model after the parameter is updated. When it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
为对创建的机器学***方,并对所有的差的平方求和计算损失函数的值。In order to train the created machine learning model, the prediction result of the machine learning model on tensor data is input into the loss function layer, and the value of the loss function of the machine learning model is calculated. The loss function represents the prediction result of the machine learning model and the actual calculation situation The gap between the labeled data. For example, the target data can include sample data and label data. After the sample data is input into the machine learning model to obtain the prediction result, the prediction result is input into the loss function layer, and the square of the difference between the label data and the prediction result is calculated, and the square of all differences is calculated. The sum calculates the value of the loss function.
进一步地,可以通过将损失函数的值与预设的阈值比较,来判断损失函数的值是否满足预设条件,例如在确定损失函数的值大于或等于阈值时,将损失函数的值输入优化器层。当然也可以通过其它方法确定损失函数的值不满足预设条件,只要能通过预设函数的值判断当前的机器学习模型不符合预期即可。不满足预设条件时,将损失函数的值输入优化器层,可以利用损失函数的值求损失函数相对于机器学习模型的参数的梯度,进而基于梯度下降算法更新机器学习模型的参数。Further, it is possible to judge whether the value of the loss function satisfies the preset condition by comparing the value of the loss function with a preset threshold, for example, when it is determined that the value of the loss function is greater than or equal to the threshold, the value of the loss function is input into the optimizer layer. Of course, other methods can also be used to determine that the value of the loss function does not meet the preset conditions, as long as the value of the preset function can be used to judge that the current machine learning model does not meet expectations. When the preset conditions are not met, the value of the loss function is input into the optimizer layer, and the value of the loss function can be used to calculate the gradient of the loss function relative to the parameters of the machine learning model, and then update the parameters of the machine learning model based on the gradient descent algorithm.
在对机器学习模型更新参数后,重新计算其对应的损失函数的值。并重新判断损失函数的值是否满足预设条件,如果不满足,可以将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述量子程序的参数,继续根据损失函数的值更新机器学习模型的参数,如果满足,则可以将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。After updating the parameters of the machine learning model, recalculate the value of the corresponding loss function. And re-judging whether the value of the loss function meets the preset condition, if not, the value of the loss function can be input into the optimizer layer, so as to update the parameters of the quantum program based on the value of the loss function, and continue according to The value of the loss function updates the parameters of the machine learning model, and if satisfied, the machine learning model after updating the parameters can be used as the machine learning model after training.
其中,在确定损失函数的值满足预设条件时,例如损失函数的值小于阈值,则表示机器学习模型对目标数据中样本数据的预测结果与标签数据的差距很小,机器学习模型能达到预期的应用效果,进而将更新参数后的机器学习模型作为训练后的机器学习模型,停止更新参数。Among them, when it is determined that the value of the loss function satisfies the preset condition, for example, the value of the loss function is less than the threshold, it means that the machine learning model predicts the sample data in the target data. The application effect, and then use the machine learning model after updating the parameters as the machine learning model after training, and stop updating the parameters.
可选地,基于所述损失函数的值更新所述量子程序的参数,包括:基于所述损失函数的值计算所述损失函数相对于所述量子程序的同一所述参数的梯度。基于多个所述梯度和梯度下降算法更新所述量子程序的所述参数。例如可以求损失函数相对于其参数的偏导数以得到损失函数相对量子程序的参数的梯度。由于前述生成了多个量子线路,进而对多个张量数据处理得到多个输出结果,对于多个输出结果,机器学习模型对其处理得到多个预测结果,多个预测结果可以得到损失函数的多个值,对于损失函数的多个值,均计算损失函数相对于同一参数的梯度,得到多个梯度。Optionally, updating the parameters of the quantum program based on the value of the loss function includes: calculating the gradient of the loss function relative to the same parameter of the quantum program based on the value of the loss function. The parameters of the quantum program are updated based on a plurality of the gradient and gradient descent algorithms. For example, the partial derivative of the loss function with respect to its parameters can be calculated to obtain the gradient of the loss function with respect to the parameters of the quantum program. Since multiple quantum circuits are generated above, multiple tensor data are processed to obtain multiple output results. For multiple output results, the machine learning model processes them to obtain multiple prediction results, and multiple prediction results can obtain the loss function. For multiple values, for multiple values of the loss function, the gradient of the loss function relative to the same parameter is calculated to obtain multiple gradients.
根据求得的多个梯度,带入梯度下降算法的相关公式中,更新机器学习模型的参数。梯度反映了损失函数变化最快的方向,通过梯度下降算法可以快速的变更参数,进而提高损失函数的值变化的速度,以快速的找到满足预设条件的损失函数的值对应的参数,得到符合要求的机器学习模型。According to the obtained multiple gradients, bring them into the relevant formulas of the gradient descent algorithm to update the parameters of the machine learning model. The gradient reflects the fastest changing direction of the loss function. The gradient descent algorithm can quickly change the parameters, thereby increasing the speed of the value change of the loss function, so as to quickly find the parameters corresponding to the value of the loss function that meets the preset conditions, and obtain the parameters that meet the The required machine learning model.
可选地,基于多个所述梯度和梯度下降算法更新所述量子程序的所述参数,包括:计算多个所述梯度的平均值。将所述平均值乘以步长得到偏置量。将所述量子程序的所述参数减去所述偏置量以更新所述量子程序的所述参数。Optionally, updating the parameters of the quantum program based on multiple gradients and gradient descent algorithms includes: calculating an average value of multiple gradients. The mean is multiplied by the step size to get the bias. The offset is subtracted from the parameter of the quantum program to update the parameter of the quantum program.
具体来讲,为更新量子程序的参数,在得到多个梯度后,可以计算多个梯度的算数平均值,例如共10个梯度,则可以将这10个梯度相加后再除以10得到平均值。得到平均值后,将平均值与步长相乘,步长可以是预设的数值,表示每次参数更新时其变化量的大小,两者相乘的乘积作为偏置量,将量子程序的参数减去偏置量得到新的参数值,在将新的参数值替换之前的参数值,完成对量子程序的参数的更新。Specifically, in order to update the parameters of the quantum program, after obtaining multiple gradients, the arithmetic mean of multiple gradients can be calculated. For example, if there are 10 gradients in total, the 10 gradients can be added and then divided by 10 to obtain the average value. After obtaining the average value, multiply the average value by the step size. The step size can be a preset value, indicating the size of the change in each parameter update. The product of the two multiplied is used as the offset value, and the quantum program Subtract the offset from the parameter to obtain a new parameter value, and replace the previous parameter value with the new parameter value to complete the update of the parameters of the quantum program.
由于生成了多个量子线路,用于执行相应的量子计算,因此可以进行分布式训练,将数据分散到不同的量子设备中训练,以提高训练的效率。Since multiple quantum circuits are generated to perform corresponding quantum calculations, distributed training can be performed, and data is distributed to different quantum devices for training to improve the efficiency of training.
基于训练完成的机器学习模型,本公开还提供一种运行机器学习模型的方法,具体可以参见图24。Based on the trained machine learning model, the present disclosure also provides a method for running the machine learning model, see FIG. 24 for details.
图24是根据一示例性实施例示出的一种机器学习模型运行方法的流程示意图,应用于包括第一机器学习框架***的电子装置,所述第一机器学习框架***包括经典模块和量子模块,所述方法包括:Fig. 24 is a schematic flowchart of a method for running a machine learning model according to an exemplary embodiment, which is applied to an electronic device including a first machine learning framework system, and the first machine learning framework system includes a classical module and a quantum module, The methods include:
步骤2410:指定经典计算层的第一运算设备和量子计算层的第二运算设备;Step 2410: designate the first computing device of the classical computing layer and the second computing device of the quantum computing layer;
步骤2420:将所述第一运算设备作为参数传入经典计算层接口,以及将所述第二运算设备作为参数传入量子计算层接口;Step 2420: Pass the first computing device as a parameter into the classical computing layer interface, and pass the second computing device as a parameter into the quantum computing layer interface;
步骤2430:调用所述经典模块通过所述经典计算层接口创建经典计算层,以及调用所述量子模块通过所述量子计算层接口创建量子计算层;Step 2430: calling the classical module to create a classical computing layer through the classical computing layer interface, and calling the quantum module to create a quantum computing layer through the quantum computing layer interface;
步骤2440:调用所述经典模块构建包括所述经典计算层和量子计算层的机器学习模型;Step 2440: calling the classical module to build a machine learning model including the classical computing layer and the quantum computing layer;
步骤2450:调用所述第一运算设备和所述第二运算设备运行所述机器学习模型。Step 2450: Call the first computing device and the second computing device to run the machine learning model.
其中,量子计算是一种遵循量子力学规律调控量子信息单元进行计算的新型计算模式,借助量子叠加和量子纠缠,能够同时处理信息的多个状态。量子计算层为包含量子线路的程序模块,可以用于实现对应量子线路的量子计算,通过对量子线路按照一定的标准进行封装,使得量子计算层便于在创建和训练机器学习模型时进行使用。对于机器学习模型通过量子计算实现的部分,均可以理解为对应的量子计算层。Among them, quantum computing is a new type of computing mode that follows the laws of quantum mechanics to control quantum information units for computing. With the help of quantum superposition and quantum entanglement, multiple states of information can be processed simultaneously. The quantum computing layer is a program module containing quantum circuits, which can be used to realize quantum computing corresponding to quantum circuits. By encapsulating the quantum circuits according to certain standards, the quantum computing layer is easy to use when creating and training machine learning models. For the part of the machine learning model realized by quantum computing, it can be understood as the corresponding quantum computing layer.
其中,经典计算是一种遵循经典物理学规律调控经典信息单元进行计算的传统计算模式,它通过一个二进制***工作,即信息使用1或0来存储。经典计算层与量子计算层相对应,其可以是对创建好的经典计算程序按照一定标准进行封装,使得经典计算层便于在创建和训练机器学习模型时进行使用。Among them, classical computing is a traditional computing mode that follows the laws of classical physics to regulate classical information units for computing. It works through a binary system, that is, information is stored using 1 or 0. The classical computing layer corresponds to the quantum computing layer, which can encapsulate the created classical computing program according to certain standards, making the classical computing layer easy to use when creating and training machine learning models.
其中,接口(interface)是一系列方法的声明,是一些方法特征的集合,一个接口只有方法的特征没有方法的实现,因此这些方法可以在不同的地方被不同的类实现,而这些实现可以具有不同的行为。经典计算层接口即为经典计算层对应的一系列方法的声明,量子计算层接口即为量子计算层对应的一系列方法的声明,下述提及的其他接口也可参照本 处解释,不再赘述。Among them, an interface is a declaration of a series of methods, and it is a collection of some method characteristics. An interface has only method characteristics and no method implementation, so these methods can be implemented by different classes in different places, and these implementations can have different behavior. The classical computing layer interface is the declaration of a series of methods corresponding to the classical computing layer, and the quantum computing layer interface is the declaration of a series of methods corresponding to the quantum computing layer. The other interfaces mentioned below can also be explained by referring to this section, no longer repeat.
其中,经典计算层包括经典神经网络层和/或激活层,经典神经网络层对应的经典神经网络层接口的类型可以如上述经典神经网络层接口的介绍,在此不在赘述。Wherein, the classic calculation layer includes a classic neural network layer and/or an activation layer, and the type of the classic neural network layer interface corresponding to the classic neural network layer can be as described above for the classic neural network layer interface, and will not be repeated here.
其中,激活层接口的类型可以如上述激活层接口的介绍,在此不在赘述。Wherein, the type of the activation layer interface may be as described above in the introduction of the activation layer interface, and will not be repeated here.
其中,量子计算层包括通用量子计算层接口(例如QuantumLayer()、QuantumLayerV2())、兼容量子计算层接口(例如Compatiblelayer())、含噪量子计算层接口(例如NoiseQuantumLayer())、量子卷积层接口(例如QConv())、量子全连接层接口(例如Qlinear());所述通用量子计算层接口用于提供基于所述第二机器学习框架***内含的量子计算编程库创建的量子程序;所述兼容量子计算层接口用于提供基于非所述第二机器学习框架***内含的量子计算编程库创建的量子程序;所述含噪量子计算层接口用于提供基于所述第二机器学习框架***内含的量子计算编程库创建的考虑噪声影响的量子程序;Among them, the quantum computing layer includes general quantum computing layer interfaces (such as QuantumLayer(), QuantumLayerV2()), compatible quantum computing layer interfaces (such as Compatiblelayer()), noisy quantum computing layer interfaces (such as NoiseQuantumLayer()), quantum convolution Layer interface (such as QConv ()), quantum fully connected layer interface (such as Qlinear ()); the general quantum computing layer interface is used to provide the quantum computing programming library created based on the second machine learning framework system. program; the compatible quantum computing layer interface is used to provide a quantum program created based on a quantum computing programming library not included in the second machine learning framework system; the noisy quantum computing layer interface is used to provide a quantum program based on the second machine learning framework system A quantum program that considers the influence of noise created by the quantum computing programming library included in the machine learning framework system;
其中,每个经典计算层接口或量子计算层接口中均包括参数machine_type_or_cloud_token,可以通过该参数实现第一运算设备或第二运算设备的指定。Wherein, each classical computing layer interface or quantum computing layer interface includes a parameter machine_type_or_cloud_token, which can be used to specify the first computing device or the second computing device.
其中,第一运算设备为经典计算设备,第二运算设备为量子运算设备。所述第一运算设备的计算硬件包括以下至少一种:中央处理器(central processing unit,CPU)、图形处理器(graphics processing unit,GPU)、张量处理器(tensor processing unit,TPU)、现场可编程逻辑门阵列(Field Programmable Gate Array,FPGA);所述第二运算设备的计算硬件包括以下至少一种:CPU、GPU、量子处理器(Quantum Processing Unit,QPU)。Wherein, the first computing device is a classical computing device, and the second computing device is a quantum computing device. The computing hardware of the first computing device includes at least one of the following: a central processing unit (central processing unit, CPU), a graphics processing unit (graphics processing unit, GPU), a tensor processing unit (tensor processing unit, TPU), an on-site A programmable logic gate array (Field Programmable Gate Array, FPGA); the computing hardware of the second computing device includes at least one of the following: CPU, GPU, and quantum processor (Quantum Processing Unit, QPU).
具体的,所述第一机器学习框架***还包括数据结构模块,所述调用所述经典模块构建包括所述经典计算层和量子计算层的机器学习模型,包括:Specifically, the first machine learning framework system also includes a data structure module, and the calling of the classical module to construct a machine learning model including the classical computing layer and the quantum computing layer includes:
调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述量子计算层之间的前向传播关系;Calling the classical module to construct a classical computing layer and calling the data structure module to construct a forward propagation relationship between the classical computing layer and the quantum computing layer;
调用所述经典模块将所述经典计算层、所述量子计算层以及所述经典计算层与所述量子计算层之间的前向传播关系进行封装,得到机器学习模型。Invoking the classical module to encapsulate the classical computing layer, the quantum computing layer, and the forward propagation relationship between the classical computing layer and the quantum computing layer to obtain a machine learning model.
其中,前向传播为将上一计算层的输出作为下一计算层的输入,并计算下一计算层的输出,一直运算到没有下一计算层为止,其中计算层可以为上述的经典计算层也可以为上述的量子计算层。Among them, the forward propagation is to use the output of the previous computing layer as the input of the next computing layer, and calculate the output of the next computing layer until there is no next computing layer, where the computing layer can be the above-mentioned classical computing layer It can also be the above-mentioned quantum computing layer.
可选的,所述经典模块还包括抽象类子模块,所述调用所述经典模块将所述经典计算层、所述量子计算层以及所述经典计算层与所述量子计算层之间的前向传播关系进行封装,得到机器学习模型,包括:Optionally, the classical module further includes an abstract class submodule, and the calling of the classical module integrates the classical computing layer, the quantum computing layer, and the preceding layer between the classical computing layer and the quantum computing layer Encapsulate the propagation relationship to obtain a machine learning model, including:
调用所述抽象类子模块基于初始化函数对所述量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述量子计算层和所述经典计算层;calling the abstract class submodule to initialize and encapsulate the quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialized and encapsulated quantum computing layer and the classical computing layer;
调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;Calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship;
调用所述抽象类子模块基于模块类对所述初始化和封装后的所述量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。The abstract class submodule is called to encapsulate the initialized and encapsulated quantum computing layer and the classical computing layer, as well as the encapsulated forward propagation relationship based on the module class, to obtain a machine learning model.
以上步骤的详细解释参与上述实施例介绍,在此不再赘述。The detailed explanation of the above steps is involved in the introduction of the above embodiments, and will not be repeated here.
与现有技术相比,本公开在提供的包括经典模块和量子模块的机器学习框架***下构建机器学习模型,该机器学习模型的经典计算层由经典模块通过经典计算层接口创建,量 子计算层由量子模块通过量子计算层接口创建,在创建之时即指定经典计算层的第一运算设备作为经典计算层接口的参数和指定量子计算层的第二运算设备作为量子计算层接口的参数,从而实现了在运行时,可以通过第一运算设备运行经典计算层和第二运算设备运行量子计算层,实现了对不同运算设备的无缝切换和统一调用,提高运算设备使用的统一性。Compared with the prior art, this disclosure builds a machine learning model under the provided machine learning framework system including classical modules and quantum modules. The classical computing layer of the machine learning model is created by the classical module through the interface of the classical computing layer, and the quantum computing layer Created by the quantum module through the quantum computing layer interface, when it is created, the first computing device of the classical computing layer is designated as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer is designated as the parameter of the quantum computing layer interface, thus At runtime, the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
在本公开提供的一实施例中,所述调用所述第一运算设备和所述第二运算设备运行所述机器学习模型,包括:In an embodiment provided by the present disclosure, the calling the first computing device and the second computing device to run the machine learning model includes:
在确定所述第二运算设备支持的计算格式不包括所述量子计算层的输入数据的格式时,调用所述经典模块通过数据格式转换接口将所述量子计算层的输入数据的格式转换为所述第二运算设备支持的计算格式;When it is determined that the computing format supported by the second computing device does not include the format of the input data of the quantum computing layer, the classical module is called to convert the format of the input data of the quantum computing layer into the format of the input data through the data format conversion interface. The computing format supported by the second computing device;
调用所述第二运算设备运行所述量子计算层计算所述输入数据对应的输出数据。Invoking the second computing device to run the quantum computing layer to calculate the output data corresponding to the input data.
其中,所述调用所述第一运算设备和所述第二运算设备运行所述机器学习模型,还包括:Wherein, the calling the first computing device and the second computing device to run the machine learning model also includes:
在确定所述第一运算设备支持的计算格式不包括所述经典计算层的输入数据的格式时,调用所述经典模块通过数据格式转换接口将所述经典计算层的输入数据的格式转换为所述第一运算设备支持的计算格式;When it is determined that the computing format supported by the first computing device does not include the format of the input data of the classical computing layer, the classical module is called to convert the format of the input data of the classical computing layer into the format of the input data through the data format conversion interface The computing format supported by the first computing device;
调用所述第一运算设备运行所述经典计算层计算所述输入数据对应的输出数据。Invoking the first computing device to run the classical computing layer to compute output data corresponding to the input data.
具体来说,机器学习模型包括多个计算层,计算层包括经典计算层和量子计算层,每个计算层可以是通过不同的运算设备进行运算的,因此上一计算层的输出数据的计算格式不一定是下一计算层的输入数据支持的计算格式,因此需要进行数据格式的转换。数据格式的转换可以通过数据格式转换接口实现,数据格式转换接口例如可以为:void to CPU(int dev=DEV_CPU)、void to GPU(int dev=DEV_GPU)、void to TPU(int dev=DEV_TPU)、void to FPGA(int dev=DEV_FPGA)、void to QPU(int dev=DEV_QPU)。void to CPU(int dev=DEV_CPU)、void to GPU(int dev=DEV_GPU)、void to TPU(int dev=DEV_TPU)、void to FPGA(int dev=DEV_FPGA)、void to QPU(int dev=DEV_QPU)分别为将数据转换成CPU、GPU、TPU、FPGA、QPU格式。Specifically, the machine learning model includes multiple computing layers. The computing layer includes the classical computing layer and the quantum computing layer. Each computing layer can be operated by different computing devices. Therefore, the computing format of the output data of the previous computing layer It is not necessarily the computing format supported by the input data of the next computing layer, so data format conversion is required. The data format conversion can be realized through the data format conversion interface. The data format conversion interface can be, for example: void to CPU(int dev=DEV_CPU), void to GPU(int dev=DEV_GPU), void to TPU(int dev=DEV_TPU), void to FPGA(int dev=DEV_FPGA), void to QPU(int dev=DEV_QPU). void to CPU(int dev=DEV_CPU), void to GPU(int dev=DEV_GPU), void to TPU(int dev=DEV_TPU), void to FPGA(int dev=DEV_FPGA), void to QPU(int dev=DEV_QPU) respectively To convert data into CPU, GPU, TPU, FPGA, QPU formats.
在本公开提供的一实施例中,若所述第二运算设备的计算硬件为CPU或GPU,则所述第二运算设备为量子虚拟机;所述调用所述第一运算设备和所述第二运算设备运行机器学习模型之前,所述方法还包括:In an embodiment provided by the present disclosure, if the computing hardware of the second computing device is CPU or GPU, then the second computing device is a quantum virtual machine; the calling the first computing device and the second computing device Before the second computing device runs the machine learning model, the method also includes:
其中,第二机器学习框架***内含的量子计算编程库例如可以为Qpanda、Qsikit、Cirq、Forest、Q#、qbsolv、Blackbird等,在此不做限定。Wherein, the quantum computing programming library included in the second machine learning framework system can be, for example, Qpanda, Qsikit, Cirq, Forest, Q#, qbsolv, Blackbird, etc., which are not limited here.
基于所述第二机器学习框架***内含的量子计算编程库构造所述量子虚拟机。The quantum virtual machine is constructed based on the quantum computing programming library included in the second machine learning framework system.
其中,虚拟机(Virtual Machine)指通过软件模拟的具有完整硬件***功能的、运行在一个完全隔离环境中的的完整计算机***。量子虚拟机就是通过经典计算机模拟量子计算机的完整计算机***。Among them, a virtual machine (Virtual Machine) refers to a complete computer system that is simulated by software and has complete hardware system functions and runs in a completely isolated environment. A quantum virtual machine is a complete computer system that simulates a quantum computer through a classical computer.
在本公开提供的一实施例中,所述基于所述第二机器学习框架***内含的量子计算编程库构造所述量子虚拟机,包括:In an embodiment provided by the present disclosure, constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
将指定计算硬件的量子机器类型作为参数传入所述第二机器学习框架***内含的量子计算编程库中的初始化函数,以及通过所述初始化函数创建所述量子虚拟机。The quantum machine type of the specified computing hardware is passed as a parameter to the initialization function in the quantum computing programming library included in the second machine learning framework system, and the quantum virtual machine is created through the initialization function.
其中,初始化函数为init(),虚拟机的具体创建方式为init(QMachineType::CPU), 该种量子虚拟机创建方式会在代码中生成一个全局的量子虚拟机qvm。Among them, the initialization function is init(), and the specific creation method of the virtual machine is init(QMachineType::CPU), which will generate a global quantum virtual machine qvm in the code.
在本公开提供的一实施例中,所述基于所述第二机器学习框架***内含的量子计算编程库构造所述量子虚拟机,包括:In an embodiment provided by the present disclosure, constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
将指定计算硬件的量子机器类型作为参数传入所述第二机器学习框架***内含的量子计算编程库中的量子机器初始化接口,以及通过所述量子机器初始化接口创建所述量子虚拟机。The quantum machine type of the specified computing hardware is passed as a parameter to the quantum machine initialization interface in the quantum computing programming library contained in the second machine learning framework system, and the quantum virtual machine is created through the quantum machine initialization interface.
其中,量子机器初始化接口为initQuantumMachine(),虚拟机的具体创建方式为Auto qvm=initQuantumMachine(QMachineType::CPU),该种虚拟虚拟机创建方式通过量子机器初始化接口得到量子虚拟机指针。Among them, the quantum machine initialization interface is initQuantumMachine(), and the specific creation method of the virtual machine is Auto qvm=initQuantumMachine(QMachineType::CPU). This virtual virtual machine creation method obtains the quantum virtual machine pointer through the quantum machine initialization interface.
在本公开提供的一实施例中,所述基于所述第二机器学习框架***内含的量子计算编程库构造所述量子虚拟机,包括:In an embodiment provided by the present disclosure, constructing the quantum virtual machine based on the quantum computing programming library included in the second machine learning framework system includes:
基于所述第二机器学习框架***内含的量子计算编程库中的指定计算硬件的量子虚拟机类创建所述量子虚拟机。The quantum virtual machine is created based on the quantum virtual machine class of the specified computing hardware in the quantum computing programming library included in the second machine learning framework system.
其中,指定计算硬件的量子虚拟机类例如可以为CPUQVM、GPUQVM或是其他,虚拟机的具体创建方式例如可以为CPUQVM*qvm=new CPUQVM,该种虚拟虚拟机创建方式直接新建一个qvm类。Wherein, the quantum virtual machine class of the specified computing hardware can be, for example, CPUQVM, GPUQVM or others, and the specific creation method of the virtual machine can be, for example, CPUQVM*qvm=new CPUQVM, and this kind of virtual virtual machine creation method directly creates a new qvm class.
参见图25,图25是根据一示例性实施例示出的一种机器学习模型运行装置的结构示意图,应用于包括机器学习框架***的电子装置,所述机器学习框架***包括经典模块和量子模块,所述运行装置2500包括:Referring to Fig. 25, Fig. 25 is a schematic structural diagram of a machine learning model running device according to an exemplary embodiment, which is applied to an electronic device including a machine learning framework system, and the machine learning framework system includes a classical module and a quantum module, The operating device 2500 includes:
运算设备确定单元2510,用于指定经典计算层的第一运算设备和量子计算层的第二运算设备;A computing device determining unit 2510, configured to specify the first computing device of the classical computing layer and the second computing device of the quantum computing layer;
接口确定单元2520,用于将所述第一运算设备作为参数传入经典计算层接口,以及将所述第二运算设备作为参数传入量子计算层接口;An interface determining unit 2520, configured to pass the first computing device into the classical computing layer interface as a parameter, and pass the second computing device into the quantum computing layer interface as a parameter;
计算层创建单元2530,用于调用所述经典模块通过所述经典计算层接口创建经典计算层,以及调用所述量子模块通过所述量子计算层接口创建量子计算层;A computing layer creation unit 2530, configured to call the classical module to create a classical computing layer through the classical computing layer interface, and call the quantum module to create a quantum computing layer through the quantum computing layer interface;
模型运行单元2540,用于调用所述经典模块构建包括所述经典计算层和量子计算层的机器学习模型,以及调用所述第一运算设备和所述第二运算设备所述运行机器学习模型。The model running unit 2540 is configured to call the classical module to build a machine learning model including the classical computing layer and the quantum computing layer, and call the first computing device and the second computing device to run the machine learning model.
可选的,在所述调用所述第一运算设备和所述第二运算设备运行所述机器学习模型方面,所述模型运行单元2540,具体用于:Optionally, in terms of calling the first computing device and the second computing device to run the machine learning model, the model running unit 2540 is specifically configured to:
在确定所述第二运算设备支持的计算格式不包括所述量子计算层的输入数据的格式时,调用所述经典模块通过数据格式转换接口将所述量子计算层的输入数据的格式转换为所述第二运算设备支持的计算格式;When it is determined that the computing format supported by the second computing device does not include the format of the input data of the quantum computing layer, the classical module is called to convert the format of the input data of the quantum computing layer into the format of the input data through the data format conversion interface. The computing format supported by the second computing device;
调用所述第二运算设备运行所述量子计算层计算所述输入数据对应的输出数据。Invoking the second computing device to run the quantum computing layer to calculate the output data corresponding to the input data.
可选的,所述第一运算设备的计算硬件包括以下至少一种:中央处理器CPU、图形处理器GPU、张量处理器TPU、现场可编程逻辑门阵列FPGA;所述第二运算设备的计算硬件包括以下至少一种:CPU、GPU、量子处理器QPU。Optionally, the computing hardware of the first computing device includes at least one of the following: a central processing unit CPU, a graphics processing unit GPU, a tensor processor TPU, a field programmable logic gate array FPGA; Computing hardware includes at least one of the following: CPU, GPU, quantum processor QPU.
可选的,若所述第二运算设备的计算硬件为CPU或GPU,则所述第二运算设备为量子虚拟机;所述调用所述第一运算设备和所述第二运算设备运行机器学习模型之前,所述运算设备确定单元2510,还用于:Optionally, if the computing hardware of the second computing device is CPU or GPU, then the second computing device is a quantum virtual machine; the calling of the first computing device and the second computing device to run machine learning Before the model, the computing device determination unit 2510 is also used to:
基于所述机器学习框架***内含的量子计算编程库构造所述量子虚拟机。The quantum virtual machine is constructed based on the quantum computing programming library contained in the machine learning framework system.
可选的,所述基于所述机器学习框架***内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元2510,具体用于:Optionally, the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
将指定计算硬件的量子机器类型作为参数传入所述机器学习框架***内含的量子计算编程库中的初始化函数,以及通过所述初始化函数创建所述量子虚拟机。The quantum machine type of the specified computing hardware is passed as a parameter to the initialization function in the quantum computing programming library included in the machine learning framework system, and the quantum virtual machine is created through the initialization function.
可选的,所述基于所述机器学习框架***内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元2510,具体用于:Optionally, the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
将指定计算硬件的量子机器类型作为参数传入所述机器学习框架***内含的量子计算编程库中的量子机器初始化接口,以及通过所述量子机器初始化接口创建所述量子虚拟机。The quantum machine type of the specified computing hardware is passed as a parameter to the quantum machine initialization interface in the quantum computing programming library contained in the machine learning framework system, and the quantum virtual machine is created through the quantum machine initialization interface.
可选的,所述基于所述机器学习框架***内含的量子计算编程库构造所述量子虚拟机,所述运算设备确定单元2510,具体用于:Optionally, the quantum virtual machine is constructed based on the quantum computing programming library included in the machine learning framework system, and the computing device determination unit 2510 is specifically used for:
基于所述机器学习框架***内含的量子计算编程库中的指定计算硬件的量子虚拟机类创建所述量子虚拟机。The quantum virtual machine is created based on the quantum virtual machine class of the specified computing hardware in the quantum computing programming library included in the machine learning framework system.
与现有技术相比,本公开在提供的包括经典模块和量子模块的机器学习框架***下构建机器学习模型,该机器学习模型的经典计算层由经典模块通过经典计算层接口创建,量子计算层由量子模块通过量子计算层接口创建,在创建之时即指定经典计算层的第一运算设备作为经典计算层接口的参数和指定量子计算层的第二运算设备作为量子计算层接口的参数,从而实现了在运行时,可以通过第一运算设备运行经典计算层和第二运算设备运行量子计算层,实现了对不同运算设备的无缝切换和统一调用,提高运算设备使用的统一性。Compared with the prior art, this disclosure builds a machine learning model under the provided machine learning framework system including classical modules and quantum modules. The classical computing layer of the machine learning model is created by the classical module through the interface of the classical computing layer, and the quantum computing layer Created by the quantum module through the quantum computing layer interface, when it is created, the first computing device of the classical computing layer is designated as the parameter of the classical computing layer interface and the second computing device of the quantum computing layer is designated as the parameter of the quantum computing layer interface, thus At runtime, the classical computing layer can be run by the first computing device and the quantum computing layer can be run by the second computing device, which realizes seamless switching and unified calling of different computing devices, and improves the uniformity of computing devices.
在上述实施例中,本公开的机器学习框架***中的经典模块还被配置为通过经典计算层接口创建经典计算层,所述经典计算层接口的参数包括所述经典计算层的第一运算设备;量子模块还被配置为通过量子计算层接口创建量子计算层,所述量子计算层接口的参数包括所述量子计算层的第二运算设备;所述经典模块还被配置为构建包括所述经典计算层和量子计算层的机器学习模型,以及调用所述第一运算设备和所述第二运算设备运行所述机器学习模型。此外,经典模块还被配置为在确定所述第二运算设备支持的计算格式不包括所述量子计算层的输入数据的格式时,通过数据格式转换接口将所述量子计算层的输入数据的格式转换为所述第二运算设备支持的计算格式。In the above embodiment, the classic module in the machine learning framework system of the present disclosure is further configured to create a classic computing layer through the classic computing layer interface, and the parameters of the classic computing layer interface include the first computing device of the classic computing layer The quantum module is also configured to create a quantum computing layer through the quantum computing layer interface, and the parameters of the quantum computing layer interface include the second computing device of the quantum computing layer; the classical module is also configured to construct the classical The machine learning model of the computing layer and the quantum computing layer, and calling the first computing device and the second computing device to run the machine learning model. In addition, the classical module is also configured to convert the input data format of the quantum computing layer to Convert to a computing format supported by the second computing device.
参见图26,图26是根据一示例性实施例示出的一种基于机器学习框架***的模型训练装置的框图,可以应用于包括如图3所示的第一机器学习框架***30的电子设备,所述第一机器学习框架***30包括数据结构模块31、量子模块32和经典模块33,如图15所示,该训练装置2600包括:Referring to FIG. 26, FIG. 26 is a block diagram of a model training device based on a machine learning framework system according to an exemplary embodiment, which can be applied to an electronic device including the first machine learning framework system 30 shown in FIG. 3, The first machine learning framework system 30 includes a data structure module 31, a quantum module 32 and a classical module 33, as shown in Figure 15, the training device 2600 includes:
创建模块2610,用于调用所述数据结构模块31获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块32和/或所述经典模块33创建机器学习模型; Creation module 2610, used to call the data structure module 31 to obtain input training data, create tensor data including the training data, and call the quantum module 32 and/or the classical module 33 to create a machine learning model;
输入训练模块2620,用于将所述张量数据输入所述机器学习模型,并调用所述经典模块33创建所述机器学习模型的训练层;The input training module 2620 is used for inputting the tensor data into the machine learning model, and calling the classic module 33 to create a training layer of the machine learning model;
参数更新模块2630,用于将所述机器学习模型的输出结果输入所述训练层,并基于所述训练层的输出结果对所述机器学习模型的参数进行更新,以得到训练后的所述机器学习模型。A parameter update module 2630, configured to input the output result of the machine learning model into the training layer, and update the parameters of the machine learning model based on the output result of the training layer, so as to obtain the trained machine learning model.
关于上述实施例中的装置,其中各个模块执行操作的具体方式已经在有关该方法的实 施例中进行了详细描述,此处将不做详细阐述说明。Regarding the apparatus in the above embodiments, the specific manner in which each module executes operations has been described in detail in the embodiments of the method, and will not be described in detail here.
本公开实施例还提供了一种存储介质,所述存储介质中存储有计算机程序,其中所述计算机程序被设置为运行时执行上述附图1所述的模型训练方法。An embodiment of the present disclosure also provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the model training method described in FIG. 1 above when running.
本公开实施例还提供了一种电子装置,包括存储器和处理器,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行上述附图1所述的模型训练方法。An embodiment of the present disclosure also provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to perform the model training described in the above-mentioned accompanying drawing 1 method.
如在本说明书和权利要求书中所使用的那样,术语“如果”可以依据上下文被解释为“当...时”或“一旦”或“响应于确定”或“响应于检测到”。类似地,短语“如果确定”或“如果检测到[所描述条件或事件]”可以依据上下文被解释为意指“一旦确定”或“响应于确定”或“一旦检测到[所描述条件或事件]”或“响应于检测到[所描述条件或事件]”。As used in this specification and claims, the term "if" may be interpreted as "when" or "once" or "in response to determining" or "in response to detecting" depending on the context. Similarly, the phrase "if determined" or "if [the described condition or event] is detected" may be construed, depending on the context, to mean "once determined" or "in response to the determination" or "once detected [the described condition or event] ]” or “in response to detection of [described condition or event]”.
虽然本公开的实施方式如上,但所述内容只是为便于理解本公开而采用的实施例,并非用以限定本公开的范围和应用场景。任何本公开所述技术领域内的技术人员,在不脱离本公开所揭露的精神和范围的前提下,可以在实施的形式上及细节上作任何的修改与变化,但本公开的专利保护范围,仍须以所附的权利要求书所界定的范围为准。Although the embodiments of the present disclosure are as above, the content described is only an embodiment adopted for the convenience of understanding the present disclosure, and is not intended to limit the scope and application scenarios of the present disclosure. Anyone skilled in the technical field described in the present disclosure can make any modifications and changes in the form and details of the implementation without departing from the spirit and scope disclosed in the present disclosure, but the patent protection scope of the present disclosure , must still be subject to the scope defined by the appended claims.

Claims (28)

  1. 一种基于机器学习框架***的模型训练方法,其特征在于,应用于包括第一机器学习框架***的电子设备,所述第一机器学习框架***包括数据结构模块、量子模块和经典模块,所述方法包括:A model training method based on a machine learning framework system, characterized in that it is applied to electronic equipment including a first machine learning framework system, and the first machine learning framework system includes a data structure module, a quantum module and a classical module, and the Methods include:
    调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型;calling the data structure module to obtain input training data, creating tensor data including the training data, and calling the quantum module and/or the classical module to create a machine learning model;
    将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层;Input the tensor data into the machine learning model, and call the classic module to create the training layer of the machine learning model;
    将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型。Inputting the output result of the machine learning model into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model.
  2. 如权利要求1所述的方法,其特征在于,所述训练层包括损失函数层和优化器层,所述经典模块包括:The method according to claim 1, wherein the training layer includes a loss function layer and an optimizer layer, and the classic module includes:
    损失函数单元,被配置为计算所述机器学习模型的损失函数;a loss function unit configured to calculate a loss function of the machine learning model;
    优化器单元,被配置为在训练所述机器学习模型时基于所述损失函数更新所述机器学习模型的参数,以对所述机器学习模型进行优化;An optimizer unit configured to update parameters of the machine learning model based on the loss function when training the machine learning model, so as to optimize the machine learning model;
    所述调用所述经典模块创建所述机器学习模型的训练层,包括:The calling of the classic module to create the training layer of the machine learning model includes:
    调用所述损失函数单元创建所述损失函数层;calling the loss function unit to create the loss function layer;
    调用所述优化器单元创建所述优化器层。The optimizer unit is invoked to create the optimizer layer.
  3. 如权利要求2所述的方法,其特征在于,所述将所述机器学习模型的输出结果输入所述训练层,以对所述机器学习模型的参数进行更新,得到训练后的所述机器学习模型,包括:The method according to claim 2, wherein the output result of the machine learning model is input into the training layer to update the parameters of the machine learning model to obtain the trained machine learning model. models, including:
    将所述机器学习模型的输出结果输入所述损失函数层,以计算所述机器学习模型的损失函数的值;inputting the output result of the machine learning model into the loss function layer to calculate the value of the loss function of the machine learning model;
    在确定所述损失函数的值不满足预设条件时,将所述损失函数的值输入所述优化器层,以基于所述损失函数的值更新所述机器学习模型的参数;When it is determined that the value of the loss function does not meet the preset condition, input the value of the loss function into the optimizer layer, so as to update the parameters of the machine learning model based on the value of the loss function;
    确定更新所述参数后的所述机器学习模型的所述损失函数的值;determining a value of the loss function of the machine learning model after updating the parameters;
    在确定该损失函数的值满足预设条件时,将更新所述参数后的所述机器学习模型作为训练后的所述机器学习模型。When it is determined that the value of the loss function satisfies the preset condition, the machine learning model after updating the parameters is used as the machine learning model after training.
  4. 如权利要求3所述的方法,其特征在于,所述基于所述损失函数的值更新所述机器学习模型的参数,包括:The method according to claim 3, wherein updating the parameters of the machine learning model based on the value of the loss function comprises:
    基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度;calculating gradients of the loss function with respect to parameters of the machine learning model based on values of the loss function;
    基于所述梯度和梯度下降算法更新所述机器学习模型的参数。Updating parameters of the machine learning model based on the gradient and gradient descent algorithm.
  5. 如权利要求4所述的方法,其特征在于,若调用所述量子模块创建机器学习模型,所述机器学习模型包括基于所述量子模块创建的量子计算层,所述基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度,包括:The method according to claim 4, wherein if the quantum module is called to create a machine learning model, the machine learning model includes a quantum computing layer created based on the quantum module, and the value based on the loss function Calculating the gradient of the loss function with respect to the parameters of the machine learning model, including:
    基于所述损失函数的值计算所述损失函数相对于所述量子计算层的输出结果的第一梯度,并计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度;calculating a first gradient of the loss function relative to the output of the quantum computing layer based on the value of the loss function, and calculating a second gradient of the output of the quantum computing layer relative to the parameters of the quantum computing layer ;
    将所述第一梯度与所述第二梯度相乘得到所述损失函数相对于所述机器学习模型的参数的梯度。Multiplying the first gradient and the second gradient to obtain the gradient of the loss function relative to the parameters of the machine learning model.
  6. 如权利要求5所述的方法,其特征在于,所述计算所述量子计算层的输出结果相 对于所述量子计算层的参数的第二梯度,包括:The method according to claim 5, wherein said calculation of the output result of said quantum computing layer with respect to the second gradient of the parameter of said quantum computing layer comprises:
    计算所述量子计算层的参数加上第一偏置量后所述量子计算层的第一输出结果,并计算所述量子计算层的参数减去第二偏置量后所述量子计算层的第二输出结果;Calculate the first output result of the quantum computing layer after the parameters of the quantum computing layer plus the first offset, and calculate the parameters of the quantum computing layer minus the second offset of the quantum computing layer The second output result;
    基于所述第一输出结果与所述第二输出结果的差值,计算所述量子计算层的输出结果相对于所述量子计算层的参数的第二梯度。Based on the difference between the first output result and the second output result, calculating a second gradient of the output result of the quantum computing layer relative to the parameters of the quantum computing layer.
  7. 如权利要求4所述的方法,其特征在于,所述将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层之后,所述方法还包括:The method according to claim 4, wherein after said inputting said tensor data into said machine learning model, and calling said classic module to create a training layer of said machine learning model, said method further comprises :
    以所述损失函数、所述机器学习模型的参数、以及基于该参数计算得到所述损失函数过程中的每一子步骤的输出结果为计算节点,并以该损失函数、该参数、该子步骤的输出结果之间的关系作为边,构建有向的计算图;Taking the loss function, the parameters of the machine learning model, and the output results of each sub-step in the process of calculating the loss function based on the parameters as calculation nodes, and using the loss function, the parameters, and the sub-steps The relationship between the output results is used as an edge to construct a directed calculation graph;
    所述基于所述损失函数的值计算该损失函数相对于所述机器学习模型的参数的梯度,包括:The calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function includes:
    基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度。Gradients of the loss function with respect to parameters of the machine learning model are calculated based on values of the loss function and the computational graph.
  8. 如权利要求7所述的方法,其特征在于,所述基于所述损失函数的值和所述计算图计算该损失函数相对于所述机器学习模型的参数的梯度,包括:The method according to claim 7, wherein the calculating the gradient of the loss function relative to the parameters of the machine learning model based on the value of the loss function and the calculation graph comprises:
    确定所述计算图中所述损失函数对应计算节点至所述机器学习模型的参数对应计算节点的路径;determining a path from the calculation node corresponding to the loss function in the calculation graph to the calculation node corresponding to the parameter of the machine learning model;
    基于所述损失函数的值计算该路径上非叶子节点的每一计算节点相对于该计算节点的前驱节点的中间梯度;calculating the intermediate gradient of each calculation node of the non-leaf node on the path relative to the predecessor node of the calculation node based on the value of the loss function;
    将计算得到的所有所述中间梯度相乘得到所述损失函数相对于所述参数的梯度。The gradient of the loss function relative to the parameter is obtained by multiplying all the calculated intermediate gradients together.
  9. 如权利要求1所述的方法,其特征在于,创建所述机器学习模型还包括:The method according to claim 1, wherein creating the machine learning model further comprises:
    调用所述量子模块创建包括量子机器学习程序、第一预设训练程序以及第一训练接口的量子计算层,所述第一训练接口用于调用所述第一预设训练程序训练所述量子机器学习程序;Calling the quantum module to create a quantum computing layer including a quantum machine learning program, a first preset training program and a first training interface, the first training interface is used to call the first preset training program to train the quantum machine learning process;
    调用所述经典模块创建包括经典机器学习程序、第二预设训练程序以及第二训练接口的经典计算层,所述第二训练接口用于调用所述第二预设训练程序训练所述经典机器学习程序;Calling the classic module to create a classic computing layer including a classic machine learning program, a second preset training program and a second training interface, the second training interface is used to call the second preset training program to train the classic machine learning process;
    调用所述经典模块创建包括所述量子计算层和所述经典计算层的机器学习模型。The classical module is called to create a machine learning model including the quantum computing layer and the classical computing layer.
  10. 如权利要求9所述的方法,其特征在于,所述训练层还进一步用于通过所述第一训练接口调用所述第一预设训练程序,并通过所述第二训练接口调用所述第二预设训练程序。The method according to claim 9, wherein the training layer is further configured to call the first preset training program through the first training interface, and call the first preset training program through the second training interface. Two preset training procedures.
  11. 如权利要求10所述的方法,其特征在于,还包括通过以下操作得到训练后的所述机器学习模型:The method according to claim 10, further comprising obtaining the trained machine learning model through the following operations:
    将所述机器学习模型的输出结果输入所述训练层,以通过所述第一训练接口调用所述第一预设训练程序,根据所述第一预设训练程序训练所述量子机器学习程序,并通过所述第二训练接口调用所述第二预设训练程序,根据所述第二预设训练程序训练所述经典机器学习程序,得到训练后的机器学习模型。inputting the output result of the machine learning model into the training layer, so as to call the first preset training program through the first training interface, and train the quantum machine learning program according to the first preset training program, And call the second preset training program through the second training interface, train the classic machine learning program according to the second preset training program, and obtain the trained machine learning model.
  12. 如权利要求11所述的方法,其特征在于,所述第一预设训练程序包括用于计算所述量子机器学习程序的输出相对于所述量子机器学习程序的第一因变量的梯度的第一 梯度计算程序,所述第一因变量为所述量子机器学习程序的输入或参数;The method of claim 11, wherein said first preset training program includes a second step for computing the gradient of the output of said quantum machine learning program with respect to a first dependent variable of said quantum machine learning program. A gradient calculation program, the first dependent variable is an input or parameter of the quantum machine learning program;
    和/或,所述第二预设训练程序包括用于计算所述经典机器学习程序的输出相对于所述经典机器学习程序的第二因变量的梯度的第二梯度计算程序,所述第二因变量为所述经典机器学习程序的输入或参数。And/or, the second preset training program includes a second gradient calculation program for calculating the gradient of the output of the classical machine learning program relative to the second dependent variable of the classic machine learning program, the second The dependent variable is the input or parameter of the classical machine learning program.
  13. 如权利要求12所述的方法,其特征在于,所述第一梯度计算程序用于根据从所述第一训练接口传入的梯度计算方法参数,选择有限差分法或参数位移法计算所述梯度。The method according to claim 12, wherein the first gradient calculation program is used to select a finite difference method or a parameter shift method to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface .
  14. 如权利要求13所述的方法,其特征在于,在所述根据所述第一训练接口传入的梯度计算方法参数,选择参数位移法计算所述梯度时,所述第一梯度计算程序包括:The method according to claim 13, characterized in that, when the parameter displacement method is selected to calculate the gradient according to the parameters of the gradient calculation method imported from the first training interface, the first gradient calculation program includes:
    参数减子程序,用于将所述量子机器学习程序的所述第一因变量减去第一偏置量,并将目标数据输入所述量子机器学习程序,得到第一结果;A parameter subtraction subroutine for subtracting a first offset from the first dependent variable of the quantum machine learning program, and inputting target data into the quantum machine learning program to obtain a first result;
    参数加子程序,用于将所述量子机器学习程序的所述第一因变量加上第二偏置量,并将目标数据输入所述量子机器学习程序,得到第二结果;A parameter adding subroutine for adding a second bias to the first dependent variable of the quantum machine learning program, and inputting target data into the quantum machine learning program to obtain a second result;
    计算子程序,用于根据所述第一结果和所述第二结果计算所述梯度;a calculation subroutine for calculating the gradient according to the first result and the second result;
    其中,所述目标数据为输入所述量子机器学习程序的数据。Wherein, the target data is the data input into the quantum machine learning program.
  15. 如权利要求1所述的方法,其特征在于,所述量子模块还包括含噪量子程序封装单元,并且创建所述机器学习模型还包括:The method according to claim 1, wherein the quantum module also includes a noisy quantum program encapsulation unit, and creating the machine learning model also includes:
    基于所述第一机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序;Create a quantum program considering the impact of noise based on the quantum computing programming library contained in the first machine learning framework system;
    将所述量子程序作为已封装的含噪量子计算层接口的参数,以及传入所述含噪量子计算层接口;Using the quantum program as a parameter of the encapsulated noise-containing quantum computing layer interface, and passing it into the noise-containing quantum computing layer interface;
    调用所述含噪量子程序封装单元通过所述含噪量子计算层接口创建含噪量子计算层;以及调用所述经典模块创建包括所述含噪量子计算层的机器学习模型。calling the noise-containing quantum program encapsulation unit to create a noise-containing quantum computing layer through the noise-containing quantum computing layer interface; and calling the classical module to create a machine learning model including the noise-containing quantum computing layer.
  16. 如权利要求15所述的方法,其特征在于,所述基于所述第一机器学习框架***内含的量子计算编程库创建考虑噪声影响的量子程序,包括:The method according to claim 15, wherein said creation of a quantum program considering the influence of noise based on the quantum computing programming library contained in the first machine learning framework system includes:
    基于所述第一机器学习框架***内含的量子计算编程库申请含噪声量子虚拟机,以及设置运行于所述含噪声虚拟机上的量子线路的噪声;Applying for a noise-containing quantum virtual machine based on the quantum computing programming library contained in the first machine learning framework system, and setting the noise of the quantum circuit running on the noise-containing virtual machine;
    申请量子比特以及创建作用于所述量子比特上的量子逻辑门,得到运行于所述含噪声虚拟机上的量子线路;Applying for qubits and creating quantum logic gates acting on the qubits to obtain quantum circuits running on the noise-containing virtual machine;
    将所述含噪声量子虚拟机、所述噪声模型和所述量子线路进行封装,得到考虑噪声影响的量子程序。Encapsulate the noise-containing quantum virtual machine, the noise model and the quantum circuit to obtain a quantum program considering the influence of noise.
  17. 如权利要求16所述的方法,其特征在于,所述噪声包括以下至少一种:逻辑门噪声、量子比特的量子态重置噪声、量子比特的测量噪声、量子比特的读取噪声。The method according to claim 16, wherein the noise comprises at least one of the following: logic gate noise, qubit quantum state reset noise, qubit measurement noise, and qubit read noise.
  18. 如权利要求17所述的方法,其特征在于,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:The method according to claim 17, wherein the noise is logic gate noise, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
    将指定的量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;Using the specified quantum logic gate type, noise model type, and parameters required by the noise model type as parameters of the logic gate noise interface, and passing in the logic gate noise interface;
    通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的所有量子比特生效;Setting the logic gate noise of the quantum circuit running on the noise-containing virtual machine through the logic gate noise interface, and the logic gate noise takes effect on all qubits in the quantum circuit;
    和/或,所述噪声为逻辑门噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:And/or, the noise is logic gate noise, and the noise of the quantum circuit running on the noise-containing virtual machine is set, including:
    将指定的量子比特、量子逻辑门类型、噪声模型类型以及所述噪声模型类型所需的参数作为逻辑门噪声接口的参数,以及传入所述逻辑门噪声接口;Using the specified qubit, quantum logic gate type, noise model type, and parameters required by the noise model type as parameters of the logic gate noise interface, and passing in the logic gate noise interface;
    通过所述逻辑门噪声接口设置运行于所述含噪声虚拟机上的量子线路的逻辑门噪声,所述逻辑门噪声对所述量子线路中的指定的量子比特生效;Setting the logic gate noise of the quantum circuit running on the noise-containing virtual machine through the logic gate noise interface, and the logic gate noise takes effect on specified qubits in the quantum circuit;
    和/或,所述噪声为量子比特的量子态重置噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:And/or, the noise is the quantum state reset noise of the qubit, and the noise of the quantum circuit running on the noise-containing virtual machine is set, including:
    将量子线路中的量子比特的量子态重置到|0>的概率和重置到|1>的概率作为重置噪声接口的参数,以及传入所述重置噪声接口;The probability of resetting the quantum state of the qubit in the quantum circuit to |0> and the probability of resetting to |1> are used as parameters of the reset noise interface, and passed into the reset noise interface;
    通过所述重置噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的量子态重置噪声;Setting the quantum state reset noise of the qubit of the quantum circuit running on the noise-containing virtual machine through the reset noise interface;
    和/或,所述噪声为量子比特的测量噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:And/or, the noise is the measurement noise of the qubit, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
    将指定的噪声模型类型以及所述噪声模型类型所需的参数作为测量噪声接口的参数,以及传入所述测量噪声接口;Using the specified noise model type and the parameters required by the noise model type as parameters of the measurement noise interface, and passing it into the measurement noise interface;
    通过所述测量噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的测量噪声。The measurement noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the measurement noise interface.
    和/或,所述噪声为量子比特的读取噪声,所述设置运行于所述含噪声虚拟机上的量子线路的噪声,包括:And/or, the noise is the read noise of the qubit, and the setting of the noise of the quantum circuit running on the noise-containing virtual machine includes:
    将|0>被读取为|0>的概率以及被读取为|1>的概率、|1>被读取为|0>的概率以及被读取为|1>的概率作为读取噪声接口的参数,以及传入所述读取噪声接口;The probability of |0> being read as |0> and the probability of being read as |1>, the probability of |1> being read as |0> and the probability of being read as |1> as the read noise The parameters of the interface, and the interface for passing in the read noise;
    通过所述读取噪声接口设置运行于所述含噪声虚拟机上的量子线路的量子比特的读取噪声。The read noise of the qubits of the quantum circuit running on the noise-containing virtual machine is set through the read noise interface.
  19. 如权利要求1所述的方法,其特征在于,应用于包括所述第一机器学习框架***且不包括第二机器学习框架***的电子装置,所述第一机器学习框架***包括兼容量子计算程序封装单元,并且创建所述机器学习模型还包括:The method according to claim 1, wherein it is applied to an electronic device that includes the first machine learning framework system and does not include the second machine learning framework system, and the first machine learning framework system includes a compatible quantum computing program Encapsulating the unit, and creating the machine learning model further includes:
    确定对应的所述第二机器学习框架***的兼容量子计算层接口,所述兼容量子计算层接口用于提供基于所述第二机器学习框架***内含的量子计算编程库创建的量子程序;Determine the corresponding compatible quantum computing layer interface of the second machine learning framework system, where the compatible quantum computing layer interface is used to provide a quantum program created based on the quantum computing programming library contained in the second machine learning framework system;
    通过所述兼容量子计算程序封装单元调用所述兼容量子计算层接口构建兼容量子计算层;Constructing a compatible quantum computing layer by calling the compatible quantum computing layer interface through the compatible quantum computing program encapsulation unit;
    构建包括所述兼容量子计算层的机器学习模型。Constructing a machine learning model including the compatible quantum computing layer.
  20. 如权利要求19所述的方法,其特征在于,所述确定对应的所述第二机器学习框架***的兼容量子计算层接口之前,所述方法还包括:The method according to claim 19, wherein, before determining the compatible quantum computing layer interface of the corresponding second machine learning framework system, the method further comprises:
    通过所述第二机器学习框架***内含的量子计算编程库构建量子线路,以及调用初始化函数对所述量子线路进行初始化,通过运行函数定义所述量子线路的运行和测量方式,得到量子程序;Constructing a quantum circuit through the quantum computing programming library contained in the second machine learning framework system, and calling an initialization function to initialize the quantum circuit, defining the operation and measurement mode of the quantum circuit through the operation function, and obtaining a quantum program;
    和/或,所述构建包括所述兼容量子计算层的机器学习模型,包括:And/or, the construction includes the machine learning model of the compatible quantum computing layer, including:
    调用所述经典模块构建经典计算层以及调用所述数据结构模块构建所述经典计算层与所述兼容量子计算层之间的前向传播关系;Calling the classical module to construct a classical computing layer and calling the data structure module to construct a forward propagation relationship between the classical computing layer and the compatible quantum computing layer;
    调用所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型。Invoking the classical module to encapsulate the classical computing layer, the compatible quantum computing layer, and the forward propagation relationship between the classical computing layer and the compatible quantum computing layer, to obtain a machine learning model.
  21. 如权利要求20所述的方法,其特征在于,所述经典模块还包括经典神经网络层单元,所述经典神经网络层单元包括以下至少一者:指定模型经典神经网络层子单元,被配置为通过已封装的经典神经网络层接口构建指定模型的所述经典神经网络层;激活层子单元,被配置为构建用于对所述经典神经网络层的输出进行非线性变换的激活层;The method according to claim 20, wherein the classic module also includes a classic neural network layer unit, and the classic neural network layer unit includes at least one of the following: a specified model classic neural network layer subunit configured as Constructing the classical neural network layer of the specified model through the encapsulated classical neural network layer interface; the activation layer subunit is configured to construct an activation layer for performing nonlinear transformation on the output of the classical neural network layer;
    所述调用所述经典模块构建经典计算层,包括:Said calling said classic module to build a classic computing layer, including:
    调用所述指定模型经典神经网络层子单元构建经典神经网络层,以及将所述经典神经网络层作为经典计算层;或者,calling the designated model classical neural network layer subunit to construct a classical neural network layer, and using the classical neural network layer as a classical computing layer; or,
    调用所述指定模型经典神经网络层子单元和所述激活层子单元构建经典神经网络层和激活层,以及将所述经典神经网络层和所述激活层作为经典计算层。Calling the specified model classical neural network layer subunit and the activation layer subunit to construct a classical neural network layer and activation layer, and using the classical neural network layer and the activation layer as a classical calculation layer.
  22. 如权利要求21所述的方法,其特征在于,所述经典模块还包括抽象类子模块,所述通过所述经典模块将所述经典计算层、所述兼容量子计算层以及所述经典计算层与所述兼容量子计算层之间的前向传播关系进行封装,得到机器学习模型,包括:The method according to claim 21, wherein the classical module further comprises an abstract class submodule, and the classical computing layer, the compatible quantum computing layer, and the classical computing layer are combined by the classical module Encapsulate the forward propagation relationship with the compatible quantum computing layer to obtain a machine learning model, including:
    调用所述抽象类子模块基于初始化函数对所述兼容量子计算层和所述经典计算层初始化和封装,得到初始化和封装后的所述兼容量子计算层和所述经典计算层;calling the abstract class submodule to initialize and encapsulate the compatible quantum computing layer and the classical computing layer based on the initialization function, and obtain the initialized and encapsulated compatible quantum computing layer and the classical computing layer;
    调用所述抽象类子模块基于前向传播函数对所述前向传播关系进行封装得到封装后的所述前向传播关系;Calling the abstract class submodule to encapsulate the forward propagation relationship based on the forward propagation function to obtain the encapsulated forward propagation relationship;
    调用所述抽象类子模块基于模块类对所述初始化和封装后的所述兼容量子计算层和所述经典计算层,以及封装后的所述前向传播关系进行封装,得到机器学习模型。The abstract class submodule is called to encapsulate the initialized and encapsulated compatible quantum computing layer and the classical computing layer, and the encapsulated forward propagation relationship based on the module class to obtain a machine learning model.
  23. 如权利要求1所述的方法,其特征在于,还包括通过以下操作得到训练后的所述机器学习模型:The method according to claim 1, further comprising obtaining the trained machine learning model through the following operations:
    调用所述量子模块和所述经典模块创建包括量子程序的机器学习模型,调用所述数据结构模块获取目标数据并创建包括所述目标数据的张量数据;Calling the quantum module and the classical module to create a machine learning model including a quantum program, calling the data structure module to obtain target data and create tensor data including the target data;
    调用所述量子模块基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组;calling the quantum module to divide the second qubits into multiple groups based on the number of first qubits required by the quantum program and the number of second qubits allowed in the quantum device;
    调用所述量子模块针对每一组所述第二量子比特,基于所述量子程序生成包括该组所述第二量子比特的量子线路;calling the quantum module for each group of the second qubits, and generating a quantum circuit including the group of the second qubits based on the quantum program;
    基于所述张量数据得到每组所述量子线路的输入数据,并将所述输入数据分别输入每一所述量子线路,得到每一所述量子线路的输出结果;Obtaining input data of each set of quantum circuits based on the tensor data, and inputting the input data into each of the quantum circuits respectively, to obtain an output result of each of the quantum circuits;
    基于所述输出结果确定所述机器学习模型对所述张量数据的预测结果;determining a prediction result of the machine learning model for the tensor data based on the output result;
    调用所述经典模块创建所述机器学习模型的训练层;calling the classic module to create the training layer of the machine learning model;
    将所述预测结果输入所述训练层,以对所述量子程序的参数进行更新,得到训练后的所述机器学习模型。The prediction result is input into the training layer to update the parameters of the quantum program to obtain the trained machine learning model.
  24. 如权利要求23所述的方法,其特征在于,所述基于所述量子程序所需的第一量子比特的数量和量子设备中允许使用的第二量子比特的数量将所述第二量子比特划分为多组,包括:The method according to claim 23, wherein the second qubits are divided based on the quantity of the first qubits required by the quantum program and the quantity of the second qubits allowed in the quantum device for multiple groups, including:
    将所述量子设备中允许使用的第二量子比特的数量除以所述量子程序所需的第一量子比特的数量得到中间数;dividing the number of second qubits allowed in the quantum device by the number of first qubits required by the quantum program to obtain an intermediate number;
    将小于等于所述中间数的最大整数作为目标数;Taking the largest integer less than or equal to the intermediate number as the target number;
    将所述第二量子比特划分为数量为所述目标数的多组。Dividing the second qubits into a plurality of groups whose number is the target number.
  25. 如权利要求23所述的方法,其特征在于,所述针对每一组所述第二量子比特, 基于所述量子程序生成包括该组所述第二量子比特的量子线路,包括:The method according to claim 23, wherein, for each group of the second qubits, generating a quantum circuit comprising the group of the second qubits based on the quantum program includes:
    针对每一组所述第二量子比特,基于所述量子程序生成用于作用于该组所述第二量子比特的量子逻辑门;For each group of the second qubits, generating quantum logic gates for acting on the group of the second qubits based on the quantum program;
    生成包括该组所述第二量子比特和所述量子逻辑门的量子线路。A quantum circuit comprising the set of said second qubits and said quantum logic gates is generated.
  26. 一种基于机器学习框架***的模型训练装置,其特征在于,应用于包括第一机器学习框架***的电子设备,所述第一机器学习框架***包括数据结构模块、量子模块和经典模块,所述装置包括:A model training device based on a machine learning framework system, characterized in that it is applied to electronic equipment including a first machine learning framework system, the first machine learning framework system includes a data structure module, a quantum module and a classical module, the Devices include:
    创建模块,用于调用所述数据结构模块获取输入的训练数据,创建包括所述训练数据的张量数据,并调用所述量子模块和/或所述经典模块创建机器学习模型;Create a module for calling the data structure module to obtain input training data, create tensor data including the training data, and call the quantum module and/or the classical module to create a machine learning model;
    输入训练模块,用于将所述张量数据输入所述机器学习模型,并调用所述经典模块创建所述机器学习模型的训练层;The input training module is used for inputting the tensor data into the machine learning model, and calling the classic module to create a training layer of the machine learning model;
    参数更新模块,用于将所述机器学习模型的输出结果输入所述训练层,并基于所述训练层的输出结果对所述机器学习模型的参数进行更新,以得到训练后的所述机器学习模型。A parameter update module, configured to input the output result of the machine learning model into the training layer, and update the parameters of the machine learning model based on the output result of the training layer, so as to obtain the machine learning after training Model.
  27. 一种非瞬态计算机可读存储介质,其特征在于,所述存储介质中存储有计算机程序,其中,所述计算机程序被设置为运行时执行所述权利要求1至25任一项中所述的方法。A non-transitory computer-readable storage medium, characterized in that a computer program is stored in the storage medium, wherein the computer program is configured to perform the operation described in any one of claims 1 to 25 when running. Methods.
  28. 一种电子装置,包括存储器和处理器,其特征在于,所述存储器中存储有计算机程序,所述处理器被设置为运行所述计算机程序以执行所述权利要求1至25任一项中所述的方法。An electronic device, comprising a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to perform the process described in any one of claims 1 to 25. described method.
PCT/CN2022/143597 2021-12-30 2022-12-29 Model training method based on machine learning framework system and related device WO2023125857A1 (en)

Applications Claiming Priority (12)

Application Number Priority Date Filing Date Title
CN202111680613.0A CN116415667A (en) 2021-12-30 2021-12-30 Data processing method, machine learning framework and related equipment
CN202111680570.6A CN116432763A (en) 2021-12-30 2021-12-30 Machine learning model operation method, machine learning framework and related equipment
CN202111680566.XA CN116415685A (en) 2021-12-30 2021-12-30 Noise-containing machine learning model creation method, machine learning framework and related equipment
CN202111680611.1 2021-12-30
CN202111680573.XA CN116432710B (en) 2021-12-30 2021-12-30 Machine learning model construction method, machine learning framework and related equipment
CN202111680570.6 2021-12-30
CN202111680566.X 2021-12-30
CN202111680611.1A CN116432691A (en) 2021-12-30 2021-12-30 Model training method based on machine learning framework and related equipment
CN202111680613.0 2021-12-30
CN202111680573.X 2021-12-30
CN202210083466.7 2022-01-24
CN202210083466.7A CN116542337A (en) 2022-01-24 2022-01-24 Data processing method, machine learning framework and related equipment

Publications (1)

Publication Number Publication Date
WO2023125857A1 true WO2023125857A1 (en) 2023-07-06

Family

ID=86998081

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2022/143597 WO2023125857A1 (en) 2021-12-30 2022-12-29 Model training method based on machine learning framework system and related device

Country Status (1)

Country Link
WO (1) WO2023125857A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117634459A (en) * 2024-01-24 2024-03-01 山东海量信息技术研究院 Target content generation and model training method, device, system, equipment and medium

Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111461336A (en) * 2020-04-03 2020-07-28 合肥本源量子计算科技有限责任公司 Noise-containing double-quantum logic gate implementation method and device based on MPI (Multi-processor interface) multi-process
CN112418387A (en) * 2020-11-18 2021-02-26 北京百度网讯科技有限公司 Quantum data processing method and apparatus
CN112651418A (en) * 2020-05-25 2021-04-13 腾讯科技(深圳)有限公司 Data classification method, classifier training method and system
CN112801281A (en) * 2021-03-22 2021-05-14 东南大学 Countermeasure generation network construction method based on quantization generation model and neural network
CN113379059A (en) * 2021-06-10 2021-09-10 北京百度网讯科技有限公司 Model training method for quantum data classification and quantum data classification method
US20210342730A1 (en) * 2020-05-01 2021-11-04 equal1.labs Inc. System and method of quantum enhanced accelerated neural network training
CN114372539A (en) * 2022-03-22 2022-04-19 合肥本源量子计算科技有限责任公司 Machine learning framework-based classification method and related equipment

Patent Citations (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111461336A (en) * 2020-04-03 2020-07-28 合肥本源量子计算科技有限责任公司 Noise-containing double-quantum logic gate implementation method and device based on MPI (Multi-processor interface) multi-process
US20210342730A1 (en) * 2020-05-01 2021-11-04 equal1.labs Inc. System and method of quantum enhanced accelerated neural network training
CN112651418A (en) * 2020-05-25 2021-04-13 腾讯科技(深圳)有限公司 Data classification method, classifier training method and system
CN112418387A (en) * 2020-11-18 2021-02-26 北京百度网讯科技有限公司 Quantum data processing method and apparatus
CN112801281A (en) * 2021-03-22 2021-05-14 东南大学 Countermeasure generation network construction method based on quantization generation model and neural network
CN113379059A (en) * 2021-06-10 2021-09-10 北京百度网讯科技有限公司 Model training method for quantum data classification and quantum data classification method
CN114372539A (en) * 2022-03-22 2022-04-19 合肥本源量子计算科技有限责任公司 Machine learning framework-based classification method and related equipment

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN117634459A (en) * 2024-01-24 2024-03-01 山东海量信息技术研究院 Target content generation and model training method, device, system, equipment and medium

Similar Documents

Publication Publication Date Title
US20210295161A1 (en) Training neural networks represented as computational graphs
AU2019284012B2 (en) Data processing method and related products
WO2020151129A1 (en) Quantum machine learning framework construction method and apparatus, and quantum computer and computer storage medium
CN112651509B (en) Method and device for determining quantum circuit
KR102302609B1 (en) Neural Network Architecture Optimization
US11295226B2 (en) Optimization recommendation services for quantum computing
WO2018171717A1 (en) Automated design method and system for neural network processor
Morgan et al. The Ring Array Processor: A multiprocessing peripheral for connectionist applications
CN108345937A (en) Cycle is merged with library
CN111448550A (en) Network-accessible machine learning model training and hosting system
KR20190003612A (en) Devices and methods for fully connected layer neural network reverse training implementation
US20190080232A1 (en) Deep neural network perforance analysis on shared memory accelerator systems
WO2023125857A1 (en) Model training method based on machine learning framework system and related device
US20230145452A1 (en) Method and apparatus for training a model
CN114330730A (en) Quantum line block compiling method, device, equipment, storage medium and product
CN108805277A (en) Depth belief network based on more FPGA accelerates platform and its design method
WO2023125858A1 (en) Data processing method, machine learning framework system and related device
KR20210035702A (en) Method of artificial neural network quantization and method of computation using artificial neural network
US20230080480A1 (en) Pipeline parallel computing using extended memory
Dousti et al. Squash 2: a hierarchical scalable quantum mapper considering ancilla sharing
US20220147808A1 (en) Compiler configurable to generate instructions executable by different deep learning accelerators from a description of an artificial neural network
CN114372583B (en) Quantum program optimization method based on machine learning framework and related equipment
CN116432691A (en) Model training method based on machine learning framework and related equipment
US11354771B1 (en) Simulation environment for efficient assessment of memory-bound platforms
CN117112145B (en) Training model distribution method, training model distribution device, computer equipment and storage medium

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 22915106

Country of ref document: EP

Kind code of ref document: A1