Code processing method and device, electronic equipment, storage medium and computer program product

By converting the quantum bits written into pointer arrays into object arrays, the problem of low processing efficiency in quantum programs is solved, the processing flow is simplified, and the development complexity is reduced.

CN120806190AActive Publication Date: 2025-10-17CHINA MOBILE (SUZHOU) SOFTWARE TECH CO LTD +1
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202511254475.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-03
Publication Date
2025-10-17
Estimated Expiration
2045-09-03

AI Technical Summary

Technical Problem

In quantum programs, after writing quantum bits into a pointer array, the processing efficiency is low when subsequently calling objects in the pointer array.

Method used

Convert the pointer array into an object array so that the object array can directly point to the specific object, avoiding indirect pointing through pointers and simplifying the processing flow.

Benefits of technology

The processing efficiency of pointer array objects used in quantum programs is improved, and the development complexity and subsequent processing complexity are reduced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120806190A_ABST
    Figure CN120806190A_ABST
Patent Text Reader

Abstract

The invention discloses a code processing method and device, electronic equipment, a storage medium and a computer program product. The method comprises the steps that an original code is obtained, the original code is used for declaring that one or more quantum bits are written into a pointer array, the pointer array comprises one or more pointers, and the one or more pointers are used for pointing to the one or more quantum bits; determining the type of the pointer array and the name of the one or more quantum bits by using the original code; constructing an object array by using the type of the pointer array and the name of the one or more quantum bits, the object array being used for pointing to the one or more quantum bits; and replacing the pointer array with the object array to obtain a compiled intermediate code. According to the technical scheme, in the code compiling process, the pointers of the pointer array are replaced with the specific objects pointed by the pointers, and the processing efficiency when the objects associated with the pointer array are called subsequently can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of quantum computing, and in particular, to a code processing method and device, electronic equipment, a storage medium, and a computer program product. BACKGROUND

[0002] In the field of quantum program compilers based on c / c++ programming languages, writing (which can also be understood as putting) a plurality of quantum bits that have been defined and declared into an array is a necessary means for designing complex quantum gates and quantum circuits, which helps to improve the writing support of the compiler front end for complex quantum circuits, and also lays a foundation for optimizing complex quantum circuits in the compiler back end.

[0003] Currently, in the above quantum program, quantum bits are written into an array as an object. However, in the scenario of writing an object into a pointer array, there is a problem of low processing efficiency when subsequently calling the object in the pointer array. SUMMARY

[0004] To solve the related technical problems, the embodiments of the present application provide a code processing method and device, electronic equipment, a storage medium, and a computer program product.

[0005] The technical solutions of the embodiments of the present application are implemented as follows: The embodiments of the present application provide a code processing method, comprising: obtaining original code, the original code being used to declare writing one or more quantum bits into a pointer array, the pointer array containing one or more pointers, the one or more pointers being used to point to the one or more quantum bits; determining the type of the pointer array and the name of the one or more quantum bits by using the original code; constructing an object array by using the type of the pointer array and the name of the one or more quantum bits, the object array being used to point to one or more quantum bits; replacing the pointer array with the object array to obtain intermediate code after compilation processing.

[0006] The embodiments of the present application also provide a code processing device, comprising: an obtaining unit configured to obtain original code, the original code being used to declare writing one or more quantum bits into a pointer array, the pointer array containing one or more pointers, the one or more pointers being used to point to the one or more quantum bits; a determining unit configured to determine the type of the pointer array and the name of the one or more quantum bits by using the original code; constructing a unit configured to construct an object array for pointing to one or more qubits by using the type of the pointer array and the name of the one or more qubits; a compiling unit configured to replace the pointer array with the object array to obtain the intermediate code after the compiling processing.

[0007] The embodiment of the present application further provides an electronic device, comprising a processor and a memory for storing a computer program capable of running on the processor, When the processor runs the computer program, the steps of the code processing method are implemented.

[0008] The embodiment of the present application further provides a storage medium having a computer program stored thereon, and the steps of the code processing method are implemented when the processor executes the computer program.

[0009] The embodiment of the present application further provides a computer program product comprising a computer program, and the steps of the code processing method are implemented when the processor executes the computer program.

[0010] The code processing method, device, electronic device, storage medium and computer program product provided by the embodiment of the present application, the code processing method comprises: obtaining an original code, the original code is used for declaring to write one or more qubits into a pointer array, the pointer array contains one or more pointers, the one or more pointers are used for pointing to the one or more qubits; by using the original code, the type of the pointer array and the name of the one or more qubits are determined; by using the type of the pointer array and the name of the one or more qubits, an object array is constructed, the object array is used for pointing to one or more qubits; the pointer array is replaced by the object array, and the intermediate code after the compiling processing is obtained. The technical scheme provided by the embodiment of the present application replaces the pointer array in the original code with the object array in the process of translating the original code into the intermediate code. Since the object array can directly point to the specific object written into the pointer array without indirectly pointing to the specific object through the pointer, the processing flow when the object pointed to by the pointer array in the subsequent quantum program is simplified, and the processing efficiency is improved. BRIEF DESCRIPTION OF DRAWINGS

