CN117850788A - Compiling method and device, storage medium and electronic device - Google Patents

Compiling method and device, storage medium and electronic device Download PDF

Info

Publication number
CN117850788A
CN117850788A CN202211223816.1A CN202211223816A CN117850788A CN 117850788 A CN117850788 A CN 117850788A CN 202211223816 A CN202211223816 A CN 202211223816A CN 117850788 A CN117850788 A CN 117850788A
Authority
CN
China
Prior art keywords
quantum
kernel function
code
compiling
module
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211223816.1A
Other languages
Chinese (zh)
Inventor
请求不公布姓名
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Benyuan Quantum Computing Technology Hefei Co ltd
Original Assignee
Benyuan Quantum Computing Technology Hefei Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Benyuan Quantum Computing Technology Hefei Co ltd filed Critical Benyuan Quantum Computing Technology Hefei Co ltd
Priority to CN202211223816.1A priority Critical patent/CN117850788A/en
Publication of CN117850788A publication Critical patent/CN117850788A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/447Target code generation
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4441Reducing the execution time required by the program code
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06NCOMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
    • G06N10/00Quantum computing, i.e. information processing based on quantum-mechanical phenomena
    • G06N10/20Models of quantum computing, e.g. quantum circuits or universal quantum computers

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Physics & Mathematics (AREA)
  • Software Systems (AREA)
  • Condensed Matter Physics & Semiconductors (AREA)
  • Evolutionary Computation (AREA)
  • Mathematical Analysis (AREA)
  • Mathematical Optimization (AREA)
  • Pure & Applied Mathematics (AREA)
  • Computing Systems (AREA)
  • Data Mining & Analysis (AREA)
  • Mathematical Physics (AREA)
  • Computational Mathematics (AREA)
  • Artificial Intelligence (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a compiling method, a compiling device, a storage medium and an electronic device, wherein the compiling method comprises the following steps: obtaining an intermediate code based on a source code of a quantum program, wherein the intermediate code comprises at least one quantum kernel function; when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not; if not, compiling the quantum kernel function in the intermediate code into a corresponding target code; and if the quantum kernel function exists, calling an object code corresponding to the quantum kernel function. By utilizing the embodiment of the invention, whether the quantum kernel function is compiled or not is determined by judging whether the quantum kernel function exists or not and the corresponding target code exists, so that the time consumption of compiling is reduced by reducing repeated compiling, and the compiling efficiency is improved.

Description

Compiling method and device, storage medium and electronic device
Technical Field
The invention belongs to the technical field of quantum compiling, and particularly relates to a compiling method, a compiling device, a storage medium and an electronic device.
Background
Compilation refers to the process of translating program code written in a high-level computer language into binary machine language code that a computer can run. Compilation may perform some or all of the following operations: lexical analysis, grammatical analysis, semantic analysis (grammar directed translation), converting an input program into an intermediate representation, object code generation, etc., which are implemented in stages during compilation to facilitate efficient design and proper conversion of source code to object code.
With the rapid but still limited progress of quantum hardware in recent years, a new field of quantum compilation has emerged, which can enable a section of program to run on quantum hardware. And the execution of each quantum program needs to be compiled, and even if repeated code segments exist in the quantum program, the full-quantity compiling is performed, so that the compiling is long in time consumption and low in compiling efficiency.
Disclosure of Invention
The invention aims to provide a compiling method, a compiling device, a storage medium and an electronic device, which determine whether to compile a quantum kernel function or not by judging whether the quantum kernel function already exists corresponding target codes, and reduce the compiling time by reducing repeated compiling, so that the compiling efficiency is improved.
One embodiment of the present application provides a compiling method, including:
obtaining an intermediate code based on a source code of a quantum program, wherein the intermediate code comprises at least one quantum kernel function;
when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not;
if not, compiling the quantum kernel function in the intermediate code into a corresponding target code;
and if the quantum kernel function exists, calling an object code corresponding to the quantum kernel function.
Optionally, the source code based on the quantum program obtains an intermediate code, including:
in response to the source code of the quantum program being absent of the un-parametrized quantum kernel function, intermediate code is generated based on the source code.
Optionally, the source code based on the quantum program obtains an intermediate code, and further includes:
directly compiling the quantum kernel function without the parameters into a corresponding target code in response to the quantum kernel function without the parameters in the source code;
intermediate code is generated based on the source code of the quantum kernel function divided by the non-parametric.
Optionally, when compiling the quantum kernel function, determining whether the quantum kernel function already has a corresponding object code includes:
identifying the marks of the quantum kernel functions in the compiling process, and judging whether the identified marks are the marks preset for the quantum kernel functions or not;
if so, judging whether the quantum kernel function already has a corresponding target code.
Optionally, the marking of the quantum kernel function includes marking of a syntax sugar operation performed on the quantum kernel function.
Optionally, compiling the quantum kernel function in the intermediate code into a corresponding target code includes:
and calling a just-in-time compiling module by using an interpreter to compile the quantum kernel function in the intermediate code into corresponding target code, wherein the interpreter is loaded with the intermediate code.
Optionally, the invoking the just-in-time compiling module with the interpreter compiles the quantum kernel function in the intermediate code into a corresponding target code, including:
and calling a just-in-time compiling module by using an interpreter, compiling the quantum kernel function in the intermediate code, and carrying out line optimization on the part which does not participate in the quantum kernel function to obtain a corresponding target code, wherein the optimization at least comprises the removal of an invalid gate.
Optionally, the calling the object code corresponding to the quantum kernel function includes:
generating a directed acyclic graph based on an object code corresponding to the quantum kernel function, wherein a starting node and an ending node of the directed acyclic graph represent quantum bits, an intermediate node represents a quantum logic gate, and edges represent the quantum bits after passing through the nodes;
modifying the directed acyclic graph based on a preset optimization rule;
generating a target quantum circuit by using the modified directed acyclic graph;
and calling the target quantum circuit.
Yet another embodiment of the present application provides a compiling apparatus, the apparatus including:
the device comprises an obtaining module, a judging module and a judging module, wherein the obtaining module is used for obtaining intermediate codes based on source codes of quantum programs, and the intermediate codes comprise at least one quantum kernel function;
the judging module is used for judging whether the quantum kernel function has corresponding object codes or not when compiling the quantum kernel function;
the compiling module is used for compiling the quantum kernel function in the intermediate code into a corresponding target code when the judging result of the judging module is that the quantum kernel function does not exist;
and the calling module is used for calling the object code corresponding to the quantum kernel function when the judging result of the judging module is that the quantum kernel function exists.
Optionally, the obtaining module is specifically configured to:
in response to the source code of the quantum program being absent of the un-parametrized quantum kernel function, intermediate code is generated based on the source code.
Optionally, the obtaining module is further specifically configured to:
directly compiling the quantum kernel function without the parameters into a corresponding target code in response to the quantum kernel function without the parameters in the source code;
intermediate code is generated based on the source code of the quantum kernel function divided by the non-parametric.
Optionally, the judging module is specifically configured to:
identifying the marks of the quantum kernel functions in the compiling process, and judging whether the identified marks are the marks preset for the quantum kernel functions or not;
if so, judging whether the quantum kernel function already has a corresponding target code.
Optionally, the marking of the quantum kernel function includes marking of a syntax sugar operation performed on the quantum kernel function.
Optionally, the compiling module is specifically configured to:
and calling a just-in-time compiling module by using an interpreter to compile the quantum kernel function in the intermediate code into corresponding target code, wherein the interpreter is loaded with the intermediate code.
Optionally, the compiling module is further specifically configured to:
and calling a just-in-time compiling module by using an interpreter, compiling the quantum kernel function in the intermediate code, and carrying out line optimization on the part which does not participate in the quantum kernel function to obtain a corresponding target code, wherein the optimization at least comprises the removal of an invalid gate.
Optionally, the calling module is specifically configured to:
generating a directed acyclic graph based on an object code corresponding to the quantum kernel function, wherein a starting node and an ending node of the directed acyclic graph represent quantum bits, an intermediate node represents a quantum logic gate, and edges represent the quantum bits after passing through the nodes;
modifying the directed acyclic graph based on a preset optimization rule;
generating a target quantum circuit by using the modified directed acyclic graph;
and calling the target quantum circuit.
An embodiment of the present application provides a storage medium having a computer program stored therein, wherein the computer program is configured to implement, when run, the method of any one of the above.
An embodiment of the application provides an electronic device comprising a memory having a computer program stored therein and a processor arranged to run the computer program to implement the method of any of the above.
Compared with the prior art, the method is based on the source code of the quantum program to obtain the intermediate code; when compiling to the quantum kernel function, then judging whether the quantum kernel function has corresponding object codes or not; if not, compiling the quantum kernel function in the intermediate code into a corresponding target code; and if so, calling an object code corresponding to the quantum kernel function. And determining whether to compile the quantum kernel function or not by judging whether the quantum kernel function exists or not and judging whether the corresponding object code exists or not, so that the time consumption of compiling is reduced, and the compiling efficiency is improved.
Drawings
Fig. 1 is a hardware block diagram of a computer terminal according to a compiling method according to an embodiment of the present invention;
FIG. 2 is a schematic flow chart of a compiling method according to an embodiment of the present invention;
FIG. 3 is a schematic diagram of a DAG according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of a compiling apparatus according to an embodiment of the present invention.
Detailed Description
The embodiments described below by referring to the drawings are illustrative only and are not to be construed as limiting the invention.
The embodiment of the invention firstly provides a compiling method which can be applied to electronic equipment such as computer terminals, in particular to common computers, quantum computers and the like.
The quantum computer is a kind of physical device which performs high-speed mathematical and logical operation, stores and processes quantum information according to the law of quantum mechanics. When a device processes and calculates quantum information and operates on a quantum algorithm, the device is a quantum computer. Quantum computers are a key technology under investigation because of their ability to handle mathematical problems more efficiently than ordinary computers, for example, to accelerate the time to crack RSA keys from hundreds of years to hours.
The following describes the operation of the computer terminal in detail by taking it as an example. Fig. 1 is a hardware block diagram of a computer terminal according to a compiling method according to an embodiment of the present invention. As shown in fig. 1, the computer terminal may include one or more (only one is shown in fig. 1) processors 102 (the processor 102 may include, but is not limited to, a microprocessor MCU or a processing device such as a programmable logic device FPGA) and a memory 104 for storing data, and optionally, a transmission device 106 for communication functions and an input-output device 108. It will be appreciated by those skilled in the art that the configuration shown in fig. 1 is merely illustrative and is not intended to limit the configuration of the computer terminal described above. For example, the computer terminal may also include more or fewer components than shown in FIG. 1, or have a different configuration than shown in FIG. 1.
The memory 104 may be used to store software programs and modules of application software, such as program instructions/modules corresponding to the compiling method in the embodiments of the application, and the processor 102 executes the software programs and modules stored in the memory 104, thereby performing various functional applications and data processing, i.e. implementing the method described above. Memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some examples, the memory 104 may further include memory remotely located relative to the processor 102, which may be connected to the computer terminal via a network. Examples of such networks include, but are not limited to, the internet, intranets, local area networks, mobile communication networks, and combinations thereof.
The transmission means 106 is arranged to receive or transmit data via a network. Specific examples of the network described above may include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, NIC) that can connect to other network devices through a base station to communicate with the internet. In one example, the transmission device 106 may be a Radio Frequency (RF) module for communicating with the internet wirelessly.
The quantum computing is a novel computing mode for regulating and controlling the quantum information unit to compute according to a quantum mechanical law, wherein the most basic principle based on the quantum computing is a quantum mechanical state superposition principle, and the quantum mechanical state superposition principle enables the state of the quantum information unit to be in a superposition state with multiple possibilities, so that quantum information processing has greater potential compared with classical information processing in efficiency. A quantum system comprises a number of particles which move according to the laws of quantum mechanics, known as a quantum state of the system in a state space.
It should be noted that a real quantum computer is a hybrid structure, which includes two major parts: part of the computers are classical computers and are responsible for performing classical computation and control; the other part is quantum equipment, which is responsible for running quantum programs so as to realize quantum computation. The quantum program is a series of instruction sequences written by a quantum language such as the qlunes language and capable of running on a quantum computer, so that the support of quantum logic gate operation is realized, and finally, quantum computing is realized. Specifically, the quantum program is a series of instruction sequences for operating the quantum logic gate according to a certain time sequence.
In practical applications, quantum computing simulations are often required to verify quantum algorithms, quantum applications, etc., due to the development of quantum device hardware. Quantum computing simulation is a process of realizing simulated operation of a quantum program corresponding to a specific problem by means of a virtual architecture (namely a quantum virtual machine) built by resources of a common computer. In general, it is necessary to construct a quantum program corresponding to a specific problem. The quantum program referred by the embodiment of the invention can be a program for representing quantum bits and evolution thereof written in classical language, wherein the quantum bits, quantum logic gates and the like related to quantum computation are all represented by corresponding classical codes.
Quantum circuits, which are one embodiment of quantum programs, also weigh sub-logic circuits, are the most commonly used general quantum computing models, representing circuits that operate on qubits under an abstract concept, the composition of which includes qubits, circuits (timelines), and various quantum logic gates, and finally the results often need to be read out by quantum measurement operations.
Unlike conventional circuits, which are connected by metal lines to carry voltage or current signals, in a quantum circuit, the circuit can be seen as being connected by time, i.e., the state of the qubit naturally evolves over time, as indicated by the hamiltonian operator, during which it is operated until a logic gate is encountered.
One quantum program is corresponding to one total quantum circuit, and the quantum program refers to the total quantum circuit, wherein the total number of quantum bits in the total quantum circuit is the same as the total number of quantum bits of the quantum program. It can be understood that: one quantum program may consist of a quantum circuit, a measurement operation for the quantum bits in the quantum circuit, a register to hold the measurement results, and a control flow node (jump instruction), and one quantum circuit may contain several tens to hundreds or even thousands of quantum logic gate operations. The execution process of the quantum program is a process of executing all quantum logic gates according to a certain time sequence. Note that the timing is the time sequence in which a single quantum logic gate is executed.
It should be noted that in classical computation, the most basic unit is a bit, and the most basic control mode is a logic gate, and the purpose of the control circuit can be achieved by a combination of logic gates. Similarly, the way in which the qubits are handled is a quantum logic gate. Quantum logic gates are used, which are the basis for forming quantum circuits, and include single-bit quantum logic gates, such as Hadamard gates (H gates, ada Ma Men), bery-X gates (X gates), bery-Y gates (Y gates), bery-Z gates (Z gates), RX gates, RY gates, RZ gates, and the like; two or more bit quantum logic gates, such as CNOT gates, CR gates, CZ gates, iSWAP gates, toffoli gates, and the like. Quantum logic gates are typically represented using unitary matrices, which are not only in matrix form, but also an operation and transformation. The effect of a general quantum logic gate on a quantum state is calculated by multiplying the unitary matrix by the matrix corresponding to the right vector of the quantum state.
Referring to fig. 2, fig. 2 is a flowchart of a compiling method according to an embodiment of the present invention, which may include the following steps:
s201: based on the source code of the quantum program, an intermediate code is obtained, wherein the intermediate code comprises at least one quantum kernel function.
It should be noted that, a quantum program may generally include a quantum part and a classical part, where the quantum part is called a quantum kernel program, where the quantum kernel program describes tasks that should be performed on a quantum processor, and the quantum kernel program can generate quantum assembly instructions in a quantum instruction set architecture by compiling, and these instructions will be executed by the quantum processor finally. The quantum kernel functions are functions in a quantum kernel program, more than one quantum kernel function may be contained in one quantum kernel program, and a module for realizing a specific function in a specific quantum kernel program may be used as one quantum kernel function, such as a quantum adder, a quantum discriminator, a quantum fourier transform, and the like.
After the source code is obtained, performing lexical analysis and grammar analysis on the source code to generate a grammar tree, wherein the grammar tree can be CST (Concret Syntax Tree, like the grammar tree) or abstract grammar tree; intermediate expressions and the like may also be generated. Taking a generated grammar tree as an example, static semantic detection is performed on a source program by traversing the grammar tree, specifically, semantic detection can be performed by traversing the grammar tree through a accessor, if an error is reported, compiling is stopped, for example, a controlled quantum logic gate (a logic gate containing a control bit, such as a CNOT gate) is detected, and if the control bit and the target bit of the quantum logic gate are the same quantum bit, an error is reported.
The static semantic detection operation may be performed using a symbol table that is generated by traversing the syntax tree, and in particular, may be generated by traversing the syntax tree by a monitor. The symbol table requires that relevant information such as the type and characteristics of some grammar symbols in the source program be continuously collected, recorded and used during the operation of the compiler. Such information is typically stored in a tabular form in the system. Such as a constant table, variable name table, array name table, process name table, label table, etc., collectively referred to as symbol table.
The intermediate code can be obtained by using the generated grammar tree, specifically, the intermediate sub-code is generated by traversing the grammar tree, and the intermediate code is obtained by combining the intermediate sub-codes, and of course, other ways for obtaining the intermediate code exist, which are not described in detail herein.
In some possible embodiments of the present invention, the quantum program-based source code, obtaining intermediate code, may include:
in response to the source code of the quantum program being absent of the un-parametrized quantum kernel function, intermediate code is generated based on the source code.
The quantum kernel function can be divided into a parameter-containing quantum kernel function and a parameter-free quantum kernel function, wherein the parameter-containing quantum kernel function indicates that parameters to be transferred are contained in the function. The determination of whether a quantum kernel function contains parameters can be determined by information recorded when traversing the syntax tree, can also be determined by a symbol table, or can be determined in other ways. When the quantum kernel function without the parameters does not exist in the source code, the intermediate code is directly generated based on the source code, and specifically, the intermediate code can be generated by using a grammar tree. And at the initial compiling time, directly compiling the quantum kernel function without parameters into an object code, so that the efficient mixed compiling of the quantum program and the classical program is realized.
In some possible embodiments of the present invention, the quantum program-based source code obtains an intermediate code, and further includes:
directly compiling the quantum kernel function without the parameters into a corresponding target code in response to the quantum kernel function without the parameters in the source code;
intermediate code is generated based on the source code of the quantum kernel function divided by the non-parametric.
When the quantum kernel function without the parameters exists in the source program, in order to reduce redundant compiling, the quantum kernel function without the parameters is directly compiled into a corresponding target code, namely the quantum kernel function is directly compiled into a corresponding quantum circuit. For source code other than the quantum kernel function without parameters, intermediate code is correspondingly generated. The intermediate code may be a pyc bytecode, which is a bytecode file (byte code) generated after compiling a Python file, and the pyc bytecode may ultimately generate an object code operation through a Python interpreter.
S202: when compiling to the quantum kernel function, judging whether the quantum kernel function has corresponding object codes.
After obtaining the intermediate code, the intermediate code needs to be compiled to obtain the target code. When the quantum kernel function is encountered during compiling, in order to reduce the compiling time length and improve the compiling efficiency, whether the corresponding target code exists or not can be judged. The object code may be stored after other quantum programs compile the quantum kernel function, or may be stored after the quantum program compiles the quantum kernel function before the quantum program.
In some possible embodiments of the present invention, when compiling the quantum kernel function, determining whether the quantum kernel function already has a corresponding object code may include:
identifying the marks of the quantum kernel functions in the compiling process, and judging whether the identified marks are the marks preset for the quantum kernel functions or not;
if so, judging whether the quantum kernel function already has a corresponding target code.
The quantum kernel function containing the parameters is specially marked in the process from the source program to the intermediate code, specifically, the quantum kernel function can be marked in the process of generating a grammar tree or in the process of traversing the grammar tree to generate the intermediate code, and the quantum kernel function is identified when the mark is identified in the compiling process. The same mark can be set for different quantum kernel functions, or different marks can be set.
In some possible embodiments of the invention, the marking of the quantum kernel function comprises marking of a syntax sugar operation performed on the quantum kernel function.
Grammatical Sugar (Syntactic Sugar), also known as Sugar coating grammar, refers to a grammar added in a computer language that has no effect on the function of the language, but is more convenient for programmers to use, and in short, grammatical Sugar allows for a more compact and readable program. In the embodiment of the invention, the quantum kernel function is marked, the mark for executing the grammatical sugar operation can be added to the quantum kernel function, the mark for executing the grammatical sugar operation can be a function, when the function is identified, the function is operated, and the function is used for determining whether the quantum kernel function has corresponding target codes or not. Specific implementations can be through the functionality of the grammatical sugar of the Python decorator, which can change the call body of the function.
If the equivalent kernel function does not have the corresponding target code, S203 is executed, and if the equivalent kernel function has the corresponding target code, S204 is executed.
S203: and compiling the quantum kernel function in the intermediate code into a corresponding target code.
There are various ways to compile the quantum kernel function into a corresponding object code, for example, a portion of the quantum kernel function in the syntax tree may be traversed, and the corresponding object code is generated according to a preset rule.
In some possible embodiments of the present invention, compiling the quantum kernel function in the intermediate code into the corresponding object code includes:
and calling a just-in-time compiling module by using an interpreter to compile the quantum kernel function in the intermediate code into corresponding target code, wherein the interpreter is loaded with the intermediate code.
The interpreter is a high-level language converter, and there are many interpreters, such as python interpreter, javaScript interpreter, java interpreter. When the intermediate code is compiled, an embedded interpreter may be loaded and the intermediate code is compiled. And when the quantum kernel function is compiled to the quantum kernel function without the corresponding object code, compiling the quantum kernel function by calling the just-in-time compiling module. Just-in-time compilation (JIT) is a process of converting some form of interpreter calculation into native program, and this process is done at runtime. The JIT module may be a JIT module of LLVM, which is originally derived from an abbreviation of the underlying virtual machine (Low Level Virtual Machine), which may provide a modern compilation target capable of supporting any programming language both static and dynamic, now referred to as a framework system of a framework compiler.
A LLVM JIT object is created by calling the JIT module of the LLVM through an interpreter, and compiling operation is performed. The specific compiling operation may be reading an intermediate code corresponding to the quantum kernel function, and compiling the intermediate code into a binary code by using a preset runtime library, where the binary code is an object code. The interpreter is used for calling JIT to carry out compiling work in the first loading process, repeated optimization and compiling in the process of line multiplexing can be reduced, and further, the compiling and running performance is improved.
In some possible embodiments of the present invention, the invoking a just-in-time compilation module with an interpreter compiles the quantum kernel function in the intermediate code into a corresponding object code, including:
and calling a just-in-time compiling module by using an interpreter, compiling the quantum kernel function in the intermediate code, and carrying out line optimization on the part which does not participate in the quantum kernel function to obtain a corresponding target code, wherein the optimization at least comprises the removal of an invalid gate.
In order to simplify the optimization of the line, the part of the quantum kernel function, which is not participated in, can be optimized during compiling, the optimization can be completed by means of pattern matching and rewriting of the DAG (Directed Acyclic Graph ), specifically, an optimization mode is preset, when the part of the quantum kernel function, which is not participated in, is traversed, when any one of the optimization modes is matched, the DAG is modified, namely, rewritten, and the target code is obtained based on the modified DAG. The optimization may be the removal of invalid gates, for example, because the H gates are unitary, acting two H gates consecutively on the same qubit is equivalent to no operation, where the two H gates are invalid gates, and the nodes corresponding to the two H gates and the edges connecting the H gates need to be deleted in the DAG. The optimization here is a simple optimization, since some nodes are removed, which can reduce the complexity of the overall DAG graph and can also reduce the repeated optimizations that are performed when the object code is repeatedly invoked.
S204: and calling an object code corresponding to the quantum kernel function.
And when the object code corresponding to the quantum kernel function exists, directly calling. If the quantum kernel function does not have the corresponding target code, compiling the quantum kernel function, writing the compiled target code into a memory, storing a corresponding function pointer, and when the quantum kernel function needs to be called, jumping to the corresponding memory area to execute the compiled target code by calling the function knitting.
In some embodiments, the compiling of the intermediate code may be just-in-time compiling, specifically, just-in-time compiling of the dependency interpreter, and the just-in-time compiling compiles the intermediate code into a binary code object and stores the binary code object into the memory when the intermediate code is loaded for the first time, and then the intermediate code is executed by jumping to a corresponding memory block to execute the instruction. For the situation that one quantum kernel function is called for many times (the purpose of writing into one function separately is to multiplex), the process of repeatedly interpreting instructions by an interpreter is reduced, and the overall operation performance is optimized.
In some possible embodiments of the present invention, the calling the object code corresponding to the quantum kernel function includes:
generating a directed acyclic graph based on an object code corresponding to the quantum kernel function, wherein a starting node and an ending node of the directed acyclic graph represent quantum bits, an intermediate node represents a quantum logic gate, and edges represent the quantum bits after passing through the nodes;
modifying the directed acyclic graph based on a preset optimization rule;
generating a target quantum circuit by using the modified directed acyclic graph;
and calling the target quantum circuit.
The preset optimization rules may include single gate merging, invalid gate removal, etc., based on which the DAG is traversed, it is determined whether there are places that need to be optimized, and if so, modifications to the DAG are made, which involve deletion of nodes and edges, modifications, etc. After the DAG is modified, the DAG is converted into a quantum circuit, and a specific conversion mode can be that the number of quantum bits is determined based on the DAG, quantum logic gates acting on the quantum bits are used for generating a target quantum circuit. The optimization of the object code may be MLIR (Multi-Level Intermediate Representation ), and the specific implementation is quantum Dialect (in terms of definition) of MLIR in advance, and line optimization is completed through API (Application Programming Interface ) provided by MLIR.
Illustratively, the directed acyclic graph generated based on the object code may be as shown in FIG. 3, where the start node and the end node represent qubits, x [0], c [0], y [0], c [ 1 ] 0 are all qubits, the intermediate node represents a quantum logic gate, the H gate, CX gate are quantum logic gates, and the function of the CX gate corresponds to an exclusive OR operation in the classical. The edges represent the quantum bits passing through the nodes, when the edges point to the middle node, the relationship exists between the corresponding quantum logic gate and the quantum bits, if only one edge points to the middle node, the quantum logic gate acts on the corresponding quantum bits, if more than one edge points to the middle node, the quantum logic gate is a controlled gate, and the target bit and the control bit are the quantum bits corresponding to the edges pointing to the quantum logic gate. When an edge points to an end node, the quantum logic gate associated with that qubit is already in the DAG. By matching the traversal of fig. 3 with a preset optimization rule, eventually without optimization, a target quantum wire may be generated based on the DAG.
Therefore, the invention obtains the intermediate code based on the source code of the quantum program; when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not; if not, compiling the quantum kernel function in the intermediate code into a corresponding target code; and if so, calling an object code corresponding to the quantum kernel function. And determining whether to compile the quantum kernel function or not by judging whether the quantum kernel function exists or not and judging whether the corresponding target code exists or not, and reducing the time consumption of compiling by reducing repeated compiling, so that the compiling efficiency is improved.
Referring to fig. 4, fig. 4 is a schematic structural diagram of a compiling apparatus according to an embodiment of the present invention, corresponding to the flow shown in fig. 2, the apparatus may include:
an obtaining module 401, configured to obtain an intermediate code based on a source code of a quantum program, where the intermediate code includes at least one quantum kernel function;
a judging module 402, configured to judge whether a corresponding object code exists in the quantum kernel function when compiling the quantum kernel function;
a compiling module 403, configured to compile the quantum kernel function in the intermediate code into a corresponding object code when the determination result of the determining module 402 is that the quantum kernel function does not exist;
and a calling module 404, configured to call an object code corresponding to the quantum kernel function when the determination result of the determining module 402 is that the quantum kernel function exists.
In some possible embodiments of the present invention, the obtaining module 401 may be specifically configured to:
in response to the source code of the quantum program being absent of the un-parametrized quantum kernel function, intermediate code is generated based on the source code.
In some possible embodiments of the present invention, the obtaining module 401 may be further specifically configured to:
directly compiling the quantum kernel function without the parameters into a corresponding target code in response to the quantum kernel function without the parameters in the source code;
intermediate code is generated based on the source code of the quantum kernel function divided by the non-parametric.
In some possible embodiments of the present invention, the determining module 402 may be specifically configured to:
identifying the marks of the quantum kernel functions in the compiling process, and judging whether the identified marks are the marks preset for the quantum kernel functions or not;
if so, judging whether the quantum kernel function already has a corresponding target code.
In some possible embodiments of the invention, the marking of the quantum kernel function comprises marking of a syntax sugar operation performed on the quantum kernel function.
In some possible embodiments of the present invention, the compiling module 403 may be specifically configured to:
and calling a just-in-time compiling module by using an interpreter to compile the quantum kernel function in the intermediate code into corresponding target code, wherein the interpreter is loaded with the intermediate code.
In some possible embodiments of the present invention, the compiling module 403 may be further specifically configured to:
and calling a just-in-time compiling module by using an interpreter, compiling the quantum kernel function in the intermediate code, and carrying out line optimization on the part which does not participate in the quantum kernel function to obtain a corresponding target code, wherein the optimization at least comprises the removal of an invalid gate.
In some possible embodiments of the present invention, the calling module 404 may be specifically configured to:
generating a directed acyclic graph based on an object code corresponding to the quantum kernel function, wherein a starting node and an ending node of the directed acyclic graph represent quantum bits, an intermediate node represents a quantum logic gate, and edges represent the quantum bits after passing through the nodes;
modifying the directed acyclic graph based on a preset optimization rule;
generating a target quantum circuit by using the modified directed acyclic graph;
and calling the target quantum circuit.
Therefore, the invention obtains the intermediate code based on the source code of the quantum program; when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not; if not, compiling the quantum kernel function in the intermediate code into a corresponding target code; and if so, calling an object code corresponding to the quantum kernel function. And determining whether to compile the quantum kernel function or not by judging whether the quantum kernel function exists or not and judging whether the corresponding target code exists or not, and reducing the time consumption of compiling by reducing repeated compiling, so that the compiling efficiency is improved.
The embodiment of the invention also provides a storage medium, in which a computer program is stored, wherein the computer program is configured to implement the steps in any of the method embodiments described above when run.
Specifically, in the present embodiment, the above-described storage medium may be configured to store a computer program for realizing the steps of:
s201: obtaining an intermediate code based on a source code of a quantum program, wherein the intermediate code comprises at least one quantum kernel function;
s202: when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not; if not, S203 is performed, and if so, S204 is performed;
s203: compiling the quantum kernel function in the intermediate code into a corresponding target code;
s204: and calling an object code corresponding to the quantum kernel function.
An embodiment of the invention also provides an electronic device comprising a memory and a processor, characterized in that the memory has stored therein a computer program, the processor being arranged to run the computer program to carry out the steps of any of the method embodiments described above.
Specifically, the electronic apparatus may further include a transmission device and an input/output device, where the transmission device is connected to the processor, and the input/output device is connected to the processor.
Specifically, in this embodiment, the above-mentioned processor may be configured to implement the following steps by a computer program:
s201: obtaining an intermediate code based on a source code of a quantum program, wherein the intermediate code comprises at least one quantum kernel function;
s202: when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not; if not, S203 is performed, and if so, S204 is performed;
s203: compiling the quantum kernel function in the intermediate code into a corresponding target code;
s204: and calling an object code corresponding to the quantum kernel function.
While the foregoing is directed to embodiments of the present invention, other and further embodiments of the invention may be devised without departing from the basic scope thereof, and the scope thereof is determined by the claims that follow.

