Have a personal or library account? Click to login
Quantum Algorithms for Matrix Operations of Row Addition, Row Swapping, Trace Calculation and Transpose Cover

Quantum Algorithms for Matrix Operations of Row Addition, Row Swapping, Trace Calculation and Transpose

Open Access
|Mar 2026

Full Article

1.
Introduction

Quantum computations offer exponential speedups over their classical counterparts by leveraging the principles of quantum mechanics. In 1980 Benioff and Manin [1] proposed the concept of quantum computing, and Feynman [2] introduced the theoretical framework of a quantum computer in 1982. In 1985 Deutsch [3] proposed the quantum parallel algorithm, showcasing the significant computational advantages of quantum computing over classical methods.

In 1994 Shor [4,5] presented a quantum algorithm for prime factorization, drawing widespread attention to the advantages of quantum computing in solving certain problems. Grover [6] followed in 1996 with a quantum search algorithm, further demonstrating quantum computing’s potential in data search applications. In 2009 the HHL algorithm [7,8] was developed for solving linear systems of equations, greatly advancing the quantum algorithm researches. Several renowned quantum operations, such as quantum Fourier transform [911] and quantum phase estimation [11,12], have significantly contributed to the field.

Matrix operations serve as the core computational power support for quantum algorithms. The efficient quantum implementation of its basic operations (including row transformation, transposition, and trace calculation, etc.) holds significant theoretical and practical value. In quantum chemistry simulations [13], the elementary row transformation of matrices can rapidly simplify the Hamiltonian matrix, reducing the time complexity of solving molecular energy spectra from the polynomial level of classical algorithms to the logarithmic level. Matrix Transpose operation enables the efficient analysis of molecular symmetry, while the calculation of the matrix trace can directly yield key physical quantities such as the number of electrons. In quantum cryptography [14], elementary row transformation can optimize the encoding of quantum states to enhance security. Matrix Transpose can achieve efficient information routing in quantum communication networks, and the matrix trace can be used for the integrity detection of quantum states. In the field of quantum machine learning [15], elementary row transformation can accelerate the feature extraction of Quantum Support Vector Machines (QSVMs). Matrix Transpose can improve the backpropagation efficiency of quantum neural networks, and the matrix trace operation can quickly calculate kernel functions to evaluate model performance. Given the wide-ranging influence of matrix operations in interdisciplinary fields, exploring efficient quantum computing methods to solve matrix-related algebraic problems has become a current research hotspot.

In recent years, researchers have proposed several quantum algorithms for matrix operations. For example, the Trotter approximation formula [16] has been utilized to simulate quantum circuits, addressing complex operations such as matrix addition, multiplication and Kronecker products. Quantum algorithms for computing vector inner products [1722] have also been developed. These algorithms leverage the superposition and entanglement of qubits and have potential applications in fields like machine learning and data processing. Based on a “sender-receiver” quantum computing model, in 2022 Wen-Tao Qi et al. [19] proposed a class of quantum algorithms for matrix operations, including matrix-vector multiplication, matrix-matrix multiplication, matrix addition, determinant computation and matrix inversion. In 2023 A.I. Zenchuk et al. [20] further developed the idea of using specific types of unitary transformations to implement linear algebra protocols according to multi-qubit Toffoli gates and the simplest single-qubit operations. Auxiliary measurements were employed to remove redundant information and extract desired results. Building on this work, in 2024 they [21] also introduced a method to encode the N rows of a matrix into pure states of N independent quantum subsystems. By selecting appropriate unitary operators, the determinant can be computed efficiently. Based on this, they [22] proposed a variational quantum singular value decomposition method based on matrix encoding, which reduced the number of measurements through controlled measurement. Furthermore, in 2025 they [23] discussed the method of encording unnormalized matrices, and the system encoding the matrix was extended adding one more probability amplitude that satisfy the normalization condition.

However, these quantum algorithms for basic matrix operations still face challenges such as high computational complexity and low success probabilities, which limit their broader applicability.

The elementary row transformations of matrices play significant roles in solving linear systems of equations, determining matrix rank, finding invertible matrices and simplifying determinant computations, with wide-ranging value in both theoretical research and practical applications. In quantum computing, elementary row transformations can simplify matrices for efficient linear equation solving, provide theoretical support for matrix decomposition, advance large-scale matrix operations, and help to interpret the geometric meanings of linear transformations. These contributions offer new insights into the designs of quantum algorithms. Currently, there are no specialized quantum algorithms specifically designed for elementary row transformations of matrices.

Drawing on the foundational ideas from [20], this paper proposes algorithms for two elementary row transformations of matrices, transpose and trace of matrices, along with their corresponding quantum circuits. These algorithms collectively involve the following key steps:

  • Construction of the initial state: matrix elements are first encoded into the probability amplitudes of quantum pure states by using tensor product operations.

  • Information separation and labeling: combining multi-qubit control operators with ancillary states, where the ancillary states are used to accurately label the useful and redundant parts of the information, achieving effective separation of the data.

  • State swaping operations: depending on the specific algorithms, C-SWAP operations are flexibly employed to precisely exchange the states of two quantum bits.

  • State superposition processing: Hadamard operators are applied to induce superposition states among quantum bits.

  • Measurement on the ancillary states: auxiliary measurement techniques are used to eliminate redundant information, ensuring that the desired results are accurately reflected in the probability amplitudes of the final state.

This paper focuses on the core algorithm layer of quantum computers and aims to design a set of quantum circuit schemes for implementing basic matrix operations. The research is carried out under the following premise: the input state, which is the quantum state encoding matrix elements, is successfully prepared. On this basis, we systematically construct a quantum circuit architecture capable of performing basic matrix operations such as row transformation transposition, and trace calculation.

This approach is of great value in the current development of quantum computing. On the one hand, quantum state preparation and quantum state measurement, as crucial aspects in quantum computing, have attracted much attention on conducting specialized studies. Transforming classical matrices into quantum pure states is a fundamental issue that needs to be addressed in the field of quantum state preparation. Moreover, converting the results of quantum state measurements into classical data interpretable by humans makes quantum readout the core part of quantum state measurement research. On the other hand, the algorithm designed in this paper fully considers the compatibility with quantum state preparation and quantum state measurement schemes, laying a solid foundation for the rapid deployment and application of quantum computers after they become practically implementable in the future.

The remainder of this paper is organized as follows: Section 2 introduces quantum algorithms for two types of elementary matrix transformations. Section 3 presents quantum algorithms for computing the trace and the transposition of a matrix. Conclusion is given in section 2.

2.
Two Types of Elementary Row Transformations

This section presents quantum algorithms for two types of elementary row transformations of an N × M matrix A = {aij} (N = 2n, M = 2m), including adding one row to another and swapping two rows.

2.1.
Adding One Row of a Matrix to Another Row

For the quantum algorithm that adds one row of a matrix to another, the quantum circuit diagram is shown in Figure 1. This algorithm requires two pure states and four auxiliary states, and is divided into seven steps.

