Some Size and Structure Metrics for Quantum Software
Abstract
Quantum software plays a critical role in exploiting the full potential of quantum computing systems. As a result, it is drawing increasing attention recently. As research in quantum programming reaches maturity with a number of active research products, software metric researchers need to focus on this new paradigm to evaluate it rigorously and quantitatively. As the first step, this paper proposes some basic size and structure measures for assessing the complexity of quantum software, which are specifically designed to quantify the quantum attributes in a quantum program. The proposed measures can be used to evaluating the complexity of quantum software from different viewpoints.
1 Introduction
Software metrics aim to measure the inherent complexity of software systems to predict the overall project cost and evaluate the quality and effectiveness of the design. Software metrics have many applications in software engineering tasks such as testing [hetzel1993making], maintenance [gilb1988principles], reengineering [arnold1992software], reuse [poulin1996measuring], and project management [fenton2014software, zuse2013framework]. Research for software measurement must adapt to the emergence of new software development methods, and metrics for new languages and design paradigms must be defined based on models relevant to these new paradigms [bieman1996metric].
Quantum programming is the process of designing and building executable quantum computer programs to achieve a particular computing result [miszczak2012high, ying2016foundations]. A number of quantum programming approaches are available, for instance, Scaffold [abhari2012scaffold], Qiskit [ibm2017qiskit], Q# [svore2018q], ProjectQ [projectq2017projectq], and Quipper[green2013quipper]. A quantum program consists of blocks of code, each of which contains classical and quantum components. Quantum operations can be divided into unitary operations (reversible and preserve the norm of the operands), and non-unitary operations (not reversible and have probabilistic implementations). A quantum program executed on a quantum computer uses a quantum register of qubits to perform quantum operations and a classical register of classic bits to record the measurements of the qubits’ states and apply quantum operators conditionally [cross2017open]. Therefore, a typical quantum program usually consists of two types of instructions. One is called classical instructions that operate on the state of classical bits and apply conditional statements. Another is called quantum instructions that operate on the state of qubits and measure the qubit values.
As research in quantum programming reaches maturity with a number of active research and practical products, software metrics researchers need to focus on this new paradigm to evaluate it in a rigorous and quantitative fashion. However, although a large number of software metrics have been proposed for classical software systems [bieman1996metric, zhao1998assessing, fenton2014software, halstead1977elements, mccabe1976complexity, henry1981software, zuse2013framework], no metric has been proposed for quantum software. Further, due to the specific features of quantum software, existing models and abstractions for classical software cannot be applied to quantum software straightforwardly.
As the first step towards evaluating quantum software, this paper proposes some basic size and structure measures for assessing the complexity of quantum software at different levels of abstraction, which are specifically designed to quantify the quantum attributes in quantum software. The proposed measures can be used to measure the complexity of quantum software from different viewpoints.
The rest of this paper is organized as follows. Section 2 reviews the fundamental terminology in quantum computing. Section 3 proposes some basic measures for quantum software size. Section 4 proposes some basic measures for quantum software structure. Related works are discussed in Section 5, and concluding remarks are given in Section 6.
2 Background
This section briefly introduces some basics of quantum mechanics, which form the basis of quantum computing. More detailed reading material can be found in the book by Nielsen & Chuang [nielsen2002quantum].
2.1 Quantum Bit (Qubit)
A classical bit is a binary unit of information used in classical computation. It can take two possible values, 0 or 1. A quantum bit (or qubit) is different from the classical bit in that its state is theoretically represented by a linear combination of two bases in the quantum state space (represented by a column vector of length 2). We can define two qubits and , which can be described as
and
Qubits and are the computational basis state of the qubit. In other words, they are a set of the basis of quantum state space.
Any qubit can be expressed as a linear combination of two basis as , where and are complex numbers, and . This restriction is also called normalization conditions.
2.2 Quantum Gate and Circuit
Just as a logic gate in a digital circuit that can modify the state of a bit, a quantum gate can change the state of a qubit. A quantum gate can have only one input and one output (transition of a single quantum state), or it can have multiple inputs and multiple outputs (transition of multiple quantum states). The number of inputs and outputs should be equal because the operators need to be reversible, which means no information can be lost in quantum computing.
2.2.1 NOT Gate
The NOT gate works on a single qubit. It can exchange the coefficients of two basis vectors:
The quantum NOT gate is an extension of the NOT gate in classical digital circuits.
A single input-output quantum gate can be represented by a matrix. The state of a quantum state after passing through the quantum gate is determined by the value of the quantum state vector left-multiplied by the quantum gate matrix. The quantum gate matrix corresponding to the NOT gate is
Therefore, the result of a qubit passing a NOT gate is
2.2.2 Hadamard Gate
The Hadamard gate also works on a single qubit, which can decompose existing quantum states according to its coefficients:
Although Hadamard gate is not directly related to the AND and OR gates in classical digital circuits, it has important applications in many quantum computing algorithms. Interested readers can try to prove that after applying the Hadamard gate twice in a row, the quantum state will return to its original state. This behavior is consistent with the NOT gate.
2.2.3 Controlled NOT Gate
Computer programs are full of conditional judgment statements: if so, what to do, otherwise, do something else. In quantum computing, we also expect that the state of one qubit can be changed by another qubit, which requires a quantum gate with multiple inputs and outputs. The following is the controlled-NOT gate (CNOT gate). It has two inputs and two outputs. If the input and output are taken as a whole, this state can be expressed by
where , , , are column vectors of length 4, which can be generated by concatenating and . This state also needs to satisfy the normalization conditions, that is
The CNOT gate is two-qubit operation, where the first qubit is usually referred to as the control qubit and the second qubit as the target qubit. When the control qubit is in state —0, it leaves the target qubit unchanged, and when the control qubit is in state —1, it leaves the control qubit unchanged and performs a Pauli-X gate on the target qubit. It can be expressed in mathematical formulas as follows:
2.2.4 Quantum Circuit
Quantum circuits, also known as quantum logic circuits, are the most commonly used general-purpose quantum computing models, which represent circuits that operate on qubits under an abstract concept. A quantum circuit is a collection of interconnected quantum gates. The actual structure of quantum circuits, the number and the type of gates, and the interconnection scheme, are all determined by the unitary transformation , performed by the circuit. The result of a quantum circuit can be read out through quantum measurements.
2.3 Quantum Programming
Quantum programming is the process of designing and building executable quantum computer programs to achieve a particular computing result [miszczak2012high, ying2016foundations]. A quantum program consists of blocks of code, each of which contains classical and quantum components. Quantum operations can be divided into unitary operations (reversible and preserve the norm of the operands), and non-unitary operations (not reversible and have probabilistic implementations). A quantum program executed on a quantum computer uses a quantum register of qubits to perform quantum operations and a classical register of classic bits to record the measurements of the qubits’ states and apply quantum operators conditionally [cross2017open]. Therefore, a typical quantum program usually consists of two types of instructions. One is called classical instructions that operate on the state of classical bits and apply conditional statements. Another is called quantum instructions that operate on the state of qubits and measure the qubit values.
3 Basic Measures for Quantum Software Size
Software size represents one of the most significant internal attributes of a software product [fenton2014software]. Many software effort estimation models [albrecht1983software, jones2008applied, boehm1995cost, boehm1984software] use some size measure as their main effort driver to estimate the software development effort. In this section, we introduce some measures that can be used to measure the size of quantum software from different levels: code, design, and specification.
3.1 Code Size
Program code is an integral component of the software. Such code includes source code, intermediate code, byte code, and even executable code. We will discuss how some widely used size measures for classical software can be extended to measure the code size of quantum software.
3.1.1 Line-of-Code (LOC)
As we know, the most commonly used measure of source code program length is the number of lines of code (LOC). LOC measure can be used to predict the quality and reliability of software productions. Similarly, for quantum software, we may also consider some LOC measures to measure the internal attribute of the software, specifically related to the quantum attributes. Let be a quantum program, some basic LOC measures for can be defined as follows:
-
•
The number of lines of code in
-
•
The number of lines of code concerning the quantum gate operations in
As we observed, all the measures defined above are general measures that can be used to measure the total size complexity of a quantum program. We can also define some measures to especially quantify the size complexity related to quantum attributes of as follows.
-
•
Number of lines of code related to the quantum operations in
-
•
Number of lines of code related to the quantum gate operations in
-
•
Number of lines of code related to the quantum measurements in
We can even consider the number of qubits or gates used in to obtain the following size measures:
-
•
Number of qubits used in
-
•
Number of gates used in
3.1.2 Halstead’s Software Science
Halstead introduces some size measures to measure the complexity of a classical program. According to Halstead, ”a computer program that implements an algorithm is considered to be a collection of tokens that can be divided into operators or operands.” Halstead observes that software measures should reflect how algorithms are implemented in different programming languages and independent of the platform and language used. These measures should be computable statically from the source code of the program.
Let be a program which is a collection of tokens, classified as either operators or operands, then some basic numbers can be defined as follows:
-
•
: the number of unique operators in
-
•
: the number of unique operands in
-
•
: the total occurrences of operators in
-
•
: the total occurrences of operands in
Based on these numbers, several measures could be defined further to quantity the program length, the potential minimum volume for an algorithm, the actual volume (number of bits to specify a program), the program level, the language level, and other features such as development effort, development time, and even the projected number of faults in the software.
-
•
The length of is defined as
-
•
The vocabulary of is defined as
-
•
The estimated length of can be denoted as
-
•
The volume of is defined as
-
•
The difficulty of is defined as
-
•
The effort of is defined as
A typical quantum program usually consists of two types of instructions: classical instructions that operate on the state of classical bits and apply conditional statements, and quantum instructions that operate on the state of qubits and measure the qubit values.
Let be a quantum program, we can define some basic numbers related to the quantum operations in as follows:
-
•
: the number of unique quantum gate operators in
-
•
: the number of unique quantum gate operands in
-
•
: the total occurrences of quantum gate operators in
-
•
: the total occurrences of quantum gate operands in
Based on these numbers, we can extend Halstead’s measures of classical software to the domain of quantum software by counting the total numbers of , which consists of classical and quantum statements, respectively, and have the following extended measures.
-
•
The length of is defined as
-
•
The vocabulary of is defined as
-
•
The estimated length of can be estimated from
-
•
The volume of may be defined as
-
•
The difficulty of is defined as
-
•
The effort of is defined as
3.2 Design Size
Quantum software design is a process to transform user requirements into a suitable form that helps the programmer in quantum software implementation (coding). As classical software design [75], a quantum software design may also involve two stages: architectural design and detailed design. Like the measure code size mentioned above, we can measure the size of a quantum software design at the architectural level.
3.2.1 Architectural Design Size
Architectural design defines a collection of quantum software components, their functions, and their interactions (interfaces) to establish a framework for developing a quantum software system. The software architecture of the system defines its high-level structure, revealing its overall organization as a set of interacting components [perry1992foundations, mary1996software]. A well-defined architecture allows an engineer to reason about system properties at a high level of abstraction. We can measure the size of a quantum architectural design based on formal architectural specifications and architectural patterns.
(1) Formal Architectural Specification. Architectural description languages (ADLs) [clements1996survey] are formal languages representing the architecture of a software system. Using ADLs, software architects can specify the system’s functionality using components and the interaction between components using connectors.
Like classical ADLs, we can use a quantum architectural description language (qADL), an extension of a classical ADL to formally specify the architectures of a quantum software system, to support the architectural-level design of quantum systems. A qADL can specify a quantum system’s architecture with those mechanisms, including specifications of classical components with interfaces and connections between interfaces (already provided in classical ADLs), specifications of quantum components, and specifications of connections between classical and quantum components.
Let be the formal architectural specification of a quantum software architecture, which consists of a group of components and the connectors between components, and we can define some general architectural size measures for as follows:
-
•
Number of all components and connectors in
-
•
Number of all components in
-
•
Number of all connectors in
We can also derive some size measures that specifically quantify the size of the quantum components or connectors related to the quantum aspects in .
-
•
Number of all quantum components in
-
•
Number of all connectors between quantum components in
-
•
Number of all connectors between a quantum component and a classical component in
(2) Architectural Patterns. The architectural pattern provides a skeleton or template for the overall software architecture or high-level design of software applications [gomaa2011software], which is used in various software applications [mary1996software].
Like classical architectural patterns, we can also identify some quantum architectural patterns for supporting quantum software design. Based on these quantum architectural patterns, we can define some size measures at the architectural design level as follows:
-
•
Number of different architectural patterns used in a quantum architectural design
-
•
Number of architectural pattern realizations for each architectural pattern type
Moreover, we can also define some size measures, especially to measure the quantum features at the architectural design level.
-
•
Number of different quantum architectural patterns used in a quantum architectural design
-
•
Number of quantum architectural pattern realizations for each quantum architectural pattern type
3.2.2 Detailed Design Size
Detailed design refines and expends the architectural design to describe the internals of the quantum software components (the algorithms, processing logic, data structures, and data definitions). Similar to classical design patterns, we can also identify some quantum design patterns for quantum software.
Let be a design for a quantum software system, and we can define some general size measures in terms of design patterns in
-
•
Number of different design patterns used in
-
•
Number of design pattern realizations for each pattern type in
Moreover, we can also define some size measures that are specific to quantify the quantum attributes in terms of quantum design patterns in as follows:
-
•
Number of different quantum design patterns used in
-
•
Number of quantum design pattern realizations for each pattern type in
3.3 Specification Size
The Unified Modeling Language (UML) [uml2009version] is a general-purpose, well-known modeling language in the field of classical software engineering. It provides a standard way to visualize the design of the classical software development life cycle. Recently, Pérez-Delgado and Perez-Gonzalez [Perez-Delgado2020quantum] present an extension of UML, called Q-UML, to model quantum software systems. The extension covers two types of UML diagrams: class diagram and sequence diagram. In this work, they claim that, in addition to the classical elements in a classical UML, the Q-UML should contain those elements related to the quantum aspects, such as quantum classes, quantum elements (quantum variables and quantum operations), quantum supremacy, quantum aggregation, and quantum communications.
Similar to the classical UML, Q-UML can serve as a base for defining some size measures for a quantum software system at the specification level. An example of some measures, which can be used to quantify the quantum aspects of the system, can be defined as follows.
-
•
Number of quantum classes (objects)
-
•
Number of quantum elements (quantum variables or quantum operations)
-
•
Number of quantum interfaces
-
•
Number of quantum attributes
-
•
Number of all quantum methods
4 Basic Measures for Quantum Software Structure
Structural analysis of programs is an essential component of software development and software evaluation efforts. Structure metrics try to take into account the complexity of individual modules and the interactions between modules in a product or system and quantify such interactions. Many approaches in structure metrics have been proposed. Some good examples include McCabe’s cyclomatic complexity measure [mccabe1976complexity] and the information flow measure by Henry and Kafura [henry1981software]. In this section, we discuss how these classical software measures can be extended to evaluate quantum software.
4.1 McCabe’s Complexity Measure
A well-known measure for measuring the complexity of a classical program’s control structure is the cyclomatic complexity measure by McCabe [mccabe1976complexity].
For a program , the cyclomatic complexity measure of is defined as the number of linearly independent paths through a control-flow graph (CFG) of and can be computed as follows:
where is the CFG of to be measured, is the number of nodes of , each representing a statement in , and is the number of edges of , each presenting the flow of control from one statement to another in .
Similarly, we can measure the complexity of a quantum program’s control structure by defining an extended cyclomatic complexity measure for quantum software. The definition of such a measure can be based on the quantum control flow graph (QCFG), which is an extension of the classical CFG to represent a quantum program.
Let be the QCFG of a quantum program to be measured, the cyclomatic complexity measure of can be defined as follows.
where is the number of nodes of , and is the number of edges of .
Similar to classical CFG, each node represents either a classical (or quantum related) statement, and each represents the flow of control from one statement to another in .
The QCFG of can be constructed based on its AST, which can be derived through the static analysis on .
4.2 Henry and Kafura’s information flow Measure
Perhaps the most common design structure measures are known as information flow measures, which deal with the complexity of a system by observing the flow of information among system components or modules. Among these measures, one well-known approach is Henry and Kafura’s information flow measure [henry1981software], which measures the total level of information flow between individual modules and the rest of a system. This measure considers the complexity of a software module, which could be affected by the following two factors.
-
•
The complexity of the module code itself.
-
•
The complexity due to the module’s connections to its environment.
The effect of the first factor has been included through the LOC (Line Of Code) measure. For the quantification of the second factor, Henry and Kafura have defined two terms: fan-in and fan-out.
-
•
The fan-in of a module is the number of local flows into , plus the number of data structures from which retrieves information.
-
•
The fan-out of a module is the number of local flows from , plus the number of data structures which updates.
Based on these concepts, Henry and Kafura define their information flow measure value as the module length multiplied by the square of fan-in multiplied by fan-out.
where is the number of programming language statements in , and and are the fan-in and fan-out of , respectively.
For a quantum software system, it is usually composed of classical and quantum modules. These modules are not independent, and therefore may have some interactions among them, which may lead to some information flows, from a classical/quantum module to a classical/quantum module. To measure the complexity of a quantum system by observing the flow of information among the system modules, we can extend Henry and Kafura’s information flow measure to the domain of quantum systems.
Let be a classical/quantum module of a quantum system, be the number of quantum programming language statements in , and and be the fan-in and fan-out of , respectively, we can define a structure measure based on the information flow to measure the complexity as a module of fan-in and fan-out in the system as follows.
5 Related Work
Although a large body of research in software metrics have been proposed for classical software [bieman1996metric, zhao1998assessing, fenton2014software, halstead1977elements, mccabe1976complexity, henry1981software], no metric has been proposed for quantum software. To the best of our knowledge, this is the first work to propose some measures for quantify attributes of quantum software from various viewpoints.
As far as we know, the only work which considers quantum software measurement is proposed by Salm et al. [salm2020criterion], who introduce a measure to measure the performance of quantum software systems.
Piattini et al. [PiattiniPPHSHGP2020] present the Talavera Manifesto for quantum software engineering and programming, which collects some principles and commitments about the field of quantum software engineering and programming. Among these principles and commitments, one is ”QSE assures the quality of quantum software,” which mentioned that new measures for quantum software and quantum processes should be developed. However, they did not define any concrete measure regarding quantum software measurement in the manifesto.
Sicilia et al. [sicilia2020source] present a preliminary study on the structure of the source code of quantum software, using initially the same metrics typically used in classical software. Their study focuses on the module structure and use of quantum gates in the libraries of Microsoft’s quantum development platform QDK (Quantum Development Kit) that uses a specific language Q#. However, they have not proposed any software measure that can be used specifically to measure the complexity of the quantum software.
6 Concluding Remarks
This paper has proposed some basic measures for quantum software design, which mainly focus on measuring the size and structure of quantum software. Some of these measures are the extensions of their classical counterparts, while the others are specifically designed to quantify the quantum features in quantum software. These measures are defined at different levels of abstraction to represent various size and structure attributes in quantum software explicitly. The proposed measures can be used to measure the complexity of quantum software from various viewpoints.
Acknowledgments
We are grateful to the anonymous reviewers for their suggestions to improve the paper and to Hidenori Ooka for his valuable discussions.