Claims (11)

1. A method of compiling, the method comprising:
obtaining an intermediate code based on a source code of a quantum program, wherein the intermediate code comprises at least one quantum kernel function;
when compiling to the quantum kernel function, judging whether the quantum kernel function has a corresponding target code or not;
if not, compiling the quantum kernel function in the intermediate code into a corresponding target code;
and if the quantum kernel function exists, calling an object code corresponding to the quantum kernel function.
2. The method of claim 1, wherein the quantum program-based source code obtains intermediate code comprising:
in response to the source code of the quantum program being absent of the un-parametrized quantum kernel function, intermediate code is generated based on the source code.
3. The method of claim 2, wherein the quantum program-based source code obtains intermediate code, further comprising:
directly compiling the quantum kernel function without the parameters into a corresponding target code in response to the quantum kernel function without the parameters in the source code;
intermediate code is generated based on the source code of the quantum kernel function divided by the non-parametric.
4. The method of claim 1, wherein determining whether the quantum kernel function already has corresponding object code when compiling to the quantum kernel function comprises:
identifying the marks of the quantum kernel functions in the compiling process, and judging whether the identified marks are the marks preset for the quantum kernel functions or not;
if so, judging whether the quantum kernel function already has a corresponding target code.
5. The method of claim 4, wherein the tagging of the quantum kernel function comprises tagging of a quantum kernel function to perform a grammatical sugar operation.
6. The method of any of claims 1-5, wherein compiling the quantum kernel function in the intermediate code into the corresponding object code comprises:
and calling a just-in-time compiling module by using an interpreter to compile the quantum kernel function in the intermediate code into corresponding target code, wherein the interpreter is loaded with the intermediate code.
7. The method of claim 6, wherein the invoking the just-in-time compilation module with the interpreter compiles the quantum kernel function in the intermediate code into corresponding object code, comprising:
and calling a just-in-time compiling module by using an interpreter, compiling the quantum kernel function in the intermediate code, and carrying out line optimization on the part which does not participate in the quantum kernel function to obtain a corresponding target code, wherein the optimization at least comprises the removal of an invalid gate.
8. The method of claim 1, wherein the invoking the object code corresponding to the quantum kernel function comprises:
generating a directed acyclic graph based on an object code corresponding to the quantum kernel function, wherein a starting node and an ending node of the directed acyclic graph represent quantum bits, an intermediate node represents a quantum logic gate, and edges represent the quantum bits after passing through the nodes;
modifying the directed acyclic graph based on a preset optimization rule;
generating a target quantum circuit by using the modified directed acyclic graph;
and calling the target quantum circuit.
9. A compiling apparatus, the apparatus comprising:
the device comprises an obtaining module, a judging module and a judging module, wherein the obtaining module is used for obtaining intermediate codes based on source codes of quantum programs, and the intermediate codes comprise at least one quantum kernel function;
the judging module is used for judging whether the quantum kernel function has corresponding object codes or not when compiling the quantum kernel function;
the compiling module is used for compiling the quantum kernel function in the intermediate code into a corresponding target code when the judging result of the judging module is that the quantum kernel function does not exist;
and the calling module is used for calling the object code corresponding to the quantum kernel function when the judging result of the judging module is that the quantum kernel function exists.
10. A storage medium having a computer program stored therein, wherein the computer program is arranged to implement the method of any of claims 1 to 8 when run.
11. An electronic device comprising a memory and a processor, characterized in that the memory has stored therein a computer program, the processor being arranged to run the computer program to implement the method of any of the claims 1 to 8.
CN202211223816.1A 2022-09-30 2022-09-30 Compiling method and device, storage medium and electronic device Pending CN117850788A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211223816.1A CN117850788A (en) 2022-09-30 2022-09-30 Compiling method and device, storage medium and electronic device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211223816.1A CN117850788A (en) 2022-09-30 2022-09-30 Compiling method and device, storage medium and electronic device