Figure 1.

Quantum circuit diagram for adding one row of a matrix to another row.

The following steps give rise to the algorithm of adding the (k + 1)th row of a matrix A to the (l + 1)th row with 0 ≤ l, kN – 1.

Step 1: Prepare the initial state

First, introduce an n-qubits register R1 and an m-qubit register C1, which are used to enumerate the rows and columns of matrix A, respectively. The entries aij of the matrix A are then encoded into a pure state as follows, |Ψ1=i=0N1j=0M1aij|iR1|jC1,ij|aij|2=1,\left| {{{\rm{\Psi }}_1}} \right\rangle = \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}} } {\left| j \right\rangle _{{C_1}}},\;\;\sum\limits_{ij} {|{a_{ij}}{|^2} = 1,} where |i〉 is the binary representation of i.

Next, introduce an N-dimensional auxiliary column vector Z (with the elements of the k + 1 and the l + 1 rows being 12{1 \over {\sqrt 2 }}, and all other elements being 0), along with an n-qubit register R2. The elements of Z are then encoded into pure states as follows, |Ψ2=12(|kR2+|lR2).\left| {{{\rm{\Psi }}_2}} \right\rangle = {1 \over {\sqrt 2 }}\left( {{{\left| k \right\rangle }_{{R_2}}} + {{\left| l \right\rangle }_{{R_2}}}} \right).

The initial state of the entire system is then given by 1|Φ0=|Ψ1|Ψ2=12( i=0N1j=0M1aij|iR1|jC1|kR2+ i=0N1j=0M1aij|iR1|jC1|lR2 ).\matrix{ {\left| {{{\rm{\Phi }}_0}} \right\rangle } \hfill & = \hfill & {\left| {{{\rm{\Psi }}_1}} \right\rangle \otimes \left| {{{\rm{\Psi }}_2}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 2 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \left. {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}} } } \right).} \hfill \cr }

Step 2: Label the two states of R2

In order to accurately separate the useful and useless information terms in |Φ0〉 for subsequent calculations, we need to label the two states of R2 in |Φ0〉 accordingly.

Introduce a 1-qubit auxiliary B1 in state |0〉B1, and the following control operator, WR2B1(1)=PR2σB1(x)+(IR2PR2)IB1,W_{{R_2}{B_1}}^{(1)} = {P_{{R_2}}} \otimes \sigma _{{B_1}}^{(x)} + ({I_{{R_2}}} - {P_{{R_2}}}) \otimes {I_{{B_1}}}, where PR2 = |kR2R2k| is a projection operator, σB1(x)\sigma _{{B_1}}^{(x)} is the standard Pauli matrix, and IB1 is the identity operator acting on B1. Applying the operator WR2B1(1)W_{{R_2}{B_1}}^{(1)} to |Φ0〉|0〉B1, we get 2|Φ1=WR2B1(1)|Φ0|0B1=12( i=0N1j=0M1aij|iR1|jC1|kR2|1B1+ i=0N1j=0M1aij|iR1|jC1|lR2|0B1 ).\matrix{ {\left| {{{\rm{\Phi }}_0}} \right\rangle } \hfill & = \hfill & {W_{{R_2}{B_1}}^{(1)}\left| {{{\rm{\Phi }}_0}} \right\rangle \otimes {{\left| 0 \right\rangle }_{{B_1}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 2 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| 1 \right\rangle }_{{B_1}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \left. {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| 0 \right\rangle }_{{B_1}}}} } } \right).} \hfill \cr }

That is, for the term where the state of R2 is |k〉R2, it is labeled by |1〉B1, and for the term where the state of R2 is |lR2, it is labeled by |0〉B1.

Kitaev A. Y. et al. [24] represented an important result: A control operator with n control qubits can be represented by O(n) Toffoli gates. Since the above control operator WR2B1(1)W_{{R_2}{B_1}}^{(1)} has an n-qubit control register, it can be expressed by using O(n) Toffoli gates. Therefore, the circuit complexity for computing |Φ1〉 is O(n) = O(log N).

Step 3: Separate the useful information from the redundant information

To precisely extract the element located at the (k + 1)th row of the second term in |Φ1〉, from the terms in |Φ1〉 labeled by |0〉B1 we only extract the terms where R1 is in the state |kR1. To do so, we introduce a 1-qubit auxiliary B2 in the state |0〉B2, and the following control operator, WR1B1B2(2)=PR1B1σB2(x)+(IR1B1PR1B1)IB2,W_{{R_1}{B_1}{B_2}}^{(2)} = {P_{{R_1}{B_1}}} \otimes \sigma _{{B_2}}^{(x)} + ({I_{{R_1}{B_1}}} - {P_{{R_1}{B_1}}}) \otimes {I_{{B_2}}}, where PR1B1 = |kR1 |0〉B1 R1k|B1 〈0| is a projection operator.

Applying the operator WR1B1B2(2)W_{{R_1}{B_1}{B_2}}^{(2)} to |Φ1〉|0〉B2, we obtain 3|Φ2=WR1B1B2(2)|Φ1|0B2=12( i=0N1j=0M1aij|iR1|jC1|kR2|1B1|0B2 +j=0M1akj|kR1|jC1|lR2|0B1|1B2 )+|g1R1C1R2|0B1|0B2.\matrix{ {|{\Phi _2}\rangle } \hfill & = \hfill & {W_{{R_1}{B_1}{B_2}}^{(2)}|{\Phi _1}\rangle \otimes |0{\rangle _{{B_2}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 2 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}} } |i{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|k{\rangle _{{R_2}}}|1{\rangle _{{B_1}}}|0{\rangle _{{B_2}}}} \right.} \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}} |k{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|0{\rangle _{{B_1}}}|1{\rangle _{{B_2}}}} \right)} \hfill \cr {} \hfill & {} \hfill & { + |{g_1}\rangle {R_1}{C_1}{R_2}|0{\rangle _{{B_1}}}|0{\rangle _{{B_2}}}.} \hfill \cr }

That is, the terms where the state of R1 and B1 are |kR1 |0〉B1 are labeled by |1〉B2, while the other terms are labeled by |0〉B2. The states |1〉B1 |0〉B2 and |0〉B1 |1〉B2 in |Φ2〉 correspond to the useful information terms required by the algorithm. All other useless information terms are represented by |g1R1C1R2.

The control operator WR1B1B2(2)W_{{R_1}{B_1}{B_2}}^{(2)} has n + 1 control qubits, so it can be represented in terms of O(n) Toffoli gates and the circuit complexity for computing |Φ2〉 is O(n) = O(log N).

Step 4: Use C-SWAP gate to exchange the states of R1 and R2 marked by |1〉B2

