Quantum algorithms exploiting principles of quantum physics can be run on quantum computers and promise to offer considerable advantages over classical counterparts. Algorithms for quantum calculations are an increasingly prominent field in quantum information research. Notable examples include Shor’s factoring algorithm [1,2], Deutsch parallelism [3], Grover’s search algorithm [4], Quantum Fourier transform [5–7], and Quantum phase estimation [7,8]. Further advancements include the HHL algorithm for solving systems of linear equations [9–16], in which the crucial point is the conditional rotation of the state of the one-qubit ancilla. This operation, where inversion of the eigenvalue requires involving classical computing, is placed between the blocks of the direct and inverse phase estimation algorithms. HHL algorithm exponentiates a Hermitian matrix of the considered linear system, which can be performed via the Trotterization formula [17,18]. There is extensive literature devoted to improvement of this algorithm, especially its scaling and complexity [19–28]. However, the principal problem of eigenvalue inversion is not resolved via quantum methods yet. For this purpose, for instance, quantum state tomography can be used to obtain the eigenvalues of the exponent of Hermitian operator (the matrix of the linear system) [17]. Nevertheless, the HHL algorithm combining the phase estimation and classical inversion of eigenvalues is very popular in implementation of various algorithms of quantum computation, and it is widely applicable in quantum machine learning [11,29,30]. In particular, it is applied in the least-square linear-regression algorithms [31–33] working with large data sets, in quantum algorithms for singular-value decomposition [21,34], in algorithms for solving linear [35] and nonlinear [36] differential equations.
Another method of matrix inversion is presented in [37,38]. Both algorithms are based on the singular value decomposition and apply the function evaluation problem [21] to approximate the inverse of singular values by oddpolynomial, which requires introducing a special scale parameter whose value depends on the length of the interval including nonzero eigenvalues.
We also refer to algorithms for matrix manipulations based on the Trotterization method and the Baker-Champbell-Hausdorff formula for exponentiating matrices [39]. In [21], algebraic manipulations with matrices are implemented using unitary transformations that include the matrices as specific blocks (block-encoding model). A similar blockencoding approach was used in the algorithm of [40] to embed the inverse of the matrix of an algebraic system into the unitary transformation. In [41], an algorithm for matrix multiplication using binary encoding of matrix elements in the states of quantum systems followed by binary multiplication using Toffoli gates was proposed. Matrix multiplication over rings, including the multiplication of Boolean matrices, was studied in [42].
In our recent work [43], we presented alternative algorithms for matrix manipulations, such as sum and product of two matrices, calculation of determinant, matrix inversion, and solving linear systems, based on special unitary transformations of a quantum system. The main distinction of these algorithms from those mentioned above is the encoding of matrix elements into the pure states of specific (sub)systems as the probability amplitudes. However, the realization of those unitary operators in terms of the basic quantum operations was not explored in that study.
Later, in [44], we proposed the implementation of unitary transformations corresponding to vector inner product, matrix sum, and product using multi-qubit conditional operators (reducible to Toffoli gates) and Hadamard transformations, including the ancilla measurement for garbage removal. The depth of these algorithms increases logarithmically with the matrix dimension, or even remains independent of it (in the case of matrix sum), demonstrating the advantage of these algorithms compared to their classical counterparts.
In this paper, we propose quantum algorithms for determinant and inversion of matrix with their application to solving systems of linear equations. The principal part of these algorithms is the algorithm for computing the determinant of a square matrix. It is built on the ideas of Refs. [43,44]:
- -
Encode matrix elements into the states of certain quantum subsystems through probability amplitudes in such a way that all necessary multiplications of matrix elements are performed automatically and appear in the probability amplitudes of the initial state of the whole system;
- -
Use multiqubit control-SWAP and control-Z operators together with ancilla to organize proper signs of each term in the superposition state and collect them in a certain state of the considered system;
- -
Use ancilla measurement to remove the garbage and present the desirable result as a pure state of a certain quantum system. Normalization of this state is defined at the stage of ancilla measurement.
We follow the above steps to propose an algorithm for calculating the determinant of a square matrix whose N rows are encoded into the pure states of N separate quantum subsystems. Notice that creating such initial states is a special problem in itself, but we do not discuss this problem here. The state of the whole system results in all possible products of N elements from different rows. One only needs to select the proper products to calculate the determinant. We propose such an algorithm and construct the quantum circuit realizing it. While developing the algorithm, our focus is on the space and depths of its specific blocks. Essentially, our algorithm is a quantum realization of the cofactor expansion of the classical algorithm, whose depth exponentially increases with matrix dimension. However, the resulting depth of our algorithm is O(N log2 N) (or O(N2 log N) in the worst case) with space of O(N log N) qubits. The final step of this algorithm is the one-qubit ancilla measurement which removes all the garbage. The probability of obtaining the required result of the ancilla measurement (which is 1) diminishes exponentially as the matrix dimension N increases, necessitating an exponentially larger number of algorithm evaluations. This is a central problem that we plan to investigate in future work.
It is remarkable that the minor modification of the algorithm for calculating the determinant yields an alternative quantum algorithm for matrix inversion using its definition in terms of algebraic complements. We again use the row-wise encoding of the matrix elements into the state of a quantum system so that all necessary products of matrix elements appear automatically in the superposition state. One thus only needs to properly sort these products. The depth of this algorithm is O(N2 log N) (or O(N2 log2 N) in the worst case) and the space is O(N log N) qubits. Based on such inversion algorithm we develop an alternative algorithm for solving systems of linear algebraic equations. Thus, we do not use Trotterization methods and classical subroutine for inversion of eigenvalues. Therefore, the proposed algorithm is purely quantum in nature, and it gives an alternative avenue to the inversion algorithm considered in [9,17,28]. The estimations for the depth and space of this algorithm remain the same.
We also emphasize that, in comparison with inversion algorithms in [37,38], which approximately calculate the inverse eigenvalues, our algorithm constructs exact (up to the available accuracy of realization of quantum operations) inverse matrix without appealing to its eigenvalues which provides certain advantages of the matrix-encoding approach.
Regarding the question of algorithmic complexity, we remark that the specialized classical techniques of matrix manipulations facilitate a reduction in the depth of the algorithms for determinant calculation to O(log2 N) [45,46] with the size O(N2.496). In our study, we employ the Leibniz formula as a definition for the matrix determinant and define the inverse matrix in terms of the algebraic complements. Our quantum algorithms based on these definitions demonstrate clear advantages over their classical counterparts. It is plausible that the adaptation of specific classical algorithms for matrix operations into their quantum equivalents could yield further enhancements in the efficiency and complexity of quantum algorithms designed for calculating determinants and inverse matrices. However, this particular aspect is not addressed in the present paper. The primary contribution of our work lies in promoting the representation of matrix elements as probability amplitudes of pure states, and we elucidate the benefits of this representation in relation to standard algorithms for matrix manipulations.
The paper is organized as follows. The algorithm for calculating the matrix determinant is proposed in Section 2. This algorithm is taken as a basis for the algorithm calculating the inverse matrix presented in Section 3. Based on those results, the algorithm for solving systems of linear algebraic equations is proposed in Section 4. The main results are discussed in Section 6. The special subroutine for the controlled measurement is presented in Section 5. Appendix A contains the modified algorithms for matrix multiplication and examples of realization of the algorithms developed in this paper.
To calculate the determinant of N × N matrix à (we assume N = 2n) with elements ajk(j, k = 0, 1, …, N – 1),
These constraints can be circumvented by incorporating additional terms with different probability amplitudes into the states |Ψi〉, but this issue will be addressed in future study. The whole system is the union of the subsystems,
Thus, our purpose is to exchange the states of subsystems Sj(j = 0, …, N – 1) so that the state of the subsystem Sk becomes |k〉Sk in each term of the first part of superposition state (5). For that we introduce the projectors
For instance, in Figure 2 for the case N = 4, there are three ancillae A0, A1, A2. Ancilla A2has to encode either only one state |2〉S3 or nothing, therefore one qubit is enough: we can encode |2〉S3 by the state |1〉A2 of ancilla A2. Next, A1 has to encode two states |1〉S2 and |1〉S2 or nothing. Two qubits are enough for that: |1〉S2 → |1〉A1, |1〉S3 → |2〉A1. Similarly, A0 requires 2 qubits as well: |0〉S1 → |1〉A0, |0〉S2 → |2〉A0, |0〉S3 → |3〉A0. Thus, we have the map
Using the projector
It is hard to derive the explicit formula for Zk(j) in general case, but it can be simply done in any particular case. For instance, in the above example of the transformation |0〉S2 → |2〉A0 we can set Z0 (2) = I ⊗σ(z). Thus, the operator VSjAk acts as follows. If the state of Sj is |k〉Sj, then the state of Ak becomes
To arrange the exchange of states between Sj and Sk, we introduce the projectors
Here, the operator SWAPSk,Sj swaps the states of the subsystems Sk and Sj by swapping states of appropriate qubits of these subsystems. Thus, the operator USkSj swaps the states of Sk and Sj if only the state of Ak is
The circuits for the operators VSjAk and

