# On the Definition of Quantum Programming Modules
## 3 Towards Modular Quantum Programming
### Quantum Modules  
Modules are considered, under the circuit representation, as \"regular\" gates that take qubit lines as inputs, apply the operations included inside the module to the joint state represented by the input qubits, and have the same number of qubits lines as output. Each operation (gate) corresponds to applying a concrete matrix to the quantum state, so the gates that make the module up are written from left to right. Figure 4 shows a simple quantum module with four (input and output) qubits (a), and a program composed by two sequential modules (b). It is also possible to apply a module to a subset of the total program qubits, as shown in Figure 5a, when there is no entanglement between the involved qubits, the equivalence with the application of identity operators to the other qubits, as shown in Figure 5b. This is because \((I\bigotimes I\bigotimes U)(|\psi\rangle\bigotimes|\theta\rangle\bigotimes| \xi\rangle)\), where \(\bigotimes\) is the tensor product, is the same as \(|\psi\rangle\bigotimes|\theta\rangle\bigotimes U|\xi\rangle\) (again, when there is no entanglement among input qubits). When the qubits are entangled, as in Figure 5c, the state cannot be factored as a product of the individual qubit states. Tensor product is the machinery used to build quantum systems from other existing quantum systems. See [18] for more details on the mathematical foundations. Figure 3: A modular approximation for quantum computing. Given a set of qubits at input, they are processed according to a given circuit, which is parametrized by many entry points where different \(U\) modules can be plugged into. It should be noted that in quantum computing, not every unitary matrix is allowed for implementing quantum systems. Those gates (as unitary matrices) are only valid coming from the combination of gates from the basic set, equivalent to the basic instruction set of a CPU. This set is dependent on the selected quantum hardware for executing the quantum program. Considering all the above, the definition of module can be as follows. A quantum module is the basic reuse unit in quantum programming, which provides a specific functionality and has an interface given by the same number of input and output qubits. A module has to satisfy the following rules, summarized in Table 1:  
1. Any quantum gate belonging to the universal gate set is considered itself a quantum module;
2. A quantum module has to be unitary and reversible. This is because every operation on a state must keep the sum of probabilities of all possible outcomes at exactly 1, and it must be possible to obtain the previous state by using only the output state, without additional information. In order to be reversible, no measurement of any of the input qubits can be made inside the module. In addition, the designer of a module \(U\) may need to include additional input qubits in order to allow the development of the module \(U^{\dagger}\) that performs the inverse operation: reconstructing the original input qubits that entered \(U\). This \(U^{\dagger}\) module is computed as the Hermitian matrix (transposed, complex conjugate) of module \(U\);
3. In case the module needs scratch qubits to store the result of temporary computations, it has to make sure that they are returned back to the value they had before entering the module. This can be a hard process in case the scratch qubits got entangled inside the module. Scratch qubits that are entangled with data qubits alter the state of the data qubits if they are reset or measured. Reclaiming scratch (ancilla) qubits is the process of returning them to their original state for future reuse [28]. This facilitates the reuse of the scratch qubits on subsequent computations, and, at the same time, the modification of the scratch qubits downstream does not affect the output qubits in case they are entangled;
4. A quantum module can be replaced indistinguishably by any other equivalent module which, given the same inputs, produces the same outputs. In other words, it  
Figure 4: (**a**) Module a simple gate.