To add the (k + 1)th row to the (l + 1)th row, we apply a C-SWAP gate operation to the terms in |Φ2〉 labeled by |1〉B2, which swaps the states of R1 and R2. This operation is given by the following gate, WR1R2B2(3)=SWAPR1,R2|1B2B21|+IR1R2|0B2B20|.W_{{R_1}{R_2}{B_2}}^{(3)} = SWA{P_{{R_1},{R_2}}} \otimes {\left| 1 \right\rangle _{{B_2}\;{B_2}}}\left\langle 1 \right| + {I_{{R_1}{R_2}}} \otimes {\left| 0 \right\rangle _{{B_2}\;{B_2}}}\left\langle 0 \right|.

Applying WR1R2B2(3)W_{{R_1}{R_2}{B_2}}^{(3)} to |Φ2〉 we obtain 4|Φ3=WR1R2B2(3)|Φ2=12( i=0N1j=0M1aij|iR1|jC1|kR2|1B1|0B2+ j=0M1akj|lR1|jC1|kR2|0B1|1B2 )+|g1R1C1R2|0B1|0B2.\matrix{ {\left| {{{\rm{\Phi }}_3}} \right\rangle } \hfill & = \hfill & {W_{{R_1}{R_2}{B_2}}^{(3)}\left| {{{\rm{\Phi }}_2}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 2 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| 1 \right\rangle }_{{B_1}}}{{\left| 0 \right\rangle }_{{B_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \left. {\sum\limits_{j = 0}^{M - 1} {{a_{kj}}{{\left| l \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| 1 \right\rangle }_{{B_2}}}} } \right)} \hfill \cr {} \hfill & {} \hfill & { + \left| {{g_1}} \right\rangle {R_1}{C_1}{R_2}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| 0 \right\rangle }_{{B_2}}}.} \hfill \cr }

Note that the SWAPs in the control operator WR1R2B2(3)W_{{R_1}{R_2}{B_2}}^{(3)} have common single control qubit and are related to different pairs of qubits. Therefore, they can be applied simultaneously.

Step 5: Label the useful and redundant information

To effectively avoid mixing useful and useless information terms in the subsequent computation process, we introduce an auxiliary qubit B3 in state |0〉B3, together with the projection operator PB1B2 = |00〉B1 B2 B1B2〈00|. We use B3 to label the useful and useless information terms in |Φ3〉 accordingly by using the following control operator, WB1B2B3(4)=PB1B2σB3(x)+(IB1B2PB1B2)IB3.W_{{B_1}{B_2}{B_3}}^{(4)} = {P_{{B_1}{B_2}}} \otimes \sigma _{{B_3}}^{(x)} + ({I_{{B_1}{B_2}}} - {P_{{B_1}{B_2}}}) \otimes {I_{{B_3}}}.

Applying WB1B2B3(4)W_{{B_1}{B_2}{B_3}}^{(4)} to |Φ3〉|0〉B3 we obtain 5|Φ4=WB1B2B3(4)|Φ3|0B3=12( i=0N1j=0M1aij|iR1|jC1|kR2|1B1|0B2+ j=0M1akj|lR1|jC1|kR2|0B1|1B2 )|0B3+|g1R1C1R2B1B2|1B3.\matrix{ {\left| {{{\rm{\Phi }}_4}} \right\rangle } \hfill & = \hfill & {W_{{B_1}{B_2}{B_3}}^{(4)}\left| {{{\rm{\Phi }}_3}} \right\rangle {{\left| 0 \right\rangle }_{{B_3}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 2 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| 1 \right\rangle }_{{B_1}}}{{\left| 0 \right\rangle }_{{B_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \left. {\sum\limits_{j = 0}^{M - 1} {{a_{kj}}{{\left| l \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| 1 \right\rangle }_{{B_2}}}} } \right){{\left| 0 \right\rangle }_{{B_3}}}} \hfill \cr {} \hfill & {} \hfill & { + \left| {{g_1}} \right\rangle {R_1}{C_1}{R_2}{B_1}{B_2}{{\left| 1 \right\rangle }_{B3}}.} \hfill \cr }

In this way, |0〉B3 (|1〉B3) labels the useful (useless) information terms.

Since the control operator WB1B2B3(4)W_{{B_1}{B_2}{B_3}}^{(4)} has two control qubit, its computational complexity is O(1).

Step 6: Use a Hadamard gate for information superposition

Applying the Hadamard operator WB1B2(5)=H2W_{{B_1}{B_2}}^{(5)} = {H^{ \otimes 2}} to B1 and B2, we obtain 6|Φ5=WB1B2(5)|Φ4=1(2)3( i=0N1j=0M1aij|iR1|jC1+ j=0M1akj|lR1|jC1 )|kR2|0B1|0B2|0B3+|g2R1C1R2B1B2B3.\matrix{ {\left| {{{\rm{\Phi }}_5}} \right\rangle } \hfill & = \hfill & {W_{{B_1}{B_2}}^{(5)}\left| {{{\rm{\Phi }}_4}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {{{\left( {\sqrt 2 } \right)}^3}}}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \left. {\sum\limits_{j = 0}^{M - 1} {{a_{kj}}{{\left| l \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}} } \right){{\left| k \right\rangle }_{{R_2}}}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| 0 \right\rangle }_{{B_2}}}{{\left| 0 \right\rangle }_{{B_3}}}} \hfill \cr {} \hfill & {} \hfill & { + \left| {{g_1}} \right\rangle {R_1}{C_1}{R_2}{B_1}{B_2}{{\left| 1 \right\rangle }_{B3}}.} \hfill \cr }

Here, the terms in the state |0〉B1|0〉B2|0〉B3 are retained, which contain all the information required for further process. The remaining terms are represented as |g2R1C1R2B1B2B3.

Since only two Hadamard gates are used here, the circuit complexity is O(1).

Step 7: Measure the auxiliary state B1B2B3 using |000〉B1B2B3

By measuring the auxiliary B1B2B3 in the state |000〉B1B2B3, we get 7|Φ6=|Ψout|kR2,\left| {{{\rm{\Phi }}_6}} \right\rangle = \left| {{{\rm{\Psi }}_{out}}} \right\rangle {\left| k \right\rangle _{{R_2}}}, where 8|Ψout=1G( ilj=0M1aij|iR1|jC1 +j=0M1(alj+akj)|lR1|jC1 )\matrix{ {\left| {{{\rm{\Psi }}_{out}}} \right\rangle } \hfill & = \hfill & {{1 \over G}\left( {\sum\nolimits_{i \ne l} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {({a_{lj}} + {a_{kj}}){{\left| l \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}} } \right)} \hfill \cr } with the normalization G=(ilj| aij |2+j| akj+alj |2)1/2.G = {\left( {\sum\limits_{i \ne l} {\sum\limits_j {{{\left| {{a_{ij}}} \right|}^2} + \sum\limits_j {{{\left| {{a_{kj}} + {a_{lj}}} \right|}^2}} } } } \right)^{1/2}}.

The success probability is G28{{{G^2}} \over 8}. This probability clearly depends only on the matrix elements aij and is independent of the matrix dimension.

The overall computational complexity of the algorithm is O(n), mainly determined by WR2B1(1)W_{{R_2}{B_1}}^{(1)} and WR1B1B2(2)W_{{R_2}{B_1}{B_2}}^{(2)}.

2.2
Swapping Two Rows of a Matrix

For the quantum algorithm that swaps two rows of a matrix, the quantum circuit diagram is shown in Figure 2. The algorithm requires two pure states and four auxiliaries, and is divided into seven steps to swap the (k + 1)th and (l + 1)th rows of matrix A (0 ≤ l, kN – 1).

Figure 2.

The quantum circuit diagram for swapping two rows of a matrix.

Step 1: Prepare the initial state

First, introduce an n-qubit register R1 and an m-qubit register C1, which enumerate the rows and columns of the matrix A, respectively. The elements of the matrix A are then encoded into the following pure state, |Ψ1=i=0N1j=0M1aij|iR1|jC1,ij|aij|2=1,\left| {{{\rm{\Psi }}_1}} \right\rangle = \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}},\;\;\sum\limits_{ij} {|{a_{ij}}{|^2} = 1,} } }