[0011] Figure 1 It is a flowchart of a code processing method of the embodiment of the present application; Figure 2 It is a flowchart of a code compiling of the application example of the present application; Figure 3 It is a structural diagram of a code processing device of the embodiment of the present application; Figure 4FIG. 1 is a structural schematic diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0012] The present application will be further described in detail below with reference to the accompanying drawings and embodiments.

[0013] In related c / c++ quantum programs, one way is to put qubits into a std::vector array. However, from the perspective of application scenarios, although the function of putting qubits into an array can be realized through a std::vector array, the definition of a pointer array syntax is prohibited in quantum programs, which leads to the fact that this function cannot be supported in quantum programs based on the c programming language, i.e., there is a limitation in application scenarios. In addition, from the perspective of development complexity, the quantum program compiler needs to support namespaces or classes and other c++ concepts when parsing the std::vector array statement, and the syntax structure needs to be considered more complex (which can also be understood as a more complex syntax tree), which makes the development more complex. To solve the above problems, another way is proposed in related technologies, specifically, the pointer of the qubit defined and declared is put into a pointer array. From the syntax statement, the pointer array is more similar to the underlying array in form, so when the pointer array statement is parsed, the development can be carried out on the basis of the original array function, which reduces the development complexity.

[0014] However, since the pointer array indirectly points to the object written into the pointer array through the pointer, there is a problem of low processing efficiency when the object in the pointer array is subsequently called.

[0015] Therefore, in various embodiments of the present application, when the source code is translated into intermediate code, the pointer array is converted into an object array to directly point to a specific object through the object array, so that the subsequent processing flow of the pointer array can be avoided, and the processing efficiency is improved.

[0016] An embodiment of the present application provides a code processing method applied to an electronic device, such as a quantum computer. Figure 1 As shown in FIG. 1, the method comprises the following steps. Step 101: obtaining an original code, the original code being used to declare that one or more qubits are written into a pointer array, the pointer array containing one or more pointers, the one or more pointers being used to point to the one or more qubits; Step 102: determining the type of the pointer array and the name of the one or more qubits by using the original code; Step 103: constructing an object array for pointing to one or more qubits by using the type of the pointer array and the name of the one or more qubits; Step 104: replacing the pointer array with the object array to obtain the compiled intermediate code.

[0017] In actual application, the electronic device can include a quantum program compiler based on a c and / or c++ programming language, and the electronic device can adopt a low level virtual machine (LLVM) framework. In addition, the electronic device can support at least a compilation function from high-level code to low-level code, the high-level code can be understood as original code (also can be understood as source code) input by a developer, and the low-level code can be understood as intermediate code after compilation. In this way, the high-level code corresponding to different programming languages can be compiled into low-level code in a unified form, so as to facilitate subsequent optimization of the low-level code in a unified form.

[0018] In actual application, before step 101, in order to realize the compilation from the original code to the intermediate code, a definition file about the intermediate code can be written in the electronic device in advance; wherein the intermediate code can include multi-level intermediate representation (MLIR) code.

[0019] It should be noted that in the case that the intermediate code includes MLIR code, a user can write a definition file about the MLIR code in the electronic device based on an operation definition specification (ODS) language, and the definition file can include definition files of MLIR Type, MLIR Value and MLIR Op; wherein the MLIR Op is a basic unit of a line in the MLIR code, that is, the MLIR code is composed of lines of code, and each line of the MLIR code can be understood as an Op; the MLIR Type and the MLIR Value are basic elements of the MLIR Op. In addition, the MLIR Value can be an operand of the MLIR Op, and has a corresponding MLIR Type; wherein the MLIR Value corresponding to the MLIR Op can be specified when the MLIR Op is defined.

[0020] Exemplarily, taking the MLIR code "%c0 = arith.constant 0: index" as an example, %c0 can be understood as the name of the MLIR Value; arith.constant can be understood as an MLIR Op for creating a constant value, 0 can be understood as the numerical value of the constant value, that is, the operand of the MLIR Op; and index can be understood as an MLIR Type for indicating the data type of the constant value.

[0021] In actual application, one or more structure bodies required for code compilation can be defined in the electronic device in advance, and the one or more structure bodies can at least mark quantum gate functions or quantum bits in the code.

