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

By converting the pointer array in quantum programming into an object array that directly points to the qubit, the problem of low processing efficiency of pointer arrays in quantum programming is solved, and the processing efficiency is improved.

CN120806190BActive Publication Date: 2026-01-23CHINA MOBILE (SUZHOU) SOFTWARE TECH CO LTD +1
View PDF 1 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

In quantum programming, the use of pointer arrays presents a problem of low processing efficiency, especially after the qubits are written into the pointer array, subsequent calls are not efficient.

Method used

Converting the pointer array into an object array allows the object array to directly point to the qubit, avoiding indirect pointing through pointers and simplifying the processing flow.

Benefits of technology

It improves the processing efficiency when using pointer array objects in quantum programs and simplifies the subsequent processing flow.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120806190B_ABST
    Figure CN120806190B_ABST
Patent Text Reader

Abstract

The application discloses a code processing method and device, electronic equipment, a storage medium and a computer program product. The method comprises the following steps: acquiring an original code, wherein the original code is used for declaring writing one or more quantum bits 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, wherein the object array is used for pointing to one or more quantum bits; and replacing the pointer array with the object array to obtain intermediate code after compilation processing. According to the technical scheme provided by the application, the pointer of the pointer array is replaced with the specific object pointed to by the pointer during the code compilation process, and the processing efficiency when subsequently calling the object associated with the pointer array can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of quantum computing, and more particularly to a code processing method, apparatus, electronic device, storage medium, and computer program product. Background Technology

[0002] In the field of quantum program compilers based on the C / C++ programming language, writing (or putting) multiple defined and declared qubits into an array is a necessary means of designing complex quantum gates and quantum circuits. This helps to improve the compiler front-end's support for writing complex quantum circuits, and at the same time, it lays the foundation for the compiler back-end to optimize complex quantum circuits.

[0003] Currently, in the aforementioned quantum program, qubits are written as objects into an array. However, in scenarios where objects are written into an array of pointers, there is a problem of low processing efficiency when subsequently accessing objects within the pointer array. Summary of the Invention

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

[0005] The technical solution of this application embodiment is implemented as follows:

[0006] This application provides a code processing method, including:

[0007] Obtain the original code, which is used to declare that one or more qubits are written into an array of pointers, the array of pointers containing one or more pointers, the one or more pointers being used to point to the one or more qubits;

[0008] Using the original code, determine the type of the pointer array and the name of the one or more qubits;

[0009] Using the type of the pointer array and the name of the one or more qubits, an object array is constructed, which is used to point to the one or more qubits;

[0010] The pointer array is replaced with the object array to obtain the intermediate code after compilation.

[0011] This application also provides a code processing apparatus, including:

[0012] An acquisition unit is used to acquire the original code, which is 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;

[0013] A determining unit is used to determine the type of the pointer array and the name of the one or more qubits using the original code;

[0014] A construction unit is configured to construct an object array, which points to one or more qubits, using the type of the pointer array and the name of the one or more qubits.

[0015] The compilation unit is used to replace the pointer array with the object array to obtain the intermediate code after compilation.

[0016] This application also provides an electronic device, including: a processor and a memory for storing a computer program capable of running on the processor.

[0017] When the processor runs the computer program, it executes the steps of any of the above code processing methods.

[0018] This application also provides a storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of any of the above-described code processing methods.

[0019] This application also provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of any of the above-described code processing methods.

[0020] The code processing method, apparatus, electronic device, storage medium, and computer program product provided in this application include: obtaining 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; using the original code, determining the type of the pointer array and the name of the one or more qubits; using the type of the pointer array and the name of the one or more qubits, constructing an object array, the object array being used to point to the one or more qubits; and replacing the pointer array with the object array to obtain intermediate code after compilation. The technical solution provided in this application replaces the pointer array in the original code with an object array during the process of translating the original code into intermediate code. Since the object array can directly point to the specific object written to the pointer array, without needing to indirectly point to the specific object through pointers, it simplifies the processing flow when using the object pointed to by the pointer array in subsequent quantum programs, thus improving processing efficiency. Attached Figure Description

[0021] Figure 1 This is a flowchart illustrating a code processing method according to an embodiment of this application;

