Skip to content

Commit

Permalink
results prints improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Henri-ColibrITD committed Apr 10, 2024
1 parent 71895ea commit 7ca4d5f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
11 changes: 8 additions & 3 deletions mpqp/execution/providers/ibm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def compute_expectation_value(
"type ExpectationMeasure"
)
nb_shots = job.measure.shots
qiskit_observable = job.measure.observable.to_other_language(Language.QISKIT)
qiskit_observable: Operator = job.measure.observable.to_other_language(
Language.QISKIT
) # pyright: ignore[reportAssignmentType]

if nb_shots != 0:
assert ibm_backend is not None
Expand Down Expand Up @@ -123,7 +125,8 @@ def check_job_compatibility(job: Job):
):
raise DeviceJobIncompatibleError(
"Cannot reconstruct state vector with this device. Please use "
f"{IBMDevice.AER_SIMULATOR_STATEVECTOR} instead"
f"{IBMDevice.AER_SIMULATOR_STATEVECTOR} instead (or change the job "
"type, by for example giving a number of shots to the measure)."
)
if job.device == IBMDevice.AER_SIMULATOR_STATEVECTOR:
if job.job_type == JobType.SAMPLE:
Expand Down Expand Up @@ -250,7 +253,9 @@ def submit_ibmq(job: Job) -> tuple[str, RuntimeJob | IBMJob]:
if job.job_type == JobType.OBSERVABLE:
assert isinstance(job.measure, ExpectationMeasure)
estimator = Runtime_Estimator(session=session)
qiskit_observable: Operator = job.measure.observable.to_other_language(Language.QISKIT)
qiskit_observable: Operator = job.measure.observable.to_other_language(
Language.QISKIT
) # pyright: ignore[reportAssignmentType]

