Skip to content

Commit

Permalink
Add M3 mitigation to estimator (Qiskit#71)
Browse files Browse the repository at this point in the history
* add M3 mitigation

* clarify no mitigation case
  • Loading branch information
t-imamichi authored and GitHub Enterprise committed Oct 8, 2021
1 parent 1e57ac2 commit 5d6c26e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion runtime/estimator.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{"name": "parameters", "description": "The parameters to be bound.", "type": "Union[list[float], list[list[float]]]", "required": false},
{"name": "class_name", "description": "The name of the evaluator class.", "type": "str", "required": false},
{"name": "transpile_options", "description": "Options for transpile.", "type": "dict", "required": false},
{"name": "run_options", "description": "Options for backend.run.", "type": "dict", "required": false}
{"name": "run_options", "description": "Options for backend run.", "type": "dict", "required": false},
{"name": "measurement_error_mitigation", "description": "Whether to apply measurement error mitigation. Default is False.", "type": "bool", "required": false}
]
}
25 changes: 20 additions & 5 deletions runtime/estimator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import numpy as np
from dataclasses import asdict

import numpy as np
from mthree.utils import final_measurement_mapping
from qiskit.evaluators.backends import ReadoutErrorMitigation
from qiskit.quantum_info import SparsePauliOp


def main(backend, user_messenger, **kwargs):

state = kwargs.pop("state")
observable = kwargs.pop("observable")
if isinstance(observable, list):
Expand All @@ -14,11 +15,28 @@ def main(backend, user_messenger, **kwargs):
class_name = kwargs.pop("class_name", "PauliExpectationValue")
transpile_options = kwargs.pop("transpile_options", {})
run_options = kwargs.pop("run_options", {})
measurement_error_mitigation = kwargs.pop("measurement_error_mitigation", False)

try:
if class_name == "PauliExpectationValue":
from qiskit.evaluators import PauliExpectationValue as Evaluator

if measurement_error_mitigation:
expval = Evaluator(state, observable, backend)
expval.set_transpile_options(**transpile_options)
mapping = final_measurement_mapping(expval.transpiled_circuits[0])
backend = ReadoutErrorMitigation(
backend=backend,
mitigation="mthree",
refresh=1800, # refresh the calibration data every 1800 seconds
shots=8192, # use M3's default shot number
qubits=list(mapping),
)
else:
# use backend as is
pass
elif class_name == "ExactExpectationValue":
# Note: ExactExpectationValue works only with Aer backend
from qiskit.evaluators import ExactExpectationValue as Evaluator
except ModuleNotFoundError:
raise RuntimeError("You are not authorized to use this program.")
Expand All @@ -28,9 +46,6 @@ def main(backend, user_messenger, **kwargs):
expval.set_run_options(**run_options)
result = expval.evaluate(parameters)

# for debug
# print(result)

ret = {
key: val.tolist() if isinstance(val, np.ndarray) else val
for key, val in asdict(result).items()
Expand Down

0 comments on commit 5d6c26e

Please sign in to comment.