[0022] Figure 2 This is a schematic diagram illustrating a code compilation process as an application example of this application;

[0023] Figure 3 This is a schematic diagram of the structure of a code processing device according to an embodiment of this application;

[0024] Figure 4 This is a schematic diagram of the structure of an electronic device according to an embodiment of this application. Detailed Implementation

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

[0026] In relevant C / C++ quantum programs, one approach is to store qubits in a `std::vector` array. However, from an application perspective, while `std::vector` allows storing qubits, it prohibits pointer array syntax in quantum programs. This limits its applicability to quantum programs based on C. Furthermore, from a development complexity perspective, parsing `std::vector` array statements requires the quantum program compiler to support C++ concepts such as namespaces or classes, and involves more complex syntax structures (more syntax tree scenarios), further increasing development complexity.

[0027] To address the aforementioned issues, another approach is proposed in related technologies: placing the pointers to the already defined qubits into a pointer array. From a syntactic perspective, pointer arrays are more closely similar to the underlying arrays. This allows for development on top of existing array functionality when parsing pointer array statements, reducing development complexity.

[0028] However, since pointer arrays indirectly point to objects written to the pointer array through pointers, there is a problem of low processing efficiency when subsequently accessing objects in the pointer array.

[0029] Based on this, in various embodiments of this application, when the source code is translated into intermediate code, the pointer array is converted into an object array so that the object array can directly point to the specific object. In this way, the subsequent processing of the pointer array can be avoided, thus improving processing efficiency.

[0030] This application provides a code processing method applied to electronic devices, such as... Figure 1 As shown, the method includes:

[0031] Step 101: Obtain the original code, which is 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;

[0032] Step 102: Using the original code, determine the type of the pointer array and the name of the one or more qubits;

[0033] Step 103: Construct an object array using the type of the pointer array and the name of the one or more qubits, the object array being used to point to the one or more qubits;

[0034] Step 104: Replace the pointer array with the object array to obtain the intermediate code after compilation.

[0035] In practical applications, the electronic device may include a quantum program compiler based on the C and / or C++ programming languages, and may employ a Low Level Virtual Machine (LLVM) framework. Furthermore, the electronic device must at least support compilation from high-level code to low-level code. The high-level code can be understood as the original code input by the developer (or source code), and the low-level code can be understood as the intermediate code after compilation. This allows high-level code corresponding to different programming languages ​​to be compiled into a unified form of low-level code, facilitating subsequent optimization of this unified low-level code.

[0036] In practical applications, before step 101, in order to compile from the original code to the intermediate code, a definition file about the intermediate code can be written into the electronic device in advance; wherein, the intermediate code may contain multi-level intermediate representation (MLIR) code.

[0037] It should be noted that when the intermediate code includes MLIR code, the user can write a definition file for the MLIR code in the electronic device based on the Operation Definition Specification (ODS) language. This definition file can include definitions for MLIR Type, MLIR Value, and MLIR Op. An MLIR Op is the basic unit of a line in MLIR code; that is, MLIR code consists of lines of code, and each line of MLIR code can be understood as an Op. MLIR Type and MLIR Value are the basic elements that make up an MLIR Op. Furthermore, an MLIR Value can be used as an operand of an MLIR Op and has a corresponding MLIR Type. The MLIR Op corresponding to the MLIR Value can be specified when defining the MLIR Op.

[0038] For example, 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 used to create a constant value; 0 can be understood as the value of the constant value, that is, the operand of the MLIR Op; and index can be understood as the MLIR Type used to indicate the data type of the constant value.

[0039] In practical applications, one or more structures required for code compilation can be predefined in the electronic device. These structures can at least mark quantum gate functions or qubits in the code.

[0040] For example, taking pointer array A as an example, a user can create a structure MLIR Type based on the ODS language and name it PointerType. PointerType serves as an identifier that can indicate the type of pointer B in pointer array A, that is, whether pointer B is a pointer that can point to a qubit (which can be called a qubit pointer).