[0022] For example, taking the pointer array A as an example, a user can create a structure body MLIR Type based on the ODS language, and name it as PointerType. The PointerType can indicate the type of the pointer B in the pointer array A, that is, whether the pointer B is a pointer capable of indicating a quantum bit (which can be referred to as a quantum bit pointer).

[0023] For example, taking the pointer array A as an example, a user can create a structure body MLIR Type based on the ODS language, and name it as ArrayType. The ArrayType can indicate the type of the pointer array A, that is, whether the pointer array A is a pointer array capable of writing a quantum bit (which can be referred to as a quantum bit pointer array). The ArrayType can include one or more (or at least one or at least one) of a type parameter, a size parameter and an array name. The element type parameter can indicate the type of an element capable of being written or received in the pointer array A, the array size parameter can indicate the size of the pointer array A, and the array name can include the name of the pointer array A.

[0024] For example, taking the pointer array A as an example, a user can create a structure body MLIR Op, and name it as ArrayOp. The ArrayOp can store an MLIR value representing a source code variable (such as a quantum bit written to the pointer array A) in the form of an operand as an array container. The ArrayOp can include a type parameter and a size parameter. The type parameter can indicate the type of the operand, and the size parameter can indicate the number of operands stored by the ArrayOp. The return value of the ArrayOp is the operand, and accordingly, the return type is the ArrayType.

[0025] In actual application, before the original code is compiled, the electronic device can obtain the original code through manual input. The original code can include source code in a C or C++ programming language, and the type of the original code is not limited in the embodiments of the present application.

[0026] Then, the electronic device can perform syntax and / or semantic analysis on the original code by a lightweight compiler (e.g., Clang) to obtain an analysis result, and convert the analysis result into a syntax tree of the original code; wherein a statement or expression of each line of the original code corresponds to a node in the syntax tree. In this way, specific information in the statement or expression can be obtained by recursively traversing the node and corresponding child nodes, so as to convert one line of the original code into one or more lines of intermediate code by using the specific information.

[0027] It should be noted that the original code can be composed of two parts, specifically can include a pointer array and a qubit. For example, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, in order to declare that the qubit &q is written into the pointer array qa[] in the form of an object, the variable declaration statement can include the pointer array qa[] on the left side and the qubit &q on the right side.

[0028] In actual application, after obtaining the syntax tree of the original code, the electronic device can traverse the syntax tree of the original code to determine the type of the pointer array; wherein the type of the pointer array represents whether the pointer array is written with a qubit, that is, whether the pointer array is a qubit pointer array.

[0029] Specifically, in an embodiment, the determining the type of the pointer array and the name of the one or more qubits by using the original code comprises: determining the type of the one or more pointers and the length of the pointer array by using the original code, respectively; determining the type of the pointer array by using the type of the one or more pointers and the length of the pointer array.

[0030] In actual application, the electronic device can traverse the syntax tree of the original code to determine the type of the one or more pointers and generate a corresponding structure; wherein the type of the one or more pointers represents whether the one or more pointers are used to point to a qubit, that is, whether the one or more pointers are qubit pointers. In addition, the electronic device can also obtain the length of the pointer array from the syntax tree of the original code, and the length of the pointer array can be understood as the size of the pointer array.

[0031] Exemplarily, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, the electronic device can traverse the pointer array qa[], determine whether the pointers (i.e., array elements) in the pointer array qa[] are quantum bit pointers, create a PointerType corresponding to the pointer if the pointer is a quantum bit pointer, and push the PointerType corresponding to the pointer into the Type Stack (which can be understood as a buffer of the structure MLIR Type).

[0032] Next, the electronic device can determine the type of the pointer array by using the type of the one or more pointers and the length of the pointer array, and generate a corresponding structure.

[0033] Exemplarily, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, the electronic device can pop the PointerType from the Type Stack, create an ArrayType corresponding to the pointer array by using the PointerType and the length of the pointer array, and then push the ArrayType corresponding to the pointer array into the Type Stack.

[0034] In addition, the electronic device can also determine the names of the one or more quantum bits by traversing the syntax tree of the original code, wherein the names of the one or more quantum bits can include the variable name of each quantum bit.

[0035] In actual application, after determining the type of the pointer array and the names of the one or more quantum bits, the electronic device can start to construct the object array.

[0036] Specifically, in an embodiment, the construction of the object array by using the type of the pointer array and the names of the one or more quantum bits includes: determining the numerical values of the one or more quantum bits by using the names of the one or more quantum bits; constructing the object array by using the numerical values of the one or more quantum bits and the type of the pointer array.

[0037] In actual application, in the object array, the one or more quantum bits can be stored in the object array as operands, in which case the object array can point to the one or more quantum bits. In addition, the object array can be understood as an intermediate storage of a quantum bit, which is not referenced by other MLIR Ops, and thus can be deleted or updated in subsequent optimization, so that the intermediate code is more concise or efficient.

