Skip to content

Commit

Permalink
notebooks for ZPowConstViaPhaseGradient and RzViaPhaseGradient (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anurudhp authored Dec 10, 2024
1 parent cb0d3c8 commit 859d9e8
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 5 deletions.
14 changes: 14 additions & 0 deletions dev_tools/qualtran_dev_tools/notebook_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
import qualtran.bloqs.rotations.phasing_via_cost_function
import qualtran.bloqs.rotations.programmable_rotation_gate_array
import qualtran.bloqs.rotations.quantum_variable_rotation
import qualtran.bloqs.rotations.rz_via_phase_gradient
import qualtran.bloqs.rotations.zpow_via_phase_gradient
import qualtran.bloqs.state_preparation.black_box_prepare
import qualtran.bloqs.state_preparation.prepare_base
import qualtran.bloqs.state_preparation.prepare_uniform_superposition
Expand Down Expand Up @@ -634,6 +636,18 @@
qualtran.bloqs.rotations.hamming_weight_phasing._HAMMING_WEIGHT_PHASING_VIA_PHASE_GRADIENT_DOC,
],
),
NotebookSpecV2(
title='ZPow Rotation via Phase Gradient',
module=qualtran.bloqs.rotations.zpow_via_phase_gradient,
bloq_specs=[
qualtran.bloqs.rotations.zpow_via_phase_gradient._ZPOW_CONST_VIA_PHASE_GRADIENT_DOC
],
),
NotebookSpecV2(
title='Rz Rotation via Phase Gradient',
module=qualtran.bloqs.rotations.rz_via_phase_gradient,
bloq_specs=[qualtran.bloqs.rotations.rz_via_phase_gradient._RZ_VIA_PHASE_GRADIENT_DOC],
),
NotebookSpecV2(
title='Programmable Rotation Gate Array',
module=qualtran.bloqs.rotations.programmable_rotation_gate_array,
Expand Down
2 changes: 2 additions & 0 deletions docs/bloqs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Bloqs Library
rotations/phasing_via_cost_function.ipynb
rotations/phase_gradient.ipynb
rotations/hamming_weight_phasing.ipynb
rotations/zpow_via_phase_gradient.ipynb
rotations/rz_via_phase_gradient.ipynb
rotations/programmable_rotation_gate_array.ipynb
qft/two_bit_ffft.ipynb
qft/approximate_qft.ipynb
Expand Down
2 changes: 2 additions & 0 deletions qualtran/bloqs/rotations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
from qualtran.bloqs.rotations.phasing_via_cost_function import PhasingViaCostFunction
from qualtran.bloqs.rotations.programmable_rotation_gate_array import ProgrammableRotationGateArray
from qualtran.bloqs.rotations.quantum_variable_rotation import QvrPhaseGradient, QvrZPow
from qualtran.bloqs.rotations.rz_via_phase_gradient import RzViaPhaseGradient
from qualtran.bloqs.rotations.zpow_via_phase_gradient import ZPowConstViaPhaseGradient
168 changes: 168 additions & 0 deletions qualtran/bloqs/rotations/rz_via_phase_gradient.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "bbc3afcf",
"metadata": {
"cq.autogen": "title_cell"
},
"source": [
"# Rz Rotation via Phase Gradient"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e6ca3000",
"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": "4d0db53e",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.bloq_doc.md"
},
"source": [
"## `RzViaPhaseGradient`\n",
"Apply a controlled-Rz using a phase gradient state.\n",
"\n",
"Implements the following unitary action:\n",
"\n",
"$$\n",
" |\\psi\\rangle \\otimes |x\\rangle \\mapsto \\text{Rz}(4 \\pi x) |\\psi\\rangle \\otimes |x\\rangle\n",
"$$\n",
"\n",
"for every state $|\\psi\\rangle$ and every $x$, or equivalently\n",
"\n",
"$$\n",
" |b\\rangle|x\\rangle \\mapsto |b\\rangle e^{- (-1)^b i x/2} |x\\rangle\n",
"$$\n",
"\n",
"for every $b \\in \\{0, 1\\}$ and every $x$.\n",
"\n",
"To apply an $\\text{Rz}(\\theta) = e^{-i Z \\theta/2}$, the angle register $x$ should store $\\theta/(4\\pi)$.\n",
"\n",
"#### Parameters\n",
" - `angle_dtype`: Data type for the `angle_data` register.\n",
" - `phasegrad_dtype`: Data type for the phase gradient register. \n",
"\n",
"#### Registers\n",
" - `q`: The qubit to apply Rz on.\n",
" - `angle`: The rotation angle in radians.\n",
" - `phase_grad`: The phase gradient register of sufficient width. \n",
"\n",
"#### References\n",
" - [Compilation of Fault-Tolerant Quantum Heuristics for Combinatorial Optimization](https://arxiv.org/abs/2007.07391). Section II-C: Oracles for phasing by cost function. Appendix A: Addition for controlled rotations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88ae8ba0",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.bloq_doc.py"
},
"outputs": [],
"source": [
"from qualtran.bloqs.rotations import RzViaPhaseGradient"
]
},
{
"cell_type": "markdown",
"id": "f3d5ab59",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.example_instances.md"
},
"source": [
"### Example Instances"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16a874c9",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.rz_via_phase_gradient"
},
"outputs": [],
"source": [
"from qualtran import QFxp\n",
"\n",
"rz_via_phase_gradient = RzViaPhaseGradient(angle_dtype=QFxp(4, 4), phasegrad_dtype=QFxp(4, 4))"
]
},
{
"cell_type": "markdown",
"id": "ca4ac4bc",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.graphical_signature.md"
},
"source": [
"#### Graphical Signature"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92d7b03c",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.graphical_signature.py"
},
"outputs": [],
"source": [
"from qualtran.drawing import show_bloqs\n",
"show_bloqs([rz_via_phase_gradient],\n",
" ['`rz_via_phase_gradient`'])"
]
},
{
"cell_type": "markdown",
"id": "03999011",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.call_graph.md"
},
"source": [
"### Call Graph"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0cc79d3",
"metadata": {
"cq.autogen": "RzViaPhaseGradient.call_graph.py"
},
"outputs": [],
"source": [
"from qualtran.resource_counting.generalizers import ignore_split_join\n",
"rz_via_phase_gradient_g, rz_via_phase_gradient_sigma = rz_via_phase_gradient.call_graph(max_depth=1, generalizer=ignore_split_join)\n",
"show_call_graph(rz_via_phase_gradient_g)\n",
"show_counts_sigma(rz_via_phase_gradient_sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 6 additions & 0 deletions qualtran/bloqs/rotations/rz_via_phase_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Bloq,
bloq_example,
BloqBuilder,
BloqDocSpec,
QBit,
QDType,
QUInt,
Expand Down Expand Up @@ -109,3 +110,8 @@ def _rz_via_phase_gradient() -> RzViaPhaseGradient:

rz_via_phase_gradient = RzViaPhaseGradient(angle_dtype=QFxp(4, 4), phasegrad_dtype=QFxp(4, 4))
return rz_via_phase_gradient


_RZ_VIA_PHASE_GRADIENT_DOC = BloqDocSpec(
bloq_cls=RzViaPhaseGradient, examples=[_rz_via_phase_gradient]
)
6 changes: 6 additions & 0 deletions qualtran/bloqs/rotations/rz_via_phase_gradient_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import sympy
from attrs import frozen

import qualtran.testing as qlt_testing
from qualtran import Bloq, BloqBuilder, QBit, QFxp, QUInt, Signature, Soquet, SoquetT
from qualtran.bloqs.basic_gates import IntState, Rz
from qualtran.bloqs.rotations.phase_gradient import PhaseGradientState
Expand Down Expand Up @@ -87,3 +88,8 @@ def test_tensor(bitsize: int):
expected = Rz(theta).tensor_contract()

np.testing.assert_allclose(actual, expected, atol=1 / 2**bitsize)


@pytest.mark.notebook
def test_notebook():
qlt_testing.execute_notebook('rz_via_phase_gradient')
Loading

0 comments on commit 859d9e8

Please sign in to comment.