[0041] For example, taking pointer array A as an example, a user can create a structure MLIR Type based on the ODS language and name it ArrayType. ArrayType can indicate the type of pointer array A, that is, whether pointer array A is a pointer array that can be written to qubits (which can be called a qubit pointer array). ArrayType can contain one or more of the following: type parameter, size parameter, and array name (or at least one of them). The element type parameter can indicate the type of elements that can be written to or received in pointer array A, the array size parameter can indicate the size of pointer array A, and the array name contains the name of pointer array A.

[0042] For example, taking a pointer array A as an example, a user can create a structure MLIR Op and name it ArrayOp. ArrayOp, as an array container, can store MLIR values ​​representing source code variables (such as qubits written to pointer array A) as operands. ArrayOp can contain a type parameter and a size parameter. The type parameter indicates the type of the operands, and the size parameter indicates the number of operands stored in ArrayOp. The return value of ArrayOp is the operand, and correspondingly, the return type is ArrayType.

[0043] In practical applications, before compiling the original code, the electronic device can obtain the original code through manual input. The original code may contain source code in C or C++ programming languages. This application embodiment does not limit the type of the original code.

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

[0045] It should be noted that the original code can consist of two parts, specifically 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 to the pointer array qa[] as an object, the variable declaration statement can include the pointer array qa[] on the left and the qubit &q on the right.

[0046] In practical applications, 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 indicates whether the pointer array is written to qubits, that is, whether the pointer array is a qubit pointer array.

[0047] Specifically, in one embodiment, determining the type of the pointer array and the name of the one or more qubits using the original code includes:

[0048] Using the original code, determine the type of the one or more pointers and the length of the pointer array, respectively;

[0049] The type of the pointer array is determined by using the type of the one or more pointers and the length of the pointer array.

[0050] In practical applications, the electronic device can traverse the syntax tree of the original code to determine the type of the one or more pointers and generate corresponding structures. The type of the one or more pointers indicates whether they are used to point to qubits, i.e., whether they are qubit pointers. Additionally, the electronic device can obtain the length of the pointer array from the syntax tree of the original code; the length of the pointer array can be understood as the size of the pointer array.

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

[0052] Next, the electronic device can use the types of the one or more pointers and the length of the pointer array to determine the type of the pointer array and generate the corresponding structure.

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

[0054] In addition, the electronic device can also determine the name of the one or more qubits by traversing the syntax tree of the original code; wherein the name of the one or more qubits may include the variable name of each qubit.

[0055] In practical applications, after determining the type of the pointer array and the name of the one or more qubits, the electronic device can begin constructing the object array.

[0056] Specifically, in one embodiment, constructing an object array using the type of the pointer array and the names of the one or more qubits includes:

[0057] The numerical value of the one or more qubits is determined using their names;

[0058] The object array is constructed using the values ​​of the one or more qubits and the type of the pointer array.

[0059] In practical applications, one or more qubits can be stored as operands in the object array, allowing the object array to point to the one or more qubits. Furthermore, the object array can be understood as a storage medium for a single qubit, not referenced by other MLIR Ops. Therefore, it can be deleted or updated in subsequent optimizations, making the intermediate code more concise or efficient.

[0060] In practical applications, since the one or more qubits are predefined or declared objects, the electronic device can use the names of the one or more qubits to determine their values ​​(which can be expressed as Values) from a pre-configured second relation. This second relation can contain a mapping between object names and their corresponding Values. The second relation can be understood as a map, storing at least the key-value pairs of names and values ​​for all objects or variables in the quantum program.

[0061] Next, using the values ​​of one or more qubits and the type of the pointer array, the electronic device can create the object array and generate the corresponding structure.

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

[0063] In practical applications, since the user may also declare operators in the original code, the electronic device can further combine operators to construct the object array.

[0064] Based on this, in one embodiment, the method may further include:

[0065] Obtain operators from the original code, the operators being used to determine the addresses of the one or more qubits;

[0066] The operator is used to update the value of the one or more qubits to obtain the address of the one or more qubits;

[0067] The object array is constructed using the addresses of the one or more qubits and the type of the pointer array.

[0068] In practical applications, the operator can be called the address-of operator, which can at least obtain the address of the quantum bit.