[0038] In actual application, since the one or more qubits are predefined or declared objects, the electronic device can determine the value (English can be expressed as Value) of the one or more qubits from a preconfigured second relationship by using the name of the one or more qubits; wherein the second relationship can include a mapping relationship between the name of the object and the Value corresponding to the object. The second relationship can be understood as a map, which at least stores the mapping relationship between the name and Value of all objects or variables in the quantum program in the form of key-value pairs.

[0039] Then, by using the value of the one or more qubits and the type of the pointer array, the electronic device can create the object array and generate the corresponding structure.

[0040] For example, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, the electronic device can traverse the qubit &q, find the Value corresponding to the qubit &q based on the variable name of the qubit &q, and push the Value into the Value Stack (which can be understood as a buffer of the structure MLIR Value). Then, pop the ArrayType from the Type Stack, and pop n times Value from the Value Stack based on the number n of qubits, denoted as nValue. Then, based on the ArrayType and nValue, create the ArrayOp corresponding to the object array, and push the ArrayOp corresponding to the object array into the Value Stack.

[0041] In actual application, since the user can also declare an operator in the original code, the electronic device can further construct the object array in combination with the operator.

[0042] Based on this, in an embodiment, the method can further include: obtaining an operator from the original code, the operator being used to determine the address of the one or more qubits; updating the value of the one or more qubits by using the operator to obtain the address of the one or more qubits; constructing the object array by using the address of the one or more qubits and the type of the pointer array.

[0043] In actual application, the operator can be referred to as an address operator, which can at least obtain the address of the qubit.

[0044] In actual application, in the case that the original code contains the operator, the electronic device can process the value of the one or more qubits by using the operator to update the value of the one or more qubits to the address of the one or more qubits. Then, by using the address of the one or more qubits and the type of the pointer array, the electronic device can create the object array and generate the corresponding structure.

[0045] For example, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, the electronic device can traverse the qubit &q and find the Value corresponding to the qubit &q based on the variable name of the qubit &q, and push the Value into the Value Stack (which can be understood as a buffer of the structure MLIR Value). Then, after popping the Value from the Value Stack, the electronic device processes the Value by using the operator & to obtain the address nValue corresponding to the qubit &q, and pushes the nValue into the Value Stack. Then, the electronic device pops the ArrayType from the Type Stack and pops n times of nValue from the Value Stack based on the number of qubits n, and records the nValues. Then, based on the ArrayType and the nValues, the electronic device creates the ArrayOp corresponding to the object array, and pushes the ArrayOp corresponding to the object array into the Value Stack.

[0046] In actual application, after completing the construction of the object array, the electronic device can replace the pointer array with the object array, that is, replace the pointers in the pointer array with the specific objects pointed to by the pointers, so as to realize the compilation processing of the original code.

[0047] Here, after step 104, the electronic device can establish a mapping relationship between the object array and the pointer array, so that the specific object pointed to by the pointer array can be quickly determined when the pointer array is called by the subsequent quantum program.

[0048] Based on this, in an embodiment, the method can further include: determining the name of the pointer array by using the original code; generating the first relationship by using the name of the pointer array and the object array.

[0049] The electronic device can obtain the name of the pointer array from the original code, and map the name of the pointer array and the object array to generate the first relationship. The first relationship can include the mapping relationship between the name of the pointer array and the object array.

[0050] For example, the electronic device can generate the first relationship by taking the name of the pointer array and the object array as a key-value pair.

[0051] In actual application, when a subsequent quantum program needs to call a quantum bit pointed to by the pointer array, the electronic device can determine the quantum bit pointed to more quickly through the object array.

[0052] Based on this, in an embodiment, the method can further include: In the case where the first instruction is received, obtaining the index of the pointer array from the first instruction, the first instruction being used to request to call a first pointer in the pointer array, the first pointer being used to point to a first quantum bit; Obtaining the object array from the intermediate code; Determining the first quantum bit by using the index of the pointer array and the object array.

[0053] The first instruction carries at least the index of the pointer array, and the index of the pointer array can reflect the quantum bit to be used, such as a quantum bit pointed to by an mth pointer in the pointer array, m being an integer greater than or equal to 1.

[0054] In actual application, after receiving the first instruction, the electronic device can obtain the index of the pointer array from the first instruction, and determine the first quantum bit from the object array by using the index of the pointer array.

[0055] For example, in the case where the ArrayOp corresponding to the object array is constructed, the electronic device can find a first operand Value from the ArrayOp by using the index qa[0] of the pointer array, and take the first operand Value as the first quantum bit.

[0056] In actual application, since there can be multiple object arrays, the electronic device can also select a corresponding object array in combination with the name of the pointer array, and then locate a specific object pointed to.

