Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
Quantum Matrix Inversion (or Quantum Linear Solver) via QSVT provides a framework for solving linear systems exponentially faster than known classical methods under certain conditions [1]. The algorithm employs a polynomial transformation of singular values to block-encode the inverse of a matrix, which can then be applied on a prepared quantum state to solve a linear equation. Given an efficient routine to embed the classical matrix as a quantum function (block-encoding), this algorithm gives a clean and optimal way to implement matrix inversion compared to other quantum methods. Quantum linear solvers based on block-encoding have many applications, e.g., in Plasma Physics and Fluid Dynamics.
  • Input: An N×NN \times N matrix AA with condition number κ\kappa (the ratio between the maximal and minimal singular values), and a vector ∣b⟩|b\rangle prepared as a quantum state.
  • Promise: The matrix AA is well-conditioned (invertible, with bounded spectrum) and can be efficiently block-encoded in a unitary UAU_A (see technical note at the end of this demo).
  • Output: A quantum state proportional to ∣x⟩=A−1∣b⟩|x\rangle = A^{-1} |b\rangle, encoding the solution of the linear system Ax=bA x = b.
Complexity: Using QSVT, matrix inversion requires O(κlog⁡(κ/ϵ))O(\kappa \log(\kappa / \epsilon)) queries of the block-encoding unitary, where ϵ\epsilon is the precision parameter. Typically, an efficient block-encoding scales as poly(log⁡N)\mathrm{poly}(\log N), resulting in an overall O~(κ polylog(N,1/ϵ))\tilde{O}(\kappa \,\mathrm{polylog}(N, 1 / \epsilon)) complexity for the QSVT linear solver. This improves exponentially in ϵ\epsilon upon the original Harrow-Hassidim-Lloyd (HHL) algorithm, and exponentially in NN compared to classical linear solvers, while matching known lower bounds for quantum linear solvers [2]. (As with all quantum linear solvers, extracting the full solution state ∣x⟩|x\rangle removes the speedup; the advantage arises when only global properties such as expectation values, overlaps, or samples are required.)
Keywords: Linear systems, Quantum Singular Value Transformation (QSVT), Block encoding, Matrix inversion, Exponential speedup, Oracle/Query complexity.
In this demo we will restrict ourselves to matrices of size N×NN\times N, with N=2nN=2^n. A generalization to arbitrary NN can be done by completing the matrix into a larger one, or modifying the QSVT routine to an arbitrary NN. We emphasize that compared to the basic HHL algorithm, the QSVT approach is not restricted to Hermitian matrices. The QSVT algorithm is based on the idea of block-encoding a matrix into a larger unitary: U(A,s)=(A/s∗∗∗),(1)U_{(A,s)} = \begin{pmatrix} A/s & * \\ * & * \end{pmatrix}, \tag{1} where ss is some scaling factor, which is sometimes necessary for this unitarity completion. Given an access to U(A,s)U_{(A,s)}, matrix inversion with QSVT implements a block-encoding for the inverse of AA U(A−1,s′)=(A−1/s′∗∗∗).U_{(A^{-1},s')} = \begin{pmatrix} A^{-1}/s' & * \\ * & * \end{pmatrix}. Subsequently, we can use this unitary to solve a linear system, A∣x⟩=∣b⟩A|x\rangle = |b\rangle, by applying it on an initially prepared state ∣b⟩n∣0⟩block|b\rangle_n|0\rangle_{\rm block} (the state ∣0⟩block|0\rangle_{\rm block} identifies the block in which the matrix is encoded). Up to post-selection we have: U(A−1,s′)∣b⟩n∣0⟩block=A−1s′∣b⟩n∣0⟩block+ garbage.U_{(A^{-1},s')} |b\rangle_n|0\rangle_{\rm block} = \frac{A^{-1}}{s'}|b\rangle_n|0\rangle_{\rm block} + \text{ garbage}. In more detail, Singular Value Decomposition (SVD) of a matrix AA is defined by A=WΣV†,A = W\Sigma V^\dagger, where WW and VV are unitary matrices, and Σ\Sigma is a diagonal matrix that contains the singular values of AA (The square root of the eigenvalues of AA†A A^{\dagger}). A singular value polynomial transform refers to Poly(SV)(A)={WPoly(Σ)V†, for odd polynomialVPoly(Σ)V†, for even polynomial  ,\mathrm{Poly}^{\rm (SV)}(A) = \left\{\begin{array}{l l} W\mathrm{Poly}(\Sigma) V^\dagger, & \text{ for odd polynomial} \\ V\mathrm{Poly}(\Sigma) V^\dagger, & \text{ for even polynomial} \end{array} \right . \,\,, where Poly(σ)\mathrm{Poly(\sigma)} is a polynomial with a well-defined parity. The QSVT routine allows to block-encode such singular value polynomial transforms, based on the Quantum Signal Processing (QSP) approach. For the case of matrix inversion, we have the identities, A†=V†ΣW,A−1=V†Σ−1W,\begin{aligned} A^\dagger &=V^\dagger \Sigma W,\\ A^{-1} &= V^\dagger \Sigma^{-1}W, \end{aligned} from which we can deduce that: Applying a QSVT of an odd polynomial that approximates Poly(σ)∼1/σ\mathrm{Poly(\sigma)}\sim 1/\sigma, on the block-encoding of A†A^{\dagger}, gives the matrix inversion of AA. Below we demonstrate how to implement a quantum linear solver for a specific problem. The BlockEncoding class provides a high-level interface. Given a block-encoding of the matrix AA and its effective condition number (σmin⁡/s)−1(\sigma_{\min}/s)^{-1}, we can easily obtain the inverse block-encoding using the inverse method, which handles the QSVT polynomial approximation internally.

Example: Inverting a Random Matrix

Setting an Example

We start by defining a specific problem: a matrix and its condition number. We take a simple use case: a random 4×44 \times 4 matrix AA that we want to invert.
Output:
Let’s verify the matrix properties:
Output:

Block-Encoding

The BlockEncoding class provides a convenient from_matrix method that automatically decomposes any matrix into a block encoding via Pauli decomposition and LCU (Linear Combination of Unitaries).
Output:
The from_matrix method automatically:
  • Decomposes the matrix into Pauli terms
  • Constructs an LCU block encoding
  • Computes the scaling factor α\alpha (Pauli 1-norm)
We evaluate the condition number of AA, κ=σmax⁡/σmin⁡=1/σmin⁡\kappa = \sigma_{\max}/\sigma_{\min} = 1/\sigma_{\min} (using σmax⁡=1\sigma_{\max}=1 after normalization), and the effective condition number of the block-encoded operator A/αA/\alpha, κeff=α κ=α/σmin⁡\kappa_{\rm eff} = \alpha\,\kappa = \alpha/\sigma_{\min}, which controls the QSVT polynomial degree. In real applications, this value needs to be approximated through prior knowledge of the problem or classical methods. For our small example, we compute it directly.
Output:

Building the Inverse by Hand with QSVT

To make the algorithm explicit, we first build the inverse block-encoding ourselves with the QSVT machinery, rather than calling the high-level inverse method (which we revisit at the end). We need a polynomial approximation of 1/x1/x. Since 1/x1/x diverges at x=0x=0 it cannot be bounded by 11 on [−1,1][-1,1], so we approximate it only on the range that contains the singular values of the block-encoded operator A/αA/\alpha, namely [σmin⁡/α,σmax⁡/α][\sigma_{\min}/\alpha, \sigma_{\max}/\alpha]. The target function is f(x)=SCALEκeff x,x∈[σmin⁡/α,σmax⁡/α],f(x) = \frac{\mathrm{SCALE}}{\kappa_{\rm eff}\, x}, \qquad x\in[\sigma_{\min}/\alpha, \sigma_{\max}/\alpha], with a factor SCALE<1\mathrm{SCALE}<1 ensuring ∣f(x)∣≤1|f(x)|\le 1 (which eases the QSVT angle finding). According to Ref. [2], a degree of ∼κefflog⁡(κeff/ϵ)\sim \kappa_{\rm eff} \log(\kappa_{\rm eff}/\epsilon) suffices to approximate ff with error ϵ\epsilon. We use qsp_approximate to fit ff as a Chebyshev sum bounded by 11 on [−1,1][-1,1], and qsvt_phases to obtain the corresponding QSVT phases.
Output:
output
Output:

Solving a Linear Equation

Next, we solve an instance of a linear equation Ax⃗=b⃗A\vec{x} = \vec{b}. For this, we define some arbitrary b⃗\vec{b} vector.
Output:
Now we build the quantum model. We prepare the initial state ∣b⟩|b\rangle and then call qsvt_inversion, driving it with the block encoding: the projector marks its block register being zero, its unitary is applied through block_encoding.unitary, and the extra QSVT auxiliary is the last block qubit.
Let us synthesize, visualize, and execute. For the execution, we choose a statevector simulator, as we are considering a small problem for demonstrating the algorithm. In addition, later on we would like to verify our result against the expected classical one.
Output:
Output:
image.png If our matrix AA is block-encoded with a block variable of size mm, then the QSVT routine block encodes the inverse of AA on m+1m+1 qubits, where the additional qubit is the QSVT auxiliary. Therefore, we need to post-select on block being zero, if we want to obtain a state ∼A−1∣b⟩\sim A^{-1}|b\rangle.

Comparing to the Expected Result

Finally, we verify our quantum solution, by comparing it to the expected classical one.
After post-selecting the block on zero, the QSVT routine returns ∣x⟩∝p ⁣(A/α)b⃗∣b⃗∣,p ⁣(A/α)≈SCALEκeff(Aα)−1=SCALE ακeff A−1.|x\rangle \propto p\!\left(A/\alpha\right)\frac{\vec b}{|\vec b|}, \qquad p\!\left(A/\alpha\right) \approx \frac{\mathrm{SCALE}}{\kappa_{\rm eff}}\left(\frac{A}{\alpha}\right)^{-1} = \frac{\mathrm{SCALE}\,\alpha}{\kappa_{\rm eff}}\,A^{-1}. Collecting the prefactors — the α\alpha cancels since κeff=ακ\kappa_{\rm eff}=\alpha\kappa — the solution is recovered as x⃗=∣b⃗∣ κSCALE Re ⁣(amplitudes).\vec x = \frac{|\vec b|\,\kappa}{\mathrm{SCALE}}\,\mathrm{Re}\!\left(\text{amplitudes}\right).
Let’s compare it with the expected solution: Comment: even after removing the global phase to ensure a real solution, the quantum solution can be obtained up to a sign.
output

One-Line Inversion with BlockEncoding.inverse

The implementation above of the inverse (finding the QSVT phases and wiring up qsvt_inversion) is packaged in the BlockEncoding.inverse method. It takes the (bare) condition number and a target precision, forms the effective condition number κeff=ακ\kappa_{\rm eff}=\alpha\kappa internally, and returns a ready-to-use BlockEncoding of A−1A^{-1}. The same linear system is then solved with a single call, reproducing the by-hand result.
Output:
Output:

References

[1]: Martyn JM, Rossi ZM, Tan AK, Chuang IL. Grand unification of quantum algorithms. PRX Quantum 2, 040203. (2021). [2]: A. M. Childs, R. Kothari, and R. D. Somma. Quantum algorithm for systems of linear equations with exponentially improved dependence on precision. SIAM Journal on Computing 46, 1920-1950 (2017).

A Note on Efficient Block-Encoding, Condition Number, and Complexity

An efficient block-encoding of a matrix AA means that the resources to construct the unitary UA,sU_{A,s}, as well as the scaling factor ss, scales poly-logarithmically with the matrix dimension. This technical note concerns the latter point. First, we note that an optimal value for the scaling factor ss is the maximal singular value σmax⁡\sigma_{\max}, namely s≥σmax⁡s\geq \sigma_{\max}. This comes directly from the requirement that ∣∣A/s∣∣≤1||A/s||\leq 1. For that reason, the effective condition number used throughout the demo, κ=s/σmin⁡\kappa = s/\sigma_{\min}, is bounded from below by the actual condition number σmax⁡/σmin⁡\sigma_{\max}/\sigma_{\min}. Next, note that if ss scales linearly with the problem size, then the exponential speedup is lost. This is because the polynomial degree then scales as O(N)O(N). In addition, the amplitude of the encoded solution scales the same (although it also depends on the input vector ∣b⟩|b\rangle), which requires an additional amplitude amplification routine that adds a factor of O(N)O(\sqrt{N}) to the overall resources.