Skip to content

Commit

Permalink
Add SparseMatrixHermitian block-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp committed Nov 2, 2024
1 parent baad879 commit b1ee833
Show file tree
Hide file tree
Showing 7 changed files with 650 additions and 0 deletions.
8 changes: 8 additions & 0 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import qualtran.bloqs.block_encoding.phase
import qualtran.bloqs.block_encoding.product
import qualtran.bloqs.block_encoding.sparse_matrix
import qualtran.bloqs.block_encoding.sparse_matrix_hermitian
import qualtran.bloqs.block_encoding.tensor_product
import qualtran.bloqs.block_encoding.unitary
import qualtran.bloqs.bookkeeping
Expand Down Expand Up @@ -740,6 +741,13 @@
module=qualtran.bloqs.block_encoding.sparse_matrix,
bloq_specs=[qualtran.bloqs.block_encoding.sparse_matrix._SPARSE_MATRIX_DOC],
),
NotebookSpecV2(
title='Sparse Matrix Hermitian',
module=qualtran.bloqs.block_encoding.sparse_matrix_hermitian,
bloq_specs=[
qualtran.bloqs.block_encoding.sparse_matrix_hermitian._SPARSE_MATRIX_HERMITIAN_DOC
],
),
NotebookSpecV2(
title='Chebyshev Polynomial',
module=qualtran.bloqs.block_encoding.chebyshev_polynomial,
Expand Down
1 change: 1 addition & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ Bloqs Library
block_encoding/phase.ipynb
block_encoding/linear_combination.ipynb
block_encoding/sparse_matrix.ipynb
block_encoding/sparse_matrix_hermitian.ipynb
block_encoding/chebyshev_polynomial.ipynb
block_encoding/lcu_block_encoding.ipynb

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 @@ -23,5 +23,6 @@
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.sparse_matrix_hermitian import SparseMatrixHermitian
from qualtran.bloqs.block_encoding.tensor_product import TensorProduct
from qualtran.bloqs.block_encoding.unitary import Unitary
204 changes: 204 additions & 0 deletions qualtran/bloqs/block_encoding/sparse_matrix_hermitian.ipynb
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
}
Loading

0 comments on commit b1ee833

Please sign in to comment.