[0057] Based on this, in an embodiment, the method can further include: Obtaining the name of the pointer array from the first instruction; The object array is obtained from the intermediate code by using the name of the pointer array and a first relationship, the first relationship including a mapping relationship between the name of the pointer array and the object array.

[0058] The first instruction can also carry the name of the pointer array, that is, the variable name of the pointer data.

[0059] In actual application, when the first relationship is established, the electronic device can find the corresponding object array from the first relationship by using the name of the pointer array, and then determine the first qubit from the found object array in combination with the index of the pointer array.

[0060] For example, when the quantum program needs to call the first qubit in the pointer array qa, the electronic device can find the corresponding object array ArrayOp from the first relationship by using the name qa of the pointer array. Then, the electronic device can find the first operand Value from ArrayOp by using the index qa[0] of the pointer array, and take the first operand Value as the first qubit.

[0061] The code processing method provided in the embodiments of the present application includes: obtaining an original code, the original code being used to declare writing one or more qubits into a pointer array, the pointer array including one or more pointers, the one or more pointers being used to point to the one or more qubits; determining the type of the pointer array and the name of the one or more qubits by using the original code; constructing an object array by using the type of the pointer array and the name of the one or more qubits, the object array being used to point to one or more qubits; and replacing the pointer array with the object array to obtain an intermediate code processed by compiling. The technical solution provided in the embodiments of the present application replaces the pointer array in the original code with the object array in the process of translating the original code into the intermediate code. Since the object array can directly point to the specific object written into the pointer array without indirectly pointing to the specific object written into the pointer array through the pointer, the processing flow when the object pointed to by the pointer array is used in the subsequent quantum program can be simplified, and the processing efficiency is improved.

[0062] The present application will be further described in detail below in combination with application examples.

[0063] The application application example focuses on providing a quantum computing compilation service supporting more circuit scenarios for users, and proposes a pointer array parsing conversion scheme applicable to C and C++ programming languages, so that the quantum program compiler can support more complex quantum circuits. Specifically, in the process of converting the source code to the MLIR code, the quantum program compiler replaces the pointers in the pointer array with the specific objects pointed to by the pointers, and places these objects as operands in the auxiliary op, so that whenever the subsequent quantum program uses the objects pointed to by the pointer array, the corresponding operand can be found in the auxiliary op, and the corresponding object is found. Thus, the subsequent pass avoids the processing flow of the pointer array.

[0064] Here, taking the declaration of the pointer number variable statement as an example, the specific process of code compilation of the quantum program compiler (i.e. the electronic device described above) is as shown in Figure 2 The specific process includes the following steps: Step 201: traversing the variable declaration statement (i.e. the original code described above); The variable declaration statement is specifically a pointer array variable declaration statement, which is in the form of: struct qubit *qa[]={&q}.

[0065] Step 202: judging whether the statement type is an array; If the statement type is an array, step 203 is executed; otherwise, the current process is ended.

[0066] Here, by traversing the left part of the variable declaration semantics, it can be judged whether the statement type is an array.

[0067] Step 203: traversing the array (i.e. the pointer array described above), i.e. qa; Step 204: whether the element type of the array is a pointer type; If the type of the element (i.e. the pointer described above) in the array is a qubit structure body pointer, step 205 is executed; otherwise, the current process is ended.

[0068] Step 205: pushing the PointerType (i.e. the type of the pointer described above) into the Type Stack; Here, the PointerType corresponding to the qubit structure body pointer is pushed into the Type Stack.

[0069] Step 206: processing the array type; Step 207: popping the Type Stack to obtain the PointerType; Step 208: creating an ArrayType; Here, the array length is obtained from the node of the syntax tree corresponding to the variable declaration statement, and then the ArrayType is created based on the PointerType and the array length (i.e., the length of the pointer array mentioned above).

[0070] Step 209: push the ArrayType into the Type Stack; Step 210: traverse the array list; Here, the array list can be traversed by traversing the right part of the variable declaration semantic.

[0071] Step 211: traverse the type information; Here, during the process of traversing the type information, steps 203 to 209 are repeated, and another ArrayType is created and pushed into the Type Stack.

[0072] Step 212: traverse the array elements, i.e., q; Step 213: traverse all elements; If all elements are not traversed, step 214 is executed; if all elements are traversed, step 220 is executed.

[0073] Step 214: process the element; Step 215: obtain the value of the element (i.e., the numerical value of the qubit mentioned above); Each variable in the quantum program will put its variable name and corresponding value into the symbol table, and the variable name and corresponding value are stored in the form of key-value pairs.

[0074] Here, since each element has been declared or defined, the corresponding value can be found in the pre-configured symbol table according to the variable name of the element (i.e., the name of the qubit mentioned above).

