Skip to content

Commit 9496059

Browse files
Update the notebook for 'Classical control' to reflect new features
1 parent ebef9bf commit 9496059

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

docs/build/classical_control.ipynb

+66
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,41 @@
182182
"This example separately simulated the message qubit after its construction, and Bob's qubit after teleportation of the message. The fact that the Bloch vectors of each respective qubit are the same indicate that the circuit successfully teleported the message qubit's state onto Bob's qubit. "
183183
]
184184
},
185+
{
186+
"cell_type": "markdown",
187+
"metadata": {},
188+
"source": [
189+
"## Multi-Qubit Measurements and Bit Masks\n",
190+
"\n",
191+
"The `cirq.KeyedCondition` class limited in that it evaluate a simple function `is the value != 0?`. When doing multi-qubit measurement we often need to do more complex function like applying a bitmask before comparison. The class [cirq.BitMaskKeyCondition](https://github.com/quantumlib/Cirq/blob/ebef9bff978f28b032f54eb5f0a2e1cb9ec6464c/cirq-core/cirq/value/condition.py#L140) provides this functionality allowing us to do:\n",
192+
"- `BitMaskKeycondition('a')` -> a != 0\n",
193+
"- `BitMaskKeyCondition('a', bitmask=13)` -> (a & 13) != 0\n",
194+
"- `BitMaskKeyCondition('a', bitmask=13, target_value=9)` -> (a & 13) != 9\n",
195+
"- `BitMaskKeyCondition('a', bitmask=13, target_value=9, equal_target=True)` -> (a & 13) == 9\n",
196+
"- `BitMaskKeyCondition.create_equal_mask('a', 13) -> (a & 13)` == 13\n",
197+
"- `BitMaskKeyCondition.create_not_equal_mask('a', 13) -> (a & 13)` != 13\n",
198+
"\n",
199+
"In this example, `X` will be applied to $q_5$ when the bits corresponding to $q_1$, $q_2$, and $q_4$ are $1$ regardless of the values of other bits."
200+
]
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": null,
205+
"metadata": {},
206+
"outputs": [],
207+
"source": [
208+
"qs = cirq.LineQubit.range(5)\n",
209+
"cond = cirq.BitMaskKeyCondition.create_equal_mask('q_{0..4}', bitmask=13)\n",
210+
"circuit = cirq.Circuit(\n",
211+
" cirq.H.on_each(qs),\n",
212+
" cirq.measure(qs, key='q_{0..4}'),\n",
213+
" cirq.X(cirq.q(5)).with_classical_controls(cond),\n",
214+
" cirq.measure(cirq.q(5), key='q_5'),\n",
215+
")\n",
216+
"circuit\n",
217+
"print(cirq.Simulator().run(circuit, repetitions=2**5).data)"
218+
]
219+
},
185220
{
186221
"cell_type": "markdown",
187222
"metadata": {
@@ -220,6 +255,37 @@
220255
"print(results.data)"
221256
]
222257
},
258+
{
259+
"cell_type": "markdown",
260+
"metadata": {},
261+
"source": [
262+
"### Sympy complex boolean functions\n",
263+
"\n",
264+
"Cirq supports evaluating complex boolean functions on multiqubit measurement keys throught Sympy's [indexed objects](https://docs.sympy.org/latest/modules/tensor/indexed.html#sympy.tensor.indexed.Indexed.base).\n",
265+
"\n",
266+
"In this example, `X` will be applied to $q_2$ if the result of the two-qubit measurment of $q_0, q_1$ has two different bits. "
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"metadata": {},
273+
"outputs": [],
274+
"source": [
275+
"m = sympy.IndexedBase('q0_q1')\n",
276+
"q0, q1, q2 = cirq.LineQubit.range(3)\n",
277+
"sympy_indexed_condition = sympy.Xor(m[0], m[1])\n",
278+
"circuit = cirq.Circuit(\n",
279+
" cirq.H.on_each(q0, q1),\n",
280+
" cirq.measure(q0, q1, key='q0_q1'),\n",
281+
" cirq.X(q2).with_classical_controls(sympy_indexed_condition),\n",
282+
" cirq.measure(q2, key='q2'),\n",
283+
")\n",
284+
"print(circuit)\n",
285+
"results = cirq.Simulator(seed=2).run(circuit, repetitions=8)\n",
286+
"print(results.data)"
287+
]
288+
},
223289
{
224290
"cell_type": "markdown",
225291
"metadata": {

0 commit comments

Comments
 (0)