Skip to content

Commit ae8da66

Browse files
Merge pull request #54 from ColibrITD-SAS/dev
doc: format fix
2 parents 9dfd710 + fdfd2a5 commit ae8da66

File tree

11 files changed

+150
-134
lines changed

11 files changed

+150
-134
lines changed

examples/notebooks/3_Expectation_value_of_observables.ipynb

+47-41
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,29 @@
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+
},
2330
{
2431
"name": "stdout",
2532
"output_type": "stream",
2633
"text": [
2734
" ┌───┐ ┌─────────┐ \n",
2835
"q_0: ┤ H ├──■───┤ Ry(2.6) ├─\n",
29-
" ───┌─┴─┐┌┴─────────┴┐\n",
30-
"q_1: ┤ Y ├┤ X ├┤ Ry(-0.87) ├\n",
31-
" └───┘└───┘└───────────┘\n"
36+
" ───┌─┴─┐┌┴─────────┴┐\n",
37+
"q_1: ─────┤ X ├┤ Ry(-0.87) ├\n",
38+
" └───┘└───────────┘\n"
3239
]
3340
}
3441
],
3542
"source": [
3643
"from mpqp import QCircuit\n",
3744
"from mpqp.gates import *\n",
38-
"circuit = QCircuit([H(0), Y(1), CNOT(0,1), Ry(2.6, 0), Ry(-0.87, 1)])\n",
45+
"circuit = QCircuit([H(0), CNOT(0,1), Ry(2.6, 0), Ry(-0.87, 1)])\n",
3946
"print(circuit)"
4047
]
4148
},
@@ -124,10 +131,9 @@
124131
"output_type": "stream",
125132
"text": [
126133
"Result: ATOSDevice, MYQLM_PYLINALG\n",
127-
" Expectation value: -3.317064001288396\n",
134+
" Expectation value: -3.5935083233096687\n",
128135
" Error/Variance: 0.0\n",
129-
"\n",
130-
"-3.317064001288396\n"
136+
"-3.5935083233096687\n"
131137
]
132138
}
133139
],
@@ -160,13 +166,12 @@
160166
"output_type": "stream",
161167
"text": [
162168
"BatchResult: 2 results\n",
163-
"Result: IBMDevice, AER_SIMULATOR\n",
164-
" Expectation value: -3.2504999999999997\n",
165-
" Error/Variance: 41.46215925\n",
166169
"Result: ATOSDevice, MYQLM_PYLINALG\n",
167-
" Expectation value: -3.2715\n",
168-
" Error/Variance: 0.1439368300002305\n",
169-
"\n"
170+
" Expectation value: -3.4769999999999994\n",
171+
" Error/Variance: 0.10704925979198084\n",
172+
"Result: IBMDevice, AER_SIMULATOR\n",
173+
" Expectation value: -3.6155\n",
174+
" Error/Variance: 22.003217250000002\n"
170175
]
171176
}
172177
],
@@ -187,25 +192,26 @@
187192
},
188193
{
189194
"cell_type": "code",
190-
"execution_count": 9,
195+
"execution_count": 11,
191196
"id": "4ac9c7e0",
192197
"metadata": {},
193198
"outputs": [],
194199
"source": [
195-
"from mpqp.measures import I, X as Pauli_X, Y as Pauli_Y, X as Pauli_Z"
200+
"from mpqp.measures import I, X, Y, Z"
196201
]
197202
},
198203
{
199204
"cell_type": "markdown",
200205
"id": "aed703a9",
201206
"metadata": {},
202207
"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",
208+
"> ⚠ **pauli_string import**: pauli atoms are named `I`, `X`, `Y`, and `Z`. If\n",
209+
"> you have conflicts with the gates of the same name, you can:\n",
204210
"> - **Rename Import:**\n",
205211
"> ```python \n",
206-
"> from mpqp.measures import X as Pauli_X \n",
212+
"> from mpqp.measures import X as obs_X \n",
207213
"> ```\n",
208-
"> - **Import Only Pauli String:**\n",
214+
"> - **Import the whole module:**\n",
209215
"> ```python\n",
210216
"> from mpqp.measures import pauli_string \n",
211217
"> pauli_string.X\n",
@@ -242,20 +248,20 @@
242248
},
243249
{
244250
"cell_type": "code",
245-
"execution_count": 10,
251+
"execution_count": 15,
246252
"id": "bc5ed517",
247253
"metadata": {},
248254
"outputs": [
249255
{
250256
"name": "stdout",
251257
"output_type": "stream",
252258
"text": [
253-
"ps_1=1*I@X + -3*X@Y\n"
259+
"ps_1=1*I@Z + -3*X@Y\n"
254260
]
255261
}
256262
],
257263
"source": [
258-
"ps_1 = I @ Pauli_Z - 3 * Pauli_X @ Pauli_Y\n",
264+
"ps_1 = I@Z - 3 * X@Y\n",
259265
"print(f\"{ps_1=}\")"
260266
]
261267
},
@@ -276,23 +282,23 @@
276282
},
277283
{
278284
"cell_type": "code",
279-
"execution_count": 11,
285+
"execution_count": 16,
280286
"id": "06784ef3",
281287
"metadata": {},
282288
"outputs": [
283289
{
284290
"name": "stdout",
285291
"output_type": "stream",
286292
"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"
293+
"ps_2 = 1*I@Z + 2.555555555*Y@I + 1*X@Z + -1*X@Z\n",
294+
" = 1*I@Z + 2.555555555*Y@I\n",
295+
" ~= 1*I@Z + 2.6*Y@I + 1*X@Z + -1*X@Z\n",
296+
" ~= 1*I@Z + 2.5556*Y@I\n"
291297
]
292298
}
293299
],
294300
"source": [
295-
"ps_2 = I @ Pauli_Z + 2.555555555 * Pauli_Y @ I + Pauli_X @ Pauli_Z - Pauli_X @ Pauli_Z\n",
301+
"ps_2 = I@Z + 2.555555555*Y@I + X@Z - X@Z\n",
296302
"print(\"ps_2 =\",repr(ps_2))\n",
297303
"print(\" =\",repr(ps_2.simplify()))\n",
298304
"print(\" ~=\",repr(ps_2.round(1)))\n",
@@ -311,7 +317,7 @@
311317
},
312318
{
313319
"cell_type": "code",
314-
"execution_count": 12,
320+
"execution_count": 17,
315321
"id": "b9cadb08",
316322
"metadata": {},
317323
"outputs": [
@@ -320,21 +326,21 @@
320326
"output_type": "stream",
321327
"text": [
322328
"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",
329+
"(1*I@Z + -3*X@Y) + (1*I@Z + 2.6*Y@I) = 2*I@Z + -3*X@Y + 2.6*Y@I\n",
324330
"\n",
325331
"Subtraction:\n",
326-
"(1*I@X + -3*X@Y) - (1*I@X + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
332+
"(1*I@Z + -3*X@Y) - (1*I@Z + 2.6*Y@I) = -3*X@Y + -2.6*Y@I\n",
327333
"\n",
328334
"Scalar product:\n",
329-
"2 * (1*I@X + -3*X@Y) = 2*I@X + -6*X@Y\n",
335+
"2 * (1*I@Z + -3*X@Y) = 2*I@Z + -6*X@Y\n",
330336
"\n",
331337
"Scalar division:\n",
332-
"(1*I@X + 2.6*Y@I) / 3 ~= 0.3333*I@X + 0.8667*Y@I\n",
338+
"(1*I@Z + 2.6*Y@I) / 3 ~= 0.3333*I@Z + 0.8667*Y@I\n",
333339
"\n",
334340
"Tensor product:\n",
335-
"(1*I@X + -3*X@Y) @ Z = 1*I@X@X + -3*X@Y@X\n",
341+
"(1*I@Z + -3*X@Y) @ Z = 1*I@Z@Z + -3*X@Y@Z\n",
336342
"\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"
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"
338344
]
339345
}
340346
],
@@ -353,9 +359,9 @@
353359
"({ps_2}) / 3 ~= {ps_2 / 3}\n",
354360
"\n",
355361
"Tensor product:\n",
356-
"({ps_1}) @ Z = {ps_1 @ Pauli_Z}\n",
362+
"({ps_1}) @ Z = {ps_1@Z}\n",
357363
"\n",
358-
"({ps_1}) @ ({ps_2}) = {ps_1 @ ps_2}\"\"\")"
364+
"({ps_1}) @ ({ps_2}) = {ps_1@ps_2}\"\"\")"
359365
]
360366
},
361367
{
@@ -368,7 +374,7 @@
368374
},
369375
{
370376
"cell_type": "code",
371-
"execution_count": 13,
377+
"execution_count": null,
372378
"id": "5b0537a1",
373379
"metadata": {},
374380
"outputs": [],
@@ -388,7 +394,7 @@
388394
},
389395
{
390396
"cell_type": "code",
391-
"execution_count": 31,
397+
"execution_count": null,
392398
"id": "1732e099",
393399
"metadata": {},
394400
"outputs": [
@@ -444,7 +450,7 @@
444450
},
445451
{
446452
"cell_type": "code",
447-
"execution_count": 24,
453+
"execution_count": null,
448454
"id": "892f0b6e",
449455
"metadata": {},
450456
"outputs": [],
@@ -455,7 +461,7 @@
455461
},
456462
{
457463
"cell_type": "code",
458-
"execution_count": 25,
464+
"execution_count": null,
459465
"id": "745aa999",
460466
"metadata": {},
461467
"outputs": [
@@ -506,7 +512,7 @@
506512
"name": "python",
507513
"nbconvert_exporter": "python",
508514
"pygments_lexer": "ipython3",
509-
"version": "3.9.5"
515+
"version": "3.9.12"
510516
}
511517
},
512518
"nbformat": 4,

mpqp/core/instruction/measurement/expectation_value.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ class Observable:
4343
observable : can be either a Hermitian matrix representing the
4444
observable or PauliString representing the observable.
4545
46+
Raises:
47+
ValueError: If the input matrix is not Hermitian or does not have a
48+
square shape.
49+
NumberQubitsError: If the number of qubits in the input observable does
50+
not match the number of target qubits.
51+
4652
Example:
4753
>>> from mpqp.core.instruction.measurement.pauli_string import I, X, Y, Z
4854
>>> matrix = np.array([[1, 0], [0, -1]])
4955
>>> ps = 3 * I @ Z + 4 * X @ Y
5056
>>> obs = Observable(matrix)
5157
>>> obs2 = Observable(ps)
5258
53-
Raises:
54-
ValueError: If the input matrix is not Hermitian or does not have a
55-
square shape.
56-
NumberQubitsError: If the number of qubits in the input observable does
57-
not match the number of target qubits.
5859
"""
5960

6061
def __init__(self, observable: Matrix | PauliString):

mpqp/core/instruction/measurement/pauli_string.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class PauliString:
3737
- **Import Only Pauli String:** ``from mpqp.core.instruction.measurement import pauli_string``, usage: ``pauli_string.X``
3838
"""
3939

40+
# TODO: reformulate ?
41+
4042
def __init__(self, monomials: Optional[list["PauliStringMonomial"]] = None):
4143
self._monomials: list[PauliStringMonomial] = []
4244

@@ -261,15 +263,15 @@ def from_matrix(cls, matrix: Matrix) -> PauliString:
261263
Returns:
262264
PauliString: Pauli string decomposition of the matrix in parameter.
263265
266+
Raises:
267+
ValueError: If the input matrix is not square or its dimensions are
268+
not a power of 2.
264269
265270
Example:
266271
>>> ps = PauliString.from_matrix(np.array([[0, 1], [1, 2]]))
267272
>>> print(ps)
268273
1*I + 1*X + -1*Z
269-
270-
Raises:
271-
ValueError: If the input matrix is not square or its dimensions are
272-
not a power of 2.
274+
273275
"""
274276
if matrix.shape[0] != matrix.shape[1]:
275277
raise ValueError("Input matrix must be square.")
@@ -497,7 +499,7 @@ def __repr__(self):
497499

498500
def __truediv__(self, other: FixedReal) -> PauliStringMonomial:
499501
return PauliStringMonomial(
500-
1 / other, [self] # pyright: ignore[reportArgumentType]
502+
1 / other, [self] # pyright: ignore[reportArgumentType]
501503
)
502504

503505
def __imul__(self, other: FixedReal) -> PauliStringMonomial:

mpqp/execution/connection/aws_connection.py

-3
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,6 @@ def get_braket_device(device: AWSDevice, is_noisy: bool = False) -> "BraketDevic
111111
ResultType(name='Variance', observables=['x', 'y', 'z', 'h', 'i'], minShots=10, maxShots=100000),
112112
ResultType(name='Probability', observables=None, minShots=10, maxShots=100000)]
113113
114-
Raises:
115-
AWSBraketRemoteExecutionError: If the device or the region could not be
116-
retrieved.
117114
"""
118115
import boto3
119116
from botocore.exceptions import NoRegionError

mpqp/execution/connection/ibm_connection.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,16 @@ def get_backend(device: IBMDevice) -> "BackendV1":
201201
Returns:
202202
The requested backend.
203203
204+
Raises:
205+
ValueError: If the required backend is a local simulator.
206+
IBMRemoteExecutionError: If the device was not found.
207+
204208
Example:
205209
>>> brisbane = get_backend(IBMDevice.IBM_BRISBANE)
206210
>>> brisbane.properties().gates[0].parameters
207211
[Nduv(datetime.datetime(2024, 1, 9, 11, 3, 18, tzinfo=tzlocal()), gate_error, , 0.00045619997922344296),
208212
Nduv(datetime.datetime(2024, 1, 9, 15, 41, 39, tzinfo=tzlocal()), gate_length, ns, 60)]
209213
210-
Raises:
211-
ValueError: If the required backend is a local simulator.
212-
IBMRemoteExecutionError: If the device was not found.
213214
"""
214215
# NOTE:
215216
# Question : when a backend is present in several IBMQ instances, which instance does it use to submit jobs

mpqp/execution/connection/qlm_connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def config_qlm_account(username: str, password: str, global_config: bool) -> boo
2828
outside MPQP.
2929
3030
Raises:
31-
QLMRemoteExecutionError: TODO
31+
QLMRemoteExecutionError
3232
"""
3333
# store the username and password in environment variables QLM_USER and QLM_PASSWD in .mpqp
3434
prev_user = get_env_variable("QLM_USER")

0 commit comments

Comments
 (0)