[0075] Step 216: push the value into the Value Stack; Step 217: process the operator & before the element; Step 218: pop the Value Stack to obtain the value; Step 219: process the value to obtain nvalue (i.e., the address of the qubit mentioned above), and then push nvalue back into the Value Stack; Here, the value is updated based on the operator to obtain nvalue, i.e., the address of q.

[0076] It should be noted that steps 214 to 219 can be executed in a loop until all elements are traversed, and then step 220 is executed.

[0077] Step 220: Process the array list; Step 221: Pop Type Stack to get ArrayType, and pop Value Stack to get nvalues; Here, Type Stack is popped once to get ArrayType pushed in step 211. In addition, Value Stack is popped n times to get n nvalues according to the number of elements n, and the n nvalues are recorded as nvalues.

[0078] Step 222: Create ArrayOp (i.e., the above object array) according to ArrayType and nvalues; Step 223: Push ArrayOp into Value Stack; Step 224: Process the variable declaration statement; Step 225: Pop Type Stack to get ArrayType, and pop Value Stack to get ArrayOp; Here, Type Stack can be popped again to get ArrayType pushed in step 209, and Value Stack can be popped again to get ArrayOp.

[0079] Step 226: Put the pointer array variable into the symbol table according to the traversal information.

[0080] Here, through ArrayType, it can be determined that the variable declaration statement is a pointer array variable declaration statement, so the pointer array variable name and ArrayOp are stored in the symbol table as a key-value pair (i.e., the above first relationship). In this way, the conversion of the variable declaration statement to the MLIR code (i.e., the above intermediate code) is realized.

[0081] When the subsequent program uses the object in the pointer array (i.e., the above first qubit), the corresponding ArrayOp is found from the symbol table through the array variable name (i.e., the name of the above pointer array), and the operand value is found according to the array index (i.e., the index of the above pointer array); wherein the operand value is the specific object pointed to by the pointer array.

[0082] In the application example, when the low c / c++ quantum program is lowered to the MLIR code, the pointer array is converted into the specific object array, the processing work of the pointer array by the subsequent pass is avoided, and the possibility of writing a complex quantum circuit by the c / c++ quantum program is improved.

[0083] To implement the code processing method of the embodiments of the application, the embodiments of the application further provide a code processing apparatus arranged on an electronic device, as shown in the figure, the apparatus comprises: Figure 3 An acquisition unit 301 is configured to acquire an original code, the original code being used to declare writing one or more qubits into a pointer array, the pointer array containing one or more pointers, the one or more pointers being used to point to the one or more qubits. A determination unit 302 is configured to determine, by using the original code, a type of the pointer array and names of the one or more qubits. A construction unit 303 is configured to construct, by using the type of the pointer array and the names of the one or more qubits, an object array, the object array being used to point to one or more qubits. A compilation unit 304 is configured to replace the pointer array with the object array to obtain an intermediate code processed by compilation.

[0084] In an embodiment, the construction unit 303 is configured to determine, by using the names of the one or more qubits, numerical values of the one or more qubits, and construct the object array by using the numerical values of the one or more qubits and the type of the pointer array.

[0085] In an embodiment, the acquisition unit 301 is further configured to acquire, from the original code, an operator, the operator being used to determine addresses of the one or more qubits. The construction unit 303 is further configured to update the numerical values of the one or more qubits by using the operator to obtain the addresses of the one or more qubits, and construct the object array by using the addresses of the one or more qubits and the type of the pointer array.

[0086] In an embodiment, the determination unit 302 is configured to determine, by using the original code, types of the one or more pointers and a length of the pointer array respectively, and determine the type of the pointer array by using the types of the one or more pointers and the length of the pointer array.

[0087] ​In an embodiment, the obtaining unit 301 is further configured to, in a case where a first instruction is received, obtain an index of the pointer array from the first instruction, the first instruction being used to request to invoke a first pointer in the pointer array, the first pointer being used to point to a first qubit; and obtain the object array from the intermediate code. The determining unit 302 is further configured to determine the first qubit by using the index of the pointer array and the object array.

[0088] In an embodiment, the obtaining unit 301 is configured to obtain a name of the pointer array from the first instruction; and obtain the object array from the intermediate code by using the name of the pointer array and a first relationship, the first relationship including a mapping relationship between the name of the pointer array and the object array.

[0089] In an embodiment, the determining unit 302 is further configured to determine the name of the pointer array by using the original code; and generate the first relationship by using the name of the pointer array and the object array.

[0090] In actual application, the obtaining unit 301 can be implemented by a communication interface in a code processing apparatus in combination with a processor, and the determining unit 302, the constructing unit 303, and the compiling unit 304 can be implemented by a processor in the code processing apparatus.

