Quantum gradient determination method and device, storage medium and electronic device
Patent Information
- Application Number
- CN202410898043.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-04
- Publication Date
- 2026-09-25
- Estimated Expiration
- 2044-07-04
AI Technical Summary
[0006]本申请实施例提供了一种量子梯度的确定方法和装置、存储介质及电子设备,以至少解决相关技术中基于模拟器的变分量子算法框架不足以在真实硬件上执行变分量子算法的问题
[0018]通过本申请,提供了一种量子梯度的确定方法,应用于量子线路,首先计算量子线路中量子门的变分参数的参数梯度值,得到变分参数的参数梯度信息,参数梯度信息包括:量子门在量子线路中的第一位置信息,变分参数在量子门中的第二位置信息,变分参数的参数值;然后获取量子线路的硬件梯度信息,将参数梯度信息和硬件梯度信息通过链式法则进行组合,从而确定出多个变分参数的量子梯度;采用上述方案,提出了一种兼容量子硬件的梯度计算方法,首先计算出参数梯度,然后与硬件梯度相结合,精准确定出硬件梯度;进而解决了相关技术中基于模拟器的变分量子算法框架不足以在真实硬件上执行变分量子算法的问题。
Smart Images

Figure CN118863079B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computers, and more specifically, to a method and apparatus for determining quantum gradients, a storage medium, and an electronic device. Background Technology
[0002] Quantum computing is a novel computing technology that promises to surpass classical computing in solving some complex problems. Utilizing the principles of quantum superposition and quantum entanglement as computational resources, quantum computing possesses inherent parallel computing capabilities. Currently, quantum computing development is at a noisy, medium-scale stage. The main goal at this stage is to use quantum computing as a means to assist classical computers, providing quantum acceleration in solving certain problems. At this stage, the development of quantum algorithms tends towards hybrid classical-quantum variable quantum algorithms.
[0003] Currently, the research and testing of variational quantum algorithms and quantum machine learning are mainly based on quantum circuit simulators, which are embedded into popular machine learning frameworks using matrix multiplication or tensor network simulations. This method is convenient and fast, and can directly utilize the automatic differentiation system of classical machine learning frameworks to generate quantum gradients, making it easy to implement.
[0004] Considering the execution of variational quantum algorithms on real quantum hardware, simulator implementations cannot directly run on quantum processors. Solving gradients on quantum hardware requires the use of actual quantum circuits, a method with significant limitations, often failing to utilize classical automatic differentiation techniques. Furthermore, running quantum circuits on quantum hardware necessitates a quantum circuit compilation and conversion process. All of these factors indicate that simulator-based variational quantum algorithm frameworks are insufficient for executing variational quantum algorithms on real hardware.
[0005] There is still no effective solution to the technical problem that simulator-based variable quantum algorithm frameworks are insufficient to execute variable quantum algorithms on real hardware. Summary of the Invention
[0006] This application provides a method and apparatus for determining quantum gradients, a storage medium, and an electronic device, to at least address the problem in related technologies that simulator-based variable quantum algorithm frameworks are insufficient to execute variable quantum algorithms on real hardware.
[0007] According to one embodiment of this application, a method for determining quantum gradients is provided, comprising: calculating the parameter gradient values of variational parameters of a quantum gate in a quantum circuit to obtain parameter gradient information of the variational parameters, wherein the parameter gradient information includes: first position information of the quantum gate in the quantum circuit, second position information of the variational parameter in the quantum gate, and parameter values of the variational parameters; obtaining hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information; and combining the parameter gradient information and the hardware gradient information using a chain rule to determine the quantum gradients of the plurality of variational parameters.
[0008] In an exemplary embodiment, calculating the parameter gradient values of the variational parameters of quantum gates in a quantum circuit to obtain parameter gradient information of multiple variational parameters includes: traversing the angle parameters of the multiple quantum gates, determining the categories of the multiple angle parameters, wherein the angle parameters include the variational parameters, and the categories of the angle parameters include: parameter class and parameter expression class; if the category of the angle parameter is the parameter class, determining that the angle parameter is a variational parameter, and determining that the parameter gradient value of the variational parameter is a target value; creating a first key-value pair with the variational parameter as the primary key and the parameter gradient list corresponding to the variational parameter as the key value, wherein the parameter gradient list... The method includes the position coordinates of the variational parameters and the parameter gradient values of the variational parameters, wherein the position coordinates are determined based on the first position information and the second position information; when the category of the angle parameter is the parameter expression class, an automatic differentiation technique is used to generate a parameter expression calculation function corresponding to the angle parameter, and the parameter expression calculation function is input into the gradient calculation function to calculate the parameter gradient values of multiple variational parameters in the parameter expression calculation function; a second key-value pair is created using the multiple variational parameters as the primary key and the parameter gradient list corresponding to the variational parameters as the key value; the first key-value pair and the second key-value pair are determined as the parameter gradient information.
[0009] In an exemplary embodiment, generating the parametric expression calculation function corresponding to the angle parameter using automatic differentiation technology includes: initializing and generating a first operand list and a first operation function list; updating the first operand list and the first operation function list by overloading the operation functions in the angle parameter to obtain a second operand list and a second operation function list; detecting the plurality of variational parameters contained in the angle parameter and determining the arrangement order of the plurality of variational parameters in the angle parameter, generating a target dictionary based on the plurality of variational parameters and the arrangement order; and generating the parametric expression calculation function based on the second operand list, the second operation function list, and the target dictionary.
[0010] In an exemplary embodiment, generating the parameter expression computation function based on the second operand list, the second operation function list, and the target dictionary includes: sequentially traversing the second operand list, the second operation function list, and the target dictionary to obtain the current operand, the current operation function, and the current variational parameter; the generation step includes: determining the category of the current operand, and calling the current operation function and the current variational parameter according to the category of the current operand to generate a sub-computation function; and repeatedly executing the generation step until the second operand list, the second operation function list, and the target dictionary have been traversed.
[0011] In one exemplary embodiment, the step of invoking the current operation function and the current variational parameter according to the category of the current operand includes: invoking the current operation function and the current variational parameter through a first invoking method when the current operand is an empty category; invoking the current operand, the current operation function, and the current variational parameter through a second invoking method when the current operand is a numeric category; invoking the current operand, the current operation function, and the current variational parameter through a third invoking method when the current operand is a parameter category; and invoking the current operand, the current operation function, and the current variational parameter through a fourth invoking method when the current operand is a parameter expression category.
[0012] In an exemplary embodiment, the step of invoking the current operand, the current operation function, and the current variational parameter via a fourth invocation method includes: obtaining multiple sub-variable parameters contained in the current operand, which is a parameter expression category, and the position information of the multiple sub-variable parameters in the current operand; determining the sub-operation functions corresponding to the multiple sub-variable parameters in the second operation function list; invoking the sub-operation functions according to the position information of the multiple sub-variable parameters to calculate the expression value of the current operand; and invoking the current operation function and the current variational parameter according to the type of the expression value.
[0013] In an exemplary embodiment, the step of combining the parameter gradient information and the hardware gradient information using a chain rule to determine the quantum gradient of the plurality of variational parameters includes: filtering out sub-parameter gradient information and sub-hardware gradient information corresponding to the target variational parameter from the parameter gradient information and the hardware gradient information according to the target variational parameter, wherein the plurality of variational parameters includes the target variational parameter; determining the correspondence between a third key value pair in the sub-parameter gradient information and a fourth key value pair in the sub-hardware gradient information based on the position coordinates contained in the sub-parameter gradient information and the sub-hardware gradient information, wherein the third key value pair and the fourth key value pair correspond one-to-one; calculating the product of the parameter gradient value of the third key value pair and the hardware gradient value of the fourth key value pair to obtain the sub-quantum gradient; and calculating the sum of the plurality of sub-quantum gradients to obtain the quantum gradient of the target variational parameter.
[0014] According to another embodiment of this application, a quantum gradient determination device is provided, comprising: a calculation module for calculating the parameter gradient values of variational parameters of a quantum gate in a quantum circuit to obtain parameter gradient information of the variational parameters, wherein the parameter gradient information includes: first position information of the quantum gate in the quantum circuit, second position information of the variational parameter in the quantum gate, and parameter values of the variational parameters; an acquisition module for acquiring hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information; and a combination module for combining the parameter gradient information and the hardware gradient information using a chain rule to determine the quantum gradients of the plurality of variational parameters.
[0015] According to yet another embodiment of this application, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and the computer program is configured to perform the steps in any of the above method embodiments when it is run.
[0016] According to yet another embodiment of this application, an electronic device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0017] According to yet another embodiment of this application, a computer program product is also provided, including a computer program that, when executed by a processor, implements the steps of the methods described in various embodiments of this application.
[0018] This application provides a method for determining quantum gradients, applied to quantum circuits. First, the parameter gradient values of the variational parameters of the quantum gates in the quantum circuit are calculated to obtain the parameter gradient information of the variational parameters. This parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameters in the quantum gate, and the parameter values of the variational parameters. Then, the hardware gradient information of the quantum circuit is obtained. The parameter gradient information and the hardware gradient information are combined using a chain rule to determine the quantum gradients of multiple variational parameters. Using the above scheme, a gradient calculation method compatible with quantum hardware is proposed. First, the parameter gradients are calculated, and then combined with the hardware gradients to accurately determine the hardware gradients. This solves the problem in related technologies where simulator-based variational quantum algorithm frameworks are insufficient to execute variational quantum algorithms on real hardware. Attached Figure Description
[0019] Figure 1 This is a hardware structure block diagram of a quantum gradient determination method according to an embodiment of this application. Figure 2
[0020] Figure 2 This is a flowchart of a method for determining a quantum gradient according to an embodiment of this application. Figure 2
[0021] Figure 3 This is a schematic diagram of an optional parameter gradient value calculation process according to an embodiment of this application. Figure 2
[0022] Figure 4 This is a schematic diagram of a quantum gradient calculation process according to an embodiment of this application. Figure 2
[0023] Figure 5 This is a structural block diagram of a quantum gradient computing system according to an embodiment of this application. Figure 2
[0024] Figure 6 This is a structural block diagram of a quantum gradient determination device according to an embodiment of this application. Detailed Implementation
[0025] The embodiments of this application will be described in detail below with reference to the accompanying drawings and examples.
[0026] It should be noted that the terms "first," "second," etc., in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0027] The methods and embodiments provided in this application can be executed on a computer terminal or similar computing device. Taking running on a computer terminal as an example, Figure 1This is a hardware structure block diagram of a computer terminal for a method of determining a mapping relationship according to an embodiment of this application. For example... Figure 1 As shown, a computer terminal may include one or more ( Figure 1 Only one is shown. A processor 102 (processor 102 may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.) and a memory 104 for storing data are also shown. The computer terminal may further include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the computer terminal described above. For example, the computer terminal may also include components that are more complex than those described above. Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0028] Memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the mapping relationship determination method in this embodiment. Processor 102 executes various functional applications and data processing by running the computer program stored in memory 104, thus implementing the above-described method. 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 instances, memory 104 may further include memory remotely located relative to processor 102, and these remote memories can be connected to a computer terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0029] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider for the computer terminal. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module used for wireless communication with the Internet.
[0030] The following is an introduction to variational quantum algorithms: Variational quantum algorithms are a class of quantum algorithms used in the era of noisy, medium-scale quantum computing. Unlike traditional quantum algorithms such as Shor's algorithm and Grover's algorithm, they do not rely on fault-tolerant qubits. Noise in existing quantum computers has a smaller impact on them compared to traditional quantum algorithms, making it easier for them to leverage quantum advantage on noisy quantum computers. The process of applying variational quantum algorithms to a problem is as follows: Select a parameterized quantum circuit U(θ) as the variational hypothesis for the problem, and encode the parameters θ corresponding to the classical problem into this quantum circuit. Obtain the loss function or gradient of the corresponding problem by measuring the quantum states obtained from running the quantum circuit. Optimize the loss function using a classical optimizer (generally gradient descent algorithm) to obtain new parameters. Iterate further using the new parameterized circuit until the loss function converges.
[0031] In simulator implementations, the simulation of quantum circuits can be accomplished using matrix multiplication or tensor networks, thus the calculation of their gradients can be directly implemented using existing automatic differentiation frameworks such as PyTorch. However, if a variational quantum algorithm is executed on real quantum hardware, the quantum gradient must be obtained by measuring the results of the quantum circuit's operation. For example, for a circuit containing only Pauli rotation gates, the gradient of the quantum circuit with respect to the quantum gate angle parameters can be obtained through parameter translation techniques, i.e. As can be seen, this gradient can only be obtained by shifting the quantum circuit parameters and then measuring the result of running the quantum circuit. On the other hand, running quantum algorithms on quantum hardware often requires considering the quantum circuit compilation process, that is, the process of converting the logical quantum circuit input by the user into a physical quantum circuit that can run on the quantum hardware. This process not only changes the structure of the original quantum circuit, but also transforms the original parameters. Therefore, the gradient generated by the transformation process also needs to be considered in the entire hardware gradient calculation process.
[0032] This embodiment provides a method for determining quantum gradients. Figure 2 This is a flowchart of a method for determining a quantum gradient according to an embodiment of this application, as shown below. Figure 2 As shown, the method for determining this quantum gradient includes:
[0033] Step S202: Calculate the parameter gradient value of the variational parameter of the quantum gate in the quantum circuit to obtain the parameter gradient information of the variational parameter. The parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter value of the variational parameter.
[0034] Step S204: Obtain the hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information;
[0035] It should be noted that the parameter gradient can be understood as the gradient of the quantum gate angle parameter with respect to the variational parameter, the hardware gradient can be understood as the gradient of the quantum circuit execution result with respect to the quantum gate angle parameter, which is generally obtained through the parameter translation technique on quantum hardware; while the quantum gradient can be understood as the gradient of the quantum circuit with respect to the variational parameter.
[0036] Optionally, the hardware gradient information can be calculated by a quantum computer.
[0037] Step S206: Combine the parameter gradient information with the hardware gradient information using a chain rule to determine the quantum gradient of the plurality of variational parameters.
[0038] The above-mentioned method for determining quantum gradients first calculates the parameter gradient values of the variational parameters of the quantum gate in the quantum circuit, obtaining the parameter gradient information of the variational parameters. This parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter value of the variational parameter. Then, the hardware gradient information of the quantum circuit is obtained, and the parameter gradient information and hardware gradient information are combined using a chain rule to determine the quantum gradients of multiple variational parameters. Using this scheme, a gradient calculation method compatible with quantum hardware is proposed. First, the parameter gradient is calculated, and then combined with the hardware gradient to accurately determine the hardware gradient. This solves the problem in related technologies where simulator-based variational quantum algorithm frameworks are insufficient to execute variational quantum algorithms on real hardware.
[0039] Optionally, calculating the parameter gradient values of the variational parameters of the quantum gates in the quantum circuit to obtain parameter gradient information of multiple variational parameters includes: traversing the angle parameters of the multiple quantum gates, determining the categories of the multiple angle parameters, wherein the angle parameters include the variational parameters, and the categories of the angle parameters include: parameter class, parameter expression class; if the category of the angle parameter is the parameter class, determining that the angle parameter is a variational parameter, and determining that the parameter gradient value of the variational parameter is a target value; creating a first key-value pair with the variational parameter as the primary key and the parameter gradient list corresponding to the variational parameter as the key value, wherein the parameter gradient list includes the... The variational parameters are defined by their position coordinates and gradient values, where the position coordinates are determined based on the first and second position information. If the angle parameter is of the parameter expression type, an automatic differentiation technique is used to generate a parameter expression calculation function corresponding to the angle parameter. This function is then input into a gradient calculation function to calculate the gradient values of multiple variational parameters within the parameter expression calculation function. A second key-value pair is created using the multiple variational parameters as the primary key and the corresponding parameter gradient list as the key. The first and second key-value pairs are then used to define the parameter gradient information.
[0040] To record the variational parameters of each gate in the quantum circuit, their corresponding gradient values, and their position information in the quantum circuit, we can use a dictionary to record them. We traverse the quantum gates in the quantum circuit and each angle parameter para in the quantum gate. If it is a Parameter class, then it is itself a variational parameter. We create a new key-value pair with the variational parameter as the key and its corresponding parameter gradient list as the value. The elements in the parameter gradient list are a two-element list in the form [(i, j), 1]. In this system, the first element (i, j) (i.e., the position coordinates mentioned above) represents the position information of the variational parameter. i represents the index of the quantum gate corresponding to the parameter in the quantum circuit (i.e., the first position information mentioned above), and j represents the index of the parameter in the quantum gate (i.e., the second position information mentioned above). The second element 1 is the gradient value. Since the variational parameter is a parameter class, its gradient value is 1. If it is a ParameterExpression class (i.e., the parameter expression class mentioned above), then it is necessary to first generate the parameter expression calculation function corresponding to the angle parameter, and input the parameter expression calculation function into the gradient calculation function to calculate the parameter gradient value of each variational parameter in the parameter expression. Then, using the variational parameter in the parameter expression as the primary key and its corresponding parameter gradient list as the value, a new key-value pair (i.e., the second key-value pair mentioned above) is generated. Thus, all variational parameters in the quantum circuit and their related parameter gradient information are obtained.
[0041] Through the above embodiments, all variational parameters and parameter gradient information in the quantum circuit are obtained without omission, thereby making the calculated quantum gradient results more accurate.
[0042] Furthermore, the step of generating the parametric expression calculation function corresponding to the angle parameter using automatic differentiation technology includes: initializing and generating a first operand list and a first operation function list; updating the first operand list and the first operation function list by overloading the operation functions in the angle parameter to obtain a second operand list and a second operation function list; detecting the plurality of variational parameters contained in the angle parameter and determining the arrangement order of the plurality of variational parameters in the angle parameter, generating a target dictionary based on the plurality of variational parameters and the arrangement order; and generating the parametric expression calculation function based on the second operand list, the second operation function list, and the target dictionary.
[0043] Furthermore, the step of generating the parameter expression calculation function based on the second operand list, the second operation function list, and the target dictionary includes: sequentially traversing the second operand list, the second operation function list, and the target dictionary to obtain the current operand, the current operation function, and the current variational parameter; the generation step involves: determining the category of the current operand, and calling the current operation function and the current variational parameter according to the category of the current operand to generate a sub-calculation function; and repeatedly executing the generation step until the second operand list, the second operation function list, and the target dictionary have been traversed.
[0044] Optionally, the step of invoking the current operation function and the current variational parameter according to the category of the current operand includes: invoking the current operation function and the current variational parameter through a first invoking method when the current operand is an empty category; invoking the current operand, the current operation function, and the current variational parameter through a second invoking method when the current operand is a numeric category; invoking the current operand, the current operation function, and the current variational parameter through a third invoking method when the current operand is a parameter category; and invoking the current operand, the current operation function, and the current variational parameter through a fourth invoking method when the current operand is a parameter expression category.
[0045] The process of generating the parameter expression, calculating the function, and calculating the parameter gradient values is as follows: Figure 3 As shown, it includes the following steps:
[0046] Step S1O1: Define the parameter type `Parameter` and the parameter expression type `ParameterExpression`. The class attributes are defined to record the root node `pivot` (of type `Parameter`), the operands for each step (a list where elements can be floating-point numbers, `ParameterExpression` types, or `Parameter` types), the operation function (a list where elements are basic binary operation functions or mathematical operation functions), and its value (the result of evaluating the expression). `Parameter` inherits from `ParameterExpression`, represents a parameter in the parameter expression, has a unique parameter name, and is hashed by parameter name using the `_hash_` method. Here, `ParameterExpression` represents the quantum gate angle parameter, while the `Parameter` involved in the computation represents the variational parameter.
[0047] Step S102: Record the calculation process by calling overloaded operation functions, including _add_, _radd_, _mul_, _rmul_, _neg_, _sub_, etc., or mathematical operation functions including sin, cos, tan, arcsin, etc. The implementation logic of binary operations is basically the same, and its specific steps are: perform a shallow copy of the operand list in S1O1, because we need them to be the same in different expressions; perform a deep copy of the operation function list; add the operands and operation functions of the current binary operation to the corresponding copied lists, and implement the corresponding operation to update the value of the current expression. Finally, return the ParameterExpression instance initialized with the copied operand list and operation function list. The definition of unary mathematical operation functions is similar to the above description, only the operands need to be set to None.
[0048] Step S103: Detect all variational parameters in the parameter expression type. This is achieved through the class method `_variables()`, which returns a dictionary containing the position of each variational parameter in the expression according to the order in which the operands were added. Specifically, initialize the index variable `vi`, iterate through the operand list obtained in step S102, and if it is a `Parameter` type, record the parameter and `vi` in the dictionary and execute `vi += 1`; if it is a `ParameterExpression`, continue calling its `_variables()` method to obtain all parameters and then execute the above steps.
[0049] Step S104: Generate the computation function _func for calculating the expression. Since we need to calculate the gradient using the autograd package, the input requires a one-dimensional array ×. In the _func function, we first use the method in S102 to retrieve all variables of this expression and their corresponding positions: varS = self._variables. Take the first element as the root operand z = ×[0]. Then iterate through the list of operation functions and the list of operands to get the current operation function f and operand op. If op is None, then call z = f(z); if op is a floating-point value or an integer, then call z = f(z, op); if op is a parameter type, then call z = f(z, x[vars[op]]); if op is a parameter expression type, then get the position of all variables in the expression opvars = op._variables.keys(); varind = [vars[ov] for ovin opvars], and then call its _func function to calculate the expression value z = f(z, op._func(x[varind]). Finally, return the expression value z.
[0050] Step S105: Calculate the gradient of the parameter expression using the grad function. We initialize the gradient calculation function by calling the grad method of the autograd package, i.e., g = autograd.grad(self._func). The input of g is a one-dimensional array as the parameter values for which the gradient needs to be calculated. If not provided, the current values of the variables in this expression are used by default.
[0051] Since the parameter transformations after quantum circuit compilation are basically simple mathematical operations, they can be achieved by manually writing simple automatic differentiation programs or by directly using existing automatic differentiation libraries. This embodiment provides an implementation method based on the automatic differentiation library autograd, which can quickly and accurately calculate the parameter gradient. This method is also applicable to other frameworks such as PyTorch, Java, etc.
[0052] Furthermore, the step of invoking the current operand, the current operation function, and the current variational parameter via the fourth invocation method includes: obtaining multiple sub-variable parameters contained in the current operand which is a parameter expression category, and the position information of the multiple sub-variable parameters in the current operand; determining the sub-operation functions corresponding to the multiple sub-variable parameters in the second operation function list; invoking the sub-operation functions according to the position information of the multiple sub-variable parameters to calculate the expression value of the current operand; and invoking the current operation function and the current variational parameter according to the type of the expression value.
[0053] If the operand is a parameter expression type, then the positions of all variables in the expression need to be obtained: opvars = op._variables.keys(); varind = [vars[ov] for ov in opvars], and then its _func function is called to calculate the expression value: z = f(z, op._func(x[varind]). Finally, the expression value z is returned.
[0054] Optionally, the step of combining the parameter gradient information and the hardware gradient information using a chain rule to determine the quantum gradient of the plurality of variational parameters includes: filtering out sub-parameter gradient information and sub-hardware gradient information corresponding to the target variational parameter from the parameter gradient information and the hardware gradient information according to the target variational parameter, wherein the plurality of variational parameters includes the target variational parameter; determining the correspondence between the third key value pair in the sub-parameter gradient information and the fourth key value pair in the sub-hardware gradient information based on the position coordinates contained in the sub-parameter gradient information and the sub-hardware gradient information, wherein the third key value pair and the fourth key value pair correspond one-to-one; calculating the product of the parameter gradient value of the third key value pair and the hardware gradient value of the fourth key value pair to obtain the sub-quantum gradient; and calculating the sum of the plurality of sub-quantum gradients to obtain the quantum gradient of the target variational parameter.
[0055] The gradient of the parameter gradient and the hardware gradient are combined using the chain rule to obtain the gradient of the quantum circuit to its variational parameters. It should be noted that regardless of whether the hardware gradient is actually obtained through hardware or through a simulator, it has a fixed form in order to match the parameter gradient obtained in the previous step. Here we give an example of using a nested list, that is, each sublist in the list gate_grads stores the hardware gradient values of all angle parameters of the quantum gate. This nested list makes it convenient for us to index the position (i, j) recorded in the key-value pair. Based on such a structure, we can easily combine it with the obtained parameter gradient information by the chain rule (1) to obtain the real gradient. Specifically, we traverse the variational parameters in _parameter_grads and their corresponding parameter gradient lists, and initialize the variable fullgrad = 0 for each variational parameter as the calculated quantum gradient. Traverse the corresponding parameter gradient list, and use the first element, i.e. the position information pos (the recorded (i, j), i.e., the position coordinate), to retrieve the corresponding hardware gradient from gate_grads, gg = gate_grads[pos[0]][pos[1]]. Take the second element as the parameter gradient gp, and execute the chain rule fullgrad += gg * gp. After the parameter gradient list has been traversed, the quantum gradient associated with the variational parameter is calculated. When the variational parameters in -parameter_grads have been traversed, the quantum gradient of all variational parameters has been calculated.
[0056] Below, we provide a concrete example. Suppose the quantum gate and parameter structure in the quantum circuit is [U1(θ1, θ2), U2(θ1), U3(θ1), U4(f1(θ1)), U5(f2(θ1+θ2))], which encompasses most cases that occur after the quantum circuit is compiled. The process for calculating the quantum gradient is shown in the appendix. Figure 4 As shown, the connection relationships in the figure are determined by the parameter gradient list determined above. That is, each quantum gradient is obtained by multiplying the hardware gradient at the starting point of the connection by the parameter gradient on the connection and then accumulating them. A connection without a parameter gradient indicates that its parameter gradient value is 1.
[0057] Optionally, this application also provides an optional quantum gradient calculation system, such as... Figure 5As shown, the system includes an automatic parameter differentiation module, a quantum circuit parameter gradient generation module, and a chain rule execution module. The automatic parameter differentiation module provides automatic differentiation functions for the parameters in the quantum circuit, which can be implemented using the autograd software package in this embodiment. The quantum circuit parameter gradient generation module generates parameter gradients and their correspondence with quantum gates by calling the automatic parameter differentiation module, thus obtaining parameter gradient information. The chain rule execution module receives parameter gradient information output by the quantum circuit parameter gradient generation module and hardware gradients returned from the quantum hardware backend or other simulator backends, and combines them using the chain rule to generate the final quantum circuit gradient, i.e., the aforementioned quantum gradient.
[0058] This embodiment provides a device for determining quantum gradients. Figure 6 This is a structural block diagram of a quantum gradient determination device according to an embodiment of this application, such as... Figure 6 As shown, it includes:
[0059] The calculation module 62 is used to calculate the parameter gradient value of the variational parameter of the quantum gate in the quantum circuit, and obtain the parameter gradient information of the variational parameter. The parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter value of the variational parameter.
[0060] The acquisition module 64 is used to acquire the hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information;
[0061] The combination module 66 is used to combine the parameter gradient information and the hardware gradient information through a chain rule to determine the quantum gradient of the plurality of variational parameters.
[0062] This application first calculates the parameter gradient values of the variational parameters of the quantum gate in a quantum circuit, obtaining the parameter gradient information of the variational parameters. The parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter value of the variational parameter. Then, the hardware gradient information of the quantum circuit is obtained, and the parameter gradient information and the hardware gradient information are combined using the chain rule to determine the quantum gradient of multiple variational parameters. Using the above scheme, a gradient calculation method compatible with quantum hardware is proposed. First, the parameter gradient is calculated, and then it is combined with the hardware gradient to accurately determine the hardware gradient. This solves the problem in related technologies that the simulator-based variational quantum algorithm framework is insufficient to execute the variational quantum algorithm on real hardware.
[0063] Optionally, the above-mentioned calculation module 62 is further configured to traverse the multiple quantum gate angle parameters, determine the categories of the multiple angle parameters, wherein the angle parameters include the variational parameters, and the categories of the angle parameters include: parameter class and parameter expression class; if the category of the angle parameter is the parameter class, determine that the angle parameter is a variational parameter, and determine that the parameter gradient value of the variational parameter is a target value; create a first key-value pair with the variational parameter as the primary key and the parameter gradient list corresponding to the variational parameter as the key value, wherein the parameter gradient list includes the position coordinates of the variational parameter and the variational parameter... The parameter gradient values of the number are calculated, and the position coordinates are determined based on the first position information and the second position information. If the category of the angle parameter is the parameter expression class, an automatic differentiation technique is used to generate a parameter expression calculation function corresponding to the angle parameter, and the parameter expression calculation function is input into the gradient calculation function to calculate the parameter gradient values of multiple variational parameters in the parameter expression calculation function. A second key-value pair is created using the multiple variational parameters as the primary key and the parameter gradient list corresponding to the variational parameters as the key value. The first key-value pair and the second key-value pair are determined as the parameter gradient information.
[0064] To record the variational parameters of each gate in the quantum circuit, their corresponding gradient values, and their position information in the quantum circuit, we can use a dictionary to record them. We traverse the quantum gates in the quantum circuit and each angle parameter para in the quantum gate. If it is a Parameter class, then it is itself a variational parameter. We create a new key-value pair with the variational parameter as the key and its corresponding parameter gradient list as the value. The elements in the parameter gradient list are a two-element list in the form [(i, j), 1]. In this system, the first element (i, j) (i.e., the position coordinates mentioned above) represents the position information of the variational parameter. i represents the index of the quantum gate corresponding to the parameter in the quantum circuit (i.e., the first position information mentioned above), and j represents the index of the parameter in the quantum gate (i.e., the second position information mentioned above). The second element 1 is the gradient value. Since the variational parameter is a parameter class, its gradient value is 1. If it is a ParameterExpression class (i.e., the parameter expression class mentioned above), then it is necessary to first generate the parameter expression calculation function corresponding to the angle parameter, and input the parameter expression calculation function into the gradient calculation function to calculate the parameter gradient value of each variational parameter in the parameter expression. Then, using the variational parameter in the parameter expression as the primary key and its corresponding parameter gradient list as the value, a new key-value pair (i.e., the second key-value pair mentioned above) is generated. Thus, all variational parameters in the quantum circuit and their related parameter gradient information are obtained.
[0065] Through the above embodiments, all variational parameters and parameter gradient information in the quantum circuit are obtained without omission, thereby making the calculated quantum gradient results more accurate.
[0066] Optionally, the above-mentioned calculation module 62 is further configured to initialize and generate a first operand list and a first operation function list; update the first operand list and the first operation function list by overloading the operation functions in the angle parameters to obtain a second operand list and a second operation function list; detect the plurality of variational parameters contained in the angle parameters and determine the arrangement order of the plurality of variational parameters in the angle parameters, generate a target dictionary according to the plurality of variational parameters and the arrangement order; and generate the parameter expression calculation function according to the second operand list, the second operation function list and the target dictionary.
[0067] Furthermore, the aforementioned calculation module 62 is also used to sequentially traverse the second operand list, the second operation function list, and the target dictionary to obtain the current operand, the current operation function, and the current variational parameter; the generation step is to determine the category of the current operand, and call the current operation function and the current variational parameter according to the category of the current operand to generate a sub-calculation function; the generation step is executed repeatedly until the second operand list, the second operation function list, and the target dictionary are traversed.
[0068] Furthermore, the aforementioned calculation module is also configured to, when the current operand is of the empty category, invoke the current operation function and the current variational parameter through a first invocation method; when the current operand is of the numeric category, invoke the current operand, the current operation function, and the current variational parameter through a second invocation method; when the current operand is of the parameter category, invoke the current operand, the current operation function, and the current variational parameter through a third invocation method; and when the current operand is of the parameter expression category, invoke the current operand, the current operation function, and the current variational parameter through a fourth invocation method.
[0069] Furthermore, the aforementioned calculation module 62 is also used to obtain multiple sub-variable parameters contained in the current operand, which is a parameter expression category, and the position information of the multiple sub-variable parameters in the current operand; determine the sub-operation function corresponding to the multiple sub-variable parameters in the second operation function list; call the sub-operation function according to the position information of the multiple sub-variable parameters to calculate the expression value of the current operand; and call the current operation function and the current variational parameter according to the type of the expression value.
[0070] Optionally, the aforementioned combined module 66 is further configured to: filter out sub-parameter gradient information and sub-hardware gradient information corresponding to the target variational parameter from the parameter gradient information and the hardware gradient information, wherein the plurality of variational parameters include the target variational parameter; determine the correspondence between the third key value pair in the sub-parameter gradient information and the fourth key value pair in the sub-hardware gradient information based on the position coordinates contained in the sub-parameter gradient information and the sub-hardware gradient information, wherein the third key value pair and the fourth key value pair correspond one-to-one; calculate the product of the parameter gradient value of the third key value pair and the hardware gradient value of the fourth key value pair to obtain the sub-quantum gradient; calculate the sum of the plurality of sub-quantum gradients to obtain the quantum gradient of the target variational parameter.
[0071] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal device (which may be a mobile phone, computer, server, or network device, etc.) to execute the methods described in the various embodiments of this application.
[0072] It should be noted that the above modules can be implemented by software or hardware. For the latter, they can be implemented in the following ways, but are not limited to: all the above modules are located in the same processor; or, the above modules are located in different processors in any combination.
[0073] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above method embodiments when run.
[0074] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.
[0075] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0076] In one exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0077] Embodiments of this application also provide a computer program product, including a non-volatile computer-readable storage medium storing the computer program product, wherein the computer program, when executed by a processor, implements the steps of the methods described in various embodiments of this application.
[0078] Specific examples in this embodiment can be found in the examples described in the above embodiments and exemplary implementations, and will not be repeated here.
[0079] Obviously, those skilled in the art should understand that the modules or steps of this application described above can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. They can be implemented using computer-executable program code, and thus can be stored in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, this application is not limited to any particular hardware and software combination.
[0080] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the principles of this application should be included within the protection scope of this application.
Claims
1. A method for determining a quantum gradient, characterized in that, Applied to quantum circuits, include: Calculate the parameter gradient value of the variational parameter of the quantum gate in the quantum circuit to obtain the parameter gradient information of the variational parameter, wherein the parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter gradient value of the variational parameter; Obtain the hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information; The parameter gradient information and the hardware gradient information are combined using a chain rule to determine the quantum gradient of multiple variational parameters; The step of combining the parameter gradient information and the hardware gradient information using a chain rule to determine the quantum gradient of multiple variational parameters includes: Based on the target variational parameter, sub-parameter gradient information and sub-hardware gradient information corresponding to the target variational parameter are filtered from the parameter gradient information and the hardware gradient information, wherein the plurality of variational parameters include the target variational parameter; The correspondence between the third key value pair in the sub-parameter gradient information and the fourth key value pair in the sub-hardware gradient information is determined based on the position coordinates contained in the sub-parameter gradient information and the sub-hardware gradient information, wherein the third key value pair and the fourth key value pair correspond one-to-one. The product of the parameter gradient value of the third bond pair and the hardware gradient value of the fourth bond pair is calculated to obtain the sub-quantum gradient. The sum of multiple sub-quantum gradients is calculated to obtain the quantum gradient of the target variational parameter.
2. The method for determining the quantum gradient according to claim 1, characterized in that, The parameter gradient values of the variational parameters of the quantum gates in the quantum circuit are calculated to obtain parameter gradient information of multiple variational parameters, including: By iterating through multiple quantum gate angle parameters, the categories of multiple angle parameters are determined, wherein the angle parameters include the variational parameters, and the categories of the angle parameters include: parameter class and parameter expression class; If the category of the angle parameter is the parameter class, the angle parameter is determined to be a variational parameter, and the parameter gradient value of the variational parameter is determined to be a target value; a first key-value pair is created with the variational parameter as the primary key and the parameter gradient list corresponding to the variational parameter as the key value, wherein the parameter gradient list includes the position coordinates of the variational parameter and the parameter gradient value of the variational parameter, and the position coordinates are determined according to the first position information and the second position information; When the category of the angle parameter is the parameter expression class, the parameter expression calculation function corresponding to the angle parameter is generated by automatic differentiation, and the parameter expression calculation function is input into the gradient calculation function to calculate the parameter gradient values of multiple variational parameters in the parameter expression calculation function; a second key-value pair is created with the multiple variational parameters as the primary key and the parameter gradient list corresponding to the variational parameters as the key value; The first key-value pair and the second key-value pair are determined as the parameter gradient information.
3. The method for determining the quantum gradient according to claim 2, characterized in that, The function for calculating the parametric expression corresponding to the angle parameter through automatic differentiation technology includes: Initialize and generate the first operand list and the first operation function list; The first operand list and the first operation function list are updated by overloading the operation functions in the angle parameters to obtain the second operand list and the second operation function list. The multiple variational parameters contained in the angle parameters are detected, and the arrangement order of the multiple variational parameters in the angle parameters is determined. A target dictionary is generated based on the multiple variational parameters and the arrangement order. The parameter expression calculation function is generated based on the second operand list, the second operation function list, and the target dictionary.
4. The method for determining the quantum gradient according to claim 3, characterized in that, The step of generating the parameter expression calculation function based on the second operand list, the second operation function list, and the target dictionary includes: By sequentially traversing the second operand list, the second operation function list, and the target dictionary, the current operand, the current operation function, and the current variational parameter are obtained; Generation steps: Determine the category of the current operand, and call the current operation function and the current variational parameter according to the category of the current operand to generate a sub-computation function; The generation steps are executed repeatedly until the second operand list, the second operation function list, and the target dictionary have been traversed.
5. The method for determining the quantum gradient according to claim 4, characterized in that, The step of invoking the current operation function and the current variational parameter according to the category of the current operand includes: When the current operand is of the empty category, the current operation function and the current variational parameter are invoked through the first invocation method; If the current operand is a numeric type, the current operand, the current operation function, and the current variational parameter are invoked using a second invocation method. When the current operand is a parameter type, the current operand, the current operation function, and the current variational parameter are invoked via a third invocation method. When the current operand is a parameter expression, the current operand, the current operation function, and the current variational parameter are invoked via the fourth invocation method.
6. The method for determining the quantum gradient according to claim 5, characterized in that, The invocation of the current operand, the current operation function, and the current variational parameter via the fourth invocation method includes: Obtain multiple subvariational parameters contained in the current operand, which is a parameter expression category, and the position information of the multiple subvariational parameters in the current operand; Determine the sub-operation functions corresponding to the plurality of sub-variable parameters in the second list of operation functions; The sub-operation function is invoked based on the position information of the multiple sub-variable parameters to calculate the expression value of the current operand; The current operation function and the current variational parameter are invoked based on the type of the expression value.
7. A device for determining a quantum gradient, characterized in that, include: The calculation module is used to calculate the parameter gradient value of the variational parameter of the quantum gate in the quantum circuit, and obtain the parameter gradient information of the variational parameter. The parameter gradient information includes: the first position information of the quantum gate in the quantum circuit, the second position information of the variational parameter in the quantum gate, and the parameter gradient value of the variational parameter. An acquisition module is used to acquire the hardware gradient information of the quantum circuit, wherein the hardware gradient information has the same format as the parameter gradient information; A combination module is used to combine the parameter gradient information and the hardware gradient information through a chain rule to determine the quantum gradient of multiple variational parameters; The combination module is further configured to filter out sub-parameter gradient information and sub-hardware gradient information corresponding to the target variational parameter from the parameter gradient information and the hardware gradient information based on the target variational parameter, wherein the plurality of variational parameters include the target variational parameter; The correspondence between the third key value pair in the sub-parameter gradient information and the fourth key value pair in the sub-hardware gradient information is determined based on the position coordinates contained in the sub-parameter gradient information and the sub-hardware gradient information, wherein the third key value pair and the fourth key value pair correspond one-to-one. The product of the parameter gradient value of the third bond pair and the hardware gradient value of the fourth bond pair is calculated to obtain the sub-quantum gradient. The sum of multiple sub-quantum gradients is calculated to obtain the quantum gradient of the target variational parameter.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the method according to any one of claims 1 to 6.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Parameter optimization method, quantum chip control method and device
CN113516246A
Quantum calculation simulation method for linear equation set and related device
CN118014089A