Quantum algorithm code generation method and device, equipment and storage medium
By generating a model to convert user prompts into textual descriptions of quantum algorithms and generating executable code, the problem of quantum algorithm development and optimization relying on human experience is solved, thus improving efficiency and code quality.
Patent Information
- Application Number
- CN202511154993.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-18
- Publication Date
- 2025-11-25
AI Technical Summary
The design and optimization of quantum algorithms rely heavily on the experience and intuition of human experts, resulting in low efficiency in development and application, and making it difficult to promote on a large scale.
By generating models, user-input prompts are converted into textual descriptions of quantum algorithms, and then executable quantum algorithm code is generated. This reduces the reliance on manually designing circuit logic and writing low-level code, improving development efficiency and code quality.
Eliminating the need for manual circuit logic design or low-level code writing improves the development efficiency of quantum algorithms, reduces the risk of debugging difficulties, and enhances the quality and accuracy of executable code.
Smart Images

Figure CN121010008A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, and particularly relates to a quantum algorithm code generation method and device, equipment and a storage medium. BACKGROUND
[0002] In the field of quantum computing, quantum algorithms run on quantum computers, rely on the physical implementation of qubits (such as superconducting circuits or photonic systems), and achieve specific computing goals through quantum gate operations and measurements. Typical applications include cryptography cracking (such as Shor's algorithm), large-scale data search (such as Grover's algorithm), quantum chemistry simulation, and combinatorial optimization problem solving.
[0003] However, the design and optimization of quantum algorithms highly depend on the experience and intuition of human experts, which is limited by individual cognitive ability and has certain limitations, and is not conducive to the large-scale development and application efficiency of quantum algorithms. SUMMARY
[0004] In view of the above problems, the present application provides a quantum algorithm code generation method, device, equipment and storage medium to realize the purpose of improving the development efficiency and application efficiency of quantum algorithms. The specific scheme is as follows:
[0005] The first aspect of the present application provides a quantum algorithm code generation method, comprising:
[0006] obtaining first prompt information;
[0007] inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information by the generation model; the quantum algorithm text description information is used to describe a candidate quantum circuit for realizing a target task;
[0008] inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code by the generation model.
[0009] In a possible implementation, the quantum algorithm code generation method further comprises:
[0010] obtaining text encoding of a quantum algorithm; the text encoding is used to define a quantum algorithm architecture;
[0011] the inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information by the generation model, comprises:
[0012] inputting the text encoding of the quantum algorithm and the first prompt information into a generation model, and generating quantum algorithm text description information conforming to the quantum algorithm architecture by the generation model.
[0013] In a possible implementation, the obtaining the text description of the quantum algorithm comprises at least one of the following:
[0014] obtaining a coding rule of the quantum algorithm; the coding rule is used to define a standardized description format of the quantum algorithm;
[0015] obtaining a performance index of the quantum algorithm; the performance index is used to define a quality of the quantum algorithm;
[0016] obtaining a design space of the quantum algorithm; the design space is used to define a quantum circuit architecture.
[0017] In a possible implementation, the quantum algorithm code generation method further comprises:
[0018] obtaining a performance parameter of the executable quantum algorithm code by executing the executable quantum algorithm code;
[0019] if the performance parameter meets a set condition, ending the generation step;
[0020] if the performance parameter does not meet the set condition, inputting, into the generation model, third prompt information generated based on the performance parameter, the text description information of the quantum algorithm, and the executable quantum algorithm code, generating, by the generation model, new text description information of the quantum algorithm, and continuing to perform the step of inputting, into the generation model, second prompt information generated based on the text description information of the quantum algorithm.
[0021] In a possible implementation, the inputting, into the generation model, third prompt information generated based on the performance parameter, the text description information of the quantum algorithm, and the executable quantum algorithm code, and generating, by the generation model, new text description information of the quantum algorithm comprises:
[0022] inputting, into the generation model, third prompt information generated based on the performance parameter, the text description information of the quantum algorithm, the executable quantum algorithm code, and the first prompt information, and generating, by the generation model, new text description information of the quantum algorithm.
[0023] In a possible implementation, the obtaining the first prompt information comprises:
[0024] processing, based on a set prompt word template, description information of the target task input by a user to generate the first prompt information.
[0025] In a possible implementation, the set prompt word template comprises at least one of the following:
[0026] a role and task definition module; a hardware parameter constraint module; an available operation list module; a format specification and example module; a task requirement refinement module; an output integrity requirement module; a design specification requirement module.
[0027] In another aspect of the present application, a quantum algorithm code generation apparatus is provided, comprising:
[0028] a first obtaining module configured to obtain first prompt information;
[0029] a first generating module configured to input at least the first prompt information into a generation model, and generate quantum algorithm text description information from the generation model; the quantum algorithm text description information is used to describe a candidate quantum circuit for implementing a target task;
[0030] a second generating module configured to input second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code from the generation model.
[0031] In a third aspect of the present application, an electronic device is provided, comprising a memory, at least one processor, and a computer program stored in the memory, wherein the processor executes the computer program to implement the following method steps:
[0032] obtaining first prompt information;
[0033] inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information from the generation model; the quantum algorithm text description information is used to describe a candidate quantum circuit for implementing a target task;
[0034] inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code from the generation model.
[0035] In a fourth aspect of the present application, a computer storage medium is provided, wherein the storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the following method steps:
[0036] obtaining first prompt information;
[0037] inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information from the generation model; the quantum algorithm text description information is used to describe a candidate quantum circuit for implementing a target task;
[0038] inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code from the generation model.
[0039] In the present application, by obtaining first prompt information, at least inputting the first prompt information into a generation model, generating quantum algorithm text description information by the generation model, obtaining second prompt information based on the quantum algorithm text description information, inputting the second prompt information into the generation model, and generating executable quantum algorithm code by the generation model, the development efficiency and application efficiency of the quantum algorithm are improved without relying on user manual design of circuit logic or writing of underlying code.
[0040] Furthermore, by at least inputting the first prompt information into a generation model, generating quantum algorithm text description information by the generation model, and further inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, the generation model can help to sort the logic of the candidate quantum circuit according to the quantum algorithm text description, and further avoid errors caused by mixing of the logic of the candidate quantum circuit and the code syntax when generating executable quantum algorithm code, thereby improving the quality of the executable quantum algorithm code.
[0041] In addition, by first generating quantum algorithm text description information, the user can independently verify the correctness of the candidate quantum circuit before code generation, thereby reducing the risk of debugging difficulty caused by directly generating quantum algorithm code. BRIEF DESCRIPTION OF DRAWINGS
[0042] The above and other features, advantages, and aspects of the present disclosure will become more apparent by describing in detail the following specific embodiments thereof with reference to the attached drawings. Throughout the drawings, the same or similar reference numerals refer to the same or similar elements. It should be understood that the drawings are schematic, and the sizes of the components and elements are not necessarily drawn to scale.
[0043] Figure 1 A flowchart of a quantum algorithm code generation method provided for Embodiment 1 of the present application;
[0044] Figure 2 A flowchart of a quantum algorithm code generation method provided for Embodiment 2 of the present application;
[0045] Figure 3 A flowchart of a quantum algorithm code generation method provided for Embodiment 4 of the present application;
[0046] Figure 4 A flowchart of a quantum algorithm code generation method provided for Embodiment 5 of the present application;
[0047] Figure 5 A flowchart of a quantum algorithm code generation method provided for Embodiment 6 of the present application;
[0048] Figure 6A structural schematic diagram of a quantum algorithm code generation device provided in the present application is shown. DETAILED DESCRIPTION
[0049] The embodiments of the present application are described below in conjunction with the accompanying drawings. The terms used in the embodiment part of the present application are only used to explain the specific embodiments of the present application, and are not intended to limit the present application.
[0050] The embodiments of the present application are described below in conjunction with the accompanying drawings. It is known to those of ordinary skill in the art that, as technology develops and new scenarios appear, the technical solutions provided by the embodiments of the present application are also applicable to similar technical problems.
[0051] The terms "first", "second", and the like in the present application are used to distinguish similar objects, and do not necessarily describe a specific order or sequence. It should be understood that the terms used in this way can be interchanged under appropriate circumstances, which is only a distinguishing way used in describing the embodiments of the present application for the same attribute objects in the description. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, so that the process, method, system, product or equipment containing a series of units does not have to be limited to those units, but can include other units not clearly listed or inherent to these processes, methods, products or equipment.
[0052] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and easy to understand, the present application will be further described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0053] Reference Figure 1 A flowchart of a quantum algorithm code generation method provided in Embodiment 1 of the present application is shown in FIG. 1. As shown in FIG. 1, the method can include, but is not limited to, the following steps: Figure 1
[0054] Step S101, obtaining first prompt information.
[0055] In the present embodiment, the natural language input by the user can be used as the first prompt information. For example, the natural language input by the user is "generate an algorithm to implement quantum Fourier transform (QFT), require to use 3 qubits, and optimize the number of gate operations." The natural language can be directly used as the first prompt information.
[0056] In the present embodiment, the structured instruction can also be generated according to the natural language input by the user as the first prompt information.
[0057] Step S102, inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information by the generation model.
[0058] The quantum algorithm text description information can be used to describe a candidate quantum circuit for implementing a target task. The candidate quantum circuit can be composed of a series of quantum gates that can operate on qubits to implement the target task.
[0059] The quantum algorithm text description information can include, but is not limited to, the type of quantum gate, the configuration of qubits, and the possible quantum circuit structure.
[0060] The generation model can be set as needed and is not limited in the present application. For example, the generation model can include, but is not limited to, generative language models (GLMs): large language models (LLMs) such as the GPT series, LLaMA, etc.
[0061] In step S103, second prompt information obtained based on the quantum algorithm text description information is input into the generation model, and executable quantum algorithm code is generated by the generation model.
[0062] In the present embodiment, the quantum algorithm text description information can be used as the second prompt information.
[0063] Of course, structured instructions can also be generated based on the quantum algorithm text description information as the second prompt information.
[0064] The target framework (such as Qiskit, Cirq, PennyLane) can be specified in the second prompt information, and the generation model can generate executable quantum algorithm code that conforms to the target framework, ensuring that the executable quantum algorithm code is compatible with the corresponding quantum processor or quantum simulator.
[0065] The executable quantum algorithm code can be executed by a quantum processor or a quantum simulator to implement a target task.
[0066] In the present embodiment, by obtaining first prompt information, at least the first prompt information is input into the generation model, and the generation model generates quantum algorithm text description information. The second prompt information obtained based on the quantum algorithm text description information is input into the generation model, and the generation model generates executable quantum algorithm code. This does not require manual design of circuit logic or writing of underlying code by the user, thereby improving the development efficiency and application efficiency of quantum algorithms.
[0067] And, by inputting at least the first prompt information into the generation model, quantum algorithm text description information is generated by the generation model, and then second prompt information obtained based on the quantum algorithm text description information is input into the generation model, which helps the generation model to sort out the logic of the candidate quantum circuit according to the quantum algorithm text description, and further avoids errors caused by mixing the logic of the candidate quantum circuit with the code syntax when generating executable quantum algorithm code, thereby improving the quality of the executable quantum algorithm code.
[0068] In addition, by first generating quantum algorithm text description information, the user can independently verify the correctness of the candidate quantum circuit before code generation, thereby reducing the risk of debugging difficulty caused by directly generating quantum algorithm code.
[0069] As another optional embodiment of the present application, referring to Figure 2 A flowchart of a quantum algorithm code generation method according to an embodiment of the present application is shown in FIG. 2. Figure 2 The method can include but is not limited to the following steps:
[0070] Step S201: obtaining first prompt information.
[0071] The detailed process of step S201 can refer to the related introduction of step S101 in embodiment 1, which will not be repeated here.
[0072] Step S202: obtaining text encoding of a quantum algorithm; the text encoding is used to define a quantum algorithm architecture.
[0073] The quantum algorithm architecture can be used to guide the generation of a candidate quantum circuit.
[0074] In the present embodiment, the text encoding of the quantum algorithm can be universal, supporting multiplexing and supporting rapid adaptation to different tasks to reduce repeated design.
[0075] Of course, the text encoding of the quantum algorithm can also be a specialized text encoding obtained by fine-tuning the universal text encoding according to the target task.
[0076] Step S203: inputting the text encoding of the quantum algorithm and the first prompt information into a generation model, and generating quantum algorithm text description information conforming to the quantum algorithm architecture by the generation model.
[0077] The text encoding of the quantum algorithm can limit the generation process of the generation model, ensuring that the quantum algorithm text description information generated thereby can correspond to a candidate quantum circuit conforming to the quantum algorithm architecture, rather than freely generating quantum algorithm text description information.
[0078] Step S203 is an implementation of step S102 in embodiment 1.
[0079] Step S204, input the second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code by the generation model.
[0080] The detailed process of step S204 can refer to the related introduction of step S103 in embodiment 1, which will not be repeated here.
[0081] In this embodiment, by obtaining the text encoding of the quantum algorithm, the text encoding is used to define the quantum algorithm architecture, inputting the text encoding of the quantum algorithm and the first prompt information into the generation model, and generating quantum algorithm text description information conforming to the quantum algorithm architecture by the generation model, the quantum operation sequence conflicting with the quantum algorithm architecture can be reduced, and on this basis, inputting the second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code by the generation model, the accuracy of the executable quantum algorithm code is improved.
[0082] As another optional embodiment of the present application, a quantum algorithm code generation method is provided for embodiment 3 of the present application. This embodiment mainly relates to an implementation of step S202. Step S202 can include but is not limited to at least one of the following:
[0083] Step S2021, obtaining the encoding rule of the quantum algorithm.
[0084] The encoding rule can be used to define the standardized description format of the quantum algorithm. For example, the encoding rule can include but is not limited to:
[0085] Description format: [operation number, (action qubit range)].
[0086] Wherein the operation number is: an integer representing different quantum operations or gates. For example, 0 represents initial; 1 represents Hadamard gate; 2 represents CNOT gate; 3 represents measurement; 4 represents X gate; 5 represents Z gate.
[0087] Action qubit range: a tuple representing the quantum bits on which the operation acts.
[0088] Complete algorithm description: use a comma-separated list to represent the operation sequence of the entire quantum algorithm, following the [operation number, (action qubit range)] format.
[0089] For example, the description of the quantum algorithm under the encoding rule can be as follows:
[0090] [0, (0,1,2,3)], [1, (0,1)], [2, (0,2)], [4, (1)], [5, (3)], [2, (1,3)], [3, (0,1,2,3)].
[0091] [0, (0,1,2,3)] can represent: initialize qubits 0,1,2,3;
[0092] [1, (0,1)] can represent: apply Hadamard gate on qubits 0 and 1;
[0093] [2, (0,2)] can represent: apply CNOT gate with control bit 0 and target bit 2;
[0094] [4, (1)] can represent: apply X gate on qubit 1;
[0095] [5, (3)] can represent: apply Z gate on qubit 3;
[0096] [2, (1,3)] can represent: apply CNOT gate with control bit 1 and target bit 3;
[0097] [3, (0,1,2,3)] can represent: measure all qubits.
[0098] Step S2022, obtaining a performance indicator of the quantum algorithm.
[0099] The performance indicator can be used to define the quality of the quantum algorithm. For example, the performance indicator can include but is not limited to at least one of the following: depth of the circuit; number of gates; accuracy of the algorithm; cost function C. The cost function C can be used to measure the efficiency of the quantum circuit, for example: where D is the depth of the circuit, G is the number of gates, and weighting coefficients of.
[0100] Step S2023, obtaining a design space of the quantum algorithm.
[0101] The design space can be used to define the quantum circuit architecture. For example, the design space can include but is not limited to: allowed quantum gate types, circuit depth and number of qubits, etc.
[0102] Step S203 can include but is not limited to the following steps:
[0103] In step S2031, at least one of the encoding rule, the performance index, and the design space of the quantum algorithm and the first prompt information are input into a generation model, and the generation model generates quantum algorithm text description information conforming to at least one of a standardized description format, a quality, and a quantum circuit architecture of the quantum algorithm.
[0104] In the embodiment, by obtaining the encoding rule, the performance index, and the design space of the quantum algorithm, the encoding rule, the performance index, the design space of the quantum algorithm, and the first prompt information are input into a generation model, and the generation model generates quantum algorithm text description information conforming to a standardized description format, a quality, and a quantum circuit architecture of the quantum algorithm, which can reduce quantum operation sequences conflicting with the encoding rule, the performance index, and the design space. On this basis, the second prompt information obtained based on the quantum algorithm text description information is input into the generation model, and the generation model generates executable quantum algorithm code, which improves the accuracy of the executable quantum algorithm code.
[0105] As another optional embodiment of the present application, referring to Figure 3 A flowchart of a quantum algorithm code generation method provided in Embodiment 4 of the present application is shown in FIG. 4, which can include but is not limited to the following steps. Figure 3
[0106] In step S301, first prompt information is obtained.
[0107] In step S302, at least the first prompt information is input into a generation model, and the generation model generates quantum algorithm text description information; the quantum algorithm text description information is used to describe a candidate quantum circuit for implementing a target task.
[0108] In step S303, second prompt information obtained based on the quantum algorithm text description information is input into the generation model, and the generation model generates executable quantum algorithm code.
[0109] The detailed processes of steps S301-S303 can be referred to the related descriptions of steps S101-S103, which will not be repeated here.
[0110] In step S304, by executing the executable quantum algorithm code, a performance parameter of the executable quantum algorithm code is obtained.
[0111] In the embodiment, the executable quantum algorithm code can be executed based on a quantum processor or a quantum simulator, and performance of the executable quantum algorithm code is evaluated to obtain performance parameters. The performance parameters can include, but are not limited to, at least one of the following: depth of a circuit; number of gates; accuracy of an algorithm; cost function C. For example, the executable quantum algorithm code is executed on a quantum simulator, and the accuracy of the expected return is 95% and the circuit depth is 10 (i.e., one implementation of the performance parameters).
[0112] In step S305, if the performance parameters meet the set conditions, the generation is ended.
[0113] The set conditions can be set as needed and are not limited in the present application. For example, the performance parameters can include accuracy of an algorithm and depth of a circuit, and the set conditions can include an accuracy threshold and a circuit depth threshold.
[0114] In step S306, if the performance parameters do not meet the set conditions, third prompt information generated based on at least the performance parameters, the quantum algorithm text description information, and the executable quantum algorithm code is input into the generation model, the generation model generates new quantum algorithm text description information, and the step of inputting second prompt information generated based on the quantum algorithm text description information into the generation model is continued.
[0115] The third prompt information generated based on at least the performance parameters, the quantum algorithm text description information, and the executable quantum algorithm code is input into the generation model, and the generation model optimizes a candidate quantum circuit for achieving a target task according to the performance parameters and the executable quantum algorithm code to generate new quantum algorithm text description information. The new quantum algorithm text description information can be used to describe the optimized candidate quantum circuit.
[0116] In the embodiment, by inputting the third prompt information generated based on at least the performance parameters, the quantum algorithm text description information, and the executable quantum algorithm code into the generation model, the generation model can automatically optimize the candidate quantum circuit according to the feedback, the new quantum algorithm text description information is more accurate, and the accuracy of the executable quantum algorithm code is improved.
[0117] As another optional embodiment of the present application, refer to Figure 4 A flowchart of a quantum algorithm code generation method provided in Embodiment 5 of the present application is shown in the figure. The present embodiment mainly describes one implementation of step S306 described above, as shown in the figure, step S306 can include, but is not limited to: Figure 4
[0118] Step S3061, if the performance parameter does not meet the set condition, input the third prompt information generated based on the performance parameter, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information into the generation model, generate new quantum algorithm text description information by the generation model, and continue to execute the step of inputting the second prompt information generated based on the quantum algorithm text description information into the generation model.
[0119] Step S3061, if the performance parameter does not meet the set condition, input the third prompt information generated based on the performance parameter, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information into the generation model, generate new quantum algorithm text description information by the generation model, and continue to execute the step of inputting the second prompt information generated based on the quantum algorithm text description information into the generation model.
[0120] For example, the third prompt information generated based on the performance parameter, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information can include but is not limited to: “using this [prompt1], [prompt2] is obtained, and its [prompt3] is executed. Please optimize [prompt1] based on the requirement of meeting [prompt0], and the optimization method includes quantum gate operation change and sequence change, quantum bit number increase and decrease, and related parameter adjustment, to further improve the performance”. Wherein, [prompt1] is an embodiment of the quantum algorithm text description information, [prompt2] is an embodiment of the executable quantum algorithm code, [prompt3] is an embodiment of the performance parameter, and [prompt0] is an embodiment of the first prompt information.
[0121] In this embodiment, inputting the third prompt information generated based on the performance parameter, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information into the generation model can guide the generation model in the generation process by the first prompt information and constrain the generation model by the performance parameter, the quantum algorithm text description information and the executable quantum algorithm code, so as to ensure that the generation model always optimizes around the initial demand of the user, improves the accuracy of the quantum algorithm text description information, does not deviate from the initial demand of the user, and ensures that the executable quantum algorithm code and the initial demand of the user match while improving the accuracy of the executable quantum algorithm code.
[0122] As another optional embodiment of the present application, refer to Figure 5A flowchart of a quantum algorithm code generation method provided in Embodiment 6 of the present application is shown in the figure. Embodiment 6 mainly provides an implementation of step S101. As shown in the figure, step S101 can include but is not limited to the following steps. Figure 5
[0123] In step S1011, the description information of the target task input by the user is processed based on the set prompt word template to generate first prompt information.
[0124] The prompt word template can be set as needed and is not limited in the present application.
[0125] In the present embodiment, key information can be extracted from the description information of the target task input by the user and embedded into the set prompt word template to obtain the first prompt information.
[0126] In the present embodiment, by processing the description information of the target task input by the user based on the set prompt word template to generate the first prompt information, the user can describe the requirements without learning quantum terminology, thereby improving the user experience.
[0127] As another optional embodiment of the present application, a quantum algorithm code generation method provided in Embodiment 7 of the present application is shown in the figure. Embodiment 7 mainly provides an implementation of the set prompt word template. Specifically, it can include but is not limited to at least one of the following:
[0128] Role and task definition module: declare that the respondent's professional field is quantum algorithm design and optimization, and dynamically inject the description of the target task through a placeholder to limit the task range to quantum circuit implementation;
[0129] Hardware parameter constraint module: specify the physical resource limit of the quantum circuit, including the number of qubits and the number of operation blocks, to control the size of the quantum bit and the division of the modular operation stage;
[0130] Available operation list module: enumerate the types of quantum operations allowed to be used and their number mapping, including initialization, basic quantum gates (H, CNOT), parameterized rotation gates (RX, RY, RZ), and measurement operations;
[0131] Format specification and example module: define the text syntax rules of quantum circuit description, including the field order of operation number, acting qubit range, and parameter, specify the representation order of CNOT gate control bit and target bit, and provide standard examples of initialization, single / double qubit operation, and measurement;
[0132] Task requirement refinement module: embed specific task objectives (such as quantum state preparation, phase estimation) and optimization constraints (circuit depth, gate number, hardware adaptability) through a placeholder;
[0133] Output integrity requirement module: Mandatory requirement for the generated quantum circuit to contain initialization steps and final measurement steps, ensuring the circuit can be executed completely.
[0134] Design explanation requirement module: Requirement for the physical principles of circuit design ideas and key steps to be explained, including quantum gate action logic and algorithm flow explanation.
[0135] For example, the set prompt word template can include:
[0136] "You are an expert in quantum algorithm design and optimization. Your task is to design a quantum circuit to achieve [specific task description]. This quantum circuit will operate on [number of qubits] qubits, containing [number of blocks] operation blocks. The available quantum operations are as follows:
[0137] 0: Initialization
[0138] 1: Hadamard gate (H)
[0139] 2: CNOT gate
[0140] 3: Rotation X gate (RX)
[0141] 4: Rotation Y gate (RY)
[0142] 5: Rotation Z gate (RZ)
[0143] 6: Measurement
[0144] Please describe your quantum circuit design using the following format: [operation number, (acting qubit range), optional parameters]
[0145] For rotation gates, specify the rotation angle (in radians) in the third position. For CNOT gates, list the control bits before the target bits. For example:
[0146] [0, (0,1,2,3)] # Initialize all qubits
[0147] [1, (0)] # Apply H gate on qubit 0
[0148] [3, (1), 0.5] # Apply RX(0.5) gate on qubit 1
[0149] [2, (0,2)] # Apply CNOT gate with control bit 0 and target bit 2
[0150] [6, (0,1,2,3)] # Measure all qubits
[0151] Please design a quantum circuit to [specific task description, e.g., 'prepare a Bell state and perform quantum phase estimation on it']. Your design should consider [any specific constraints or optimization goals]. Please provide a complete description of the quantum circuit, including initialization and final measurement steps. Also, briefly explain your design approach and the purpose of each key step.
[0152] Next, let's design a quantum circuit to implement the Grover search algorithm. For this task, we will use the following encoding rules: 0: initialization; 1: Hadamard gate (H); 2: CNOT gate; 3: rotation X gate (RX); 4: rotation Y gate (RY); 5: rotation Z gate (RZ); 6: measurement. The performance metric will be the cost function C = 0.7D + 0.3G, where D is the circuit depth and G is the number of gates. The design space will use 4 qubits with a maximum circuit depth of 20.
[0153] Using the following encoding rules: 0: initialization; 1: Hadamard gate (H); 2: CNOT gate; 3: rotation X gate (RX); 4: rotation Y gate (RY); 5: rotation Z gate (RZ); 6: measurement.
[0154] 4: rotation Y gate (RY); 5: rotation Z gate (RZ); 6: measurement.
[0155] Performance metric: The cost function C = 0.7D + 0.3G will be used, where D is the circuit depth and G is the number of gates.
[0156] Design space: A maximum circuit depth of 20 will be used with 4 qubits.
[0157] Step 2: Prompt input (prompt0) (i.e., an implementation of the first prompt information):
[0158] Based on the given prompt template, construct the following prompt:
[0159] "You are an expert in quantum algorithm design and optimization. Your task is to design a quantum circuit to implement the Grover search algorithm. This quantum circuit will operate on 4 qubits and contain 5 operation blocks. The available quantum operations are as follows: 0: initialization; 1: Hadamard gate (H); 2: CNOT gate; 3: rotation X gate (RX); 4: rotation Y gate (RY); 5: rotation Z gate (RZ); 6: measurement. Please describe your quantum circuit design using the following format: [operation number, (target qubit range), optional parameters]. For rotation gates, specify the rotation angle in radians in the third position. For CNOT gates, list the control qubits before the target qubits. For example:
[0160] [0, (0,1,2,3)] # Initialize all qubits
[0161] [1, (0)] # Apply H gate on qubit 0
[0162] [3, (1), 0.5] # Apply RX(0.5) gate on qubit 1
[0163] [2, (0,2)] # Apply CNOT gate with control bit 0 and target bit 2
[0164] [6, (0,1,2,3)] # Measure all qubits
[0165] Here is the quantum circuit design for a 4-qubit Grover search algorithm to find the marked state |1111>. The design aims to minimize the circuit depth and gate count. The complete quantum circuit description includes initialization and final measurement steps. The design rationale and purpose of each key step are briefly explained.
[0166] Step 3: Generate the quantum algorithm text description information (denoted as prompt1) from prompt0:
[0167] Input prompt0 into a large language model (i.e., an implementation of a generative model) to output the generated quantum algorithm text description information (denoted as prompt1),
[0168] The content is as follows:
[0169] "Here is the quantum circuit design for a 4-qubit Grover search algorithm to find the marked state |1111>:
[0170] [0, (0,1,2,3)] # Initialize all qubits to |0000>
[0171] [1, (0,1,2,3)] # Apply H gates to all qubits, creating a uniform superposition
[0172] [2, (0,1)] # Apply CNOT gate with control bit 0 and target bit 1
[0173] [2, (1,2)] # Apply CNOT gate with control bit 1 and target bit 2
[0174] [2, (2,3)] # Apply CNOT gate with control bit 2 and target bit 3
[0175] [5, (3), π] # Apply RZ(π) gate on qubit 3
[0176] [2, (2,3)] # Apply CNOT gate with control bit 2 and target bit 3
[0177] [2, (1,2)] # Apply CNOT gate with control bit 1 and target bit 2
[0178] [2, (0,1)] # Apply CNOT gate, control bit 0, target bit 1
[0179] [1, (0,1,2,3)] # Apply H gate to all qubits
[0180] [3, (0,1,2,3), pi] # Apply RX(pi) gate to all qubits
[0181] [1, (0,1,2,3)] # Apply H gate again to all qubits
[0182] [6, (0,1,2,3)] # Measure all qubits
[0183] Design thought explanation:
[0184] 1. Initialization and creation of uniform superposition: First, initialize all qubits to |0000>, then apply H gates to create a uniform superposition.
[0185] 2. Oracle implementation: Use CNOT gates and RZ gates to build an oracle that inverts the phase of the target state |1111>.
[0186] 3. Amplitude amplification: Use a combination of H gates, RX(pi) gates, and H gates to implement the Grover diffusion operator, amplifying the amplitude of the target state.
[0187] 4. Measurement: Finally, measure all qubits to obtain the result.
[0188] This design considers minimizing circuit depth and gate count while ensuring the correct implementation of the Grover algorithm.
[0189] Step 4: Generate executable quantum algorithm code:
[0190] Input the quantum algorithm text description information prompt1 again into the large language model, and the large voice model outputs the executable quantum algorithm code using the Qiskit framework to convert the quantum algorithm text description information:
[0191] ```python
[0192] from qiskit import QuantumCircuit, Aer, execute
[0193] from math import pi
[0194] # Create a 4-qubit quantum circuit
[0195] qc = QuantumCircuit(4, 4)
[0196] # Initialize and create a uniform superposition
[0197] qc.h(range(4))
[0198] # Oracle implementation
[0199] qc.cx(0, 1)
[0200] qc.cx(1, 2)
[0201] qc.cx(2, 3)
[0202] qc.rz(pi, 3)
[0203] qc.cx(2, 3)
[0204] qc.cx(1, 2)
[0205] qc.cx(0, 1)
[0206] # Amplitude amplification
[0207] qc.h(range(4))
[0208] qc.rx(pi, range(4))
[0209] qc.h(range(4))
[0210] # Measurement
[0211] qc.measure(range(4), range(4))
[0212] # Run the circuit on a quantum simulator
[0213] simulator = Aer.get_backend('qasm_simulator')
[0214] job = execute(qc, simulator, shots=1000)
[0215] result = job.result()
[0216] counts = result.get_counts(qc)
[0217] print("Measurement results:", counts)
[0218] ```
[0219] Step 5: Execute the quantum algorithm code and evaluate performance, resulting in performance parameters (which can be represented as prompt2)
[0220] Execute the circuit on a quantum simulator, resulting in the following performance parameters:
[0221] Measurement results: {'1111': 784, '0000': 52, '0001': 41, '0010': 37, '0100':29, '1000': 25, '0011': 9, '0101': 7, '0110': 5, '1001': 4, '1010': 3, '1100': 2, '1011': 1, '1101': 1}
[0222] The accuracy of the expected return is 78.4% (784 / 1000 measurements result in the target state |1111>).
[0223] The circuit depth is 13.
[0224] The number of gates is 25.
[0225] According to the cost function C = 0.7D + 0.3G, we can calculate:
[0226] C = 0.7 × 13 + 0.3 × 25 = 16.6
[0227] Step 6: Iterative optimization
[0228] The large language model continues to analyze the performance parameters and considers whether further iterations are needed, and its final output is as follows:
[0229] "Analyzing the performance parameters as follows:
[0230] 1. Accuracy: Improved from 78.4% to 94.2%, which is a significant improvement, indicating that the optimization strategy is very effective.
[0231] 2. Circuit depth: Increased from 13 to 16, an increase of 3 units. This increase is acceptable because it brings a significant improvement in accuracy.
[0232] 3. Number of gates: Remained at 25, without increasing. This is a positive result because performance is improved without increasing the number of gates.
[0233] 4. Cost function C: Increased from 16.6 to 18.7. Although there is an increase, considering the significant improvement in accuracy, this increase is acceptable.
[0234] Considering that a very high accuracy (94.2%) has been reached while maintaining a relatively low circuit complexity, it can be considered that the current performance parameters have met the set conditions and the generation step can be ended.
[0235] If the current performance parameters do not meet the set conditions, prompt3 and prompt1 and prompt2 are constructed to form prompt4 (i.e., a new prompt1), and step 4 is continued to be executed.
[0236] Next, the quantum algorithm code generation apparatus provided in the present application will be introduced. The quantum algorithm code generation apparatus introduced below can be correspondingly referred to the quantum algorithm code generation method introduced above.
[0237] Reference Figure 6 The quantum algorithm code generation apparatus comprises a first obtaining module 100, a first generation module 200 and a second generation module 300.
[0238] The first obtaining module is configured to obtain first prompt information.
[0239] The first generation module is configured to input at least the first prompt information into a generation model, and generate quantum algorithm text description information from the generation model; the quantum algorithm text description information is used to describe a candidate quantum circuit for implementing a target task.
[0240] The second generation module is configured to input second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code from the generation model.
[0241] The quantum algorithm code generation apparatus can further comprise:
[0242] The second obtaining module is configured to obtain text encoding of a quantum algorithm; the text encoding is used to define a quantum algorithm architecture.
[0243] The first generation module 200 can be specifically configured to:
[0244] input the text encoding of the quantum algorithm and the first prompt information into a generation model, and generate quantum algorithm text description information conforming to the quantum algorithm architecture from the generation model.
[0245] The second obtaining module can be specifically configured to at least one of:
[0246] obtain coding rules of a quantum algorithm; the coding rules are used to define a standardized description format of the quantum algorithm;
[0247] obtain performance indicators of a quantum algorithm; the performance indicators are used to define the quality of the quantum algorithm;
[0248] obtaining a design space of a quantum algorithm; the design space is used to define a quantum circuit architecture.
[0249] The quantum algorithm code generation apparatus can further include:
[0250] a third obtaining module configured to obtain a performance parameter of the executable quantum algorithm code by executing the executable quantum algorithm code.
[0251] an ending module configured to end the generation step if the performance parameter meets a set condition;
[0252] a third generation module configured to input, if the performance parameter does not meet the set condition, third prompt information generated based on the performance parameter, the quantum algorithm text description information and the executable quantum algorithm code into the generation model, generate new quantum algorithm text description information by the generation model, and trigger the second generation module 300 to continue the step of inputting the second prompt information generated based on the quantum algorithm text description information into the generation model.
[0253] The process that the third generation module inputs the third prompt information generated based on the performance parameter, the quantum algorithm text description information and the executable quantum algorithm code into the generation model and generates new quantum algorithm text description information by the generation model can specifically include:
[0254] inputting third prompt information generated based on the performance parameter, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information into the generation model, and generating new quantum algorithm text description information by the generation model.
[0255] In the embodiment, the first obtaining module 100 can specifically be configured to:
[0256] process the description information of the target task input by the user based on a set prompt word template to generate the first prompt information.
[0257] The set prompt word template can include at least one of the following:
[0258] a role and task definition module; a hardware parameter constraint module; an available operation list module; a format specification and example module; a task requirement refinement module; an output integrity requirement module; a design specification requirement module.
[0259] In another embodiment of the present application, an electronic device is provided, which includes a memory, at least one processor and a computer program stored in the memory, the processor executes the computer program to implement the following method steps:
[0260] obtain first prompt information;
[0261] input at least the first prompt information into a generation model, and generate quantum algorithm text description information by the generation model; the quantum algorithm text description information is used for describing a candidate quantum circuit for implementing a target task;
[0262] input second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code by the generation model.
[0263] In another embodiment of the present application, a computer storage medium is provided, and the storage medium carries one or more computer programs, when the one or more computer programs are executed by an electronic device, the electronic device can implement the following method steps:
[0264] obtain first prompt information;
[0265] input at least the first prompt information into a generation model, and generate quantum algorithm text description information by the generation model; the quantum algorithm text description information is used for describing a candidate quantum circuit for implementing a target task;
[0266] input second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code by the generation model.
[0267] In addition, it should be noted that the above-described device embodiments are only schematic, and the units described as separate components can or can not be physically separated, and the components shown as units can or can not be physical units, that is, they can be located in one place, or can be distributed on multiple network units. According to actual needs, part or all of the modules can be selected to achieve the purpose of the present embodiment. In addition, the connection relationship between the modules in the device embodiment provided by the present application indicates that there is a communication connection between them, which can be implemented as one or more communication buses or signal lines.
[0268] Those skilled in the art can clearly understand that the application can be implemented by means of software plus necessary universal hardware, and of course can also be implemented by means of dedicated hardware including special integrated circuit, special CPU, special memory, special component, etc. Generally, any function completed by computer program can be easily implemented by corresponding hardware, and the specific hardware structure for implementing the same function can also be various, such as analog circuit, digital circuit or special circuit, etc. However, for the application, software program implementation is a better embodiment. Based on such understanding, the technical solution of the application or the part of the application which makes contribution to the prior art can be embodied in the form of software product, which is stored in readable storage medium, such as computer floppy disk, U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk, etc., and includes a plurality of instructions for making a computer device (which can be personal computer, training device or network device, etc.) execute the method described in various embodiments of the application.
[0269] In the above embodiments, the implementation can be achieved by software, hardware, firmware or any combination thereof, entirely or partially. When implemented by software, the implementation can be achieved in the form of a computer program product, entirely or partially.
[0270] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the flow or function described in the embodiments of the application is generated entirely or partially. The computer can be a general-purpose computer, a special-purpose computer, a computer network or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another, for example, the computer instructions can be transmitted from one website, computer, training device or data center to another website, computer, training device or data center through wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) mode. The computer-readable storage medium can be any available medium that can be stored by the computer or a data storage device such as training device, data center, etc. integrated with one or more available media sets. The available medium can be magnetic medium (such as floppy disk, hard disk, magnetic tape), optical medium (such as DVD) or semiconductor medium (such as solid state disk (SSD)) etc.
Claims
1. A quantum algorithm code generation method characterized by, The method comprises the following steps: obtaining first prompt information; inputting the first prompt information into a generation model to generate quantum algorithm text description information by the generation model; the quantum algorithm text description information is used for describing a candidate quantum circuit for realizing a target task; inputting second prompt information obtained based on the quantum algorithm text description information into the generation model to generate executable quantum algorithm code by the generation model.
2. The quantum algorithm code generation method of claim 1, wherein, The quantum algorithm code generation method further comprises the following steps: obtaining text encoding of a quantum algorithm; the text encoding is used for defining a quantum algorithm architecture; the step of inputting the first prompt information into the generation model to generate quantum algorithm text description information by the generation model comprises the following steps: inputting the text encoding of the quantum algorithm and the first prompt information into the generation model to generate quantum algorithm text description information conforming to the quantum algorithm architecture by the generation model.
3. The quantum algorithm code generation method of claim 2, wherein, The step of obtaining the text encoding of the quantum algorithm comprises at least one of the following steps: obtaining coding rules of the quantum algorithm; the coding rules are used for defining a standardized description format of the quantum algorithm; obtaining performance indicators of the quantum algorithm; the performance indicators are used for defining the quality of the quantum algorithm; obtaining a design space of the quantum algorithm; the design space is used for defining a quantum circuit architecture.
4. The quantum algorithm code generation method of claim 1, wherein, The quantum algorithm code generation method further comprises the following steps: obtaining performance parameters of the executable quantum algorithm code by executing the executable quantum algorithm code; if the performance parameters meet a set condition, ending the generation step; if the performance parameters do not meet the set condition, inputting third prompt information generated based on the performance parameters, the quantum algorithm text description information and the executable quantum algorithm code into the generation model to generate new quantum algorithm text description information by the generation model, and continuing the step of inputting second prompt information generated based on the quantum algorithm text description information into the generation model.
5. The quantum algorithm code generation method of claim 4, wherein, The step of inputting the third prompt information into the generation model to generate new quantum algorithm text description information by the generation model comprises the following steps: inputting third prompt information generated based on the performance parameters, the quantum algorithm text description information, the executable quantum algorithm code and the first prompt information into the generation model to generate new quantum algorithm text description information by the generation model.
6. The quantum algorithm code generation method of claim 1, wherein, The step of obtaining the first prompt information comprises the following steps: processing description information of the target task input by a user based on a set prompt word template to generate the first prompt information.
7. The quantum algorithm code generation method of claim 6, wherein, The set prompt word template comprises at least one of the following: a role and task definition module; a hardware parameter constraint module; an available operation list module; a format specification and example module; a task requirement refinement module; an output integrity requirement module; a design specification requirement module.
8. A quantum algorithm code generation apparatus characterized by comprising: The method comprises the following steps: a first obtaining module is configured to obtain first prompt information; a first generation module is configured to input the first prompt information into a generation model to generate quantum algorithm text description information by the generation model; The quantum algorithm text description information is used for describing a candidate quantum circuit for implementing a target task; The second generation module is configured to input second prompt information obtained based on the quantum algorithm text description information into the generation model, and generate executable quantum algorithm code by the generation model.
9. An electronic device, comprising: The computer program is stored in the memory and executed by the at least one processor to implement the following method steps: obtaining first prompt information; inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information by the generation model; the quantum algorithm text description information is used for describing a candidate quantum circuit for implementing a target task; inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code by the generation model.
10. A computer storage medium, characterized in that, The storage medium carries one or more computer programs, and when the one or more computer programs are executed by an electronic device, the electronic device can implement the following method steps: obtaining first prompt information; inputting at least the first prompt information into a generation model, and generating quantum algorithm text description information by the generation model; the quantum algorithm text description information is used for describing a candidate quantum circuit for implementing a target task; inputting second prompt information obtained based on the quantum algorithm text description information into the generation model, and generating executable quantum algorithm code by the generation model.
Citation Information
Cited By
Operator generation method, electronic equipment, storage medium and program product
CN121212225A
An operator generation method, electronic device, storage medium, and program product
CN121212225B