Next, introduce an N × N auxiliary matrix Z such that the elements in the (l + 1)-th row and (k + 1)-th column, the (k + 1)-th row and (k + 1)-th column, and the (l + 1)-th row and (l + 1)-th column are 13{1 \over {\sqrt 3 }}, the rest elements are 0; along with two n-qubit registers R2 and C2. The elements of Z are then encoded into the following pure state, |Ψ2=13(|lR2|kC2+|kR2|kC2+|lR2|lC2).\left| {{{\rm{\Psi }}_2}} \right\rangle = {1 \over {\sqrt 3 }}\left( {{{\left| l \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}} + {{\left| k \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}} + {{\left| l \right\rangle }_{{R_2}}}{{\left| l \right\rangle }_{{C_2}}}} \right).

The initial state is of the form, 9|Φ0=|Ψ1|Ψ2=13( i=0N1j=0M1aij|iR1|jC1|lR2|kC2+i=0N1j=0M1aij|iR1|jC1|kR2|kC2 +i=0N1j=0M1aij|iR1|jC1|lR2|lC2 ).\matrix{ {\left| {{{\rm{\Phi }}_0}} \right\rangle } \hfill & = \hfill & {\left| {{{\rm{\Psi }}_1}} \right\rangle \otimes \left| {{{\rm{\Psi }}_2}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 3 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}} } } \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| l \right\rangle }_{{C_2}}}} } } \right).} \hfill \cr }

Step 2: Label the distinct states of R2 and C2

To accurately separate the useful from the useless information terms in |Φ0〉, introduce an auxiliary qubit B1 in state |0〉B1, and the following control operator, WR2C2B1(1)=PR2C2σB1(x)+(IR2C2PR2C2)IB1,W_{{R_2}{C_2}{B_1}}^{(1)} = {P_{{R_2}{C_2}}} \otimes \sigma _{{B_1}}^{(x)} + ({I_{{R_2}{C_2}}} - {P_{{R_2}{C_2}}}) \otimes {I_{{B_1}}}, where PR2C2 = |lR2|kC2R2l|C2k| is the projection operator. Applying WR2C2B1(1)W_{{R_2}{C_2}{B_1}}^{(1)} to |Φ0〉|0〉B1, we get 10|Φ1=WR2C2B1(1)|Φ0|0B1=13( i=0N1j=0M1aij|iR1|jC1|lR2|kC2|1B1+i=0N1j=0M1aij|iR1|jC1|kR2|kC2|0B1 +i=0N1j=0M1aij|iR1|jC1|lR2|lC2|0B1 ).\matrix{ {\left| {{{\rm{\Phi }}_1}} \right\rangle } \hfill & = \hfill & {W_{{R_2}{C_2}{B_1}}^{(1)}\left| {{{\rm{\Phi }}_0}} \right\rangle \otimes {{\left| 0 \right\rangle }_{{B_1}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 3 }}\left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}{{\left| 1 \right\rangle }_{{B_1}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}{{\left| 0 \right\rangle }_{{B_1}}}} } } \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{i = 0}^{N - 1} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| l \right\rangle }_{{C_2}}}{{\left| 0 \right\rangle }_{{B_1}}}} } } \right).} \hfill \cr }

In this way, the terms where the states of R2 and C2 are different are labeled by |1〉B1, and the terms where the states of R2 and C2 are the same are labeled by |0〉B1.

Since the control operator WR2C2B1(1)W_{{R_2}{C_2}{B_1}}^{(1)} has n control qubit, its computational complexity is O(n) = O(log N).

Step 3: Separate the useful information terms from the useless information terms

We proceed to separate the useful information terms from the useless ones.

  • 1)

    In the first sum, extract all elements except for those in the (k + 1)th and (l + 1)th rows.

  • 2)

    In the second sum, extract all elements in the (l + 1)th row.

  • 3)

    In the third sum, extract all elements in the (k + 1)th row.

  • 4)

    For the terms in |Φ1〉 labeled by |1〉B1, extract all terms except those where R1 is in the states |kR1 and |lR1.

  • 5)

    For the two sums in |Φ1〉 labeled by |0〉B1, extract the terms where R1 is in the state |lR1 and the terms where R1 is in the state |kR1.

We introduce an auxiliary qubit B2 in state |0〉B2, and use the following control operators, W(1)=PR1R2σB21(x)+(IR1R2PR1R2)IB21,W(2)=PR1C2σB22(x)+(IR1C2PR1C2)IB21,\matrix{ {{W^{(1)}}} \hfill & = \hfill & {{P_{{R_1}{R_2}}} \otimes \sigma _{{B_2}^1}^{(x)} + ({I_{{R_1}{R_2}}} - {P_{{R_1}{R_2}}}) \otimes {I_{{B_2}^1}},} \hfill \cr {{W^{(2)}}} \hfill & = \hfill & {{P_{{R_1}{C_2}}} \otimes \sigma _{{B_2}^2}^{(x)} + ({I_{{R_1}{C_2}}} - {P_{{R_1}{C_2}}}) \otimes {I_{{B_2}^1}},} \hfill \cr } where PR1R2 = |kR1 |lR2 R1k|R2l| and PR1C2 = |lR1 |kC2 R1l|C2k| are projection operators. σB2i(x)\sigma _{{B_2}^i}^{(x)} and IB2i are the operators acting on the i-th qubit of the auxiliary B2 (i = 1,2).