Publications (1)

Publication Number Publication Date
CN117850788A true CN117850788A (en) 2024-04-09

Family

ID=90536665

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211223816.1A Pending CN117850788A (en) 2022-09-30 2022-09-30 Compiling method and device, storage medium and electronic device

Country Status (1)

Country Link
CN (1) CN117850788A (en)

Similar Documents

Publication Publication Date Title
US6078744A (en) Method and apparatus for improving compiler performance during subsequent compilations of a source program
CN110149800B (en) Apparatus for processing abstract syntax tree associated with source code of source program
US7725883B1 (en) Program interpreter
US10908885B2 (en) Quantum compiler
CN100462920C (en) Unwind information for optimized programs
US7877741B2 (en) Method and corresponding apparatus for compiling high-level languages into specific processor architectures
KR101418969B1 (en) Processor and methods of compiling
CN110990019B (en) Java class analysis method and device, storage medium and electronic equipment
CN111832736A (en) Method, apparatus and computer program product for processing machine learning models
US8997066B2 (en) Emulating pointers
CN109933327B (en) OpenCL compiler design method and system based on code fusion compiling framework
CN112270176B (en) Method, apparatus, and computer storage medium for mode conversion in a deep learning framework
CN116594622A (en) Python program compiling method and system based on type deduction and data flow analysis
CN117472388B (en) Method, system and related equipment for optimizing program performance
US20170168787A1 (en) Optimized compiling of a template function
CN116775127B (en) Static symbol execution pile inserting method based on RetroWrite frames
Pinto et al. Pegasus: performance engineering for software applications targeting hpc systems
CN117850788A (en) Compiling method and device, storage medium and electronic device
CN116414396A (en) LLVM (logical level virtual machine) target definition file generation method and device and electronic equipment
US20090112568A1 (en) Method for Generating a Simulation Program Which Can Be Executed On a Host Computer
CN118246377B (en) Simulator architecture, simulation method, simulation equipment and medium of tensor processor
CN111913712A (en) Method and apparatus for deploying neural network model at Web end
Strout et al. Language-Agnostic Optimization and Parallelization for Interpreted Languages
CN115409194B (en) Quantum program generation system and quantum computer operation system
CN112667245B (en) Code processing method and device, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination