Skip to content

Commit 698fa9d

Browse files
Create SparseMatrix block encoding
1 parent 2c2866b commit 698fa9d

File tree

7 files changed

+459
-1
lines changed

7 files changed

+459
-1
lines changed

dev_tools/autogenerate-bloqs-notebooks-v2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@
610610
qualtran.bloqs.block_encoding.product._PRODUCT_DOC,
611611
qualtran.bloqs.block_encoding.linear_combination._LINEAR_COMBINATION_DOC,
612612
qualtran.bloqs.block_encoding.phase._PHASE_DOC,
613+
qualtran.bloqs.block_encoding.sparse_matrix._SPARSE_MATRIX_DOC,
613614
],
614615
directory=f'{SOURCE_DIR}/bloqs/block_encoding/',
615616
),

qualtran/bloqs/block_encoding/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
from qualtran.bloqs.block_encoding.linear_combination import LinearCombination
2525
from qualtran.bloqs.block_encoding.phase import Phase
2626
from qualtran.bloqs.block_encoding.product import Product
27+
from qualtran.bloqs.block_encoding.sparse_matrix import SparseMatrix
2728
from qualtran.bloqs.block_encoding.tensor_product import TensorProduct
2829
from qualtran.bloqs.block_encoding.unitary import Unitary