When WR1R2C2B2(2)=W(2)W(1)W_{{R_1}{R_2}{C_2}{B_2}}^{(2)} = {W^{(2)}}{W^{(1)}} acts on |Φ1〉|0〉B2, we obtain 11|Φ2=WR1R2C2B2(2)|Φ1|0B213( il,kj=0M1aij|iR1|jC1|lR2|kC2|1B1|00B2+j=0M1alj|lR1|jC1|kR2|kC2|0B1|01B2 +j=0M1akj|kR1|jC1|lR2|lC2|0B1|10B2 )+|g1R1C1R2C2B1B2.\matrix{ {\left| {{{\rm{\Phi }}_2}} \right\rangle } \hfill & = \hfill & {W_{{R_1}{R_2}{C_2}{B_2}}^{(2)}\left| {{{\rm{\Phi }}_1}} \right\rangle \otimes {{\left| 0 \right\rangle }_{{B_2}}}} \hfill \cr {} \hfill & {} \hfill & {{1 \over {\sqrt 3 }}\left( {\sum\limits_{i \ne l,k} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}{{\left| i \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}{{\left| 1 \right\rangle }_{{B_1}}}{{\left| {00} \right\rangle }_{{B_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \sum\limits_{j = 0}^{M - 1} {{a_{lj}}{{\left| l \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| k \right\rangle }_{{R_2}}}{{\left| k \right\rangle }_{{C_2}}}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| {01} \right\rangle }_{{B_2}}}} } \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}{{\left| k \right\rangle }_{{R_1}}}{{\left| j \right\rangle }_{{C_1}}}{{\left| l \right\rangle }_{{R_2}}}{{\left| l \right\rangle }_{{C_2}}}{{\left| 0 \right\rangle }_{{B_1}}}{{\left| {10} \right\rangle }_{{B_2}}}} } \right)} \hfill \cr {} \hfill & {} \hfill & { + {{\left| {{g_1}} \right\rangle }_{{R_1}}}{{_{{C_1}}}_{{R_2}}}{{_{{C_2}}}_{{B_1}}}_{{B_2}}.} \hfill \cr }

That is, the terms labeled by |1〉B1 |00〉B2, |0〉B1 |01〉B2 and |0〉B1 |10〉B2 are the useful information terms required by the algorithm, while the other terms are the useless information terms denoted by |g1R1C1R2C2B1B2.

Since the control operator WR1R2C2B2(2)W_{{R_1}{R_2}{C_2}{B_2}}^{(2)} has 2n control qubit, its computational complexity is O(n) = O(log N).

Step 4: Use C-SWAP gates to swap the states of R1 and R2, as well as the states of R1 and C2

To swap the (k + 1)th and (l + 1)th rows of the matrix, we apply C-SWAP gate to the terms in |Φ2〉 labeled by |10〉B2 and |01〉B2, so as to swap the states of R1 and R2, as well as the states of R1 and C2. Denote W(1)=SWAPR1,C2|1B21B211|+IR1C2|0B21B210|,W(2)=SWAPR1,R2|1B22B221|+IR1R2|0B22B220|,\matrix{ {{W^{(1)}}} \hfill & = \hfill & {SWA{P_{{R_1},{C_2}}} \otimes {{\left| 1 \right\rangle }_{{B_2}^1\;}}_{{B_2}^1}\left\langle 1 \right| + {I_{{R_1}{C_2}}} \otimes {{\left| 0 \right\rangle }_{{B_2}^1}}{\;_{{B_2}^1}}\left\langle 0 \right|,} \hfill \cr {{W^{(2)}}} \hfill & = \hfill & {SWA{P_{{R_1},{R_2}}} \otimes {{\left| 1 \right\rangle }_{{B_2}^2\;}}_{{B_2}^2}\left\langle 1 \right| + {I_{{R_1}{R_2}}} \otimes {{\left| 0 \right\rangle }_{{B_2}^2}}{\;_{{B_2}^2}}\left\langle 0 \right|,} \hfill \cr } where |1〉B21 stands for that the first qubit of B2 is in the state |1〉, and |1〉B22 for that the second qubit of B2 in the state |1〉.

Applying WR1R2C2B2(3)=W(2)W(1)W_{{R_1}{R_2}{C_2}{B_2}}^{(3)} = {W^{(2)}}{W^{(1)}} to |Φ2〉, we obtain 12|Φ3=WR1R2C2B2(3)|Φ2=13( il,kj=0M1aij|iR1|jC1|lR2|kC2|1B1|00B2+j=0M1alj|kR1|jC1|lR2|kC2|0B1|01B2 +j=0M1akj|lR1|jC1|lR2|kC2|0B1|10B2 )++|g2R1C1R2C2B1B2.\matrix{ {\left| {{\Phi _3}} \right\rangle } \hfill & = \hfill & {W_{{R_1}{R_2}{C_2}{B_2}}^{(3)}\left| {{\Phi _2}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 3 }}\left( {\sum\limits_{i \ne l,k} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}|i{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|1{\rangle _{{B_1}}}|00{\rangle _{{B_2}}}} } } \right.} \hfill \cr {} \hfill & {} \hfill & { + \sum\limits_{j = 0}^{M - 1} {{a_{lj}}|k{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|0{\rangle _{{B_1}}}|01{\rangle _{{B_2}}}} } \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}|l{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|0{\rangle _{{B_1}}}|10{\rangle _{{B_2}}}} } \right) + } \hfill \cr {} \hfill & {} \hfill & { + {{\left| {{g_2}} \right\rangle }_{{R_1}{C_1}{R_2}{C_2}{B_1}{B_2}}}.} \hfill \cr }

The SWAP gates transfer the elements originally in the (k + 1)th row to the (l + 1)th row in the terms labeled by |0〉B1 |01〉B2, and the elements originally in the (l + 1)th row to the (k + 1)th row in the terms labeled by |0〉B1 |10〉B2.

Note that the SWAPs in the control operator WR1R2B2(3)W_{{R_1}{R_2}{B_2}}^{(3)} have common single control qubit and are related to different pairs of qubits. Therefore, they can be applied simultaneously.

Step 5: Label the useful and useless information terms

Introduce an auxiliary qubit B3 in state |0〉B3. To use B3 to label the useful and useless information terms in |Φ3〉, we apply the following control operator, WB1B2B3(4)=V(1)V(2)V(3),W_{{B_1}{B_2}{B_3}}^{(4)} = {V^{(1)}}{V^{(2)}}{V^{(3)}}, where V(i)=PB1B2(i)σB3(x)+(IB1B2PB1B2)IB3{V^{(i)}} = P_{{B_1}{B_2}}^{(i)} \otimes \sigma _{{B_3}}^{(x)} + \left( {{I_{{B_1}{B_2}}} - {P_{{B_1}{B_2}}}} \right) \otimes {I_{{B_3}}}, with the projection operators PB1B2(i)(i=1,2,3)P_{{B_1}{B_2}}^{(i)}(i = 1,2,3) given by PB1B2(1)=|100B1B2B1B2100|,PB1B2(2)=|001B1B2B1B2001|,PB1B2(3)=|010B1B2B1B2010|.\matrix{ {P_{{B_1}{B_2}}^{(1)}} \hfill & = \hfill & {|100{\rangle _{{B_1}{B_2}}}{\;_{{B_1}}}_{{B_2}}\langle 100|,} \hfill \cr {P_{{B_1}{B_2}}^{(2)}} \hfill & = \hfill & {|001{\rangle _{{B_1}{B_2}}}{\,_{{B_1}}}_{{B_2}}\langle 001|,} \hfill \cr {P_{{B_1}{B_2}}^{(3)}} \hfill & = \hfill & {|010{\rangle _{{B_1}{B_2}}}{\;_{{B_1}}}_{{B_2}}\langle 010|.} \hfill \cr }

