Skip to content
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

Create SparseMatrix block encoding #1143

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev_tools/autogenerate-bloqs-notebooks-v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@
qualtran.bloqs.block_encoding.product._PRODUCT_DOC,
qualtran.bloqs.block_encoding.linear_combination._LINEAR_COMBINATION_DOC,
qualtran.bloqs.block_encoding.phase._PHASE_DOC,
qualtran.bloqs.block_encoding.sparse_matrix._SPARSE_MATRIX_DOC,
],
directory=f'{SOURCE_DIR}/bloqs/block_encoding/',
),
Expand Down
1 change: 1 addition & 0 deletions qualtran/bloqs/block_encoding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
from qualtran.bloqs.block_encoding.linear_combination import LinearCombination
from qualtran.bloqs.block_encoding.phase import Phase
from qualtran.bloqs.block_encoding.product import Product
from qualtran.bloqs.block_encoding.sparse_matrix import SparseMatrix
from qualtran.bloqs.block_encoding.tensor_product import TensorProduct
from qualtran.bloqs.block_encoding.unitary import Unitary
132 changes: 132 additions & 0 deletions qualtran/bloqs/block_encoding/block_encoding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,138 @@
"show_call_graph(linear_combination_block_encoding_g)\n",
"show_counts_sigma(linear_combination_block_encoding_sigma)"
]
},
{
"cell_type": "markdown",
"id": "e36b6098",
"metadata": {
"cq.autogen": "SparseMatrix.bloq_doc.md"
},
"source": [
"## `SparseMatrix`\n",
"Block encoding of a sparse-access matrix.\n",
"\n",
"Given row, column, and entry oracles $O_r$, $O_c$, and $O_A$ for an $s$-sparse matrix\n",
"$A \\in \\mathbb{C}^{2^n \\times 2^n}$, i.e. one where each row / column has exactly $s$ non-zero\n",
"entries, computes a $(s, n+1, \\epsilon)$-block encoding of $A$ as follows:\n",
"```\n",
" ┌────┐ ┌────┐\n",
" |0> ─┤ ├─ |0> ─────────────┤ ├───────────────\n",
" │ │ ┌──┐ │ │ ┌──┐\n",
" │ U │ = │ n│ ┌────┐ │ O │ ┌────┐ │ n│\n",
"|0^n> ─┤ A ├─ |0^n> ─┤H ├─┤ ├─┤ A ├─X─┤ ├─┤H ├─\n",
" │ │ └──┘ │ O │ │ │ │ │ O* │ └──┘\n",
"|Psi> ─┤ ├─ |Psi> ──────┤ c ├─┤ ├─X─┤ r ├──────\n",
" └────┘ └────┘ └────┘ └────┘\n",
"```\n",
"\n",
"To encode a matrix of irregular dimension, the matrix should first be embedded into one of\n",
"dimension $2^n \\times 2^n$ for suitable $n$.\n",
"To encode a matrix where each row / column has at most $s$ non-zero entries, some zeroes should\n",
"be treated as if they were non-zero so that each row / column has exactly $s$ non-zero entries.\n",
"\n",
"#### Parameters\n",
" - `row_oracle`: The row oracle $O_r$. See `RowColumnOracle` for definition.\n",
" - `col_oracle`: The column oracle $O_c$. See `RowColumnOracle` for definition.\n",
" - `entry_oracle`: The entry oracle $O_A$. See `EntryOracle` for definition.\n",
" - `eps`: The precision of the block encoding. \n",
"\n",
"#### Registers\n",
" - `system`: The system register.\n",
" - `ancilla`: The ancilla register.\n",
" - `resource`: The resource register (present only if bitsize > 0). \n",
"\n",
"#### References\n",
" - [Lecture Notes on Quantum Algorithms for Scientific Computation](https://arxiv.org/abs/2201.08309). Lin Lin (2022). Ch. 6.5.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87710c02",
"metadata": {
"cq.autogen": "SparseMatrix.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.block_encoding import SparseMatrix"
]
},
{
"cell_type": "markdown",
"id": "8b9bd4f0",
"metadata": {
"cq.autogen": "SparseMatrix.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ad71b09",
"metadata": {
"cq.autogen": "SparseMatrix.sparse_matrix_block_encoding"
},
"outputs": [],
"source": [
"from qualtran.bloqs.block_encoding.sparse_matrix import FullRowColumnOracle, UniformEntryOracle\n",
"\n",
"row_oracle = FullRowColumnOracle(2)\n",
"col_oracle = FullRowColumnOracle(2)\n",
"entry_oracle = UniformEntryOracle(system_bitsize=2, entry=0.3)\n",
"sparse_matrix_block_encoding = SparseMatrix(row_oracle, col_oracle, entry_oracle, eps=0)"
]
},
{
"cell_type": "markdown",
"id": "ca16f4df",
"metadata": {
"cq.autogen": "SparseMatrix.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c5da6eb",
"metadata": {
"cq.autogen": "SparseMatrix.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([sparse_matrix_block_encoding],\n",
" ['`sparse_matrix_block_encoding`'])"
]
},
{
"cell_type": "markdown",
"id": "847bacb4",
"metadata": {
"cq.autogen": "SparseMatrix.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "42070c26",
"metadata": {
"cq.autogen": "SparseMatrix.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"sparse_matrix_block_encoding_g, sparse_matrix_block_encoding_sigma = sparse_matrix_block_encoding.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(sparse_matrix_block_encoding_g)\n",
"show_counts_sigma(sparse_matrix_block_encoding_sigma)"
]
}
],
"metadata": {
Expand Down
Loading
Loading