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.In this demo we will restrict ourselves to matrices of size , with . A generalization to arbitrary can be done by completing the matrix into a larger one, or modifying the QSVT routine to an arbitrary . 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: where is some scaling factor, which is sometimes necessary for this unitarity completion. Given an access to , matrix inversion with QSVT implements a block-encoding for the inverse of Subsequently, we can use this unitary to solve a linear system, , by applying it on an initially prepared state (the state identifies the block in which the matrix is encoded). Up to post-selection we have: In more detail, Singular Value Decomposition (SVD) of a matrix is defined by where and are unitary matrices, and is a diagonal matrix that contains the singular values of (The square root of the eigenvalues of ). A singular value polynomial transform refers to where 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, from which we can deduce that: Applying a QSVT of an odd polynomial that approximates , on the block-encoding of , gives the matrix inversion of . Below we demonstrate how to implement a quantum linear solver for a specific problem. TheComplexity: Using QSVT, matrix inversion requires queries of the block-encoding unitary, where is the precision parameter. Typically, an efficient block-encoding scales as , resulting in an overall complexity for the QSVT linear solver. This improves exponentially in upon the original Harrow-Hassidim-Lloyd (HHL) algorithm, and exponentially in 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 removes the speedup; the advantage arises when only global properties such as expectation values, overlaps, or samples are required.)
- Input: An matrix with condition number (the ratio between the maximal and minimal singular values), and a vector prepared as a quantum state.
- Promise: The matrix is well-conditioned (invertible, with bounded spectrum) and can be efficiently block-encoded in a unitary (see technical note at the end of this demo).
- Output: A quantum state proportional to , encoding the solution of the linear system .
Keywords: Linear systems, Quantum Singular Value Transformation (QSVT), Block encoding, Matrix inversion, Exponential speedup, Oracle/Query complexity.
BlockEncoding class provides a high-level interface.
Given a block-encoding of the matrix and its effective condition number , 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 matrix that we want to invert.Output:
Output:
Block-Encoding
TheBlockEncoding 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:
from_matrix method automatically:
- Decomposes the matrix into Pauli terms
- Constructs an LCU block encoding
- Computes the scaling factor (Pauli 1-norm)
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-levelinverse method (which we revisit at the end). We need a polynomial approximation of .
Since diverges at it cannot be bounded by on , so we approximate it only on the range that contains the singular values of the block-encoded operator , namely .
The target function is
with a factor ensuring (which eases the QSVT angle finding).
According to Ref. [2], a degree of suffices to approximate with error . We use qsp_approximate to fit as a Chebyshev sum bounded by on , and qsvt_phases to obtain the corresponding QSVT phases.
Output:

Output:
Solving a Linear Equation
Next, we solve an instance of a linear equation . For this, we define some arbitrary vector.Output:
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.
Output:
Output:

block being zero, if we want to obtain a state .
Comparing to the Expected Result
Finally, we verify our quantum solution, by comparing it to the expected classical one.
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 internally, and returns a ready-to-use BlockEncoding of .
The same linear system is then solved with a single call, reproducing the by-hand result.
Output:
Output: