|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Bloqs with specialized controlled implementations\n", |
| 9 | + "\n", |
| 10 | + "In some cases, a bloq may have a specialized singly-controlled version (e.g. `LCUBlockEncoding`).\n", |
| 11 | + "Qualtran provides a convenience methods `get_ctrl_system_1bit_cv` and `get_ctrl_system_1bit_cv_from_bloqs` to override the `get_ctrl_system`. These methods ensure that multiply-controlled bloqs are correctly reduced to the provided singly-controlled variants.\n", |
| 12 | + "\n", |
| 13 | + "- `get_ctrl_system_1bit_cv_from_bloqs` - Override when a specialized controlled-by-1 implementation is available.\n", |
| 14 | + "- `get_ctrl_system_1bit_cv` - Override when both specialized controlled-by-1 and controlled-by-0 implementations are available.\n", |
| 15 | + "\n", |
| 16 | + "The following demonstrates an example for a bloq implementing $T^\\dagger X T$, where the controlled version only needs to control the $X$." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "id": "1", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "import attrs\n", |
| 27 | + "from qualtran import Bloq, BloqBuilder, Soquet, SoquetT, Signature, CtrlSpec, AddControlledT\n", |
| 28 | + "from qualtran.bloqs.basic_gates import TGate, XGate, CNOT\n", |
| 29 | + "\n", |
| 30 | + "\n", |
| 31 | + "@attrs.frozen\n", |
| 32 | + "class BloqWithSpecializedCtrl(Bloq):\n", |
| 33 | + " \"\"\"Bloq implementing $T^\\dagger X T$\"\"\"\n", |
| 34 | + " is_controlled: bool = False\n", |
| 35 | + "\n", |
| 36 | + " @property\n", |
| 37 | + " def signature(self) -> 'Signature':\n", |
| 38 | + " n_ctrls = 1 if self.is_controlled else 0\n", |
| 39 | + " return Signature.build(ctrl=n_ctrls, q=1)\n", |
| 40 | + " \n", |
| 41 | + " def build_composite_bloq(self, bb: 'BloqBuilder', q: 'Soquet', **soqs) -> dict[str, 'SoquetT']:\n", |
| 42 | + " ctrl = soqs.pop('ctrl', None)\n", |
| 43 | + " \n", |
| 44 | + " q = bb.add(TGate(), q=q)\n", |
| 45 | + " if self.is_controlled:\n", |
| 46 | + " ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q)\n", |
| 47 | + " else:\n", |
| 48 | + " ctrl, q = bb.add(XGate(), ctrl=ctrl, target=q)\n", |
| 49 | + " q = bb.add(TGate().adjoint(), q=q)\n", |
| 50 | + " \n", |
| 51 | + " out_soqs = {'q': q}\n", |
| 52 | + " if ctrl:\n", |
| 53 | + " out_soqs |= {'ctrl': ctrl}\n", |
| 54 | + " return out_soqs\n", |
| 55 | + " \n", |
| 56 | + " def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlledT']:\n", |
| 57 | + " from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs\n", |
| 58 | + "\n", |
| 59 | + " return get_ctrl_system_1bit_cv_from_bloqs(\n", |
| 60 | + " self,\n", |
| 61 | + " ctrl_spec,\n", |
| 62 | + " current_ctrl_bit=1 if self.is_controlled else None,\n", |
| 63 | + " bloq_with_ctrl=attrs.evolve(self, is_controlled=True),\n", |
| 64 | + " ctrl_reg_name='ctrl',\n", |
| 65 | + " )" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": null, |
| 71 | + "id": "2", |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "from qualtran.drawing import show_bloq, show_call_graph\n", |
| 76 | + "\n", |
| 77 | + "bloq = BloqWithSpecializedCtrl().controlled().controlled()\n", |
| 78 | + "show_bloq(bloq.decompose_bloq().flatten())\n", |
| 79 | + "show_call_graph(bloq)" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "markdown", |
| 84 | + "id": "3", |
| 85 | + "metadata": {}, |
| 86 | + "source": [ |
| 87 | + "## Propagating the Adjoint\n", |
| 88 | + "\n", |
| 89 | + "In the above bloq, calling controlled on the adjoint does not push the controls into the bloq, and therefore does not use the specialized implementation provided." |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "id": "4", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "BloqWithSpecializedCtrl().adjoint().controlled()" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "id": "5", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "This can be fixed by overriding the adjoint using a special wrapper for this case - `AdjointWithSpecializedCtrl`. This is a subclass of the default `Adjoint` metabloq, and ensures that single-qubit controls are pushed into the underlying bloq." |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "id": "6", |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "@attrs.frozen\n", |
| 118 | + "class BloqWithSpecializedCtrlWithAdjoint(Bloq):\n", |
| 119 | + " \"\"\"Bloq implementing $T^\\dagger X T$\"\"\"\n", |
| 120 | + " is_controlled: bool = False\n", |
| 121 | + "\n", |
| 122 | + " @property\n", |
| 123 | + " def signature(self) -> 'Signature':\n", |
| 124 | + " n_ctrls = 1 if self.is_controlled else 0\n", |
| 125 | + " return Signature.build(ctrl=n_ctrls, q=1)\n", |
| 126 | + " \n", |
| 127 | + " def build_composite_bloq(self, bb: 'BloqBuilder', q: 'Soquet', **soqs) -> dict[str, 'SoquetT']:\n", |
| 128 | + " ctrl = soqs.pop('ctrl', None)\n", |
| 129 | + " \n", |
| 130 | + " q = bb.add(TGate(), q=q)\n", |
| 131 | + " if self.is_controlled:\n", |
| 132 | + " ctrl, q = bb.add(CNOT(), ctrl=ctrl, target=q)\n", |
| 133 | + " else:\n", |
| 134 | + " ctrl, q = bb.add(XGate(), ctrl=ctrl, target=q)\n", |
| 135 | + " q = bb.add(TGate().adjoint(), q=q)\n", |
| 136 | + " \n", |
| 137 | + " out_soqs = {'q': q}\n", |
| 138 | + " if ctrl:\n", |
| 139 | + " out_soqs |= {'ctrl': ctrl}\n", |
| 140 | + " return out_soqs\n", |
| 141 | + " \n", |
| 142 | + " def get_ctrl_system(self, ctrl_spec: 'CtrlSpec') -> tuple['Bloq', 'AddControlledT']:\n", |
| 143 | + " from qualtran.bloqs.mcmt.specialized_ctrl import get_ctrl_system_1bit_cv_from_bloqs\n", |
| 144 | + "\n", |
| 145 | + " return get_ctrl_system_1bit_cv_from_bloqs(\n", |
| 146 | + " self,\n", |
| 147 | + " ctrl_spec,\n", |
| 148 | + " current_ctrl_bit=1 if self.is_controlled else None,\n", |
| 149 | + " bloq_with_ctrl=attrs.evolve(self, is_controlled=True),\n", |
| 150 | + " ctrl_reg_name='ctrl',\n", |
| 151 | + " )\n", |
| 152 | + "\n", |
| 153 | + " def adjoint(self):\n", |
| 154 | + " from qualtran.bloqs.mcmt.specialized_ctrl import AdjointWithSpecializedCtrl, SpecializeOnCtrlBit\n", |
| 155 | + " \n", |
| 156 | + " return AdjointWithSpecializedCtrl(self, SpecializeOnCtrlBit.ONE)" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": null, |
| 162 | + "id": "7", |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "BloqWithSpecializedCtrlWithAdjoint().adjoint().controlled()" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "id": "8", |
| 173 | + "metadata": {}, |
| 174 | + "outputs": [], |
| 175 | + "source": [ |
| 176 | + "assert BloqWithSpecializedCtrlWithAdjoint().adjoint().controlled() == BloqWithSpecializedCtrlWithAdjoint(is_controlled=True).adjoint()" |
| 177 | + ] |
| 178 | + } |
| 179 | + ], |
| 180 | + "metadata": { |
| 181 | + "kernelspec": { |
| 182 | + "display_name": "Python 3 (ipykernel)", |
| 183 | + "language": "python", |
| 184 | + "name": "python3" |
| 185 | + }, |
| 186 | + "language_info": { |
| 187 | + "codemirror_mode": { |
| 188 | + "name": "ipython", |
| 189 | + "version": 3 |
| 190 | + }, |
| 191 | + "file_extension": ".py", |
| 192 | + "mimetype": "text/x-python", |
| 193 | + "name": "python", |
| 194 | + "nbconvert_exporter": "python", |
| 195 | + "pygments_lexer": "ipython3", |
| 196 | + "version": "3.11.9" |
| 197 | + } |
| 198 | + }, |
| 199 | + "nbformat": 4, |
| 200 | + "nbformat_minor": 5 |
| 201 | +} |
0 commit comments