-
Notifications
You must be signed in to change notification settings - Fork 228
Added a demo for Resource Estimation of Matrix Inversion using QSVT #1667
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 52 commits
26f16ad
e148308
e9f4bba
0ed9e8d
098aac9
cecc65b
cc31fe1
c32a5f4
53d4c65
d84f591
2bca59a
e2c179f
785496a
021effd
4fe7f90
4264774
780f202
62a3440
da7e221
76831ce
81839d0
f7acfa4
a910bea
a25b241
216052b
c5d3fd3
81de671
e6f098b
b307980
b8f9f46
90a5ca2
f769a26
2bb8cbc
b7a6be1
5fee966
d5e4995
eb18a8b
e3bb164
f32f3b7
3d0c693
1340d52
15debee
1da0832
e4c807f
10fd917
b79d4f9
625f048
b1ab856
0c729e7
439ad88
c8d9524
6f15444
c810048
11c4c4e
1967d87
7611e70
8d6179f
4bdd428
4a4e27d
43cab9d
9fba4c0
3e572ed
ffe443a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,360 @@ | ||||||
| r""" | ||||||
| Optimizing QSVT data loading by exploiting structure | ||||||
| ==================================================== | ||||||
| .. meta:: | ||||||
| :property="og:description": Learn how to estimate the cost of matrix inversion with QSVT for CFD applications | ||||||
| :property="og:image": https://pennylane.ai/qml/_static/demonstration_assets/resource_estimation.jpeg | ||||||
|
|
||||||
| .. related:: | ||||||
| tutorial_apply_qsvt QSVT in Practice | ||||||
| tutorial_lcu_blockencoding Linear combination of unitaries and block encodings | ||||||
|
|
||||||
| Solving systems of linear equations is important for a wide range of industries, such as healthcare, | ||||||
| transportation, finance, chemistry, and even quantum computing. The Quantum Singular Value Transformation (QSVT) algorithm can implement matrix inversion to solve such | ||||||
| equations on a quantum computer [#chuang2021]_. For more information on how to use PennyLane's | ||||||
| :func:`~.pennylane.qsvt` functionality to run matrix inversion on a quantum computer see our demo on `QSVT in | ||||||
| Practice <tutorial_apply_qsvt>`_. | ||||||
|
|
||||||
| It turns out that the bottleneck of QSVT is | ||||||
| typically the cost of encoding the matrix onto a quantum computer in the first place! While this **data loading** cost is | ||||||
| significant for any general matrix, in real life our problems often have patterns and structure! | ||||||
|
|
||||||
| By exploiting the structure of a problem, we can significantly reduce the quantum resource cost of the algorithm, thereby making QSVT based matrix inversion more accessible to implement on nearer term fault-tolerant quantum | ||||||
| hardware. | ||||||
|
|
||||||
| This demo, based on our recent paper `Quantum compilation framework for data loading | ||||||
drdren marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| <https://arxiv.org/abs/2512.05183>`_ [#linaje2025]_, will showcase an optimized block encoding strategy that | ||||||
| uses the sparsity of the matrix to significantly reduce the cost of QSVT. We will focus on a particular matrix | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that exploits the structure of the matrix to ...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous paragraph starts with "By exploiting the structure of the ...". It reads like a bit of a broken record to me. |
||||||
| inversion problem that arises in computational fluid dynamics (CFD) [#lapworth2022]_. | ||||||
|
|
||||||
| Problem Setup | ||||||
| ------------- | ||||||
| The two-dimensional lid-driven cavity flow (2D-LDC) is a canonical benchmark in CFD for verifying numerical | ||||||
| schemes that solve the incompressible Navier–Stokes equations. Determining the | ||||||
| fluid flow within the cavity requires solving the pressure correction equations by inverting the associated | ||||||
| pressure correction matrix (:math:`A`). A key observation is that :math:`A` is highly structured and sparse. | ||||||
| The figure below highlights the non-zero entries of :math:`A` with a dimensionality of :math:`(64 \times 64)` | ||||||
| [#lapworth2022]_. | ||||||
|
|
||||||
| | | ||||||
|
|
||||||
| .. figure:: ../_static/demonstration_assets/re_qsvt_cfd/sparse_diagonal_matrix.png | ||||||
| :align: center | ||||||
| :width: 50% | ||||||
| :target: javascript:void(0) | ||||||
|
|
||||||
| | | ||||||
|
|
||||||
| To invert this matrix using QSVT, we will need to **load** it onto the quantum computer using a block encoding. | ||||||
| The standard technique for block encoding any (square) matrix :math:`A` is the method of `linear combination of | ||||||
| unitaries (LCUs) <tutorial_lcu_blockencoding>`_. However, it suffers from a few fatal flaws (try saying that five | ||||||
| times fast). | ||||||
|
|
||||||
| The number of terms in the LCU scales as :math:`O(4^{n})` in the number of qubits, and thus the cost of the block | ||||||
| encoding also scales exponentially. Even computing the LCU decomposition becomes a computational bottleneck. | ||||||
| Furthermore, there is no way of knowing a priori how many terms there will be in the LCU. For these reasons, a | ||||||
| general "one size fits all" block encoding scheme is usually too expensive for our systems of interest. | ||||||
|
|
||||||
| .. _naive_block_encoding: | ||||||
| Resource cost of naive block encoding | ||||||
| ------------------------------------------ | ||||||
| Let's explore the average cost of block encoding a matrix in the standard way. We suppose that this matrix has size :math:`2^{20} \times 2^{20}` and can be written as the superposition of $4^{20}$ Pauli words. Note that not all matrices of that size can be written in this manner, so the resulting resource estimate is not necessarily general. However, it does illustrate the cost of naively block encoding a particular matrix of this size. | ||||||
|
|
||||||
drdren marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| .. code-block:: python | ||||||
|
|
||||||
| import pennylane.estimator as qre | ||||||
|
|
||||||
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| num_qubits = 20 | ||||||
| matrix_size = 2**num_qubits | ||||||
|
|
||||||
| lcu_A = qre.PauliHamiltonian( | ||||||
| num_qubits = num_qubits, | ||||||
| pauli_terms = {"Z"*(num_qubits//2): 4**num_qubits}, | ||||||
| ) # 4^20 Pauli words comprise this matrix | ||||||
|
|
||||||
| def Standard_BE(prep, sel): | ||||||
| return qre.ChangeOpBasis(prep, sel, qre.Adjoint(prep)) | ||||||
|
|
||||||
| Prep = qre.QROMStatePreparation(num_qubits) # Preparing a single qubit in the target state | ||||||
| Select = qre.SelectPauli(lcu_A) # Select the operators in the LCU | ||||||
|
|
||||||
| resources = qre.estimate(Standard_BE)(Prep, Select) # Estimate the resource requirement | ||||||
| print(resources) | ||||||
|
|
||||||
|
|
||||||
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| With one line, we can see that that the estimated T gate cost of naive block encoding this matrix is :math:`1 \times 10^{12}`. This block encoding is called many times within an instance of the QSVT algorithm, and can be the dominant cost. Now that we have established a baseline of the `standard' cost, we ask: Can we do better? | ||||||
|
||||||
| With one line, we can see that that the estimated T gate cost of naive block encoding this matrix is :math:`1 \times 10^{12}`. This block encoding is called many times within an instance of the QSVT algorithm, and can be the dominant cost. Now that we have established a baseline of the `standard' cost, we ask: Can we do better? | |
| With one line, we can see that that the estimated $T$ gate cost of naive block encoding of this matrix is :math:`1 \times 10^{12}`. This block encoding is called many times within an instance of the QSVT algorithm, and can be the dominant cost. Now that we have established a baseline of the `standard' cost, we ask: Can we do better? |
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
drdren marked this conversation as resolved.
Show resolved
Hide resolved
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this part up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before the image?
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Jaybsoni marked this conversation as resolved.
Show resolved
Hide resolved
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
drdren marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Jaybsoni marked this conversation as resolved.
Show resolved
Hide resolved
drdren marked this conversation as resolved.
Show resolved
Hide resolved
Jaybsoni marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Standard block encoding without any structural knowledge
qsvt_op = qre.QSVT(
block_encoding = Standard_BE(Prep, Select),
encoding_dims = (2num_qubits, 2num_qubits), # The shape of matrix A
poly_deg = 10**8,
)
gate_set = {'X', 'Y', 'Z', 'Hadamard', 'T', 'S', 'CNOT'}
resources = qre.estimate(qsvt_op, gate_set)
print(resources)
--- Resources: ---
Total wires: 123
algorithmic wires: 60
allocated wires: 63
zero state: 63
any state: 0
Total gates : 5.498E+21
'T': 4.398E+20,
'CNOT': 1.649E+21,
'X': 2.199E+20,
'Z': 3.299E+20,
'S': 3.299E+20,
'Hadamard': 2.529E+21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that there is a blue line on the left hand side of this figure, probably an artifact from a screenshot. That should go! Also, is there a higher resolution version of this image?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cropped out the blue line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got this directly from the paper. I guess we could try to recreate it if it's too blurry?