Skip to content

Commit 0ce1e7e

Browse files
doc: combined notebook 3 and 6
1 parent b256a28 commit 0ce1e7e

File tree

2 files changed

+321
-419
lines changed

2 files changed

+321
-419
lines changed

examples/notebooks/3_Expectation_value_of_observables.ipynb

+321-14
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
"id": "a3c9a6e8",
2121
"metadata": {},
2222
"outputs": [
23-
{
24-
"name": "stderr",
25-
"output_type": "stream",
26-
"text": [
27-
"<frozen importlib._bootstrap>:228: RuntimeWarning: scipy._lib.messagestream.MessageStream size changed, may indicate binary incompatibility. Expected 56 from C header, got 64 from PyObject\n"
28-
]
29-
},
3023
{
3124
"name": "stdout",
3225
"output_type": "stream",
@@ -131,10 +124,10 @@
131124
"output_type": "stream",
132125
"text": [
133126
"Result: ATOSDevice, MYQLM_PYLINALG\n",
134-
" Expectation value: -3.3170640012883936\n",
127+
" Expectation value: -3.317064001288396\n",
135128
" Error/Variance: 0.0\n",
136129
"\n",
137-
"-3.3170640012883936\n"
130+
"-3.317064001288396\n"
138131
]
139132
}
140133
],
@@ -168,11 +161,11 @@
168161
"text": [
169162
"BatchResult: 2 results\n",
170163
"Result: IBMDevice, AER_SIMULATOR\n",
171-
" Expectation value: -3.0685000000000002\n",
172-
" Error/Variance: 41.63205175\n",
164+
" Expectation value: -3.2504999999999997\n",
165+
" Error/Variance: 41.46215925\n",
173166
"Result: ATOSDevice, MYQLM_PYLINALG\n",
174-
" Expectation value: -3.05\n",
175-
" Error/Variance: 0.14420550306266672\n",
167+
" Expectation value: -3.2715\n",
168+
" Error/Variance: 0.1439368300002305\n",
176169
"\n"
177170
]
178171
}
@@ -181,6 +174,320 @@
181174
"results = run(circuit, [ATOSDevice.MYQLM_PYLINALG, IBMDevice.AER_SIMULATOR])\n",
182175
"print(results)"
183176
]
177+
},
178+
{
179+
"cell_type": "markdown",
180+
"id": "057b9856",
181+
"metadata": {},
182+
"source": [
183+
"## Pauli String representation\n",
184+
"\n",
185+
"An observable can also be represented by a Pauli string. This section will demonstrate how to create and manipulate PauliString objects."
186+
]
187+
},
188+
{
189+
"cell_type": "code",
190+
"execution_count": 9,
191+
"id": "4ac9c7e0",
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"from mpqp.measures import I, X as Pauli_X, Y as Pauli_Y, X as Pauli_Z"
196+
]
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"id": "aed703a9",
201+
"metadata": {},
202+
"source": [
203+
"> ⚠ **pauli_string import**: pauli atoms are named I, X, Y, and Z. If you have conflicts with `mpqp.gates import X, Y, Z,` , you can:\n",
204+
"> - **Rename Import:**\n",
205+
"> ```python \n",
206+
"> from mpqp.measures import X as Pauli_X \n",
207+
"> ```\n",
208+
"> - **Import Only Pauli String:**\n",
209+
"> ```python\n",
210+
"> from mpqp.measures import pauli_string \n",
211+
"> pauli_string.X\n",
212+
"> ```\n",
213+
">"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"id": "30d41d0f",
219+
"metadata": {},
220+
"source": [
221+
"A `PauliString` is based on the following hierarchy:\n",
222+
"- an *atom* is the most elemental building brick of pauli string, it it either\n",
223+
" `I`, `X`, `Y` or `Z`;\n",
224+
"- a *monomial* is a tensor product of *atoms* multiplied by a real coefficient,\n",
225+
" for instance `0.3 * I⊗Z⊗Y`. In MPQP, the tensor product is denoted as `@`, so\n",
226+
" the previous example would be expressed as `0.3 * I@Z@Y`;\n",
227+
"- a *string* is a sum of *monomials*, for instance `0.3 * I@Z@Y + X@X@X`.\n",
228+
"\n",
229+
"In practice, you never need to handle these types independently, you can express\n",
230+
"everything in term of expression based on the fundamental atoms. Let's see how."
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"id": "7d07ec4b",
236+
"metadata": {},
237+
"source": [
238+
"### Creating and Manipulating PauliString Objects\n",
239+
"\n",
240+
"Let's illustrate how to create and manipulate PauliString objects:"
241+
]
242+
},
243+
{
244+
"cell_type": "code",
245+
"execution_count": 10,
246+
"id": "bc5ed517",
247+
"metadata": {},
248+
"outputs": [
249+
{
250+
"name": "stdout",
251+
"output_type": "stream",
252+
"text": [
253+
"ps_1=1*I@X + -3*X@Y\n"
254+
]
255+
}
256+
],
257+
"source": [
258+
"ps_1 = I @ Pauli_Z - 3 * Pauli_X @ Pauli_Y\n",
259+
"print(f\"{ps_1=}\")"
260+
]
261+
},
262+
{
263+
"cell_type": "markdown",
264+
"id": "7824baf5",
265+
"metadata": {},
266+
"source": [
267+
"#### Simplifying and Rounding PauliStrings\n",
268+
"\n",
269+
"When dealing with PauliStrings, simplification and rounding are common operations:\n",
270+
"\n",
271+
"- **Simplify**: We combine like terms and eliminate those with zero coefficients;\n",
272+
"- **Round**: We can round the coefficients to a specified number of decimals (5 by default).\n",
273+
"\n",
274+
"> `str` on a `PauliString` will call both methods: `round()` and `simplify()`"
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": 11,
280+
"id": "06784ef3",
281+
"metadata": {},
282+
"outputs": [
283+
{
284+
"name": "stdout",
285+
"output_type": "stream",
286+
"text": [
287+
"ps_2 = 1*I@X + 2.555555555*Y@I + 1*X@X + -1*X@X\n",
288+
" = 1*I@X + 2.555555555*Y@I\n",
289+
" ~= 1*I@X + 2.6*Y@I + 1*X@X + -1*X@X\n",
290+
" ~= 1*I@X + 2.5556*Y@I\n"
291+
]
292+
}
293+
],
294+
"source": [
295+
"ps_2 = I @ Pauli_Z + 2.555555555 * Pauli_Y @ I + Pauli_X @ Pauli_Z - Pauli_X @ Pauli_Z\n",
296+
"print(\"ps_2 =\",repr(ps_2))\n",
297+
"print(\" =\",repr(ps_2.simplify()))\n",
298+
"print(\" ~=\",repr(ps_2.round(1)))\n",
299+
"print(\" ~=\",ps_2)"
300+
]
301+
},
302+
{
303+
"cell_type": "markdown",
304+
"id": "bb3a48ec",
305+
"metadata": {},
306+
"source": [
307+
"#### Arithmetic Operations\n",
308+
"\n",
309+
"We can perform various arithmetic operations on PauliString objects, including addition, subtraction, scalar multiplication, scalar division, and matrix multiplication:"
310+
]
311+
},
312+
{
313+
"cell_type": "code",
314+
"execution_count": 12,
315+
"id": "b9cadb08",
316+
"metadata": {},
317+
"outputs": [
318+
{
319+
"name": "stdout",
320+
"output_type": "stream",
321+
"text": [
322+
"Addition:\n",
323+
"(1*I@X + -3*X@Y) + (1*I@X + 2.6*Y@I) = 2*I@X + -3*X@Y + 2.6*Y@I\n",
324+
"\n",
325+
"Subtraction:\n",
326+
"(1*I@X + -3*X@Y) - (1*I@X + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
327+
"\n",
328+
"Scalar product:\n",
329+
"2 * (1*I@X + -3*X@Y) = 2*I@X + -6*X@Y\n",
330+
"\n",
331+
"Scalar division:\n",
332+
"(1*I@X + 2.6*Y@I) / 3 ~= 0.3333*I@X + 0.8667*Y@I\n",
333+
"\n",
334+
"Tensor product:\n",
335+
"(1*I@X + -3*X@Y) @ Z = 1*I@X@X + -3*X@Y@X\n",
336+
"\n",
337+
"(1*I@X + -3*X@Y) @ (1*I@X + 2.6*Y@I) = 1*I@X@I@X + -3*I@X@X@Y + 2.6*Y@I@I@X + -7.8*Y@I@X@Y\n"
338+
]
339+
}
340+
],
341+
"source": [
342+
"ps_2 = ps_2.round(1).simplify()\n",
343+
"print(f\"\"\"Addition:\n",
344+
"({ps_1}) + ({ps_2}) = {ps_1 + ps_2}\n",
345+
"\n",
346+
"Subtraction:\n",
347+
"({ps_1}) - ({ps_2}) = {ps_1 - ps_2}\n",
348+
"\n",
349+
"Scalar product:\n",
350+
"2 * ({ps_1}) = {2 * ps_1}\n",
351+
"\n",
352+
"Scalar division:\n",
353+
"({ps_2}) / 3 ~= {ps_2 / 3}\n",
354+
"\n",
355+
"Tensor product:\n",
356+
"({ps_1}) @ Z = {ps_1 @ Pauli_Z}\n",
357+
"\n",
358+
"({ps_1}) @ ({ps_2}) = {ps_1 @ ps_2}\"\"\")"
359+
]
360+
},
361+
{
362+
"cell_type": "markdown",
363+
"id": "59c34f41",
364+
"metadata": {},
365+
"source": [
366+
"### Create an Observable with a Pauli string"
367+
]
368+
},
369+
{
370+
"cell_type": "code",
371+
"execution_count": 13,
372+
"id": "5b0537a1",
373+
"metadata": {},
374+
"outputs": [],
375+
"source": [
376+
"obs1 = Observable(ps_1)"
377+
]
378+
},
379+
{
380+
"cell_type": "markdown",
381+
"id": "3b411669",
382+
"metadata": {},
383+
"source": [
384+
"Since there is an equivalence between definition from a Pauli string or from a\n",
385+
"Hermitian matrix, both these observable wan can also be expressed in term of the\n",
386+
"mean through which it was not defined (Pauli for matrix and vice versa):"
387+
]
388+
},
389+
{
390+
"cell_type": "code",
391+
"execution_count": 31,
392+
"id": "1732e099",
393+
"metadata": {},
394+
"outputs": [
395+
{
396+
"name": "stdout",
397+
"output_type": "stream",
398+
"text": [
399+
"`obs` created with matrix:\n",
400+
"matrix:\n",
401+
"[[ 4.+0.j 2.+0.j 3.+0.j 8.+0.j]\n",
402+
" [ 2.+0.j -3.+0.j 1.+0.j 0.+0.j]\n",
403+
" [ 3.+0.j 1.+0.j -1.+0.j 5.+0.j]\n",
404+
" [ 8.+0.j 0.+0.j 5.+0.j 2.+0.j]]\n",
405+
"Pauli string:\n",
406+
"0.5*I@I + 3.5*I@X + 1*I@Z + 1.5*X@I + 4.5*X@X + 1.5*X@Z + -3.5*Y@Y + -1.5*Z@X + 2.5*Z@Z\n",
407+
"\n",
408+
"\n",
409+
"`obs1` created with Pauli string:\n",
410+
"Pauli string:\n",
411+
"1*I@X + -3*X@Y\n",
412+
"matrix:\n",
413+
"[[0.+0.j 1.+0.j 0.+0.j 0.-3.j]\n",
414+
" [1.+0.j 0.+0.j 0.+3.j 0.+0.j]\n",
415+
" [0.+0.j 0.-3.j 0.+0.j 1.+0.j]\n",
416+
" [0.+3.j 0.+0.j 1.+0.j 0.+0.j]]\n"
417+
]
418+
}
419+
],
420+
"source": [
421+
"print(\"`obs` created with matrix:\")\n",
422+
"print(\"matrix:\")\n",
423+
"print(obs.matrix)\n",
424+
"print(\"Pauli string:\")\n",
425+
"print(obs.pauli_string)\n",
426+
"\n",
427+
"\n",
428+
"print(\"\\n\\n`obs1` created with Pauli string:\")\n",
429+
"print(\"Pauli string:\")\n",
430+
"print(obs1.pauli_string)\n",
431+
"print(\"matrix:\")\n",
432+
"print(obs1.matrix)"
433+
]
434+
},
435+
{
436+
"cell_type": "markdown",
437+
"id": "a63b436e",
438+
"metadata": {},
439+
"source": [
440+
"### ExpectationMeasure\n",
441+
"\n",
442+
"Next, we measure the expectation value of the observable by adding it to the circuit."
443+
]
444+
},
445+
{
446+
"cell_type": "code",
447+
"execution_count": 24,
448+
"id": "892f0b6e",
449+
"metadata": {},
450+
"outputs": [],
451+
"source": [
452+
"circuit = circuit.without_measurements()\n",
453+
"circuit.add(ExpectationMeasure([0, 1], observable=obs1, shots=1000))"
454+
]
455+
},
456+
{
457+
"cell_type": "code",
458+
"execution_count": 25,
459+
"id": "745aa999",
460+
"metadata": {},
461+
"outputs": [
462+
{
463+
"name": "stdout",
464+
"output_type": "stream",
465+
"text": [
466+
"BatchResult: 3 results\n",
467+
"Result: IBMDevice, AER_SIMULATOR\n",
468+
" Expectation value: 0.08399999999999999\n",
469+
" Error/Variance: 9.98776\n",
470+
"Result: ATOSDevice, MYQLM_PYLINALG\n",
471+
" Expectation value: 0.010000000000000009\n",
472+
" Error/Variance: 0.10000486474653607\n",
473+
"Result: ATOSDevice, MYQLM_CLINALG\n",
474+
" Expectation value: -0.017999999999999933\n",
475+
" Error/Variance: 0.10001671531970716\n",
476+
"\n"
477+
]
478+
}
479+
],
480+
"source": [
481+
"results = run(\n",
482+
" circuit,\n",
483+
" [\n",
484+
" ATOSDevice.MYQLM_PYLINALG,\n",
485+
" IBMDevice.AER_SIMULATOR,\n",
486+
" ATOSDevice.MYQLM_CLINALG,\n",
487+
" ],\n",
488+
")\n",
489+
"print(results)"
490+
]
184491
}
185492
],
186493
"metadata": {
@@ -199,7 +506,7 @@
199506
"name": "python",
200507
"nbconvert_exporter": "python",
201508
"pygments_lexer": "ipython3",
202-
"version": "3.9.12"
509+
"version": "3.9.5"
203510
}
204511
},
205512
"nbformat": 4,

0 commit comments

Comments
 (0)