A Test Case Generation Method Based on Quantum Programming Language Syntax Rules
By using a test case generation method based on the grammar rules of quantum programming languages, the problem of insufficient test case generation performance in existing technologies is solved, achieving efficient and automated quantum software stack testing, improving grammatical correctness and coverage, and discovering compiler defects.
Patent Information
- Application Number
- CN202210902691.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-29
- Publication Date
- 2025-10-28
- Estimated Expiration
- 2042-07-29
AI Technical Summary
Existing technologies have shortcomings in the performance of test cases generated when building test cases for quantum software stacks, particularly in terms of syntax correctness and coverage, making it difficult to effectively trigger deep defects in the compiler.
A test case generation method based on the syntax rules of quantum programming language is adopted, including code snippet generation, template selection and generation, API selection and generation, regression test case snippet insertion and test case assembly modules. High-quality test cases are generated by randomly selecting statement templates and API interfaces and combining them with regression test case sets.
It improved the syntax pass rate and semantic diversity of test cases, increased coverage of the quantum software stack, discovered potential compiler defects, saved human resources, and achieved efficient automated testing.
Smart Images

Figure CN115344476B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of automated software testing technology, and relates to test cases, specifically a test case generation method based on the syntax rules of quantum programming language. Background Technology
[0002] Potential defects in the compiler can lead to defects or even security vulnerabilities in the compiled application. When an application exhibits abnormal behavior due to compilation errors, developers often find it difficult to pinpoint the cause of the error, increasing the burden of program maintenance.
[0003] As the core software connecting quantum programming languages and underlying hardware design, the correctness and completeness of quantum compiler implementations are crucial to the development of quantum software stacks. However, quantum software stacks are still in the early stages of research and development, and existing academic research on testing quantum software stacks is insufficient. Therefore, it is urgent to develop testing methods for quantum software stacks, which is of great significance for promoting the development and popularization of quantum software.
[0004] Q# is a high-level quantum programming language developed by Microsoft, enabling programmers to quickly and easily create quantum programs at the algorithm and application levels. Furthermore, Q# is developed as a standalone language, which, compared to quantum programming languages that are essentially extension packages, offers greater scalability and avoids potential problems with other quantum programming languages, such as the inability to dynamically run circuits or the inability to integrate semantic differences between quantum and classical computing. Therefore, this invention selects the Q# compiler QsCompiler as the test object.
[0005] Constructing high-quality test cases is a major challenge in the field of fuzz testing. For fuzz testing, high-quality test cases are not only syntactically correct and varied in syntax, but also improve code coverage to achieve deep coverage of compiler code branches.
[0006] Currently, there are two main methods for building test cases: generation-based and mutation-based. The former first generates program fragments and then assembles them into a complete program; the latter requires collecting existing programs and performing mutation operations such as trimming and insertion.
[0007] Generative test case construction methods can be divided into two categories: those based on grammar rule templates and those based on machine learning techniques. The former takes a set of language grammar rules as input and generates test cases based on those rules; the latter requires collecting existing programs as a corpus, training a model, and using the model-generated programs as test cases. Test cases generated based on grammar rule templates are characterized by high randomness and grammatical accuracy, but they usually have a fixed format, and the types of statements they can contain depend on the number of templates. Machine learning-based generation methods can generate highly random test cases without a fixed format, but their grammatical pass rate is generally lower than that of grammar rule template-based methods.
[0008] Both mutation-based and machine learning model-based test case construction methods require access to a large amount of open-source code. However, this approach currently struggles to achieve satisfactory results in testing quantum software stacks. The main reason is that the quantum programs currently available for building datasets are either related to a few well-known quantum algorithms or are practices and reproductions of basic tutorials. The number of collectable quantum programs is limited, and their diversity is significantly insufficient. Using these programs for test case construction results in low coverage of the software under test, making it difficult to trigger potential defects deep within the compiler's code. Summary of the Invention
[0009] In view of the shortcomings of the existing technology, the purpose of this invention is to provide a test case generation method based on the syntax rules of quantum programming language, so as to solve the technical problem that the performance of test cases obtained by the existing generation methods needs to be further improved.
[0010] To solve the above-mentioned technical problems, the present invention adopts the following technical solution:
[0011] A test case generation method based on the syntax rules of a quantum programming language, comprising the following five modules;
[0012] The code snippet generation module is used to generate code snippets required by other modules.
[0013] The template selection and generation module is used to randomly select a statement template each time it runs, and combine it with the code snippet generation module to generate a complete code block;
[0014] The API selection and generation module is used to randomly select an API interface each time it runs, and combine it with the code snippet generation module to generate API call statements and the required variable declaration statements;
[0015] The regression test case fragment insertion module is used only when generating API statements to obtain a set of regression test cases and extract them as functions.
[0016] The test case assembly module combines three components: the first component is the namespaces that need to be included in the program; the second component is one or more operations and functions, where one operation contains the generated Q# statement block and API call statements, and the remaining functions are regression test case fragments; the third component is the main function that calls the custom operation and the entry point.
[0017] The present invention also has the following technical features:
[0018] Specifically, in the code snippet generation module, when filling in statement templates or API call statements, it is necessary to generate variable names, variable values, and expressions;
[0019] The variable name refers to the specific parameter of the API call statement. It is generated by a specific string and a number, where the number represents the ordinal number of the variable.
[0020] The variable values are used to provide initial values for the declaration of variables, and their generation process involves generating random values or boundary values based on the variable type.
[0021] Expressions are operation statements, generated by connecting variables using unary or binary operators.
[0022] Preferably, in the code snippet generation module, when generating an expression, both the return type and the return value of the expression need to be recorded. The return type is used to determine whether it can be placed in a specific position in the template, and the return value can be passed to an existing variable as a value modification operation, or passed to a newly declared variable as an initialization operation.
[0023] Preferably, in the code snippet generation module, a Python program simulating the semantics of quantum operators is added when generating the expression to calculate the result of the expression.
[0024] Specifically, the template selection and generation module requires understanding the basic elements of the Q# language, summarizing syntax rules, and then designing statement templates; the syntax rules include data types, statement types, and expressions.
[0025] Specifically, in the API selection and generation module, the detailed information about the API is first obtained, and then the API and the information required for generation are stored in a JSON file for querying when generating API statements; finally, the call statement for the Any function is generated.
[0026] Preferably, the detailed information about the API includes the API type, parameter types and their meanings, return value type and its meaning, and the operation performed by the API.
[0027] Preferably, the specific process for generating the call statement for the Any function is as follows:
[0028] First, retrieve and parse the parameter types from the JSON file to determine that the type of the predicate parameter is operation and the type of the array parameter is array.
[0029] Secondly, instantiate the parameter type and randomly assign it a Q# variable type;
[0030] Finally, since the Any function returns a Boolean value, a variable needs to be defined to receive the return value.
[0031] Specifically, in the regression test case fragment insertion module, the regression test case set of Q# is collected, and the fragments in the regression test case set are combined into the test case generation process.
[0032] Preferably, the regression test case set includes two parts: a basic test case set, i.e., the original test case set, and a test case set added during the iteration process, including test cases introduced to fix faults and test cases introduced to add new functions.
[0033] Compared with the prior art, the present invention has the following technical effects:
[0034] (I) The generation technology of this invention implements a test case generator for the Q# language. Using this generator as the source of test cases can not only save a lot of human resources, but also increase the semantic diversity of test cases and improve the syntax pass rate of test cases, providing a solid foundation for realizing automated and efficient quantum software stack testing.
[0035] (II) By collecting API interface documentation and regression test case sets, and combining them with random parameter passing functionality, this invention enables testing of API implementations in the quantum software stack.
[0036] (III) The generation technology proposed in this invention can not only ensure the randomness and diversity of test cases, but also avoid the problem of reduced testing efficiency caused by an excessively large testing scope. It can perform continuous and efficient testing on specific modules of the QDK quantum software stack. Attached Figure Description
[0037] Figure 1 This is the basic flowchart generated by the Q# program.
[0038] Figure 2 This is an example of the Q# program generation process.
[0039] Figure 3 This is an example of Q# expression generation.
[0040] Figure 4 It is a syntax rule template for If-Else statements.
[0041] Figure 5 This describes the process of generating the call statement for the Any function.
[0042] Figure 6 It is the function ResultAsBoolNeq.
[0043] Figure 7 This is a defect in function implementation triggered by code generation technology.
[0044] Figure 8 This is a documentation defect triggered by code generation technology.
[0045] The specific content of the present invention will be further explained in detail below with reference to the embodiments. Detailed Implementation
[0046] It should be noted that, unless otherwise specified, all devices and algorithms in this invention are based on devices and algorithms known in the prior art. API, or Application Programming Interface, is a programming interface for applications.
[0047] A Q# program generally consists of the following parts: First, the entry point; a Q# program begins execution from the entry point. Second, when using Q# built-in libraries or third-party libraries in the program, the corresponding namespace must be specified at the beginning of the program. Before performing specific operations, variables must be declared. Q# supports variable types including common basic types used in classical computing such as Int, Double, Bool, and String, as well as ranges, arrays, tuples, user-defined types, and quantum computing-specific types. Finally, this invention can add classical and quantum operations. Classical operations are similar in expression to those in traditional programming languages; to build a quantum circuit and view its results, one can add qubit declarations, quantum gates, and measurements.
[0048] Traditional test case building techniques based on language syntax rules focus primarily on ensuring the diversity, context relevance, and syntactic correctness of generated content, without sufficient research into how to test specific modules. Furthermore, in quantum computing, quantum states and their corresponding operations are represented by algebraic structures such as vectors and matrices. Therefore, quantum compilers have far more modules implementing mathematical operations than classical compilers, leading to more implementation defects related to mathematical operations. For example, calculating powers to the base 0 or taking the square root of 0 can result in incorrect calculations under certain boundary values if not properly considered. Moreover, a low-level implementation defect can propagate to all modules that call the function, making it difficult for developers to accurately locate and fix it.
[0049] To more effectively test specific modules of the quantum software stack, this invention proposes a novel quantum program generation method based on syntax rules. The general idea of this method is as follows: First, the syntax rules are manually summarized and a Q# generation template is designed. Then, the generator randomly selects a template and generates the required content according to the template, obtaining multiple program fragments. Finally, the multiple program fragments are spliced together, call statements are added, and entry points are specified to obtain a complete Q# program.
[0050] Based on the essential components of a quantum program, this invention designs and implements a syntax-based Q# test case generator, the overall process of which is as follows: Figure 1 As shown.
[0051] This invention provides a test case generation method based on the syntax rules of quantum programming languages, which includes the following five modules.
[0052] The code snippet generation module is used to generate code snippets required by other modules.
[0053] The template selection and generation module is used to randomly select a statement template each time it runs, and combine it with the code snippet generation module to generate a complete code block.
[0054] The API selection and generation module is used to randomly select an API interface each time it runs, and combine it with the code snippet generation module to generate API call statements and the required variable declaration statements.
[0055] The regression test case fragment insertion module is used only when generating API statements. It retrieves a set of regression test cases from the official repository and extracts them as functions. This invention uses this as an alternative for API parameter passing.
[0056] The test case assembly module combines three components: the first component is the namespaces that need to be included in the program; the second component is one or more operations and functions, where one operation contains the generated Q# statement block and API call statements, and the remaining functions are regression test case fragments; the third component is the main function that calls the custom operation and the entry point.
[0057] Figure 2 A specific example demonstrates the basic process of generating Q# programs. Figure 2 (a) and (b) in the text refer to the content related to template selection and module generation. Figure 2 (a) shows the variable declaration and the If-Else statement template. Combined with the code snippet generation module, the generator will generate a code snippet like this. Figure 2 The content shown in (b) is shown in the image. Figure 2 (c) and (d) are related to API selection and module generation. Figure 2 (c) in the code snippet shows the basic information of ApplyToFirstQubit. Combined with the code snippet generation module, the generator will fill in the parameters required by the API and generate the API call statement. Figure 2 (e) in the code represents the relevant content of the regression test case fragment insertion module. Q# regression test cases are stored as function units, and in this example, they are passed as parameters to the ApplyToFirstQubit call statement. Figure 2 (f) in the text refers to the relevant content of the test case assembly module. Figure 2 (f) in the diagram shows the test cases that the generator finally produces, which include the required library file references, basic quantum statements, API call statements, and regression test case snippets.
[0058] Following the above technical solutions, specific embodiments of the present invention are given below. It should be noted that the present invention is not limited to the following specific embodiments, and all equivalent modifications made based on the technical solutions of this application fall within the protection scope of the present invention. The following embodiments... Figure 1 The five modules divided into sections will be explained in more detail.
[0059] Example:
[0060] This embodiment presents a test case generation method based on the syntax rules of a quantum programming language, such as... Figure 1 As shown, the generation method includes the following five modules.
[0061] First, the code snippet generation module:
[0062] When filling in statement templates or API call statements, it is necessary to generate variable names, variable values, and expressions.
[0063] The variable name refers to the specific parameter of the API call statement. It is generated by a specific string and a number, where the number represents the ordinal number of the variable.
[0064] The variable values are used to provide initial values for variable declarations. The generation process involves generating random values or boundary values based on the variable type. Random values are obtained by calling a random function, while boundary values are set and expandable.
[0065] Expressions are operation statements, generated by connecting variables using unary or binary operators. Figure 3 This demonstrates the evolution of generating an expression of length 3.
[0066] The length of the currently generated expression, `Depth`, indicates the number of iterations performed. In each iteration, the generator randomly selects operators and matches appropriate variables for concatenation. For example... Figure 3 As shown, the first iteration generates the expression "(-)" using the negative operator "-" and the variable "a" of type Int. The expression returns -1. The second iteration generates the expression "(-a)" using the division operator " / " and the variables "c" and "(-a)" of type Int. The expression returns -0.333.3. The third iteration generates the expression "+" using the addition operator "+" and the variables "b" and "(-a)" of type Double. The expression returns -0.666.7.
[0067] When generating an expression, both its return type and return value must be recorded. The return type is used to determine whether it can be placed in a specific position within the template, and the return value can be passed to an existing variable for value modification or to a newly declared variable for initialization. For example, a conditional expression is a specific position within the template, in which case the expression's return value must be of type Boolean.
[0068] To ensure the syntactic correctness of the generated expressions, a Python program simulating the semantics of quantum operators was added to calculate the expression results. This program ensures that the generator accurately generates expressions of the specified type.
[0069] Second, the template selection and generation module:
[0070] First, the task to be completed is template design. Therefore, this invention needs to understand the basic elements of the Q# language, summarize the syntax rules, and then design statement templates.
[0071] The Q# Language Guide provides a complete specification and documentation for the Q# quantum programming language. By reading these documents and combining them with existing Q# application code, this invention summarizes the basic elements of the Q# language as shown in Table 1.
[0072] Table 1. Basic Elements of the Q# Language
[0073]
[0074]
[0075] By understanding the usage of basic Q# elements, analyzing their syntax structure, and combining this with existing Q# programs, this invention summarizes the syntax rules of language elements in Q#. Figure 4 This demonstrates a syntax rule template for the If-Else statement in the Q# programming language.
[0076] like Figure 4 As shown, the definition of the If-Else statement has three forms: If, If-Else, and If-Elif-Else. Each format includes a condition and a statement body. The condition is the expression that produces a truth value and is used to point to different operations. The statement body describes the operation that needs to be performed in the specific condition.
[0077] The Q# syntax rules currently covered in the tools implemented by this invention include data types, statement types, expressions, and other quantum property operations.
[0078] Third, the API selection and generation module:
[0079] This module is used to perform targeted testing of the quantum programming language API.
[0080] By crawling the API's description page, this invention can obtain detailed information about the API, such as its type, parameter types and their meanings, return value types and their meanings, and the operations performed by the API. Through unified processing, this invention stores the API and the information needed for generation into a JSON file for querying when generating API statements.
[0081] Figure 5This demonstrates the call statement for generating the Any function. The Any function is an API located in the Microsoft.Quantum.Arrays library. It has two parameters: the first parameter is named `predicate`, and it needs to be of type `operation`; the second parameter is named `array`, and it needs to be of type `array`. The API returns a Boolean value. In this example, there is a special consideration: the type of the `predicate` parameter must match the actual type of the `array` parameter.
[0082] After obtaining the above information, this invention can begin generating the call statement for the Any function. First, the parameter types need to be retrieved and parsed from the JSON file, determining that the type of the predicate parameter is operation and the type of the array parameter is array. Then, the parameter type 'T' is instantiated and randomly assigned a variable type Q#, such as Double. Since the predicate parameter requires an operation with input type Double and output type Bool, the API list and regression test case pool are searched for suitable operation types, and the variable pool is searched for variables of type Double array. If found, the invention directly passes in a variable of the corresponding type; otherwise, a new variable is declared and passed in. Finally, because the return value type of the Any function is Bool, a variable needs to be defined to receive the return value. If the API's return value type is Unit, meaning no return value is generated, the result does not need to be received by a variable.
[0083] As related software continues to advance, documentation versions will also be updated. Therefore, this invention organizes the relevant code for API parsing and processing and provides convenient interfaces so that after a new version of the documentation is released, the latest version of API information can be quickly obtained to assist in the generation of test cases.
[0084] Fourth, the regression test case fragment insertion module:
[0085] In software development, regression testing is commonly used to ensure software quality. A regression test case set mainly consists of two parts: a basic test case set (the original test case set) and a test case set added during iterations, including test cases introduced to fix bugs and test cases introduced to add new features. Since the regression test case set includes tests for implemented functions, this invention collects and processes the regression test case set of Q#, incorporating fragments from the regression test case set into the test case generation process to enhance the testing of specific modules of the quantum compiler.
[0086] exist Figure 5 In the example, assuming an operation with an input type of Result and a return type of Bool is required, this invention can search within collected regression test case fragments and ultimately obtain the function ResultAsBoolNeq, as shown below. Figure 6 As shown.
[0087] This invention passes it as a parameter to the Any function, which generates API statements and adds regression test case fragments, effectively enhancing the detection of specific modules.
[0088] Fifth, the test case assembly module:
[0089] When generating a complete quantum program, the generator needs to maintain three data structures: a variable pool, a list of generation statements, and a list of namespaces.
[0090] The generator ensures the context-dependent nature of generated code by maintaining a pool of variables with mutable scope. In most cases, the variables in this pool have global scope, but when a code block is entered for generation, the scope of the newly generated variables becomes local. Therefore, upon exiting the code block, variables that were only valid within that block must be deleted.
[0091] The number of statements generated for each test case is specified, and the number of statements determines the number of times the generation module is executed. Each time the generation module is called, a statement type is randomly selected for generation, and the generated result is stored in a statement list. If a new namespace needs to be introduced in the code block, the corresponding statement is added to the namespace list.
[0092] After performing the generation operation a specified number of times, the generator concatenates the contents generated by each module together in the following order: namespace declaration, custom operation or function, entry point, and main function.
[0093] The following algorithm describes the generation process of Q# functions:
[0094]
[0095]
[0096] The second line of the algorithm above defines the variablePool variable, which is used to record and update the defined variables of the current context during the generation process, that is, a variable pool with variable scope.
[0097] The `namespaces` and `generateStatements` variables are used to record the referenced namespaces and generated statements, while the `count` variable specifies how many random statements to generate. In each loop, a statement type is randomly selected for code generation, and the generated statements are stored in the `generateStatements` variable. Finally, on line 16 of the algorithm, the generated elements are merged into a single test case and returned.
[0098] Performance testing:
[0099] (1) Experimental environment:
[0100] In terms of hardware, the experimental platform of this invention comprises two components: one is a high-performance server equipped with an Intel i9-9940x processor with a clock speed of 3.30GHz and an Ubuntu 18.04 operating system (kernel version 5.4.0), 128GB of memory, and three RTX 2080Ti graphics cards. The other is a computer host equipped with an Intel Xeon Gold 6133 processor with a clock speed of 2.50GHz and a Windows Server 2016 operating system, with 4GB of memory.
[0101] In terms of software, the main development languages used in the experiments conducted in this invention are Python and JavaScript. The Python interpreter version used is Python v3.8.5, and the JavaScript interpreter version is Node v14.15.4.
[0102] (2) Evaluation of the role of specific modules:
[0103] Since this invention achieves targeted generation of test case content through specific modules for testing the QsCompiler API implementation, this section evaluates the roles of the API selection and generation module and the regression test case fragment insertion module. This invention uses the patented tool to generate 1000 Q# test cases, then removes the API selection and generation module and the regression test case fragment insertion module before generating the same number of Q# test cases again. Evaluations were then conducted on three aspects: code length, syntax accuracy, and ability to detect suspicious results. The comparison results are shown in Table 2.
[0104] Table 2 Comparison Data
[0105] Evaluation criteria This invention (removing specific modules) This invention Average number of characters 1575 1684 Average number of rows 48 51 Grammar accuracy 82.5% 93.8% Number of suspicious use cases 12 106
[0106] As shown in Table 2, removing the API selection and generation module and the regression test case fragment insertion module has little impact on code length; a Q# program of a certain length can still be generated for testing. The syntax accuracy is lower than the generator containing all modules. A significant difference lies in the number of suspicious test cases. After removing the two modules, only 12 suspicious results were obtained from 1000 test cases after mutation and differential testing, while the original generator generated 106. Therefore, the API selection and generation and regression test case fragment insertion modules, while maintaining the generated length and syntax accuracy, increase targeted testing of the API implementation and discover more suspicious test cases.
[0107] Comparative example:
[0108] This comparative example presents a method for generating Q# programs, specifically the Sequence-To-Sequence generation model for Q# programs proposed by Miguel Trinca et al., named qsharp-fuzz. This model is trained using 244 Q# programs as a dataset for 50 epochs. This comparative example achieves the same purpose as this invention but uses a different generation technique. Therefore, this invention uses two tools to generate 1000 Q# programs respectively and compares their code length, syntax accuracy, and API diversity to demonstrate the effectiveness of this invention. The comparison results are shown in Table 3.
[0109] Table 3 Comparison data between comparative examples and the implementation tools of this invention.
[0110] Evaluation criteria Comparative Example This invention Average number of characters 678 1684 Average number of rows 28 51 Grammar accuracy 79.6% 93.8% API call count 68 544
[0111] As can be seen from Table 3:
[0112] Code length: Among the 1000 Q# programs generated by the qsharp-fuzz model, each program had an average of 678 characters and 28 lines. The tool implemented in this invention generated an average of 1684 characters and 51 lines. It can be seen that because the designed templates basically cover the Q# syntax, longer programs can be generated, avoiding the limitations of the training set on the generation model. Furthermore, in the latest version of this invention, the length of code blocks can be specified by the user.
[0113] Syntax accuracy: Of the 1000 Q# programs generated by the qsharp-fuzz model, 796 passed the syntax check. The syntax accuracy of the programs generated by the tool implemented in this invention is 93.8%.
[0114] API call count: Of the 1000 programs open-sourced by qsharp-fuzz on GitHub, those that passed syntax checking were placed in the `valid` folder, and those that failed syntax checking were placed in the `invalid` folder. Taking the generated programs in the `valid` folder alone as an example, there were 67 API calls, and taking the generated programs in the `invalid` folder alone, there were 56 API calls, for a total of 68 API calls. The program generated by the tool implemented in this invention contains 544 API calls.
[0115] Application example:
[0116] This application example is based on the test case generation method based on the quantum programming language syntax rules given in the above embodiments. Specifically, the following two Q# programs generated by the implementation tool of this invention trigger potential defects related to the API; the first is related to the implementation of the function, and the second is related to the documentation description of the function.
[0117] Figure 7 The test case shown successfully triggered a boundary implementation defect in the Chunks function through random parameter generation. Specifically, when the value of parameter NISLParameter0 was 0, the test case execution time was excessively long. The Chunks function is used to split a raw array into multiple subarrays of equal length. When the specified split length is 0, a while loop within the function cannot update the corresponding variables, thus the condition remains true and the loop cannot terminate automatically. After identifying the cause of this defect, this invention submitted the issue to the Q# GitHub open-source repository, which was confirmed and fixed.
[0118] Figure 8 The test case shown successfully detected a flaw in the standard documentation by randomly generating parameters. The standard documentation specifies the range of parameter n for ApproximateFactorial as AbsD(n) < 170.0. However, when the generator is given a negative number less than 170 for parameter n, the compiler fails to execute the test case and displays the error message "Parameter n cannot be negative". After submitting the report, the developers confirmed this was a documentation flaw and corrected the description in the documentation, changing AbsD(n) < 170.0 to n < 170.
Claims
1. A test case generation method based on the syntax rules of a quantum programming language, characterized in that, The generation method includes the following five modules; The code snippet generation module is used to generate code snippets required by other modules. The template selection and generation module is used to randomly select a statement template each time it runs, and combine it with the code snippet generation module to generate a complete code block; The API selection and generation module is used to randomly select an API interface each time it runs, and combine it with the code snippet generation module to generate API call statements and the required variable declaration statements; The regression test case fragment insertion module is used only when generating API statements to obtain a set of regression test cases and extract them as functions. The test case assembly module combines three components: the first component is the namespaces that need to be included in the program; the second component is one or more operations and functions, where one operation contains the generated Q# statement block and API call statements, and the remaining functions are regression test case fragments; the third component is the main function that calls the custom operation and the entry point.
2. The test case generation method based on quantum programming language syntax rules as described in claim 1, characterized in that, In the code snippet generation module, when filling in statement templates or API call statements, it is necessary to generate variable names, variable values, and expressions; The variable name refers to the specific parameter of the API call statement. It is generated by a specific string and a number, where the number represents the ordinal number of the variable. The variable values are used to provide initial values for the declaration of variables, and their generation process involves generating random values or boundary values based on the variable type. Expressions are operation statements, generated by connecting variables using unary or binary operators.
3. The test case generation method based on quantum programming language syntax rules as described in claim 2, characterized in that, In the code snippet generation module, when generating an expression, both the return type and the return value of the expression need to be recorded. The return type is used to determine whether it can be placed in a specific position in the template, and the return value can be passed to an existing variable as a value modification operation, or passed to a newly declared variable as an initialization operation.
4. The test case generation method based on quantum programming language syntax rules as described in claim 2, characterized in that, In the code snippet generation module, a Python program simulating the semantics of quantum operators is added when generating expressions to calculate the results of the expressions.
5. The test case generation method based on quantum programming language syntax rules as described in claim 1, characterized in that, The template selection and generation module requires understanding the basic elements of the Q# language, summarizing syntax rules, and then designing statement templates; the syntax rules include data types, statement types, and expressions.
6. The test case generation method based on quantum programming language syntax rules as described in claim 1, characterized in that, In the API selection and generation module, the detailed information about the API is first obtained, and then the API and the information required for generation are stored in a JSON file for querying when generating API statements; finally, the call statement for the Any function is generated.
7. The test case generation method based on quantum programming language syntax rules as described in claim 6, characterized in that, The detailed information about the API includes the API type, parameter types and their meanings, return value type and its meaning, and the operations performed by the API.
8. The test case generation method based on quantum programming language syntax rules as described in claim 6, characterized in that, The specific process of generating the call statement for the Any function is as follows: First, retrieve and parse the parameter types from the JSON file to determine that the type of the predicate parameter is operation and the type of the array parameter is array. Secondly, instantiate the parameter type and randomly assign it a Q# variable type; Finally, since the Any function returns a Boolean value, a variable needs to be defined to receive the return value.
9. The test case generation method based on quantum programming language syntax rules as described in claim 1, characterized in that, The regression test case fragment insertion module collects the regression test case set of Q# and incorporates fragments from the regression test case set into the test case generation process.
10. The test case generation method based on quantum programming language syntax rules as described in claim 9, characterized in that, The regression test case set consists of two parts: the basic test case set, i.e. the original test case set, and the test case set added during the iteration process, including test cases introduced to fix faults and test cases introduced to add new features.
Citation Information
Patent Citations
Automatic testing system based on grammatical rules and method
CN102360336A
Compiler defect positioning method based on reinforcement learning
CN112181420A