[0069] In practical applications, when the original code contains the operator, the electronic device can use the operator to process the values ​​of the one or more qubits to update the values ​​of the one or more qubits to their addresses. Then, using the addresses 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.

[0070] For example, taking the variable declaration statement "struct qubit *qa[]={&q}" as an example, the electronic device can traverse the qubit &q, and based on the variable name of the qubit &q, find the value corresponding to the qubit &q, and push the value into the Value Stack (which can be understood as a buffer of the MLIR Value structure). Next, after popping the value from the Value Stack, the value is processed using the & operator to obtain the address nValue corresponding to the qubit &q, and nValue is pushed into the Value Stack. Then, ArrayType is popped from the Type Stack, and based on the number of qubits n, nValues ​​are popped from the Value Stack n times, denoted as nValues. Then, based on ArrayType and nValues, the ArrayOp corresponding to the object array is created, and the ArrayOp corresponding to the object array is pushed into the Value Stack.

[0071] In practical applications, after the object array is constructed, 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 they point to, thus realizing the compilation processing of the original code.

[0072] Here, after step 104, the electronic device can establish a mapping relationship between the object array and the pointer array so that when the quantum program calls the pointer array, it can quickly determine the specific object pointed to by the pointer array.

[0073] Based on this, in one embodiment, the method may further include:

[0074] Using the original code, determine the name of the pointer array;

[0075] The first relationship is generated using the name of the pointer array and the object array.

[0076] Here, the electronic device can obtain the name of the pointer array from the original code and map the name of the pointer array to the object array to generate the first relationship; wherein, the first relationship may include the mapping relationship between the name of the pointer array and the object array.

[0077] For example, the electronic device may use the name of the pointer array and the object array as key-value pairs to generate the first relationship.

[0078] In practical applications, when a subsequent quantum program needs to call the qubit pointed to by the pointer array, the electronic device can determine the pointed-to qubit more quickly through the object array.

[0079] Based on this, in one embodiment, the method may further include:

[0080] Upon receiving a first instruction, the index of the pointer array is obtained from the first instruction, the first instruction being used to request the invocation of the first pointer in the pointer array, the first pointer being used to point to the first qubit;

[0081] Obtain the array of objects from the intermediate code;

[0082] The first qubit is determined using the index of the pointer array and the object array.

[0083] The first instruction carries at least the index of the pointer array, which reflects the qubit to be used, such as the qubit pointed to by the m-th pointer in the pointer array, where m is an integer greater than or equal to 1.

[0084] In practical applications, after receiving the first instruction, the electronic device can obtain the index of the pointer array from the first instruction and use the index of the pointer array to determine the first quantum bit from the object array.

[0085] For example, in a scenario where an ArrayOp corresponding to an object array is constructed, the electronic device can use the index qa[0] of the pointer array to find the first operand Value from the ArrayOp and use the first operand Value as the first qubit.

[0086] In practical applications, since there may be multiple object arrays, the electronic device can also select the corresponding object array by combining the name of the pointer array, and then locate the specific object it points to.

[0087] Based on this, in one embodiment, the method may further include:

[0088] Obtain the name of the pointer array from the first instruction;

[0089] Using the name of the pointer array and a first relationship, the object array is obtained from the intermediate code. The first relationship includes the mapping relationship between the name of the pointer array and the object array.

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

[0091] In practical applications, once the first relationship is established, the electronic device can find the corresponding object array from the first relationship by the name of the pointer array, and then determine the first qubit from the found object array by combining the index of the pointer array.

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

[0093] The code processing method provided in this application includes: obtaining original code, which declares that one or more qubits are written into a pointer array, the pointer array containing one or more pointers pointing to the one or more qubits; using the original code, determining the type of the pointer array and the name of the one or more qubits; using the type of the pointer array and the name of the one or more qubits, constructing an object array, the object array pointing to the one or more qubits; and replacing the pointer array with the object array to obtain intermediate code after compilation. The technical solution provided in this application replaces the pointer array in the original code with an object array during the translation of the original code into intermediate code. Since the object array can directly point to the specific object written to the pointer array, without needing to indirectly point to the specific object written to the pointer array through pointers, it simplifies the processing flow when using the object pointed to by the pointer array in subsequent quantum programs, thus improving processing efficiency.

