-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SparseMatrixHermitian
block-encoding
- Loading branch information
Showing
7 changed files
with
650 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
qualtran/bloqs/block_encoding/sparse_matrix_hermitian.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8e5e678f", | ||
"metadata": { | ||
"cq.autogen": "title_cell" | ||
}, | ||
"source": [ | ||
"# Sparse Matrix Hermitian" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "70980f2b", | ||
"metadata": { | ||
"cq.autogen": "top_imports" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran import Bloq, CompositeBloq, BloqBuilder, Signature, Register\n", | ||
"from qualtran import QBit, QInt, QUInt, QAny\n", | ||
"from qualtran.drawing import show_bloq, show_call_graph, show_counts_sigma\n", | ||
"from typing import *\n", | ||
"import numpy as np\n", | ||
"import sympy\n", | ||
"import cirq" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8db414b7", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.bloq_doc.md" | ||
}, | ||
"source": [ | ||
"## `SparseMatrixHermitian`\n", | ||
"Hermitian Block encoding of a sparse-access Hermitian matrix.\n", | ||
"\n", | ||
"Given column and entry oracles $O_c$ and $O_A$ for an $s$-sparse Hermitian 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", | ||
"a |0> ─┤ ├─ |0> ───────────────────────X────────────────────\n", | ||
" │ │ ┌──┐ | ┌──┐\n", | ||
" │ U │ = │ n│ ┌────┐ ┌────┐ | ┌────┐ ┌────┐ │ n│\n", | ||
"l |0^n> ─┤ A ├─ |0^n> ─┤H ├─┤ O ├─┤ ├─X──|─┤ ├─┤ O* ├─┤H ├─\n", | ||
" │ │ └──┘ | c | │ │ | | │ │ | c | └──┘\n", | ||
" │ │ └────┘ │ O │ │ | │ O* │ └────┘\n", | ||
"b |0> ─┤ ├─ |0> ────────|────┤ A ├─|──X─┤ A ├───|─────────\n", | ||
" | | ┌────┐ | | | | | ┌────┐\n", | ||
" | | | O | | | | | | | O* |\n", | ||
"j |Psi> ─┤ ├─ |Psi> ──────┤ c ├─┤ ├─X────┤ ├─┤ c ├──────\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", | ||
"For encoding a non-hermitian matrix, or a slightly more efficient (but non Hermitian-encoding)\n", | ||
"of a matrix, use :class:`SparseMatrix` instead.\n", | ||
"\n", | ||
"#### Parameters\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", | ||
" - `is_controlled`: if True, returns the controlled block-encoding. \n", | ||
"\n", | ||
"#### Registers\n", | ||
" - `ctrl`: The single qubit control register. (present only if `is_controlled` is `True`)\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. Proposition 6.8, Fig 6.7.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f31bfd74", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.bloq_doc.py" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran.bloqs.block_encoding import SparseMatrixHermitian" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "435f31d2", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.example_instances.md" | ||
}, | ||
"source": [ | ||
"### Example Instances" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "720f3f9b", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.sparse_matrix_symb_hermitian_block_encoding" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran.bloqs.block_encoding.sparse_matrix import TopLeftRowColumnOracle\n", | ||
"from qualtran.bloqs.block_encoding.sparse_matrix_hermitian import UniformSqrtEntryOracle\n", | ||
"\n", | ||
"n = sympy.Symbol('n', positive=True, integer=True)\n", | ||
"col_oracle = TopLeftRowColumnOracle(system_bitsize=n)\n", | ||
"entry_oracle = UniformSqrtEntryOracle(system_bitsize=n, entry=0.3)\n", | ||
"sparse_matrix_symb_hermitian_block_encoding = SparseMatrixHermitian(\n", | ||
" col_oracle, entry_oracle, eps=0\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "70e512ff", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.sparse_matrix_hermitian_block_encoding" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran.bloqs.block_encoding.sparse_matrix import TopLeftRowColumnOracle\n", | ||
"from qualtran.bloqs.block_encoding.sparse_matrix_hermitian import UniformSqrtEntryOracle\n", | ||
"\n", | ||
"col_oracle = TopLeftRowColumnOracle(system_bitsize=2)\n", | ||
"entry_oracle = UniformSqrtEntryOracle(system_bitsize=2, entry=0.3)\n", | ||
"sparse_matrix_hermitian_block_encoding = SparseMatrixHermitian(col_oracle, entry_oracle, eps=0)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "6e8d3efa", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.graphical_signature.md" | ||
}, | ||
"source": [ | ||
"#### Graphical Signature" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "96d58575", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.graphical_signature.py" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran.drawing import show_bloqs\n", | ||
"show_bloqs([sparse_matrix_symb_hermitian_block_encoding, sparse_matrix_hermitian_block_encoding],\n", | ||
" ['`sparse_matrix_symb_hermitian_block_encoding`', '`sparse_matrix_hermitian_block_encoding`'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e0108dfc", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.call_graph.md" | ||
}, | ||
"source": [ | ||
"### Call Graph" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "87933b2f", | ||
"metadata": { | ||
"cq.autogen": "SparseMatrixHermitian.call_graph.py" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from qualtran.resource_counting.generalizers import ignore_split_join\n", | ||
"sparse_matrix_symb_hermitian_block_encoding_g, sparse_matrix_symb_hermitian_block_encoding_sigma = sparse_matrix_symb_hermitian_block_encoding.call_graph(max_depth=1, generalizer=ignore_split_join)\n", | ||
"show_call_graph(sparse_matrix_symb_hermitian_block_encoding_g)\n", | ||
"show_counts_sigma(sparse_matrix_symb_hermitian_block_encoding_sigma)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.