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

Refactor block encoding bloqs to use abstract base class. #967

Merged
merged 23 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
149 changes: 88 additions & 61 deletions qualtran/bloqs/block_encoding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "774a1b4a",
"id": "f74727f8",
"metadata": {
"cq.autogen": "title_cell"
},
Expand All @@ -11,28 +11,38 @@
"\n",
"High level bloqs for defining bloq encodings and operations on block encodings.\n",
"\n",
"Given an operator $V$ which can be expressed as a linear combination of unitaries\n",
"In general, given an $s$-qubit operator $H$ then the $(s+a)$-qubit unitary $U$ is\n",
"a $(\\alpha, a, \\epsilon)$-block encoding of $H$ if it satisfies:\n",
"\n",
"$$\n",
" V = \\sum_l^L w_l U_l,\n",
" \\lVert H - \\alpha (\\langle G|_a\\otimes I_s U |G\\rangle_a \\otimes I_s) \\rVert\n",
" \\le \\epsilon,\n",
"$$\n",
"where $w_l \\ge 0$, $w_l \\in \\mathbb{R}$, and $U_l$ is a unitary, then the block\n",
"encoding $\\mathcal{B}\\left[\\frac{V}{\\lambda}\\right]$ satisifies\n",
"\n",
"where $a$ is an ancilla register and $s$ is the system register, $U$ is a unitary sometimes\n",
"called a signal oracle and encodes $H$ in its top right corner, $\\alpha \\ge\n",
"\\lVert H\\rVert$ (where $\\lVert \\cdot \\rVert$ denotes the spectral norm), and\n",
"$\\epsilon$ is the precision to which the block encoding is prepared. The state\n",
"$|G\\rangle_a$ is sometimes called the signal state, and its form depends on the\n",
"details of the block encoding. \n",
"\n",
"For LCU based block encodings \n",
"we have\n",
"$$\n",
" _a\\langle 0| \\mathcal{B}\\left[\\frac{V}{\\lambda}\\right] |0\\rangle_a\n",
" |\\psi\\rangle_s = \\frac{V}{\\lambda}|\\psi\\rangle_s\n",
"U = \\sum_l |l\\rangle\\langle l| \\otimes U_l\n",
"$$\n",
"where the subscripts $a$ and $s$ signify ancilla and system registers\n",
"respectively, and $\\lambda = \\sum_l w_l$. The ancilla register is at least of size $\\log L$. In our\n",
"implementations we typically split the ancilla registers into selection registers (i.e.\n",
"the $l$ registers above) and junk registers which are extra qubits needed by\n",
"state preparation but not controlled upon during SELECT."
"and $|G\\rangle = \\sum_l \\sqrt{\\frac{\\alpha_l}{\\alpha}}|0\\rangle_a$, which define the\n",
"usual SELECT and PREPARE oracles.\n",
"\n",
"Other ways of building block encodings exist so we define the abstract base\n",
"class `BlockEncoding` bloq, which expects values for $\\alpha$, $\\epsilon$,\n",
"system and ancilla registers and a bloq which prepares the state $|G\\rangle$. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4bc97c7",
"id": "032a712d",
"metadata": {
"cq.autogen": "top_imports"
},
Expand All @@ -49,17 +59,17 @@
},
{
"cell_type": "markdown",
"id": "1b5bfa35",
"id": "d16f721f",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.bloq_doc.md"
"cq.autogen": "LCUBlockEncoding.bloq_doc.md"
},
"source": [
"## `BlackBoxBlockEncoding`\n",
"Black box implementation of a block encoding using SELECT and PREPARE.\n",
"## `LCUBlockEncoding`\n",
"LCU based block encoding using SELECT and PREPARE oracles.\n",
"\n",
"Builds the block encoding via\n",
"$$\n",
" \\mathcal{B}[V] = \\mathrm{PREPARE}^\\dagger \\cdot \\mathrm{SELECT} \\cdot \\mathrm{PREPARE},\n",
" U[H] = \\mathrm{PREPARE}^\\dagger \\cdot \\mathrm{SELECT} \\cdot \\mathrm{PREPARE},\n",
"$$\n",
"where\n",
"$$\n",
Expand All @@ -70,33 +80,46 @@
" \\mathrm{SELECT} |l\\rangle_a|\\psi\\rangle_s = |l\\rangle_a U_l |\\psi\\rangle_s.\n",
"$$\n",
"\n",
"The ancilla register is at least of size $\\log L$.\n",
"\n",
"In our implementations we typically split the ancilla registers into\n",
"selection registers (i.e. the $l$ registers above) and junk registers which\n",
"are extra qubits needed by state preparation but not controlled upon during\n",
"SELECT.\n",
"\n",
"Here $|G\\rangle = \\mathrm{PREPARE}|0\\rangle$.\n",
"\n",
"#### Parameters\n",
" - `select`: The bloq implementing the `SelectOracle` interface.\n",
" - `prepare`: The bloq implementing the `SelectOracle` interface. \n",
" - `prepare`: The bloq implementing the `PrepareOracle` interface. \n",
"\n",
"#### Registers\n",
" - `selection`: The combined selection register.\n",
" - `junk`: Additional junk registers not prepared upon.\n",
" - `system`: The combined system register.\n"
" - `system`: The combined system register. \n",
"\n",
"#### References\n",
" - [Hamiltonian Simulation by Qubitization](https://quantum-journal.org/papers/q-2019-07-12-163/). Sec 3.1, page 7 and 8 for high level overview and definitions. A block encoding is called a standard form encoding there.\n",
" - [The power of block-encoded matrix powers: improved regression techniques via faster Hamiltonian simulation](https://arxiv.org/abs/1804.01973). Definition 3 page 8.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d0e1f95",
"id": "0dc5476c",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.bloq_doc.py"
"cq.autogen": "LCUBlockEncoding.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.block_encoding import BlackBoxBlockEncoding"
"from qualtran.bloqs.block_encoding import LCUBlockEncoding"
]
},
{
"cell_type": "markdown",
"id": "ba37811d",
"id": "5ab0f455",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.example_instances.md"
"cq.autogen": "LCUBlockEncoding.example_instances.md"
},
"source": [
"### Example Instances"
Expand All @@ -105,26 +128,32 @@
{
"cell_type": "code",
"execution_count": null,
"id": "38a4e1fc",
"id": "349a4f99",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.black_box_block_bloq"
"cq.autogen": "LCUBlockEncoding.black_box_block_bloq"
},
"outputs": [],
"source": [
"from qualtran.bloqs.block_encoding import BlackBoxBlockEncoding, BlackBoxPrepare, BlackBoxSelect\n",
"from qualtran.bloqs.hubbard_model import PrepareHubbard, SelectHubbard\n",
"\n",
"# 3x3 hubbard model U/t = 4\n",
"dim = 3\n",
"select = BlackBoxSelect(SelectHubbard(x_dim=dim, y_dim=dim))\n",
"prepare = BlackBoxPrepare(PrepareHubbard(x_dim=dim, y_dim=dim, t=1, mu=4))\n",
"black_box_block_bloq = BlackBoxBlockEncoding(select=select, prepare=prepare)"
"select = SelectHubbard(x_dim=dim, y_dim=dim)\n",
"U = 4\n",
"t = 1\n",
"prepare = PrepareHubbard(x_dim=dim, y_dim=dim, t=t, mu=U)\n",
"N = dim * dim * 2\n",
"qlambda = 2 * N * t + (N * U) // 2\n",
"black_box_block_bloq = LCUBlockEncoding(\n",
" select=select, prepare=prepare, alpha=qlambda, epsilon=0.0\n",
")"
]
},
{
"cell_type": "markdown",
"id": "fafa7659",
"id": "cb3538d3",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.graphical_signature.md"
"cq.autogen": "LCUBlockEncoding.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
Expand All @@ -133,9 +162,9 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4e0223af",
"id": "b6354639",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.graphical_signature.py"
"cq.autogen": "LCUBlockEncoding.graphical_signature.py"
},
"outputs": [],
"source": [
Expand All @@ -146,9 +175,9 @@
},
{
"cell_type": "markdown",
"id": "8664826d",
"id": "09bb24fc",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.call_graph.md"
"cq.autogen": "LCUBlockEncoding.call_graph.md"
},
"source": [
"### Call Graph"
Expand All @@ -157,9 +186,9 @@
{
"cell_type": "code",
"execution_count": null,
"id": "fdb0d03b",
"id": "fc3c71be",
"metadata": {
"cq.autogen": "BlackBoxBlockEncoding.call_graph.py"
"cq.autogen": "LCUBlockEncoding.call_graph.py"
},
"outputs": [],
"source": [
Expand All @@ -171,7 +200,7 @@
},
{
"cell_type": "markdown",
"id": "995d6d20",
"id": "2ed5cb18",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.bloq_doc.md"
},
Expand All @@ -198,13 +227,13 @@
" - `order`: order of Chebychev polynomial. \n",
"\n",
"#### References\n",
" - [Quantum computing enhanced computational catalysis]( https://arxiv.org/abs/2007.14460). Page 45; Theorem 1.\n"
" - [Quantum computing enhanced computational catalysis](https://arxiv.org/abs/2007.14460). Page 45; Theorem 1.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1b553eeb",
"id": "721cad00",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.bloq_doc.py"
},
Expand All @@ -215,7 +244,7 @@
},
{
"cell_type": "markdown",
"id": "f8f02214",
"id": "5bc78942",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.example_instances.md"
},
Expand All @@ -226,25 +255,31 @@
{
"cell_type": "code",
"execution_count": null,
"id": "df1de952",
"id": "8af92b76",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.chebyshev_poly"
},
"outputs": [],
"source": [
"from qualtran.bloqs.block_encoding import BlackBoxBlockEncoding, BlackBoxPrepare, BlackBoxSelect\n",
"from qualtran.bloqs.block_encoding import LCUBlockEncoding\n",
"from qualtran.bloqs.hubbard_model import PrepareHubbard, SelectHubbard\n",
"\n",
"dim = 3\n",
"select = BlackBoxSelect(SelectHubbard(x_dim=dim, y_dim=dim))\n",
"prepare = BlackBoxPrepare(PrepareHubbard(x_dim=dim, y_dim=dim, t=1, mu=4))\n",
"black_box_block_bloq = BlackBoxBlockEncoding(select=select, prepare=prepare)\n",
"select = SelectHubbard(x_dim=dim, y_dim=dim)\n",
"U = 4\n",
"t = 1\n",
"prepare = PrepareHubbard(x_dim=dim, y_dim=dim, t=t, mu=U)\n",
"N = dim * dim * 2\n",
"qlambda = 2 * N * t + (N * U) // 2\n",
"black_box_block_bloq = LCUBlockEncoding(\n",
" select=select, prepare=prepare, alpha=qlambda, epsilon=0.0\n",
")\n",
"chebyshev_poly = ChebyshevPolynomial(black_box_block_bloq, order=3)"
]
},
{
"cell_type": "markdown",
"id": "135e9723",
"id": "c8dc645a",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.graphical_signature.md"
},
Expand All @@ -255,7 +290,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "34b86d0b",
"id": "50b2e9c7",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.graphical_signature.py"
},
Expand All @@ -268,7 +303,7 @@
},
{
"cell_type": "markdown",
"id": "f7ffbd99",
"id": "8abadaed",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.call_graph.md"
},
Expand All @@ -279,7 +314,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "62c9b270",
"id": "8209eb98",
"metadata": {
"cq.autogen": "ChebyshevPolynomial.call_graph.py"
},
Expand All @@ -299,16 +334,8 @@
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
Loading
Loading