2
2
3
3
import math
4
4
from numbers import Complex
5
- from textwrap import dedent
6
5
from typing import Optional
7
6
8
7
import numpy as np
@@ -61,10 +60,11 @@ def amplitudes(self):
61
60
return self .vector
62
61
63
62
def __str__ (self ):
64
- return f"""
65
- State vector: { self .vector }
66
- Probabilities: { self .probabilities }
67
- Number of qubits: { self .nb_qubits } """
63
+ cleaned_vector = str (self .vector ).replace ("\n " , " " )
64
+ cleaned_probas = str (self .probabilities ).replace ("\n " , " " )
65
+ return f"""State vector: { cleaned_vector }
66
+ Probabilities: { cleaned_probas }
67
+ Number of qubits: { self .nb_qubits } """
68
68
69
69
70
70
@typechecked
@@ -261,7 +261,6 @@ def __init__(
261
261
assert sample .count is not None
262
262
counts [sample .index ] = sample .count
263
263
self ._counts = counts
264
- # if is_counts shots != 0
265
264
assert shots != 0
266
265
self ._probabilities = np .array (counts , dtype = float ) / self .shots
267
266
for sample in self ._samples :
@@ -272,6 +271,7 @@ def __init__(
272
271
" either `count` or `probability` (and the non-None "
273
272
"attribute amongst the two must be the same in all samples)."
274
273
)
274
+ self .samples .sort (key = lambda sample : sample .bin_str )
275
275
else :
276
276
raise ValueError (f"{ job .job_type } not handled" )
277
277
@@ -345,24 +345,24 @@ def counts(self) -> list[int]:
345
345
346
346
def __str__ (self ):
347
347
header = f"Result: { type (self .device ).__name__ } , { self .device .name } "
348
+
348
349
if self .job .job_type == JobType .SAMPLE :
349
- samples_str = ( "\n " + " " * 16 ) .join (map (str , self .samples ))
350
+ samples_str = "\n " .join (map (lambda s : f" { s } " , self .samples ))
350
351
cleaned_probas = str (self ._probabilities ).replace ("\n " , " " )
351
- return header + dedent (
352
- f"""
353
- Counts: { self ._counts }
354
- Probabilities: { cleaned_probas }
355
- { samples_str }
356
- Error: { self .error } \n \n """
357
- )
352
+ return f"""{ header }
353
+ Counts: { self ._counts }
354
+ Probabilities: { cleaned_probas }
355
+ { samples_str }
356
+ Error: { self .error } """
357
+
358
358
if self .job .job_type == JobType .STATE_VECTOR :
359
- return f"""{ header } \n { self ._state_vector } \n \n """
359
+ return header + "\n " + str (self .state_vector )
360
+
360
361
if self .job .job_type == JobType .OBSERVABLE :
361
- return header + dedent (
362
- f"""
363
- Expectation value: { self .expectation_value }
364
- Error/Variance: { self .error } \n \n """
365
- )
362
+ return f"""{ header }
363
+ Expectation value: { self .expectation_value }
364
+ Error/Variance: { self .error } """
365
+
366
366
raise NotImplementedError (
367
367
f"Job type { self .job .job_type } not implemented for __str__ method"
368
368
)
@@ -403,22 +403,22 @@ class BatchResult:
403
403
BatchResult: 3 results
404
404
Result: ATOSDevice, MYQLM_PYLINALG
405
405
State vector: [ 0.5+0.j 0.5+0.j 0.5+0.j -0.5+0.j]
406
- Probabilities: [0.25 0.25 0.25 0.25]
407
- Number of qubits: 2
406
+ Probabilities: [0.25 0.25 0.25 0.25]
407
+ Number of qubits: 2
408
408
Result: ATOSDevice, MYQLM_PYLINALG
409
409
Counts: [250, 0, 0, 250]
410
410
Probabilities: [0.5 0. 0. 0.5]
411
- State: 00, Index: 0, Count: 250, Probability: 0.5
412
- State: 11, Index: 3, Count: 250, Probability: 0.5
411
+ State: 00, Index: 0, Count: 250, Probability: 0.5
412
+ State: 11, Index: 3, Count: 250, Probability: 0.5
413
413
Error: 0.034
414
414
Result: ATOSDevice, MYQLM_PYLINALG
415
415
Expectation value: -3.09834
416
416
Error/Variance: 0.021
417
417
>>> print(batch_result[0])
418
418
Result: ATOSDevice, MYQLM_PYLINALG
419
- State vector: [ 0.5+0.j 0.5+0.j 0.5+0.j -0.5+0.j]
420
- Probabilities: [0.25 0.25 0.25 0.25]
421
- Number of qubits: 2
419
+ State vector: [ 0.5+0.j 0.5+0.j 0.5+0.j -0.5+0.j]
420
+ Probabilities: [0.25 0.25 0.25 0.25]
421
+ Number of qubits: 2
422
422
"""
423
423
424
424
def __init__ (self , results : list [Result ]):
0 commit comments