Acting WB1B2B3(4)W_{{B_1}{B_2}{B_3}}^{(4)} on |Φ3〉|0〉B3. we obtain 13|Φ4=WB1B2B3(4)|Φ3|0B3=13( il,kj=0M1aij|iR1|jC1|lR2|kC2|1B1|00B2+j=0M1alj|kR1|jC1|lR2|kC2|0B1|01B2 +j=0M1akj|lR1|jC1|lR2|kC2|0B1|10B2 )|1B3+|g2R1C1R2C2B1B2|0B3.\matrix{ {\left| {{\Phi _4}} \right\rangle } \hfill & = \hfill & {W_{{B_1}{B_2}{B_3}}^{(4)}\left| {{\Phi _3}} \right\rangle |0{\rangle _{{B_3}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {\sqrt 3 }}\left( {\sum\limits_{i \ne l,k} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}} } |i{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|1{\rangle _{{B_1}}}|00{\rangle _{{B_2}}}} \right.} \hfill \cr {} \hfill & {} \hfill & { + \sum\limits_{j = 0}^{M - 1} {{a_{lj}}} |k{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|0{\rangle _{{B_1}}}|01{\rangle _{{B_2}}}} \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}} |l{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|0{\rangle _{{B_1}}}|10{\rangle _{{B_2}}}} \right)|1{\rangle _{{B_3}}}} \hfill \cr {} \hfill & {} \hfill & { + {{\left| {{g_2}} \right\rangle }_{{R_1}{C_1}{R_2}{C_2}{B_1}{B_2}}}|0{\rangle _{{B_3}}}.} \hfill \cr }

Here, |1〉B3 labels the useful information term, while |0〉B3 labels the useless information term.

Since the control operator WB1B2B3(4)W_{{B_1}{B_2}{B_3}}^{(4)} has one control qubit, its computational complexity is O(1).

Step 6: Use Hadamard gates for information superposition

Applying the Hadamard operator WB1B2(5)=H3W_{{B_1}{B_2}}^{(5)} = {H^{ \otimes 3}} to B1 and B2, we have 14|Φ5=WB1B2(5)|Φ4=1(2)3·3( il,kj=0M1aij|iR1|jC1 +j=0M1alj|kR1|jC1+j=0M1akj|lR1|jC1 )|lR2|kC2|0B1|00B2|1B3+|g3R1C1R2C2B1B2B3,\matrix{ {\left| {{\Phi _5}} \right\rangle } \hfill & = \hfill & {W_{{B_1}{B_2}}^{(5)}\left| {{\Phi _4}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {{{(\sqrt 2 )}^3}\cdot\sqrt 3 }}\left( {\sum\limits_{i \ne l,k} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}} } |i{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}} \right.} \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{lj}}} |k{\rangle _{{R_1}}}|j{\rangle _{{C_1}}} + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}} |l{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}} \right)} \hfill \cr {} \hfill & {} \hfill & {|l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}|0{\rangle _{{B_1}}}|00{\rangle _{{B_2}}}|1{\rangle _{{B_3}}} + {{\left| {{g_3}} \right\rangle }_{{R_1}{C_1}{R_2}{C_2}{B_1}{B_2}{B_3}}},} \hfill \cr } where the terms in the state |0〉B1|00〉B2|1〉B3 are retained, the other terms are denoted by |g3R1C1R2B1B2B3.

At this point, we have successfully swaped the elements of the (k + 1) -th row of matrix A with the corresponding elements of the (l + 1)-th row.

Since only three Hadamard gates are used here, the circuit complexity is O(1).

Step 7: Measure the auxiliary B1B2B3 in the state |0001〉B1B2B3

We measure the auxiliary B1B2B3 in the state |0001〉B1B2B3 to remove the useless information terms, and obtain 15|Φ6=|Ψout|lR2|kC2,\left| {{\Phi _6}} \right\rangle = \left| {{\Psi _{out}}} \right\rangle |l{\rangle _{{R_2}}}|k{\rangle _{{C_2}}}, where 16|Ψout=( il,kj=0M1aij|iR1|jC1 +j=0M1alj|kR1|jC1+j=0M1akj|lR1|jC1 ).\matrix{ {\left| {{\Psi _{out}}} \right\rangle } \hfill & = \hfill & {\left( {\sum\limits_{i \ne l,k} {\sum\limits_{j = 0}^{M - 1} {{a_{ij}}} } |i{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}} \right.} \hfill \cr {} \hfill & {} \hfill & {\left. { + \sum\limits_{j = 0}^{M - 1} {{a_{lj}}} |k{\rangle _{{R_1}}}|j{\rangle _{{C_1}}} + \sum\limits_{j = 0}^{M - 1} {{a_{kj}}} |l{\rangle _{{R_1}}}|j{\rangle _{{C_1}}}} \right).} \hfill \cr }

The success probability is 124{1 \over {24}}. This probability is independent of the matrix dimension.

The overall computational complexity of the algorithm is O(n), mainly determined by WR2C2B1(1)W_{{R_2}{C_2}{B_1}}^{(1)} and WR1R2C2B2(2)W_{{R_1}{R_2}{C_2}{B_2}}^{(2)}.

For specific examples of this algorithm, please refer to Appendix A.

3.
Matrix Trace Calculation and Transpose

In this section we present quantum algorithms for trace calculation of an N × N matrix and transposition of an N × M matrix.

3.1.
Matrix Trace Calculation

The quantum circuit for calculating the trace of the matrix S is shown in Figure 3. Assume that a matrix S={aik} is of size N × N with N = 2n. This algorithm requires one pure state and three auxiliary qubits, and is divided into the following six steps.

Figure 3.

Quantum circuit for calculating matrix trace.

Step 1: Prepare the initial state

First, introduce two n-qubit registers R and C, which are used to enumerate the rows and columns of the matrix S, and encode the elements of the matrix S to obtain the following pure initial state, 17|Φ0=i=0N1k=0N1aik|iR|kC,ik| aik |2=1.\left| {{\Phi _0}} \right\rangle = \sum\limits_{i = 0}^{N - 1} {\sum\limits_{k = 0}^{N - 1} {{a_{ik}}} } |i{\rangle _R}|k{\rangle _C},\quad \sum\limits_{ik} {{{\left| {{a_{ik}}} \right|}^2}} = 1.

Step 2: Mark the terms where the states of R and C are the same

