Skip to content

Commit dde12e2

Browse files
authored
Use QECGatesCost for phase_estimation_of_quantum_walk (#1383)
* Use QECGatesCost for phase_estimation_of_quantum_walk * put bloq example back
1 parent 5b5a9ad commit dde12e2

1 file changed

Lines changed: 52 additions & 26 deletions

File tree

qualtran/bloqs/phase_estimation/phase_estimation_of_quantum_walk.ipynb

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,6 @@
77
"# Phase Estimation of Quantum Walks"
88
]
99
},
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-
},
3110
{
3211
"cell_type": "markdown",
3312
"metadata": {},
@@ -51,6 +30,15 @@
5130
"from qualtran.bloqs.chemistry.hubbard_model.qubitization import get_walk_operator_for_hubbard_model"
5231
]
5332
},
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+
},
5442
{
5543
"cell_type": "code",
5644
"execution_count": null,
@@ -129,6 +117,44 @@
129117
"print(circuit)"
130118
]
131119
},
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+
},
132158
{
133159
"cell_type": "markdown",
134160
"metadata": {},
@@ -151,7 +177,7 @@
151177
"walk, _ = get_walk_operator_for_1d_ising_model(num_sites, eps)\n",
152178
"\n",
153179
"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",
155181
"print(result)"
156182
]
157183
},
@@ -178,7 +204,7 @@
178204
"m_bits = int(np.ceil(np.log2(qlambda * np.pi * np.sqrt(2) / delta_E)))\n",
179205
"walk = get_walk_operator_for_hubbard_model(x_dim, y_dim, t, mu)\n",
180206
"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",
182208
"print(result)"
183209
]
184210
},
@@ -188,9 +214,11 @@
188214
"metadata": {},
189215
"outputs": [],
190216
"source": [
217+
"# Or, we can just use the included bloq example directly\n",
218+
"\n",
191219
"from qualtran.bloqs.phase_estimation.qubitization_qpe import _qubitization_qpe_hubbard_model_large\n",
192220
"qpe = _qubitization_qpe_hubbard_model_large.make()\n",
193-
"%time result = qpe.t_complexity()\n",
221+
"%time result = get_cost_value(qpe, QECGatesCost())\n",
194222
"print(result)"
195223
]
196224
},
@@ -211,9 +239,7 @@
211239
"from qualtran.drawing import show_flame_graph\n",
212240
"\n",
213241
"qpe_small = _qubitization_qpe_hubbard_model_small.make()\n",
214-
"\n",
215242
"print(qpe_small.t_complexity())\n",
216-
"\n",
217243
"show_flame_graph(qpe_small)"
218244
]
219245
}

0 commit comments

Comments
 (0)