qualtran/bloqs/block_encoding/block_encoding.ipynb

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,141 @@
13041304
"show_call_graph(linear_combination_block_encoding_g)\n",
13051305
"show_counts_sigma(linear_combination_block_encoding_sigma)"
13061306
]
1307+
},
1308+
{
1309+
"cell_type": "markdown",
1310+
"id": "e36b6098",
1311+
"metadata": {
1312+
"cq.autogen": "SparseMatrix.bloq_doc.md"
1313+
},
1314+
"source": [
1315+
"## `SparseMatrix`\n",
1316+
"Block encoding of a sparse-access matrix.\n",
1317+
"\n",
1318+
"Given row, column, and entry oracles $O_r$, $O_c$, and $O_A$ for an $s$-sparse matrix\n",
1319+
"$A \\in \\mathbb{C}^{2^n \\times 2^n}$, i.e. one where each row / column has exactly $s$ non-zero\n",
1320+
"entries, computes a $(s, n+1, \\epsilon)$-block encoding of $A$ as follows:\n",
1321+
"```\n",
1322+
" ┌────┐ ┌────┐\n",
1323+
" |0> ─┤ ├─ |0> ─────────────┤ ├───────────────\n",
1324+
" │ │ ┌──┐ │ │ ┌──┐\n",
1325+
" │ U │ = │ n│ ┌────┐ │ O │ ┌────┐ │ n│\n",
1326+
"|0^n> ─┤ A ├─ |0^n> ─┤H ├─┤ ├─┤ A ├─X─┤ ├─┤H ├─\n",
1327+
" │ │ └──┘ │ O │ │ │ │ │ O* │ └──┘\n",
1328+
"|Psi> ─┤ ├─ |Psi> ──────┤ c ├─┤ ├─X─┤ r ├──────\n",
1329+
" └────┘ └────┘ └────┘ └────┘\n",
1330+
"```\n",
1331+
"\n",
1332+
"To encode a matrix of irregular dimension, the matrix should first be embedded into one of\n",
1333+
"dimension $2^n \\times 2^n$ for suitable $n$.\n",
1334+
"To encode a matrix where each row / column has at most $s$ non-zero entries, some zeroes should\n",
1335+
"be treated as if they were non-zero so that each row / column has exactly $s$ non-zero entries.\n",
1336+
"\n",
1337+
"#### Parameters\n",
1338+
" - `row_oracle`: The row oracle $O_r$. See `RowColumnOracle` for definition.\n",
1339+
" - `col_oracle`: The column oracle $O_c$. See `RowColumnOracle` for definition.\n",
1340+
" - `entry_oracle`: The entry oracle $O_A$. See `EntryOracle` for definition.\n",
1341+
" - `eps`: The precision of the block encoding. \n",
1342+
"\n",
1343+
"#### Registers\n",
1344+
" - `system`: The system register.\n",
1345+
" - `ancilla`: The ancilla register.\n",
1346+
" - `resource`: The resource register (present only if bitsize > 0). \n",
1347+
"\n",
1348+
"#### References\n",
1349+
" - [Lecture Notes on Quantum Algorithms for Scientific Computation]( https://arxiv.org/pdf/2201.08309). Lin Lin (2022). Ch. 6.5.\n"
1350+
]
1351+
},
1352+
{
1353+
"cell_type": "code",
1354+
"execution_count": null,
1355+
"id": "87710c02",
1356+
"metadata": {
1357+
"cq.autogen": "SparseMatrix.bloq_doc.py"
1358+
},
1359+
"outputs": [],
1360+
"source": [
1361+
"from qualtran.bloqs.block_encoding import SparseMatrix"
1362+
]
1363+
},
1364+
{
1365+
"cell_type": "markdown",
1366+
"id": "8b9bd4f0",
1367+
"metadata": {
1368+
"cq.autogen": "SparseMatrix.example_instances.md"
1369+
},
1370+
"source": [
1371+
"### Example Instances"
1372+
]
1373+
},
1374+
{
1375+
"cell_type": "code",
1376+
"execution_count": null,
1377+
"id": "6ad71b09",
1378+
"metadata": {
1379+
"cq.autogen": "SparseMatrix.sparse_matrix_block_encoding"
1380+
},
1381+
"outputs": [],
1382+
"source": [
1383+
"from qualtran.bloqs.block_encoding.sparse_matrix import (\n",
1384+
" UniformEntryOracle,\n",
1385+
" UniformRowColumnOracle,\n",
1386+
")\n",
1387+
"\n",
1388+
"row_oracle = UniformRowColumnOracle(n=2, num_nonzero=4)\n",
1389+
"col_oracle = UniformRowColumnOracle(n=2, num_nonzero=4)\n",
1390+
"entry_oracle = UniformEntryOracle(n=2, entry=0.3)\n",
1391+
"sparse_matrix_block_encoding = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0)"
1392+
]
1393+
},
1394+
{
1395+
"cell_type": "markdown",
1396+
"id": "ca16f4df",
1397+
"metadata": {
1398+
"cq.autogen": "SparseMatrix.graphical_signature.md"
1399+
},
1400+
"source": [
1401+
"#### Graphical Signature"
1402+
]
1403+
},
1404+
{
1405+
"cell_type": "code",
1406+
"execution_count": null,
1407+
"id": "6c5da6eb",
1408+
"metadata": {
1409+
"cq.autogen": "SparseMatrix.graphical_signature.py"
1410+
},
1411+
"outputs": [],
1412+
"source": [
1413+
"from qualtran.drawing import show_bloqs\n",
1414+
"show_bloqs([sparse_matrix_block_encoding],\n",
1415+
" ['`sparse_matrix_block_encoding`'])"
1416+
]
1417+
},
1418+
{
1419+
"cell_type": "markdown",
1420+
"id": "847bacb4",
1421+
"metadata": {
1422+
"cq.autogen": "SparseMatrix.call_graph.md"
1423+
},
1424+
"source": [
1425+
"### Call Graph"
1426+
]
1427+
},
1428+
{
1429+
"cell_type": "code",
1430+
"execution_count": null,
1431+
"id": "42070c26",
1432+
"metadata": {
1433+
"cq.autogen": "SparseMatrix.call_graph.py"
1434+
},
1435+
"outputs": [],
1436+
"source": [
1437+
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
1438+
"sparse_matrix_block_encoding_g, sparse_matrix_block_encoding_sigma = sparse_matrix_block_encoding.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
1439+
"show_call_graph(sparse_matrix_block_encoding_g)\n",
1440+
"show_counts_sigma(sparse_matrix_block_encoding_sigma)"
1441+
]
13071442
}
13081443
],
13091444
"metadata": {

0 commit comments

Comments
 (0)