To calculate the sum of the diagonal elements of matrix S, we need to mark the diagonal elements of S, i.e., the terms where the states of registers R and C are the same. To do this, introduce an n-qubit auxiliary A in the state |0〉A, and an operator Wj(m)=Pj(m)σA(j)(x)+(IjPj(m))IA(j),W_j^{(m)} = P_j^{(m)} \otimes \sigma _{{A^{(j)}}}^{(x)} + \left( {{I_j} - P_j^{(m)}} \right) \otimes {I_{{A^{(j)}}}}, where Pj(m)=|mjR|mjCRmj|Cmj|,m=0,1,j=1,2,,nP_j^{(m)} = {\left| {{m_j}} \right\rangle _R}{\left| {{m_j}} \right\rangle _C}_R{\left. {{m_j}} \right|_C}\langle {m_j}|,m = 0,1,j = 1,2, \ldots ,n, is the projection operator acting on the j-th qubit of registers R and C, σA(j)(x)\sigma _{{A^{(j)}}}^{(x)} is the Pauli matrix acting on the j-th qubit of auxiliary A, IA(j) is the identity operator acting on the j-th qubit of auxiliary A.

Applying the operator WRCA(1)=j=1nWj0Wj1W_{RCA}^{(1)} = \prod\nolimits_{j = 1}^n {W_j^0} W_j^1 to |Φ0〉|0〉A, we obtain 18|Φ1=WRCA(1)|Φ0|0A=(i=0N1aii|iR|iC)|N1A+|g1RCA.\matrix{ {\left| {{\Phi _1}} \right\rangle } \hfill & = \hfill & {W_{RCA}^{(1)}\left| {{\Phi _0}} \right\rangle |0{\rangle _A}} \hfill \cr {} \hfill & = \hfill & {\left( {\sum\limits_{i = 0}^{N - 1} {{a_{ii}}} |i{\rangle _R}|i{\rangle _C}} \right)|N - 1{\rangle _A} + {{\left| {{g_1}} \right\rangle }_{RCA}}.} \hfill \cr }

In the process of realizing the quantum state |Φ1〉, we check whether the j-th qubits of registers R and C are the same. If their j-th qubits are the same, the j-th qubit of A is flipped from |0〉 to |1〉. If the states of all n qubits of R and C are the same, the states of all n qubits of A are flipped from |0〉A to |N – 1〉A.

The control operator WRCA(1)W_{RCA}^{(1)} has 2n control qubits, so the circuit complexity for computing |Φ1〉 is O(n).

Step 3: Mark the useful and useless information terms

Currently, all useful information terms are stored in the first sum of |Φ1〉, while the second sum |g1RCA represents useless information terms that are not needed by the algorithm.

To separately mark the useful and useless information terms in the state |Φ1〉, we introduce the projection operator PA = |N – 1〉A AN – 1|, and a 1-qubit auxiliary B1 in the state |0〉B1. The control operator WAB1(2)=PAσB1(x)+(IAPA)IB1W_{A{B_1}}^{(2)} = {P_A} \otimes \sigma _{{B_1}}^{(x)} + \left( {{I_A} - {P_A}} \right) \otimes {I_{{B_1}}} is then applied to A and B1, giving rise to 19|Φ2=WAB1(2)|Φ1|0B1=(i=0N1aii|iR|iC)|N1A|1B1+|g1RCA|0B1.\matrix{ {\left| {{\Phi _2}} \right\rangle } \hfill & = \hfill & {W_{A{B_1}}^{(2)}\left| {{\Phi _1}} \right\rangle |0{\rangle _{{B_1}}}} \hfill \cr {} \hfill & = \hfill & {\left( {\sum\limits_{i = 0}^{N - 1} {{a_{ii}}} |i{\rangle _R}|i{\rangle _C}} \right) \otimes |N - 1{\rangle _A} \otimes |1{\rangle _{{B_1}}}} \hfill \cr {} \hfill & {} \hfill & { + {{\left| {{g_1}} \right\rangle }_{RCA}} \otimes |0{\rangle _{{B_1}}}.} \hfill \cr }

Here, the useful information terms are marked by |1〉B1, and the useless information terms are marked by |0〉B1.

Since the control operator WAB1(2)W_{A{B_1}}^{(2)} has n control qubits, its computational complexity is O(n) = O(log N).

Step 4: Use the Hadamard gate for information superposition