[0094] The following section provides a more detailed description of this application with reference to application examples.

[0095] This application example focuses on providing users with quantum computing compilation services that support more circuit scenarios. It proposes a pointer array parsing and conversion scheme applicable to C and C++ programming languages, enabling the quantum program compiler to support more complex quantum circuits. Specifically, during the conversion of source code to MLIR code, the quantum program compiler replaces the pointers in the pointer array with the specific objects they point to, and places these objects as operands in auxiliary operations (ops). Thus, whenever the quantum program subsequently uses an object pointed to by the pointer array, it can find the corresponding operand from the auxiliary ops, thereby finding the corresponding object. This avoids the need for subsequent pass operations to process the pointer array.

[0096] Here, taking the declaration of pointer variable statements as an example, the specific process of code compilation by the quantum program compiler (i.e., the aforementioned electronic device) is as follows: Figure 2 As shown, it includes the following steps:

[0097] Step 201: Traverse the variable declaration statements (i.e., the original code above);

[0098] Specifically, the variable declaration statement is a pointer array variable declaration statement, in the form of: struct qubit *qa[]={&q}.

[0099] Step 202: Determine if the statement type is an array;

[0100] If the statement type is an array, then step 203 is executed; otherwise, the current process ends.

[0101] Here, by traversing the left-hand side of the variable declaration semantics, it is possible to determine whether the statement type is an array.

[0102] Step 203: Traverse the array (i.e., the pointer array mentioned above), i.e., qa;

[0103] Step 204: Check if the array elements are pointer types;

[0104] If the type of an element in the array (i.e., the pointer mentioned above) is a pointer to a qubit structure, then step 205 is executed; otherwise, the current process ends.

[0105] Step 205: Push PointerType (i.e., the type of the pointer mentioned above) onto the Type Stack;

[0106] Here, the PointerType corresponding to the quantum bit structure pointer is pushed into the Type Stack.

[0107] Step 206: Process array types;

[0108] Step 207: Pop up the Type Stack and obtain the PointerType;

[0109] Step 208: Create ArrayType;

[0110] Here, after obtaining the array length from the node of the syntax tree corresponding to the variable declaration statement, ArrayType (i.e., the type of the pointer array mentioned above) is created using PointerType and array length (i.e., the length of the pointer array mentioned above).

[0111] Step 209: Push ArrayType onto the Type Stack;

[0112] Step 210: Traverse the array list;

[0113] Here, by traversing the right-hand side of the variable declaration semantics, it is possible to traverse the array list.

[0114] Step 211: Traverse and process type information;

[0115] Here, during the process of traversing and processing type information, steps 203 to 209 will be executed repeatedly. During this process, another ArrayType will be created and pushed into the Type Stack.

[0116] Step 212: Traverse the array elements, i.e., q;

[0117] Step 213: Traverse all elements;

[0118] If not all elements have been traversed, proceed to step 214; if all elements have been traversed, proceed to step 220.

[0119] Step 214: Process the elements;

[0120] Step 215: Obtain the value of the element (i.e., the numerical value of the qubit mentioned above).

[0121] In the quantum program, each variable puts its own variable name and corresponding value into a symbol table, and the variable name and corresponding value are stored in the form of key-value pairs.

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

[0123] Step 216: Push the value onto the Value Stack;

[0124] Step 217: The & operator before processing elements;

[0125] Step 218: Pop up the Value Stack and retrieve the value;

[0126] Step 219: Process the value to obtain the nvalue (i.e., the address of the qubit mentioned above), and then push the nvalue back into the Value Stack;

[0127] Here, the value is updated based on the operator to obtain nvalue, which is the address of q.

[0128] It should be noted that steps 214 to 219 can be executed repeatedly until all elements have been traversed, and then step 220 can be executed.

[0129] Step 220: Process the array list;

[0130] Step 221: Pop the Type Stack, get the ArrayType, and pop the Value Stack to get the nvalues;

[0131] Here, the Type Stack is popped once to obtain the ArrayType pushed in step 211. Additionally, the Value Stack is popped n times according to the number of elements n to obtain n nvalues, and these n nvalues ​​are denoted as nvalues.

[0132] Step 222: Create ArrayOp (i.e., the array of objects mentioned above) based on ArrayType and nvalues;

[0133] Step 223: Push ArrayOp onto the Value Stack;

[0134] Step 224: Process variable declaration statements;

[0135] Step 225: Pop the Type Stack and get the ArrayType; pop the Value Stack and get the ArrayOp;

[0136] Here, the Type Stack can be popped again to obtain the ArrayType pushed in step 209, and the Value Stack can be popped again to obtain the ArrayOp.

[0137] Step 226: Combine the traversal information to put the pointer array variable into the symbol table.

[0138] Here, by using ArrayType, we can determine that the variable declaration statement is a pointer array variable declaration statement. Therefore, the pointer array variable name and ArrayOp are stored as key-value pairs (i.e., the first relationship mentioned above) in the symbol table. In this way, the conversion from the variable declaration statement to MLIR code (i.e., the intermediate code mentioned above) is achieved.

[0139] When subsequent programs use the object in the pointer array (i.e., the first qubit mentioned above), they find the corresponding ArrayOp in the symbol table through the array variable name (i.e., the name of the pointer array mentioned above), and then find the operand value according to the array index (i.e., the index of the pointer array mentioned above); where operand value is the specific object pointed to by the pointer array.

[0140] In the application examples of this application, when reducing the C / C++ quantum program to MLIR code, the pointer array is converted into a specific object array, avoiding the subsequent processing of the pointer array by the pass, thereby increasing the possibility of writing complex quantum circuits in the C / C++ quantum program.

[0141] To implement the code processing method of this application embodiment, this application embodiment also provides a code processing device, which is installed on an electronic device, such as... Figure 3 As shown, the device includes:

[0142] Acquisition unit 301 is used to acquire 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;

[0143] The determining unit 302 is used to determine the type of the pointer array and the name of the one or more qubits using the original code;

[0144] Construction unit 303 is used to construct an object array 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;

[0145] Compilation unit 304 is used to replace the pointer array with the object array to obtain intermediate code after compilation.

[0146] In one embodiment, the construction unit 303 is used to determine the value of the one or more qubits using the names of the one or more qubits; and to construct the object array using the value of the one or more qubits and the type of the pointer array.

[0147] In one embodiment, the acquisition unit 301 is further configured to acquire operators from the original code, the operators being used to determine the addresses of the one or more qubits;

[0148] The construction unit 303 is further configured to update the value of the one or more qubits using the operator to obtain the address of the one or more qubits; and to construct the object array using the address of the one or more qubits and the type of the pointer array.

[0149] In one embodiment, the determining unit 302 is configured to use the original code to determine the type of the one or more pointers and the length of the pointer array, respectively; and to use the type of the one or more pointers and the length of the pointer array to determine the type of the pointer array.

[0150] In one embodiment, the acquisition unit 301 is further configured to, upon receiving a first instruction, acquire an index of the pointer array from the first instruction, the first instruction being configured to request the invocation of a first pointer in the pointer array, the first pointer being configured to point to a first qubit; and acquire the object array from the intermediate code;

[0151] The determining unit 302 is further configured to determine the first qubit using the index of the pointer array and the object array.

[0152] In one embodiment, the acquisition unit 301 is configured to acquire the name of the pointer array from the first instruction; and acquire the object array from the intermediate code using the name of the pointer array and a first relationship, wherein the first relationship includes a mapping relationship between the name of the pointer array and the object array.

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

[0154] In practical applications, the acquisition unit 301 can be implemented by a communication interface in the code processing device combined with a processor, and the determination unit 302, the construction unit 303 and the compilation unit 304 can be implemented by a processor in the code processing device.

[0155] It should be noted that the code processing device provided in the above embodiments is only illustrated by the division of the above-described program units. In practical applications, the above processing can be assigned to different program units as needed, that is, the internal structure of the device can be divided into different program units to complete all or part of the processing described above. In addition, the code processing device and the code processing method embodiments provided in the above embodiments belong to the same concept, and their specific implementation process can be found in the method embodiments, which will not be repeated here.

[0156] Based on the hardware implementation of the above program modules, and in order to implement the code processing method of the embodiments of this application, the embodiments of this application also provide an electronic device, such as... Figure 4 As shown, the electronic device 400 includes:

[0157] Communication interface 401 enables interaction with other devices;

[0158] The processor 402 is connected to the communication interface 401 to enable interaction with other devices and to execute the methods provided by one or more of the above-mentioned technical solutions when running a computer program;

[0159] The computer program is stored in memory 403.

[0160] Specifically, the communication interface 401 is used to obtain the original code, which is used to declare that one or more qubits are written into a pointer array, the pointer array containing one or more pointers, which are used to point to the one or more qubits;

[0161] The processor 402 is configured to use the original code to determine the type of the pointer array and the name of the one or more qubits; use the type of the pointer array and the name of the one or more qubits to construct an object array, the object array being used to point to the one or more qubits; and replace the pointer array with the object array to obtain the intermediate code after compilation.

[0162] In one embodiment, the processor 402 is configured to determine the value of the one or more qubits using the names of the one or more qubits; and to construct the object array using the value of the one or more qubits and the type of the pointer array.

[0163] In one embodiment, the processor 402 is further configured to obtain operators from the original code, the operators being used to determine the addresses of the one or more qubits; update the values ​​of the one or more qubits using the operators to obtain the addresses of the one or more qubits; and construct the object array using the addresses of the one or more qubits and the type of the pointer array.

[0164] In one embodiment, the processor 402 is configured to use the original code to determine the type of the one or more pointers and the length of the pointer array, respectively; and to use the type of the one or more pointers and the length of the pointer array to determine the type of the pointer array.

[0165] In one embodiment, the processor 402 is further configured to, upon receiving a first instruction, obtain the index of the pointer array from the first instruction, the first instruction being used to request the invocation of a first pointer in the pointer array, the first pointer being used to point to a first qubit; obtain the object array from the intermediate code; and determine the first qubit using the index of the pointer array and the object array.

[0166] In one embodiment, the processor 402 is configured to obtain the name of the pointer array from the first instruction; and obtain the object array from the intermediate code using the name of the pointer array and a first relationship, wherein the first relationship includes a mapping relationship between the name of the pointer array and the object array.

[0167] In one embodiment, the processor 402 is further configured to use the original code to determine the name of the pointer array; and use the name of the pointer array and the object array to generate the first relationship.

[0168] 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.

[0169] Of course, in practical applications, the various components in electronic device 400 are coupled together through bus system 404. It can be understood that bus system 404 is used to realize the connection and communication between these components. In addition to a data bus, bus system 404 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, in... Figure 4 The general designated all buses as Bus System 404.

[0170] The memory 403 in this embodiment is used to store various types of data to support the operation of the electronic device 400. Examples of such data include any computer program used to operate on the electronic device 400.

[0171] The methods disclosed in the embodiments of this application can be applied to the processor 402, or implemented by the processor 402. The processor 402 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the integrated logic circuit of the hardware in the processor 402 or by instructions in the form of software. The processor 402 may be a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The processor 402 can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this application. The general-purpose processor may be a microprocessor or any conventional processor, etc. The steps of the methods disclosed in the embodiments of this application can be directly reflected as being executed by a hardware decoding processor, or being executed by a combination of hardware and software modules in the decoding processor. The software modules may be located in a storage medium, which is located in the memory 403. The processor 402 reads the information in the memory 403 and completes the steps of the aforementioned method in combination with its hardware.

[0172] In an exemplary embodiment, the electronic device 400 may 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 components to perform the aforementioned method.

[0173] It is understood that the memory (memory 403) in this embodiment of the application can be volatile memory or non-volatile memory, or both. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), ferromagnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD-ROM); magnetic surface memory can be disk storage or magnetic tape storage. Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, 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 (DDRSDRAM), Enhanced Synchronous Dynamic Random Access Memory (ESDRAM), SyncLink Dynamic Random Access Memory (SLDRAM), and Direct Rambus Random Access Memory (DRRAM).The memories described in the embodiments of this application are intended to include, but are not limited to, these and any other suitable types of memories.