[0091] It should be noted that the code processing apparatus provided in the above embodiments is only used as an example for the division of the above program units, and in actual application, the above processing can be completed by different program units according to needs, that is, the internal structure of the apparatus is divided into different program units to complete all or part of the above processing. In addition, the code processing apparatus and the code processing method provided in the above embodiments belong to the same concept, and the specific implementation process is described in the method embodiments, which will not be repeated here.

[0092] Based on the hardware implementation of the above program modules, and in order to implement the method of the code processing provided in the embodiments of the present application, the embodiments of the present application further provide an electronic device, as shown in the following Figure 4 The electronic device 400 includes: A communication interface 401 capable of interacting with other devices; A processor 402 connected with the communication interface 401 to realize interaction with other devices, used to run a computer program to execute the method provided in one or more of the technical solutions described above; A memory 403, in which the computer program is stored.

[0093] Specifically, the communication interface 401 is configured to acquire original code, the original code being used to declare writing one or more qubits into a pointer array, the pointer array containing one or more pointers, the one or more pointers being used to point to the one or more qubits. The processor 402 is configured to determine a type of the pointer array and a name of the one or more qubits by using the original code, construct an object array by using the type of the pointer array and the name of the one or more qubits, the object array being used to point to one or more qubits, and replace the pointer array with the object array to obtain intermediate code after compilation processing.

[0094] In an embodiment, the processor 402 is configured to determine a value of the one or more qubits by using the name of the one or more qubits, and construct the object array by using the value of the one or more qubits and the type of the pointer array.

[0095] In an embodiment, the processor 402 is further configured to acquire an operator from the original code, the operator being used to determine an address of the one or more qubits, update the value of the one or more qubits by using the operator to obtain the address of the one or more qubits, and construct the object array by using the address of the one or more qubits and the type of the pointer array.

[0096] In an embodiment, the processor 402 is configured to determine the type of the pointer array by using the original code and respectively determining a type of the one or more pointers and a length of the pointer array.

[0097] In an embodiment, the processor 402 is further configured to acquire an index of the pointer array from a first instruction in a case where the first instruction is received, the first instruction being used to request calling a first pointer in the pointer array, the first pointer being used to point to a first qubit, acquire the object array from the intermediate code, and determine the first qubit by using the index of the pointer array and the object array.

[0098] In an embodiment, the processor 402 is configured to acquire a name of the pointer array from the first instruction, and acquire the object array from the intermediate code by using the name of the pointer array and a first relationship, the first relationship containing a mapping relationship between the name of the pointer array and the object array.

[0099] In an embodiment, the processor 402 is further configured to determine a name of the pointer array using the original code; and generate the first relationship using the name of the pointer array and the object array.

[0100] It should be noted that the specific processing procedures of the communication interface 401 and the processor 402 can be understood with reference to the above method.

[0101] Of course, in actual application, various components in the electronic device 400 are coupled together through the bus system 404. It can be understood that the bus system 404 is used to realize the connection and communication between the components. In addition to the data bus, the bus system 404 also includes a power bus, a control bus, and a status signal bus. However, in order to clearly illustrate the present application, all buses are marked as the bus system 404 in the Figure 4 .

[0102] The memory 403 in the embodiment of the present application is used to store various types of data to support the operation of the electronic device 400. Examples of these data include: any computer programs used for operation on the electronic device 400.

[0103] The method disclosed in the above embodiment of the present application can be applied to the processor 402 or implemented by the processor 402. The processor 402 can be an integrated circuit chip with a signal processing capability. In the implementation process, each step of the above method can be completed by the integrated logic circuit or the instruction in the form of software in the processor 402. The processor 402 mentioned above can be a general processor, a digital signal processor (DSP), or other programmable logic device, discrete gate or transistor logic device, discrete hardware component, etc. The processor 402 can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general processor can be a microprocessor or any conventional processor, etc. In combination with the steps of the method disclosed in the embodiments of the present application, the hardware decoding processor can be directly embodied to execute the above method, or the hardware and software modules in the decoding processor can be combined to execute the above method. The software module can be located in the storage medium, which is located in the memory 403, and the processor 402 reads the information in the memory 403 and combines the hardware to complete the steps of the above method.

[0104] In an exemplary embodiment, the electronic device 400 can be implemented by one or more Application Specific Integrated Circuits (ASICs), DSPs, Programmable Logic Devices (PLDs), Complex Programmable Logic Devices (CPLDs), Field-Programmable Gate Arrays (FPGAs), general-purpose processors, controllers, microcontrollers (MCUs), microprocessors, or other electronic elements for executing the aforementioned methods.