Up to this point, we have obtained the diagonal elements of S. To compute the sum we apply the Hadamard operator WRCA(3)=H3nW_{RCA}^{(3)} = {H^{ \otimes 3n}} to all qubits in |Φ2〉 except for the auxiliary B1. We get 20|Φ3=WRCA(3)|Φ2=123n/2(i=0N1aii)|0R|0C|0A|1B1+|g2RCAB1.\matrix{ {\left| {{\Phi _3}} \right\rangle } \hfill & = \hfill & {W_{RCA}^{(3)}\left| {{\Phi _2}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {{1 \over {{2^{3n/2}}}}\left( {\sum\limits_{i = 0}^{N - 1} {{a_{ii}}} } \right)|0{\rangle _R}|0{\rangle _C}|0{\rangle _A}|1{\rangle _{{B_1}}} + {{\left| {{g_2}} \right\rangle }_{RCA{B_1}}}.} \hfill \cr }

In the state |Φ3〉, we select the terms in the state |0〉r|0〉C|0〉A|1〉B1, as these terms contain all the useful information required for the trace calculation. The other terms are represented by |g2RCAB1.

Since 3n Hadamard gates are used here, the circuit complexity is O(n).

Step 5: Re-mark the useful and useless information terms

Since new useless information terms were generated in the previous step and the useful information is stored in the first term of |Φ3〉, we need to re-mark the useful and useless information terms. To do this, we introduce an auxiliary qubit B2 in state |0〉A2, as well as the projection operator PRCAB1 = |0〉R|0〉C|0〉A|1〉B1 R〈0|C〈0|A〈0|B1 〈1|. We apply the following control operator WRCAB1B2(4)=PRCAB1σB2(x)+(IRCAB1PRCAB1)IB2W_{RCA{B_1}{B_2}}^{(4)} = {P_{RCA{B_1}}} \otimes \sigma _{{B_2}}^{(x)} + \left( {{I_{RCA{B_1}}} - {P_{RCA{B_1}}}} \right) \otimes {I_{{B_2}}} to |Φ3〉 ⊗ |0〉B2. We obtain 21|Φ4=WRCAB1B2(4)|Φ3|0B2=123n/2(i=0N1aii)|0R|0C|0A|1B1|1B2+|g2RCAB1|0B2.\matrix{ {\left| {{\Phi _4}} \right\rangle } \hfill & = \hfill & {W_{RCA{B_1}{B_2}}^{(4)}\left| {{\Phi _3}} \right\rangle |0{\rangle _{{B_2}}}} \hfill \cr {} \hfill & = \hfill & {{1 \over {{2^{3n/2}}}}\left( {\sum\limits_{i = 0}^{N - 1} {{a_{ii}}} } \right)|0{\rangle _R}|0{\rangle _C}|0{\rangle _A}|1{\rangle _{{B_1}}}|1{\rangle _{{B_2}}}} \hfill \cr {} \hfill & {} \hfill & { + {{\left| {{g_2}} \right\rangle }_{RCA{B_1}}}|0{\rangle _{{B_2}}}.} \hfill \cr }

Here, the useful information terms are marked by |1〉B2, and the useless information terms are marked by |0〉B2.

Since the control operator WRCAB1B2(4)W_{RCA{B_1}{B_2}}^{(4)} has 3n + 1 control qubit, its computational complexity is O(n).

Step 6: Measure the auxiliary qubit B2 in the state |1〉B2

The trace of the matrix S is stored in the quantum state |Φ4〉 in the form of probability amplitudes, with the useful information marked by |1〉B2. To extract the useful information, we measure the auxiliary qubit B2 in the state |1〉B2, and obtain 22|Φ5=i=0N1aii| i=0N1aii ||0R|0C|0A|1B1.\left| {{\Phi _5}} \right\rangle = {{\sum\nolimits_{i = 0}^{N - 1} {{a_{ii}}} } \over {\left| {\sum\nolimits_{i = 0}^{N - 1} {{a_{ii}}} } \right|}}|0{\rangle _R}|0{\rangle _C}|0{\rangle _A}|1{\rangle _{{B_1}}}.

The trace is obtained based on the measurement probability | i=0N1aii |223n{{{{\left| {\sum\nolimits_{i = 0}^{N - 1} {{a_{ii}}} } \right|}^2}} \over {{2^{3n}}}}. This probability is related of the matrix dimension.

The overall computational complexity of the algorithm is O(n), mainly determined by WRCA(1),WAB1(2),WRCA(3)W_{RCA}^{(1)},W_{A{B_1}}^{(2)},W_{RCA}^{(3)} and WRCAB1B2(4)W_{RCA{B_1}{B_2}}^{(4)}

3.2.
Matrix Transpose

The quantum circuit for matrix transpose is shown in Figure 4. Assume that a matrix S={aik} is of size N × M with N = 2n and M = 2m. The algorithm requires two pure states and is divided into two steps.

Figure 4.

Quantum Circuit for Matrix Transpose.

Step 1: Prepare the initial state

First, introduce an n -qubit register R and an m-qubit register C, where R and C enumerate the rows and columns of the matrix S, respectively. The elements of matrix S are encoded into the following pure state, |Ψ=i=0N1k=0M1aik|iR|kC,ik| aik |2=1,|\Psi \rangle = \sum\limits_{i = 0}^{N - 1} {\sum\limits_{k = 0}^{M - 1} {{a_{ik}}} } |i{\rangle _R}|k{\rangle _C},\quad \sum\limits_{ik} {{{\left| {{a_{ik}}} \right|}^2}} = 1,

Then, introduce a pure state |0〉D for an m-qubit register D. The initial state is give by 23|Φ0=|0D|Ψ=i=0N1k=0M1aik|0D|iR|kC.\matrix{ {\left| {{\Phi _0}} \right\rangle } \hfill & = \hfill & {|0{\rangle _D} \otimes |\Psi \rangle } \hfill \cr {} \hfill & = \hfill & {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{k = 0}^{M - 1} {{a_{ik}}} } |0{\rangle _D}|i{\rangle _R}|k{\rangle _C}.} \hfill \cr }

Step 2: Use a SWAP gate to exchange the states of D and C

We use a SWAP gate to exchange the states of the D and C registers. The operator SWAPDC is applied to |Φ0〉, yielding 24|Φ1=SWAPDC|Φ0=|Ψout|0C,\matrix{ {\left| {{\Phi _1}} \right\rangle } \hfill & = \hfill & {SWA{P_{DC}}\left| {{\Phi _0}} \right\rangle } \hfill \cr {} \hfill & = \hfill & {\left| {{\Psi _{out}}} \right\rangle |0{\rangle _C},} \hfill \cr } where 25|Ψout=(i=0N1k=0M1aik|kD|iR).\left| {{\Psi _{out}}} \right\rangle = \left( {\sum\limits_{i = 0}^{N - 1} {\sum\limits_{k = 0}^{M - 1} {{a_{ik}}} } |k{\rangle _D}|i{\rangle _R}} \right).

Now, the new rows and new columns of the current target matrix are enumerated by the states D and R respectively. Clearly, the success probability of this algorithm is 1.

Since the exchange operator SWAPDC only involves exchange operations when acting on different qubit pairs, its computational complexity is O(m).

Remark 1.

Formally the non-square matrix can be made a square matrix by including the proper number of zero rows or columns. In this case transposition of non-square matrix can be done as a transposition of a square matrix, i.e. by the swap operator SWAPRC. The complexity of this operation is O(n).

4.
Conclusion

We have developed quantum algorithms for two types of elementary row transformations of matrices, the trace and transpose of matrices. The operational mechanism of all these algorithms is as follows: First, construct the initial state of the entire system. Second, utilize unitary operations and ancillary qubits to mark the information items required by the algorithm. Third, apply the Hadamard transformation to superpose these information items. Finally, measure the ancillary qubits to eliminate the useless information items and retain only the useful ones.

In all these algorithms, the final result is stored in the probability amplitude of certain quantum states. Our success probability for adding one row of a matrix to another is G28{{{G^2}} \over 8}. The success probability for swapping two rows of a matrix is 124{1 \over {24}}, while the success probability of matrix transpose is 1. The success probability for the trace calculation is | i=0N1aii |223n{{{{\left| {\sum\nolimits_{i = 0}^{N - 1} {{a_{ii}}} } \right|}^2}} \over {{2^{3n}}}}. The success probabilities either depend solely on the elements of the matrix itself, or are fixed or related to the dimensions of the matrix. The larger the dimension, the smaller the success probability. To address this challenge, amplitude amplification techniques may be employed. It is worth noting that the computational complexity of these algorithms is related to the dimension of the matrices. It increases logarithmically as the matrix dimension grows. Specifically, the computational complexity is O(n) for elementary row operations and trace calculation, and O(m) for matrix transpose.

It would be desired to develop new methods to improve the success probability given by the final measurement in the algorithm, or explore the designs of other quantum algorithms for matrix operations based on multi-qubit Toffoli type gates and the simplest single qubit operations. It would be also interesting to conduct further researches on quantum algorithms for other matrix operations.

DOI: https://doi.org/10.2478/qic-2025-0031 | Journal eISSN: 3106-0544 | Journal ISSN: 1533-7146
Language: English
Page range: 568 - 585
Submitted on: Jul 17, 2025
|
Accepted on: Sep 10, 2025
|
Published on: Mar 9, 2026
In partnership with: Paradigm Publishing Services
Publication frequency: 1 issue per year

© 2026 Yu-Hang Liu, Yuan-Hong Tao, Jing-Run Lan, Shao-Ming Fei, published by Cerebration Science Publishing Co., Limited
This work is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 License.