V and U operators for (a) the general case and (b) the 4 × 4-matrix case. In the latter case, ñ0 = ñ1 = 2, ñ2 = 1, ñk < n. Hence, A1 is a two-qubit ancilla.
The depth of each operator VSjAk is O(n) (due to the projectors
Applying the operators
There are Ñ Hadamard operators in HA,
Applying the operator
Here we introduce the determinant det(Ã) represented by the Leibniz formula,
The depth of W(1) is O(Nn2). Next, we can set states of all Sj in the first term of
We note that the operator HA in
Now we measure the ancilla B with the output |1〉B. The probability of this result of measurement is 2−Ñ|det(Ã)|2. After the measurement we get the following state:
The particular circuit for calculating the 4 × 4 determinant using the proposed algorithm is given in Figure 2. The circuit for the determinant of N × N matrix includes N log N qubits of the subsystem S, Ñ = O(N log N) qubits of subsystem A, and one qubit of ancilla B, therefore the space of this circuit is O(N log N) qubits.

Quantum circuit illustrating computation of determinant of a 4 × 4 matrix, see notations in Figure 1. We omit superscript (D) in |Φk〉(k = 1, …,4) and |Ψout〉 for brevity. Operators HA and XS can be applied in parallel since they affect different qubits. The lower circuit is the notation for multi-qubit controlled σ(x) operation.
We remark here that the minor modification of the algorithm for calculating the determinant of a square N × N matrix in Section 2 allows to calculate the inverse of a square (N – 1) × (N – 1) matrix. Moreover, this modification leads to a minor increase in the depth of the algorithm which becomes O(N2 log N) instead of O(N log2 N) in the algorithm for calculating the determinant. These facts motivate working out the quantum inversion algorithm based on the definition of the inverse matrix in terms of algebraic complements, although quantum analogs of other algorithms of matrix inversion might be more promising.
We impose a special structure on the elements in the first row and first column of the matrix à given in (1):
The algorithm discussed below allows to invert the (N – 1) × (N – 1) matrix A (rather than the matrix à itself) which is embedded into Ã,
According to the standard definition, the element
From another point of view, the minor Mij appears among the terms included in det(Ã). In fact, one of the terms in this determinant is following (remove the (i + 1)th row and 1st column from Ã; notice that (i + 1)th row contains the elements q, ai1, ai3, …, ai(N–1)):
In turn, this term includes the following term (remove the 1st row and jth column from the determinant (26); the jth column contains the elements 1, a1j, a2j, …, a(N–1)j):
Thus, each element
The circuit for the algorithm is presented in Figure 3. According to (2), the pure state |Φ0〉 of the whole system is given in (4) with normalization (3) and constraints (22) for the elements in the first row and first column of the matrix Ã, see (23). From this state, we select only those terms that compose the determinant of à and therefore contain the terms that appear in the resulting inverse matrix

(Color online) (a) Structure of quantum circuit for matrix inversion (ancillae are shown in green). (b) The structure of the block P.
However, not all terms in the first part of |Φ0〉 are needed in our algorithm. We have to keep only those of them that include the 1st power of the factor q, (see constraints (22)), i.e., exactly one state |0〉Sj, 1 ≤ j ≤ N – 1, can appear in the product |k1〉S1 … |kN–1〉SN–1. We also have to associate each of the probability amplitudes in (5) with the appropriate element
To select the terms that compose
The idea is that this projector allows labeling the 1st and (j + 1)th (j = 0, …, N – 1) columns and the 1st and (i + 1)th (0 ≤ i ≤ N – 1) rows in the matrix Ã(23). As a result, we select those terms from the superposition state whose probability amplitudes compose Mij.
Now we can construct the controlled operator
This operator conditionally applies
Next, applying the operator
Finally, applying
The depth of the operator
The state |Ψout〉 encodes A−1 in the normalized form, i.e., 〈Ψout|Ψout〉 = 1, while the state
We note that this depth is obtained under the assumption that the depth of the operator
Having constructed the inverse matrix A−1 encoded into the state of the subsystem R ∪ C, we obtain a tool for solving a system of linear algebraic equations represented in the matrix form, Ax = b, whose solution reads x = A−1b.
Therefore, we have to apply the multiplication algorithm proposed in Ref. [44] (see also Appendix A.1, for its modified version) to the matrices A−1 and b. The structure of the circuit is shown in Figure 4a. A more detailed circuit is presented in Figure 4b.

(Color online) (a) Structure of quantum circuit for solving a system of linear algebraic equations. Both blocks share the same ancillae (not shown in figure). (b) Circuit for quantum algorithm solving a system of linear algebraic equations Ax = b. Circuit of matrix inversion (up to the operator
Notice that we can use the same ancillae in both blocks of this circuit (ancillae are not shown in Figure 4a). Moreover, the subsystem S encoding the matrix A can be used as ancilla in further calculations.
In this case we do not measure ancillae B (unlike Section 3.2) and thus start with the state |Φ4〉 given in (33). The vector b is encoded as
The zero value for b0 is prescribed by the encoding of A−1 in (36), where the 0-states of R and C are not used. Out of system S ∪ A (which forms an ancilla in Figure 4b), we take the one-qubit ancilla
Now we refer to the Appendix, Section A.1, where the matrix multiplication algorithm is described. Applying the result of this Appendix we identify K = N, M = 1 and replace the subsystems R1, C1, R2 with R, C, b, while the subsystem C2 is not used because the second matrix in our multiplication is just a column. Thus,
Applying the operator
Performing measurement over
In all algorithms presented above the weak point is the small probability of success in the measurement of the ancilla state. In fact, this probability is ∼ 2−O(N log N) in all cases, in other words, it exponentially decreases with an increase in the dimensionality N of the matrix à (1). To smooth this effect we suggest to replace the usual ancilla measurement by the controlled measurement [48], which can be presented as a special subroutine.
We call B the one-qubit ancilla whose state we have to measure and write the state of the whole system as
Now we introduce the controlled measurement
In this way we avoid necessity of performing multiple runs of the algorithm to get the needed result of the ancilla measurement. However, we also lose the possibility to define the normalization (〈E|E〉)1/2, which might be important information. For instance, in the algorithm for determinant calculation this normalization equals the absolute value of the matrix determinant, which is the half of useful information (another half is the phase). But in the other two algorithms this normalization doesn’t include the principal information. So, getting information about that normalization is the problem to be resolved. Another problem is the practical realization of the above controlled measurement that is also beyond the scope of this paper. The circuit for the subroutine of controlled measurement is shown in Figure 5.

(Color online) Replacement of ordinary measurement of ancilla state (left circuit) with the subroutine of controlled measurement (right circuit).
We propose two quantum algorithms for matrix manipulation, which are calculating the determinant and matrix inversion. Both are based on special row-wise encoding of the matrix elements in the pure states of quantum subsystems.
The algorithm for computing the determinant of a square matrix is discussed in detail. It uses multiqubit control-SWAP and control-Z operations. The depth of this algorithm is O(N log2 N). This algorithm utilized the subroutine realizing the completely antisymmetric tensor ε (operator
It is remarkable that the (N – 1) × (N – 1)-matrix inversion algorithm is essentially the algorithm for calculating the determinant of the N × N matrix obtained by replacing the first row by the row of
As an application of inversion we develop the algorithm for solving systems of linear algebraic equations. This algorithm consists of two blocks: matrix inversion and matrix multiplication. At that, the matrix inversion is the most resource-consuming block which finally determines the depth of the algorithm (which is O(N2 log N)), and probability of the required ancilla state after measurement, ∼ 2−O(N log N). We have to note that the depths of the algorithms for calculating the determinant and matrix inverse were obtained under the assumption that the U-, V- operators in (11) can be applied in parallel. Otherwise we have to change the depths O(N log2 N) and O(N2 log N) to O(N2 log 2N) in both cases.
We emphasize that the matrix-multiplication algorithm is modified in comparison with that proposed in [44]. The detailed description of the modified version is presented in Appendix, Section A.1.
One notable aspect is the use of ancillae to gather all the garbage generated during the calculation, which can then be discarded with a measurement of a one-qubit ancilla. The weak point of algorithms is the probability of obtaining the required state of ancilla after measuring. This probability is ∼ 2−O(N log N) in all three algorithms.
Combining the algorithm of matrix inversion with the algorithm of matrix multiplication we demonstrate the usage of two types of matrix encoding into the state of a quantum system. The first one is the row-wise encoding used in the input of both algorithm for calculating the determinant of the N × N matrix and matrix inversion algorithm (for simplicity, we assume that N = 2n). It requires N subsystems of log N qubits each, and the state of ith subsystem encodes the elements of the ith row of the matrix. The result of matrix inversion is stored in the two subsystems of log N qubits each. These subsystems enumerate rows and columns, respectively. This form of encoding of the inverse matrix allows to perform its multiplication with column b properly encoded in the state of log N-qubit subsystem to obtain the solution x of the linear system. This solution is a column whose elements are encoded into the state of a log N-qubit subsystem R.