[0105] It can be understood that the memory (the memory 403) of the embodiments of the present application can be a volatile memory or a non-volatile memory, and can also include both volatile and non-volatile memories. The non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a ferromagnetic random access memory (FRAM), a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM). The magnetic surface memory can be a disk memory or a tape memory. The volatile memory can be a random access memory (RAM) used as an external cache. By way of example but not limitation, many forms of RAM can be used, such as static random access memory (SRAM), synchronous static random access memory (SSRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), sync link dynamic random access memory (SLDRAM), and direct rambus random access memory (DRRAM).The memory described in the embodiments of the present application is intended to include, but not limited to, these and any other suitable types of memory.

[0106] In the example embodiments, the embodiments of the present application also provide a storage medium, specifically a computer readable storage medium, such as the memory 403 storing a computer program executable by the processor 402 of the electronic device 400 to complete the steps of the aforementioned code processing method. The computer readable storage medium can be FRAM, ROM, PROM, EPROM, EEPROM, Flash Memory, magnetic surface memory, optical disc, or CD-ROM, etc.

[0107] In the example embodiments, the embodiments of the present application also provide a computer program product including a computer program executable by the processor 402 of the electronic device 400 to complete the steps of the aforementioned code processing method.

[0108] It should be noted that "first", "second", etc. are used to distinguish similar objects, and do not necessarily mean a specific order or sequence.

[0109] In addition, the technical solutions described in the embodiments of the present application can be combined arbitrarily without conflict.

[0110] The above is only a preferred embodiment of the present application, and is not intended to limit the protection scope of the present application.

Claims

1. A code processing method, characterized in that: The method comprises: Obtaining original code, where the original code is used to declare that one or more quantum bits are written into a pointer array, where the pointer array includes one or more pointers, and the one or more pointers are used to point to the one or more quantum bits; Determine, using the original code, the type of the pointer array and the name of the one or more quantum bits; Constructing an object array using the type of the pointer array and the names of the one or more quantum bits, wherein the object array is used to point to the one or more quantum bits; The pointer array is replaced with the object array to obtain compiled intermediate code.

2. The method according to claim 1, characterized in that The step of constructing an object array using the type of the pointer array and the names of the one or more quantum bits includes: Determining, using the names of the one or more qubits, values ​​of the one or more qubits; The object array is constructed using the values ​​of the one or more quantum bits and the type of the pointer array.

3. The method according to claim 2, characterized in that The method further comprises: Obtaining an operator from the original code, the operator being used to determine an address of the one or more quantum bits; Using the operator to update the values ​​of the one or more quantum bits to obtain addresses of the one or more quantum bits; The object array is constructed using the addresses of the one or more quantum bits and the type of the pointer array.

4. The method according to claim 1, wherein Determining the type of the pointer array and the name of the one or more quantum bits by using the original code includes: Using the original code, respectively determining the type of the one or more pointers and the length of the pointer array; The type of the pointer array is determined using the type of the one or more pointers and the length of the pointer array.

5. The method according to claim 1, wherein The method further comprises: Upon receiving a first instruction, obtaining an index of the pointer array from the first instruction, wherein the first instruction is used to request calling a first pointer in the pointer array, and the first pointer is used to point to a first qubit; Obtaining the object array from the intermediate code; The first quantum bit is determined using the index of the pointer array and the object array.

6. The method according to claim 5, characterized in that The obtaining of the object array from the intermediate code comprises: Obtaining the name of the pointer array from the first instruction; The object array is obtained from the intermediate code using the name of the pointer array and a first relationship, where the first relationship includes a mapping relationship between the name of the pointer array and the object array.

7. The method according to claim 6, characterized in that The method further comprises: Using the original code, determine the name of the pointer array; The first relationship is generated using the name of the pointer array and the object array.

8. A code processing device, characterized in that: The device comprises: an acquisition unit, configured to acquire an original code, wherein the original code is used to declare that one or more quantum bits are written into a pointer array, wherein the pointer array includes one or more pointers, and the one or more pointers are used to point to the one or more quantum bits; a determining unit, configured to determine a type of the pointer array and a name of the one or more quantum bits using the original code; a construction unit, configured to construct an object array using the type of the pointer array and the names of the one or more quantum bits, wherein the object array is used to point to the one or more quantum bits; The compilation unit is used to replace the pointer array with the object array to obtain the compiled intermediate code.

9. An electronic device, characterized in that: include: a processor and a memory for storing a computer program capable of being executed on the processor, Wherein, when the processor is used to run the computer program, it executes the steps of the method according to any one of claims 1 to 7.

10. A storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

11. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Quantum computing program compiling method and device

    CN117910588A

  • Pointer vectorizing system

    JP1993303500A

  • Retargetable compilation for quantum computing systems

    US11010145B1

  • Method of compiling source code

    US20070240137A1