ibm_job = estimator.run(
qiskit_circuit, qiskit_observable, shots=job.measure.shots
Expand Down
37 changes: 14 additions & 23 deletions mpqp/execution/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import math
from numbers import Complex
from textwrap import dedent
from typing import Optional

import numpy as np
Expand Down Expand Up @@ -61,12 +60,11 @@ def amplitudes(self):
return self.vector

def __str__(self):
return dedent(
f"""\
State vector: {self.vector}
Probabilities: {self.probabilities}
Number of qubits: {self.nb_qubits}"""
)
cleaned_vector = str(self.vector).replace("\n", " ")
cleaned_probas = str(self.probabilities).replace("\n", " ")
return f"""State vector: {cleaned_vector}
Probabilities: {cleaned_probas}
Number of qubits: {self.nb_qubits}"""


@typechecked
Expand Down Expand Up @@ -263,7 +261,6 @@ def __init__(
assert sample.count is not None
counts[sample.index] = sample.count
self._counts = counts
# if is_counts shots != 0
assert shots != 0
self._probabilities = np.array(counts, dtype=float) / self.shots
for sample in self._samples:
Expand Down Expand Up @@ -352,25 +349,19 @@ def __str__(self):
if self.job.job_type == JobType.SAMPLE:
samples_str = "\n".join(map(lambda s: f" {s}", self.samples))
cleaned_probas = str(self._probabilities).replace("\n", " ")
return header + dedent(
f"""\
Counts: {self._counts}
Probabilities: {cleaned_probas}
{samples_str}
Error: {self.error}
"""
)
return f"""{header}
Counts: {self._counts}
Probabilities: {cleaned_probas}
{samples_str}
Error: {self.error}"""

if self.job.job_type == JobType.STATE_VECTOR:
return f"{header}\n{self._state_vector}\n"
return header + "\n" + str(self.state_vector)

if self.job.job_type == JobType.OBSERVABLE:
return header + dedent(
f"""\
Expectation value: {self.expectation_value}
Error/Variance: {self.error}
"""
)
return f"""{header}
Expectation value: {self.expectation_value}
Error/Variance: {self.error}"""

raise NotImplementedError(
f"Job type {self.job.job_type} not implemented for __str__ method"
Expand Down
16 changes: 8 additions & 8 deletions mpqp/execution/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def _run_single(
Result: IBMDevice, AER_SIMULATOR
Counts: [512, 0, 0, 488]
Probabilities: [0.512 0. 0. 0.488]
State: 00, Index: 0, Count: 512, Probability: 0.512
State: 11, Index: 3, Count: 488, Probability: 0.488
State: 00, Index: 0, Count: 512, Probability: 0.512
State: 11, Index: 3, Count: 488, Probability: 0.488
Error: None
"""
job = generate_job(circuit, device, values)
Expand Down Expand Up @@ -172,8 +172,8 @@ def run(
Result: IBMDevice, AER_SIMULATOR
Counts: [512, 0, 0, 488]
Probabilities: [0.512 0. 0. 0.488]
State: 00, Index: 0, Count: 512, Probability: 0.512
State: 11, Index: 3, Count: 488, Probability: 0.488
State: 00, Index: 0, Count: 512, Probability: 0.512
State: 11, Index: 3, Count: 488, Probability: 0.488
Error: None
>>> batch_result = run(
... c,
Expand All @@ -184,14 +184,14 @@ def run(
Result: AWSDevice, BRAKET_LOCAL_SIMULATOR
Counts: [492, 0, 0, 508]
Probabilities: [0.492 0. 0. 0.508]
State: 00, Index: 0, Count: 492, Probability: 0.492
State: 11, Index: 3, Count: 508, Probability: 0.508
State: 00, Index: 0, Count: 492, Probability: 0.492
State: 11, Index: 3, Count: 508, Probability: 0.508
Error: None
Result: ATOSDevice, MYQLM_PYLINALG
Counts: [462, 0, 0, 538]
Probabilities: [0.462 0. 0. 0.538]
State: 00, Index: 0, Count: 462, Probability: 0.462
State: 11, Index: 3, Count: 538, Probability: 0.538
State: 00, Index: 0, Count: 462, Probability: 0.462
State: 11, Index: 3, Count: 538, Probability: 0.538
Error: 0.015773547629015002
"""

Expand Down
63 changes: 59 additions & 4 deletions tests/execution/test_result.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from __future__ import annotations

from numpy import pi
import numpy as np
import pytest
from numpy import pi

from mpqp import QCircuit
from mpqp.execution import Job, JobType, Result, Sample, StateVector
from mpqp.execution.devices import IBMDevice
from mpqp.gates import *
from mpqp.measures import BasisMeasure
from mpqp.execution import Result, StateVector, Sample, Job, JobType
from mpqp.execution.devices import IBMDevice


@pytest.mark.parametrize(
Expand All @@ -32,7 +33,7 @@ def test_result_wrong_type(job_type: JobType, data: float | StateVector | list[S
@pytest.mark.parametrize(
"job_type, data",
[
(JobType.STATE_VECTOR, StateVector([0.5, 0.5, 0.5, 0.5])),
(JobType.STATE_VECTOR, StateVector(np.ones(4, dtype=np.complex64) / 2)),
(JobType.OBSERVABLE, 0.4),
(
JobType.SAMPLE,
Expand All @@ -54,3 +55,57 @@ def test_result_right_type(job_type: JobType, data: float | StateVector | list[S
shots = 0
r = Result(j, data, shots=shots)
assert r.device == r.job.device


@pytest.mark.parametrize(
"result, expected_string",
[
(
Result(
Job(
JobType.STATE_VECTOR,
QCircuit(2),
IBMDevice.AER_SIMULATOR_STATEVECTOR,
),
StateVector(np.ones(4, dtype=np.complex64) / 2),
),
"""Result: IBMDevice, AER_SIMULATOR_STATEVECTOR
State vector: [0.5+0.j 0.5+0.j 0.5+0.j 0.5+0.j]
Probabilities: [0.25 0.25 0.25 0.25]
Number of qubits: 2""",
),
(
Result(
Job(
JobType.SAMPLE,
QCircuit(2),
IBMDevice.AER_SIMULATOR,
measure=BasisMeasure([0, 1]),
),
[
Sample(2, index=0, count=135),
Sample(2, index=1, count=226),
Sample(2, index=2, count=8),
Sample(2, index=3, count=231),
],
shots=600,
),
"""Result: IBMDevice, AER_SIMULATOR
Counts: [135, 226, 8, 231]
Probabilities: [0.225 0.37666667 0.01333333 0.385 ]
State: 00, Index: 0, Count: 135, Probability: 0.225
State: 01, Index: 1, Count: 226, Probability: 0.37666666666666665
State: 10, Index: 2, Count: 8, Probability: 0.013333333333333334
State: 11, Index: 3, Count: 231, Probability: 0.385
Error: None""",
),
(
Result(Job(JobType.OBSERVABLE, QCircuit(2), IBMDevice.AER_SIMULATOR), 0.65),
"""Result: IBMDevice, AER_SIMULATOR
Expectation value: 0.65
Error/Variance: None""",
),
],
)
def test_result_str(result: Result, expected_string: str):
assert str(result) == expected_string

0 comments on commit 7ca4d5f

Please sign in to comment.