[0174] In an exemplary embodiment, this application also provides a storage medium, namely a computer storage medium, specifically a computer-readable storage medium, such as a memory 403 storing a computer program, which can be executed by the processor 402 of the electronic device 400 to complete the steps of the aforementioned code processing method. The computer-readable storage medium may be a memory such as FRAM, ROM, PROM, EPROM, EEPROM, Flash Memory, magnetic surface memory, optical disc, or CD-ROM.

[0175] In an exemplary embodiment, this application also provides a computer program product, including a computer program that can be executed by a processor 402 of an electronic device 400 to complete the steps of the aforementioned code processing method.

[0176] It should be noted that terms such as "first" and "second" are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.

[0177] Furthermore, the technical solutions described in the embodiments of this application can be combined arbitrarily without conflict.

[0178] The above description is merely a preferred embodiment of this application and is not intended to limit the scope of protection of this application.

Claims

1. A code processing method, characterized in that, The method includes: Obtain the original code, which is used to declare that one or more qubits are written into an array of pointers, the array of pointers containing one or more pointers, the one or more pointers being used to point to the one or more qubits; Using the original code, determine the type of the pointer array and the name of the one or more qubits, wherein the type of the pointer array indicates whether the pointer array is written to the qubits; Using the type of the pointer array and the name of the one or more qubits, an object array is constructed, which is used to point to the one or more qubits; The pointer array is replaced with the object array to obtain the intermediate code after compilation; wherein... The construction of an object array using the type of the pointer array and the name of the one or more qubits includes: The numerical value of the one or more qubits is determined using their names; The object array is constructed using the values ​​of the one or more qubits and the type of the pointer array; The step of using the original code to determine the type of the pointer array and the name of the one or more qubits includes: Using the original code, the types of the one or more pointers and the length of the pointer array are determined, wherein the types of the one or more pointers characterize whether the one or more pointers are used to point to qubits; The type of the pointer array is determined by using the type of the one or more pointers and the length of the pointer array.

2. The method according to claim 1, characterized in that, The method further includes: Obtain operators from the original code, the operators being used to determine the addresses of the one or more qubits; The operator is used to update the value of the one or more qubits to obtain the address of the one or more qubits; The object array is constructed using the addresses of the one or more qubits and the type of the pointer array.

3. The method according to claim 1, characterized in that, The method further includes: Upon receiving a first instruction, the index of the pointer array is obtained from the first instruction, the first instruction being used to request the invocation of the first pointer in the pointer array, the first pointer being used to point to the first qubit; Obtain the array of objects from the intermediate code; The first qubit is determined using the index of the pointer array and the object array.

4. The method according to claim 3, characterized in that, The step of obtaining the object array from the intermediate code includes: Obtain the name of the pointer array from the first instruction; Using the name of the pointer array and a first relationship, the object array is obtained from the intermediate code. The first relationship includes the mapping relationship between the name of the pointer array and the object array.

5. The method according to claim 4, characterized in that, The method further includes: 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.

6. A code processing device, characterized in that, The device includes: An acquisition unit is used to acquire the original code, which is 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; A determining unit is used to determine the type of the pointer array and the name of the one or more qubits using the original code, wherein the type of the pointer array indicates whether the pointer array is written to the qubits; A construction unit is configured to construct an object array, which points to one or more qubits, using the type of the pointer array and the name of the one or more qubits. A compilation unit is used to replace the pointer array with the object array to obtain intermediate code after compilation; wherein, The construction unit is used to determine the value of the one or more qubits using the name of the one or more qubits; and to construct the object array using the value of the one or more qubits and the type of the pointer array. The determining unit is used to determine the type of the one or more pointers and the length of the pointer array using the original code, wherein the type of the one or more pointers indicates whether the one or more pointers are used to point to qubits; and to determine the type of the pointer array using the type of the one or more pointers and the length of the pointer array.

7. An electronic device, characterized in that, include: The processor and the memory used to store computer programs that can run on the processor. When the processor is used to run the computer program, it performs the steps of the method according to any one of claims 1 to 5.

8. A storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 5.

9. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Method of compiling source code

    US20070240137A1