Skip to content

Commit 8d17a29

Browse files
authored
Merge pull request #123 from ColibrITD-SAS/feat-pauli-decomposition
Feat pauli decomposition
2 parents ca3fb90 + 11a9191 commit 8d17a29

File tree

12 files changed

+1142
-203
lines changed

12 files changed

+1142
-203
lines changed

docs/tools.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,14 @@ Theoretical simulations
128128
129129
from mpqp.tools.theoretical_simulation import *
130130
131-
.. automodule:: mpqp.tools.theoretical_simulation
131+
.. automodule:: mpqp.tools.theoretical_simulation
132+
133+
Observable decomposition
134+
------------------------
135+
136+
.. code-block:: python
137+
:class: import
138+
139+
from mpqp.tools.obs_decomposition import *
140+
141+
.. automodule:: mpqp.tools.obs_decomposition

examples/notebooks/3_Expectation_value_of_observables.ipynb

+118-56
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,32 @@
115115
},
116116
{
117117
"cell_type": "code",
118-
"execution_count": 6,
118+
"execution_count": 22,
119119
"id": "b1cb8ad4",
120120
"metadata": {},
121121
"outputs": [
122122
{
123123
"name": "stdout",
124124
"output_type": "stream",
125125
"text": [
126-
"Result: ATOSDevice, MYQLM_PYLINALG\n",
127-
" Expectation value: -3.5935083233096687\n",
126+
"BatchResult: 2 results\n",
127+
"Result: circuit 1, ATOSDevice, MYQLM_PYLINALG\n",
128+
" Expectation value: -0.12200000000000011\n",
129+
" Error/Variance: 0.09998468351171062\n",
130+
"Result: circuit 1, IBMDevice, AER_SIMULATOR\n",
131+
" Expectation value: 1.9081958235744878e-17\n",
128132
" Error/Variance: 0.0\n",
129-
"-3.5935083233096687\n"
133+
"expectation_value: -0.12200000000000011\n",
134+
"expectation_value: 1.9081958235744878e-17\n"
130135
]
131136
}
132137
],
133138
"source": [
134139
"from mpqp.execution import run, ATOSDevice, IBMDevice\n",
135-
"result = run(circuit, ATOSDevice.MYQLM_PYLINALG)\n",
136-
"print(result)\n",
137-
"print(result.expectation_value)"
140+
"results = run(circuit, [ATOSDevice.MYQLM_PYLINALG, IBMDevice.AER_SIMULATOR])\n",
141+
"print(results)\n",
142+
"for result in results:\n",
143+
" print(f\"expectation_value:\", result.expectation_value)"
138144
]
139145
},
140146
{
@@ -159,12 +165,12 @@
159165
"output_type": "stream",
160166
"text": [
161167
"BatchResult: 2 results\n",
162-
"Result: ATOSDevice, MYQLM_PYLINALG\n",
163-
" Expectation value: -3.6279999999999997\n",
164-
" Error/Variance: 0.10472127540648739\n",
165-
"Result: IBMDevice, AER_SIMULATOR\n",
166-
" Expectation value: -3.2729999999999992\n",
167-
" Error/Variance: 22.060085000000004\n"
168+
"Result: circuit 1, ATOSDevice, MYQLM_PYLINALG\n",
169+
" Expectation value: -3.722\n",
170+
" Error/Variance: 0.10571682351161849\n",
171+
"Result: circuit 1, IBMDevice, AER_SIMULATOR\n",
172+
" Expectation value: -3.5935083233096723\n",
173+
" Error/Variance: 0.0\n"
168174
]
169175
}
170176
],
@@ -231,12 +237,6 @@
231237
"everything in term of expression based on the fundamental atoms. Let's see how."
232238
]
233239
},
234-
{
235-
"cell_type": "markdown",
236-
"id": "9ab10e2e",
237-
"metadata": {},
238-
"source": []
239-
},
240240
{
241241
"cell_type": "markdown",
242242
"id": "7d07ec4b",
@@ -257,7 +257,7 @@
257257
"name": "stdout",
258258
"output_type": "stream",
259259
"text": [
260-
"ps_1=1*I@Z + -3*X@Y\n"
260+
"ps_1=1*I@Z - 3*X@Y\n"
261261
]
262262
}
263263
],
@@ -266,6 +266,65 @@
266266
"print(f\"{ps_1=}\")"
267267
]
268268
},
269+
{
270+
"cell_type": "markdown",
271+
"id": "937f1a84",
272+
"metadata": {},
273+
"source": [
274+
"#### Using Symbolic Variables in a PauliString\n",
275+
"\n",
276+
"We can also define `PauliString` objects with symbolic coefficients using `sympy`:"
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": 11,
282+
"id": "032ff198",
283+
"metadata": {},
284+
"outputs": [
285+
{
286+
"name": "stdout",
287+
"output_type": "stream",
288+
"text": [
289+
"(θ)*I@X + (k)*Z@Y\n"
290+
]
291+
}
292+
],
293+
"source": [
294+
"from sympy import symbols\n",
295+
"\n",
296+
"theta, k = symbols(\"θ k\")\n",
297+
"ps_symbol = theta * I @ X + k * Z @ Y\n",
298+
"print(ps_symbol)"
299+
]
300+
},
301+
{
302+
"cell_type": "markdown",
303+
"id": "d153fda4",
304+
"metadata": {},
305+
"source": [
306+
"We can substitute numerical values for the symbolic coefficients:"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": 12,
312+
"id": "5dd3c74b",
313+
"metadata": {},
314+
"outputs": [
315+
{
316+
"name": "stdout",
317+
"output_type": "stream",
318+
"text": [
319+
"3.1416*I@X - 1*Z@Y\n"
320+
]
321+
}
322+
],
323+
"source": [
324+
"ps_symbol = ps_symbol.subs({theta: np.pi, k: -1})\n",
325+
"print(ps_symbol)"
326+
]
327+
},
269328
{
270329
"cell_type": "markdown",
271330
"id": "7824baf5",
@@ -283,17 +342,17 @@
283342
},
284343
{
285344
"cell_type": "code",
286-
"execution_count": 11,
345+
"execution_count": 13,
287346
"id": "06784ef3",
288347
"metadata": {},
289348
"outputs": [
290349
{
291350
"name": "stdout",
292351
"output_type": "stream",
293352
"text": [
294-
"ps_2 = 1*I@Z + 2.555555555*Y@I + 1*X@Z + -1*X@Z\n",
295-
" = 2.555555555*Y@I + 1*I@Z\n",
296-
" ~= 1*I@Z + 2.6*Y@I + 1*X@Z + -1*X@Z\n",
353+
"ps_2 = 1*I@Z + 2.555555555*Y@I + 1*X@Z - 1*X@Z\n",
354+
" = 1*I@Z + 2.555555555*Y@I\n",
355+
" ~= 1*I@Z + 2.6*Y@I + 1*X@Z - 1*X@Z\n",
297356
" ~= 1*I@Z + 2.5556*Y@I\n"
298357
]
299358
}
@@ -318,7 +377,7 @@
318377
},
319378
{
320379
"cell_type": "code",
321-
"execution_count": 12,
380+
"execution_count": 14,
322381
"id": "b9cadb08",
323382
"metadata": {},
324383
"outputs": [
@@ -327,20 +386,20 @@
327386
"output_type": "stream",
328387
"text": [
329388
"Addition:\n",
330-
"(1*I@Z + -3*X@Y) + (1*I@Z + 2.6*Y@I) = 2*I@Z + -3*X@Y + 2.6*Y@I\n",
389+
"(1*I@Z - 3*X@Y) + (1*I@Z + 2.6*Y@I) = 2*I@Z - 3*X@Y + 2.6*Y@I\n",
331390
"\n",
332391
"Subtraction:\n",
333-
"(1*I@Z + -3*X@Y) - (1*I@Z + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
392+
"(1*I@Z - 3*X@Y) - (1*I@Z + 2.6*Y@I) = -3*X@Y - 2.6*Y@I\n",
334393
"\n",
335394
"Scalar product:\n",
336-
"2 * (1*I@Z + -3*X@Y) = 2*I@Z + -6*X@Y\n",
395+
"2 * (1*I@Z - 3*X@Y) = 2*I@Z - 6*X@Y\n",
337396
"\n",
338397
"Scalar division:\n",
339398
"(1*I@Z + 2.6*Y@I) / 3 ~= 0.3333*I@Z + 0.8667*Y@I\n",
340399
"\n",
341400
"Tensor product:\n",
342-
"(1*I@Z + -3*X@Y) @ Z = 1*I@Z@Z + -3*X@Y@Z\n",
343-
"(1*I@Z + -3*X@Y) @ (1*I@Z + 2.6*Y@I) = 1*I@Z@I@Z + -3*I@Z@X@Y + 2.6*Y@I@I@Z + -7.8*Y@I@X@Y\n"
401+
"(1*I@Z - 3*X@Y) @ Z = 1*I@Z@Z - 3*X@Y@Z\n",
402+
"(1*I@Z - 3*X@Y) @ (1*I@Z + 2.6*Y@I) = 1*I@Z@I@Z + 2.6*I@Z@Y@I - 3*X@Y@I@Z - 7.8*X@Y@Y@I\n"
344403
]
345404
}
346405
],
@@ -373,7 +432,7 @@
373432
},
374433
{
375434
"cell_type": "code",
376-
"execution_count": 13,
435+
"execution_count": 15,
377436
"id": "5b0537a1",
378437
"metadata": {},
379438
"outputs": [],
@@ -393,7 +452,7 @@
393452
},
394453
{
395454
"cell_type": "code",
396-
"execution_count": 14,
455+
"execution_count": 23,
397456
"id": "1732e099",
398457
"metadata": {},
399458
"outputs": [
@@ -403,29 +462,32 @@
403462
"text": [
404463
"`obs` created with matrix:\n",
405464
"matrix:\n",
406-
"[[ 4.+0.j 2.+0.j 3.+0.j 8.+0.j]\n",
407-
" [ 2.+0.j -3.+0.j 1.+0.j 0.+0.j]\n",
408-
" [ 3.+0.j 1.+0.j -1.+0.j 5.+0.j]\n",
409-
" [ 8.+0.j 0.+0.j 5.+0.j 2.+0.j]]\n",
465+
"[[4, 2 , 3 , 8],\n",
466+
" [2, -3, 1 , 0],\n",
467+
" [3, 1 , -1, 5],\n",
468+
" [8, 0 , 5 , 2]]\n",
410469
"Pauli string:\n",
411-
"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",
470+
"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",
412471
"\n",
413472
"\n",
414473
"`obs1` created with Pauli string:\n",
415474
"Pauli string:\n",
416-
"1*I@Z + -3*X@Y\n",
475+
"1*I@Z - 3*X@Y\n",
417476
"matrix:\n",
418-
"[[ 1.+0.j 0.+0.j 0.+0.j 0.-3.j]\n",
419-
" [ 0.+0.j -1.+0.j 0.+3.j 0.+0.j]\n",
420-
" [ 0.+0.j 0.-3.j 1.+0.j 0.+0.j]\n",
421-
" [ 0.+3.j 0.+0.j 0.+0.j -1.+0.j]]\n"
477+
"[[1 , 0 , 0 , -3j],\n",
478+
" [0 , -1 , 3j, 0 ],\n",
479+
" [0 , -3j, 1 , 0 ],\n",
480+
" [3j, 0 , 0 , -1 ]]\n"
422481
]
423482
}
424483
],
425484
"source": [
485+
"from mpqp.tools import pprint\n",
486+
"\n",
487+
"\n",
426488
"print(\"`obs` created with matrix:\")\n",
427489
"print(\"matrix:\")\n",
428-
"print(obs.matrix)\n",
490+
"pprint(obs.matrix)\n",
429491
"print(\"Pauli string:\")\n",
430492
"print(obs.pauli_string)\n",
431493
"\n",
@@ -434,7 +496,7 @@
434496
"print(\"Pauli string:\")\n",
435497
"print(obs1.pauli_string)\n",
436498
"print(\"matrix:\")\n",
437-
"print(obs1.matrix)"
499+
"pprint(obs1.matrix)"
438500
]
439501
},
440502
{
@@ -449,7 +511,7 @@
449511
},
450512
{
451513
"cell_type": "code",
452-
"execution_count": 15,
514+
"execution_count": 17,
453515
"id": "892f0b6e",
454516
"metadata": {},
455517
"outputs": [],
@@ -460,7 +522,7 @@
460522
},
461523
{
462524
"cell_type": "code",
463-
"execution_count": 16,
525+
"execution_count": 18,
464526
"id": "745aa999",
465527
"metadata": {},
466528
"outputs": [
@@ -469,15 +531,15 @@
469531
"output_type": "stream",
470532
"text": [
471533
"BatchResult: 3 results\n",
472-
"Result: ATOSDevice, MYQLM_CLINALG\n",
473-
" Expectation value: 0.08200000000000007\n",
474-
" Error/Variance: 0.10000294289963965\n",
475-
"Result: ATOSDevice, MYQLM_PYLINALG\n",
476-
" Expectation value: 0.12600000000000006\n",
477-
" Error/Variance: 0.10000590573151757\n",
478-
"Result: IBMDevice, AER_SIMULATOR\n",
479-
" Expectation value: 0.14800000000000002\n",
480-
" Error/Variance: 9.987479999999998\n"
534+
"Result: circuit 1, ATOSDevice, MYQLM_PYLINALG\n",
535+
" Expectation value: -0.031999999999999945\n",
536+
" Error/Variance: 0.100047316133245\n",
537+
"Result: circuit 1, IBMDevice, AER_SIMULATOR\n",
538+
" Expectation value: 1.9081958235744878e-17\n",
539+
" Error/Variance: 0.0\n",
540+
"Result: circuit 1, ATOSDevice, MYQLM_CLINALG\n",
541+
" Expectation value: -0.29800000000000015\n",
542+
" Error/Variance: 0.0996658079447173\n"
481543
]
482544
}
483545
],
@@ -510,7 +572,7 @@
510572
"name": "python",
511573
"nbconvert_exporter": "python",
512574
"pygments_lexer": "ipython3",
513-
"version": "3.9.12"
575+
"version": "3.9.5"
514576
}
515577
},
516578
"nbformat": 4,

mpqp/core/instruction/measurement/expectation_value.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Observable:
6161
[ 0.+0.j, 0.+4.j, 3.+0.j, 0.+0.j],
6262
[ 0.-4.j, 0.+0.j, 0.+0.j, -3.+0.j]],
6363
dtype=complex64))
64-
>>> Observable(3 * I @ Z + 4 * X @ Y).pauli_string.sort_monomials()
64+
>>> Observable(3 * I @ Z + 4 * X @ Y).pauli_string.sorted_monomials()
6565
3*I@Z + 4*X@Y
6666
6767
"""

0 commit comments

Comments
 (0)