|
7 | 7 | "# Phase Estimation of Quantum Walks" |
8 | 8 | ] |
9 | 9 | }, |
10 | | - { |
11 | | - "cell_type": "code", |
12 | | - "execution_count": null, |
13 | | - "metadata": {}, |
14 | | - "outputs": [], |
15 | | - "source": [ |
16 | | - "# Copyright 2023 Google LLC\n", |
17 | | - "#\n", |
18 | | - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
19 | | - "# you may not use this file except in compliance with the License.\n", |
20 | | - "# You may obtain a copy of the License at\n", |
21 | | - "#\n", |
22 | | - "# https://www.apache.org/licenses/LICENSE-2.0\n", |
23 | | - "#\n", |
24 | | - "# Unless required by applicable law or agreed to in writing, software\n", |
25 | | - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
26 | | - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
27 | | - "# See the License for the specific language governing permissions and\n", |
28 | | - "# limitations under the License." |
29 | | - ] |
30 | | - }, |
31 | 10 | { |
32 | 11 | "cell_type": "markdown", |
33 | 12 | "metadata": {}, |
|
51 | 30 | "from qualtran.bloqs.chemistry.hubbard_model.qubitization import get_walk_operator_for_hubbard_model" |
52 | 31 | ] |
53 | 32 | }, |
| 33 | + { |
| 34 | + "cell_type": "markdown", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "### Phase estimation circuit\n", |
| 38 | + "\n", |
| 39 | + "We start by quickly sketching the phase estimation circuit in terms of the walk operator." |
| 40 | + ] |
| 41 | + }, |
54 | 42 | { |
55 | 43 | "cell_type": "code", |
56 | 44 | "execution_count": null, |
|
129 | 117 | "print(circuit)" |
130 | 118 | ] |
131 | 119 | }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "### Computing costs\n", |
| 125 | + "\n", |
| 126 | + "Usually, you'd define a Bloq that captures the entire phase estimation circuit, but we can use a little helper function to compute the gate counts directly from the bloqs encountered within the Cirq circuit." |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": null, |
| 132 | + "metadata": {}, |
| 133 | + "outputs": [], |
| 134 | + "source": [ |
| 135 | + "from qualtran import Bloq\n", |
| 136 | + "from qualtran.resource_counting import get_cost_value, QECGatesCost, GateCounts\n", |
| 137 | + "\n", |
| 138 | + "def get_qec_gates_cost_for_circuit(circuit):\n", |
| 139 | + " # Usually, you'd combine this into a bloq of its own, but we\n", |
| 140 | + " # use this helper function to add up the costs of the bloqs\n", |
| 141 | + " # found within the circuit.\n", |
| 142 | + " cost_key = QECGatesCost()\n", |
| 143 | + " costs_cache = {}\n", |
| 144 | + " total_cost = cost_key.zero()\n", |
| 145 | + " for op in circuit.all_operations():\n", |
| 146 | + " if not isinstance(op.gate, Bloq):\n", |
| 147 | + " # Skip state prep and QFT for now\n", |
| 148 | + " continue\n", |
| 149 | + " \n", |
| 150 | + " bloq = op.gate\n", |
| 151 | + " total_cost += get_cost_value(bloq, cost_key, costs_cache=costs_cache)\n", |
| 152 | + " \n", |
| 153 | + " return total_cost\n", |
| 154 | + "\n", |
| 155 | + "get_qec_gates_cost_for_circuit(circuit)" |
| 156 | + ] |
| 157 | + }, |
132 | 158 | { |
133 | 159 | "cell_type": "markdown", |
134 | 160 | "metadata": {}, |
|
151 | 177 | "walk, _ = get_walk_operator_for_1d_ising_model(num_sites, eps)\n", |
152 | 178 | "\n", |
153 | 179 | "circuit = cirq.Circuit(phase_estimation(walk, m=m_bits))\n", |
154 | | - "%time result = t_complexity_compat(circuit[1:-1])\n", |
| 180 | + "%time result = get_qec_gates_cost_for_circuit(circuit)\n", |
155 | 181 | "print(result)" |
156 | 182 | ] |
157 | 183 | }, |
|
178 | 204 | "m_bits = int(np.ceil(np.log2(qlambda * np.pi * np.sqrt(2) / delta_E)))\n", |
179 | 205 | "walk = get_walk_operator_for_hubbard_model(x_dim, y_dim, t, mu)\n", |
180 | 206 | "circuit = cirq.Circuit(phase_estimation(walk, m=m_bits))\n", |
181 | | - "%time result = t_complexity_compat(circuit[1:-1])\n", |
| 207 | + "%time result = get_qec_gates_cost_for_circuit(circuit)\n", |
182 | 208 | "print(result)" |
183 | 209 | ] |
184 | 210 | }, |
|
188 | 214 | "metadata": {}, |
189 | 215 | "outputs": [], |
190 | 216 | "source": [ |
| 217 | + "# Or, we can just use the included bloq example directly\n", |
| 218 | + "\n", |
191 | 219 | "from qualtran.bloqs.phase_estimation.qubitization_qpe import _qubitization_qpe_hubbard_model_large\n", |
192 | 220 | "qpe = _qubitization_qpe_hubbard_model_large.make()\n", |
193 | | - "%time result = qpe.t_complexity()\n", |
| 221 | + "%time result = get_cost_value(qpe, QECGatesCost())\n", |
194 | 222 | "print(result)" |
195 | 223 | ] |
196 | 224 | }, |
|
211 | 239 | "from qualtran.drawing import show_flame_graph\n", |
212 | 240 | "\n", |
213 | 241 | "qpe_small = _qubitization_qpe_hubbard_model_small.make()\n", |
214 | | - "\n", |
215 | 242 | "print(qpe_small.t_complexity())\n", |
216 | | - "\n", |
217 | 243 | "show_flame_graph(qpe_small)" |
218 | 244 | ] |
219 | 245 | } |
|
0 commit comments