From 1b965de07d691cbcced67a0d03fcf8f5b682adcb Mon Sep 17 00:00:00 2001 From: ikkoham Date: Wed, 10 Aug 2022 21:38:46 +0900 Subject: [PATCH 1/5] Add run_options to Primitives --- qiskit/primitives/base_estimator.py | 43 +++++++++++++++++-- qiskit/primitives/base_sampler.py | 42 ++++++++++++++++-- qiskit/primitives/estimator.py | 2 + qiskit/primitives/sampler.py | 4 +- ...imitives-run_options-eb4a360c3f1e197d.yaml | 6 +++ test/python/primitives/test_estimator.py | 18 ++++++++ test/python/primitives/test_sampler.py | 14 ++++++ 7 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 releasenotes/notes/primitives-run_options-eb4a360c3f1e197d.yaml diff --git a/qiskit/primitives/base_estimator.py b/qiskit/primitives/base_estimator.py index 830cb54c12fc..99456266676a 100644 --- a/qiskit/primitives/base_estimator.py +++ b/qiskit/primitives/base_estimator.py @@ -113,6 +113,7 @@ from qiskit.exceptions import QiskitError from qiskit.opflow import PauliSumOp from qiskit.providers import JobV1 as Job +from qiskit.providers import Options from qiskit.quantum_info.operators import SparsePauliOp from qiskit.quantum_info.operators.base_operator import BaseOperator from qiskit.utils.deprecation import deprecate_arguments, deprecate_function @@ -133,6 +134,7 @@ def __init__( circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, observables: Iterable[SparsePauliOp] | SparsePauliOp | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, + run_options: dict | None = None, ): """ Creating an instance of an Estimator, or using one in a ``with`` context opens a session that @@ -145,6 +147,7 @@ def __init__( will be bound. Defaults to ``[circ.parameters for circ in circuits]`` The indexing is such that ``parameters[i, j]`` is the j-th formal parameter of ``circuits[i]``. + run_options: runtime options. Raises: QiskitError: For mismatch of circuits and parameters list. @@ -185,6 +188,9 @@ def __init__( f"Different numbers of parameters of {i}-th circuit: " f"expected {circ.num_parameters}, actual {len(params)}." ) + self._run_options = Options() + if run_options is not None: + self._run_options.update_options(**run_options) def __new__( cls, @@ -258,6 +264,27 @@ def parameters(self) -> tuple[ParameterView, ...]: """ return tuple(self._parameters) + @property + def run_options(self) -> Options: + """Return options values for the estimator. + + Returns: + run_options + """ + return self._run_options + + def set_run_options(self, **fields) -> BaseEstimator: + """Set options values for the estimator. + + Args: + **fields: The fields to update the options + + Returns: + self + """ + self._run_options.update_options(**fields) + return self + @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " "and will be removed no sooner than 3 months after the releasedate. " @@ -386,12 +413,14 @@ def __call__( f"The number of circuits is {len(self.observables)}, " f"but the index {max(observables)} is given." ) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) return self._call( circuits=circuits, observables=observables, parameter_values=parameter_values, - **run_options, + **run_opts.__dict__, ) def run( @@ -495,8 +524,16 @@ def run( f"not match the number of qubits of the {i}-th observable " f"({observable.num_qubits})." ) - - return self._run(circuits, observables, parameter_values, parameter_views, **run_options) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) + + return self._run( + circuits, + observables, + parameter_values, + parameter_views, + **run_opts.__dict__, + ) @abstractmethod def _call( diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index 42591e582045..73cd98c9352b 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -101,6 +101,7 @@ from qiskit.circuit.parametertable import ParameterView from qiskit.exceptions import QiskitError from qiskit.providers import JobV1 as Job +from qiskit.providers import Options from qiskit.utils.deprecation import deprecate_arguments, deprecate_function from .sampler_result import SamplerResult @@ -118,12 +119,14 @@ def __init__( self, circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, + run_options: dict | None = None, ): """ Args: circuits: Quantum circuits to be executed. parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. + run_options: runtime options. Raises: QiskitError: For mismatch of circuits and parameters list. @@ -153,6 +156,9 @@ def __init__( f"Different number of parameters ({len(self._parameters)}) " f"and circuits ({len(self._circuits)})" ) + self._run_options = Options() + if run_options is not None: + self._run_options.update_options(**run_options) def __new__( cls, @@ -209,6 +215,27 @@ def parameters(self) -> tuple[ParameterView, ...]: """ return tuple(self._parameters) + @property + def run_options(self) -> Options: + """Return options values for the estimator. + + Returns: + run_options + """ + return self._run_options + + def set_run_options(self, **fields) -> BaseSampler: + """Set options values for the estimator. + + Args: + **fields: The fields to update the options + + Returns: + self + """ + self._run_options.update_options(**fields) + return self + @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " "and will be removed no sooner than 3 months after the releasedate. " @@ -285,11 +312,13 @@ def __call__( f"The number of circuits is {len(self.circuits)}, " f"but the index {max(circuits)} is given." ) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) return self._call( circuits=circuits, parameter_values=parameter_values, - **run_options, + **run_opts.__dict__, ) def run( @@ -358,8 +387,15 @@ def run( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) - - return self._run(circuits, parameter_values, parameter_views, **run_options) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) + + return self._run( + circuits, + parameter_values, + parameter_views, + **run_opts.__dict__, + ) @abstractmethod def _call( diff --git a/qiskit/primitives/estimator.py b/qiskit/primitives/estimator.py index e0ae5c9924e3..78ffbd6e078d 100644 --- a/qiskit/primitives/estimator.py +++ b/qiskit/primitives/estimator.py @@ -54,6 +54,7 @@ def __init__( circuits: QuantumCircuit | Iterable[QuantumCircuit] | None = None, observables: BaseOperator | PauliSumOp | Iterable[BaseOperator | PauliSumOp] | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, + run_options: dict | None = None, ): if isinstance(circuits, QuantumCircuit): circuits = (circuits,) @@ -69,6 +70,7 @@ def __init__( circuits=circuits, observables=observables, # type: ignore parameters=parameters, + run_options=run_options, ) self._is_closed = False diff --git a/qiskit/primitives/sampler.py b/qiskit/primitives/sampler.py index e5cee820ffce..134259973b7d 100644 --- a/qiskit/primitives/sampler.py +++ b/qiskit/primitives/sampler.py @@ -53,12 +53,14 @@ def __init__( self, circuits: QuantumCircuit | Iterable[QuantumCircuit] | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, + run_options: dict | None = None, ): """ Args: circuits: circuits to be executed parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. + run_options: runtime options. Raises: QiskitError: if some classical bits are not used for measurements. @@ -74,7 +76,7 @@ def __init__( preprocessed_circuits.append(circuit) else: preprocessed_circuits = None - super().__init__(preprocessed_circuits, parameters) + super().__init__(preprocessed_circuits, parameters, run_options) self._is_closed = False def _call( diff --git a/releasenotes/notes/primitives-run_options-eb4a360c3f1e197d.yaml b/releasenotes/notes/primitives-run_options-eb4a360c3f1e197d.yaml new file mode 100644 index 000000000000..153dbabd31e9 --- /dev/null +++ b/releasenotes/notes/primitives-run_options-eb4a360c3f1e197d.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Added ``run_options`` arguments in constructor of primitives and ``run_options`` methods to + primitives. It is now possible to set default ``run_options`` in addition to passing + ``run_options`` at runtime. diff --git a/test/python/primitives/test_estimator.py b/test/python/primitives/test_estimator.py index 5ce1571867a9..58212a358627 100644 --- a/test/python/primitives/test_estimator.py +++ b/test/python/primitives/test_estimator.py @@ -568,6 +568,24 @@ def test_run_with_shots_option(self): self.assertIsInstance(result, EstimatorResult) np.testing.assert_allclose(result.values, [-1.307397243478641]) + def test_run_options(self): + """Test for run_options""" + with self.subTest("init"): + estimator = Estimator(run_options={"shots": 3000}) + self.assertEqual(estimator.run_options.get("shots"), 3000) + with self.subTest("set_run_options"): + estimator.set_run_options(shots=1024, seed=15) + self.assertEqual(estimator.run_options.get("shots"), 1024) + self.assertEqual(estimator.run_options.get("seed"), 15) + with self.subTest("run"): + result = estimator.run( + [self.ansatz], + [self.observable], + parameter_values=[[0, 1, 1, 2, 3, 5]], + ).result() + self.assertIsInstance(result, EstimatorResult) + np.testing.assert_allclose(result.values, [-1.307397243478641]) + if __name__ == "__main__": unittest.main() diff --git a/test/python/primitives/test_sampler.py b/test/python/primitives/test_sampler.py index 7f258a644d9c..b4e40bcf8bb1 100644 --- a/test/python/primitives/test_sampler.py +++ b/test/python/primitives/test_sampler.py @@ -651,6 +651,20 @@ def test_run_with_shots_option_none(self): ).result() self.assertDictAlmostEqual(result_42.quasi_dists, result_15.quasi_dists) + def test_run_options(self): + """Test for run_options""" + with self.subTest("init"): + sampler = Sampler(run_options={"shots": 3000}) + self.assertEqual(sampler.run_options.get("shots"), 3000) + with self.subTest("set_run_options"): + sampler.set_run_options(shots=1024, seed=15) + self.assertEqual(sampler.run_options.get("shots"), 1024) + self.assertEqual(sampler.run_options.get("seed"), 15) + with self.subTest("run"): + params, target = self._generate_params_target([1]) + result = sampler.run([self._pqc], parameter_values=params).result() + self._compare_probs(result.quasi_dists, target) + if __name__ == "__main__": unittest.main() From a83c308d748a7b040c37a5c53dd697a57071bc1b Mon Sep 17 00:00:00 2001 From: ikkoham Date: Wed, 10 Aug 2022 21:55:42 +0900 Subject: [PATCH 2/5] rm unnecessary comments --- qiskit/primitives/base_estimator.py | 6 +++--- qiskit/primitives/base_sampler.py | 4 ++-- qiskit/primitives/sampler.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/qiskit/primitives/base_estimator.py b/qiskit/primitives/base_estimator.py index 99456266676a..2c405e0c1666 100644 --- a/qiskit/primitives/base_estimator.py +++ b/qiskit/primitives/base_estimator.py @@ -127,7 +127,7 @@ class BaseEstimator(ABC): Base class for Estimator that estimates expectation values of quantum circuits and observables. """ - __hash__ = None # type: ignore + __hash__ = None def __init__( self, @@ -339,7 +339,7 @@ def __call__( # Allow objects circuits = [ - self._circuit_ids.get(id(circuit)) # type: ignore + self._circuit_ids.get(id(circuit)) if not isinstance(circuit, (int, np.integer)) else circuit for circuit in circuits @@ -350,7 +350,7 @@ def __call__( "initialize the session." ) observables = [ - self._observable_ids.get(id(observable)) # type: ignore + self._observable_ids.get(id(observable)) if not isinstance(observable, (int, np.integer)) else observable for observable in observables diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index 73cd98c9352b..9dbae175a1e4 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -113,7 +113,7 @@ class BaseSampler(ABC): Base class of Sampler that calculates quasi-probabilities of bitstrings from quantum circuits. """ - __hash__ = None # type: ignore + __hash__ = None def __init__( self, @@ -270,7 +270,7 @@ def __call__( # Allow objects circuits = [ - self._circuit_ids.get(id(circuit)) # type: ignore + self._circuit_ids.get(id(circuit)) if not isinstance(circuit, (int, np.integer)) else circuit for circuit in circuits diff --git a/qiskit/primitives/sampler.py b/qiskit/primitives/sampler.py index 134259973b7d..7d740f8a8a32 100644 --- a/qiskit/primitives/sampler.py +++ b/qiskit/primitives/sampler.py @@ -16,7 +16,7 @@ from __future__ import annotations from collections.abc import Iterable, Sequence -from typing import Any, cast +from typing import Any import numpy as np @@ -167,5 +167,5 @@ def _preprocess_circuit(circuit: QuantumCircuit): ) c_q_mapping = sorted((c, q) for q, c in q_c_mapping.items()) qargs = [q for _, q in c_q_mapping] - circuit = cast(QuantumCircuit, circuit.remove_final_measurements(inplace=False)) + circuit = circuit.remove_final_measurements(inplace=False) return circuit, qargs From 96b8479fe4865f7dfe3cce16f056b814ba60f3fb Mon Sep 17 00:00:00 2001 From: ikkoham Date: Thu, 11 Aug 2022 23:57:01 +0900 Subject: [PATCH 3/5] initial commit of Settings dataclass --- qiskit/primitives/base_sampler.py | 42 +++++++++++++------------------ qiskit/primitives/sampler.py | 7 ++++-- qiskit/primitives/settings.py | 38 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 qiskit/primitives/settings.py diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index 9dbae175a1e4..6aa818585554 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -92,6 +92,7 @@ from abc import ABC, abstractmethod from collections.abc import Iterable, Sequence from copy import copy +from dataclasses import asdict from typing import cast from warnings import warn @@ -105,6 +106,7 @@ from qiskit.utils.deprecation import deprecate_arguments, deprecate_function from .sampler_result import SamplerResult +from .settings import Settings class BaseSampler(ABC): @@ -117,9 +119,9 @@ class BaseSampler(ABC): def __init__( self, + settings: Settings, circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, - run_options: dict | None = None, ): """ Args: @@ -156,9 +158,7 @@ def __init__( f"Different number of parameters ({len(self._parameters)}) " f"and circuits ({len(self._circuits)})" ) - self._run_options = Options() - if run_options is not None: - self._run_options.update_options(**run_options) + self._settings = settings def __new__( cls, @@ -216,25 +216,17 @@ def parameters(self) -> tuple[ParameterView, ...]: return tuple(self._parameters) @property - def run_options(self) -> Options: - """Return options values for the estimator. + def settings(self) -> Settings: + """Return settings values for the sampler. Returns: - run_options + Settings for sampler. """ - return self._run_options + return self._settings - def set_run_options(self, **fields) -> BaseSampler: - """Set options values for the estimator. - - Args: - **fields: The fields to update the options - - Returns: - self - """ - self._run_options.update_options(**fields) - return self + @settings.setter + def settings(self, settings): + self._settings = settings @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " @@ -312,13 +304,13 @@ def __call__( f"The number of circuits is {len(self.circuits)}, " f"but the index {max(circuits)} is given." ) - run_opts = copy(self.run_options) - run_opts.update_options(**run_options) + run_opts = asdict(self.settings.run_options) + run_opts.update(run_options) return self._call( circuits=circuits, parameter_values=parameter_values, - **run_opts.__dict__, + **run_opts, ) def run( @@ -387,14 +379,14 @@ def run( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) - run_opts = copy(self.run_options) - run_opts.update_options(**run_options) + run_opts = asdict(self.settings.run_options) + run_opts.update(run_options) return self._run( circuits, parameter_values, parameter_views, - **run_opts.__dict__, + **run_opts, ) @abstractmethod diff --git a/qiskit/primitives/sampler.py b/qiskit/primitives/sampler.py index 7d740f8a8a32..a410f0677a04 100644 --- a/qiskit/primitives/sampler.py +++ b/qiskit/primitives/sampler.py @@ -29,6 +29,7 @@ from .base_sampler import BaseSampler from .primitive_job import PrimitiveJob from .sampler_result import SamplerResult +from .settings import ReferenceSettings from .utils import final_measurement_mapping, init_circuit @@ -53,7 +54,7 @@ def __init__( self, circuits: QuantumCircuit | Iterable[QuantumCircuit] | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, - run_options: dict | None = None, + settings: ReferenceSettings | None = None, ): """ Args: @@ -76,7 +77,9 @@ def __init__( preprocessed_circuits.append(circuit) else: preprocessed_circuits = None - super().__init__(preprocessed_circuits, parameters, run_options) + if settings is None: + settings = ReferenceSettings() + super().__init__(settings, preprocessed_circuits, parameters) self._is_closed = False def _call( diff --git a/qiskit/primitives/settings.py b/qiskit/primitives/settings.py new file mode 100644 index 000000000000..4297c1f12e68 --- /dev/null +++ b/qiskit/primitives/settings.py @@ -0,0 +1,38 @@ +# This code is part of Qiskit. +# +# (C) Copyright IBM 2022. +# +# This code is licensed under the Apache License, Version 2.0. You may +# obtain a copy of this license in the LICENSE.txt file in the root directory +# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. +# +# Any modifications or derivative works of this code must retain this +# copyright notice, and modified files need to carry a notice indicating +# that they have been altered from the originals. +"""Run options for Primitives.""" +from __future__ import annotations + +from dataclasses import dataclass, field + +import numpy as np + + +@dataclass +class RunOptions: + ... + + +@dataclass +class Settings: + run_options: RunOptions + + +@dataclass +class ReferenceRunOptions(RunOptions): + shots: int | None = None + seed: int | np.random.Generator | None = None + + +@dataclass +class ReferenceSettings(Settings): + run_options: RunOptions = field(default_factory=ReferenceRunOptions) From d0de626e1cd31102fe67e8c6c42a1964d5a13d6a Mon Sep 17 00:00:00 2001 From: ikkoham Date: Fri, 2 Sep 2022 16:59:48 +0900 Subject: [PATCH 4/5] Revert "initial commit of Settings dataclass" This reverts commit 96b8479fe4865f7dfe3cce16f056b814ba60f3fb. --- qiskit/primitives/base_sampler.py | 42 ++++++++++++++++++------------- qiskit/primitives/sampler.py | 7 ++---- qiskit/primitives/settings.py | 38 ---------------------------- 3 files changed, 27 insertions(+), 60 deletions(-) delete mode 100644 qiskit/primitives/settings.py diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index 6aa818585554..9dbae175a1e4 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -92,7 +92,6 @@ from abc import ABC, abstractmethod from collections.abc import Iterable, Sequence from copy import copy -from dataclasses import asdict from typing import cast from warnings import warn @@ -106,7 +105,6 @@ from qiskit.utils.deprecation import deprecate_arguments, deprecate_function from .sampler_result import SamplerResult -from .settings import Settings class BaseSampler(ABC): @@ -119,9 +117,9 @@ class BaseSampler(ABC): def __init__( self, - settings: Settings, circuits: Iterable[QuantumCircuit] | QuantumCircuit | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, + run_options: dict | None = None, ): """ Args: @@ -158,7 +156,9 @@ def __init__( f"Different number of parameters ({len(self._parameters)}) " f"and circuits ({len(self._circuits)})" ) - self._settings = settings + self._run_options = Options() + if run_options is not None: + self._run_options.update_options(**run_options) def __new__( cls, @@ -216,17 +216,25 @@ def parameters(self) -> tuple[ParameterView, ...]: return tuple(self._parameters) @property - def settings(self) -> Settings: - """Return settings values for the sampler. + def run_options(self) -> Options: + """Return options values for the estimator. Returns: - Settings for sampler. + run_options """ - return self._settings + return self._run_options - @settings.setter - def settings(self, settings): - self._settings = settings + def set_run_options(self, **fields) -> BaseSampler: + """Set options values for the estimator. + + Args: + **fields: The fields to update the options + + Returns: + self + """ + self._run_options.update_options(**fields) + return self @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " @@ -304,13 +312,13 @@ def __call__( f"The number of circuits is {len(self.circuits)}, " f"but the index {max(circuits)} is given." ) - run_opts = asdict(self.settings.run_options) - run_opts.update(run_options) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) return self._call( circuits=circuits, parameter_values=parameter_values, - **run_opts, + **run_opts.__dict__, ) def run( @@ -379,14 +387,14 @@ def run( f"The number of values ({len(parameter_value)}) does not match " f"the number of parameters ({circuit.num_parameters}) for the {i}-th circuit." ) - run_opts = asdict(self.settings.run_options) - run_opts.update(run_options) + run_opts = copy(self.run_options) + run_opts.update_options(**run_options) return self._run( circuits, parameter_values, parameter_views, - **run_opts, + **run_opts.__dict__, ) @abstractmethod diff --git a/qiskit/primitives/sampler.py b/qiskit/primitives/sampler.py index a410f0677a04..7d740f8a8a32 100644 --- a/qiskit/primitives/sampler.py +++ b/qiskit/primitives/sampler.py @@ -29,7 +29,6 @@ from .base_sampler import BaseSampler from .primitive_job import PrimitiveJob from .sampler_result import SamplerResult -from .settings import ReferenceSettings from .utils import final_measurement_mapping, init_circuit @@ -54,7 +53,7 @@ def __init__( self, circuits: QuantumCircuit | Iterable[QuantumCircuit] | None = None, parameters: Iterable[Iterable[Parameter]] | None = None, - settings: ReferenceSettings | None = None, + run_options: dict | None = None, ): """ Args: @@ -77,9 +76,7 @@ def __init__( preprocessed_circuits.append(circuit) else: preprocessed_circuits = None - if settings is None: - settings = ReferenceSettings() - super().__init__(settings, preprocessed_circuits, parameters) + super().__init__(preprocessed_circuits, parameters, run_options) self._is_closed = False def _call( diff --git a/qiskit/primitives/settings.py b/qiskit/primitives/settings.py deleted file mode 100644 index 4297c1f12e68..000000000000 --- a/qiskit/primitives/settings.py +++ /dev/null @@ -1,38 +0,0 @@ -# This code is part of Qiskit. -# -# (C) Copyright IBM 2022. -# -# This code is licensed under the Apache License, Version 2.0. You may -# obtain a copy of this license in the LICENSE.txt file in the root directory -# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. -# -# Any modifications or derivative works of this code must retain this -# copyright notice, and modified files need to carry a notice indicating -# that they have been altered from the originals. -"""Run options for Primitives.""" -from __future__ import annotations - -from dataclasses import dataclass, field - -import numpy as np - - -@dataclass -class RunOptions: - ... - - -@dataclass -class Settings: - run_options: RunOptions - - -@dataclass -class ReferenceRunOptions(RunOptions): - shots: int | None = None - seed: int | np.random.Generator | None = None - - -@dataclass -class ReferenceSettings(Settings): - run_options: RunOptions = field(default_factory=ReferenceRunOptions) From cb807a11070d5662a79337058ffcc3de2e68206e Mon Sep 17 00:00:00 2001 From: ikkoham Date: Mon, 5 Sep 2022 20:36:14 +0900 Subject: [PATCH 5/5] fix lint, improve docs, don't return self --- qiskit/primitives/base_estimator.py | 6 +----- qiskit/primitives/base_sampler.py | 6 +----- qiskit/primitives/estimator.py | 11 +++++++++++ qiskit/primitives/sampler.py | 2 +- test/python/primitives/test_sampler.py | 2 +- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/qiskit/primitives/base_estimator.py b/qiskit/primitives/base_estimator.py index 2c405e0c1666..1353f4722788 100644 --- a/qiskit/primitives/base_estimator.py +++ b/qiskit/primitives/base_estimator.py @@ -278,12 +278,8 @@ def set_run_options(self, **fields) -> BaseEstimator: Args: **fields: The fields to update the options - - Returns: - self """ self._run_options.update_options(**fields) - return self @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " @@ -323,7 +319,7 @@ def __call__( circuits: the list of circuit indices or circuit objects. observables: the list of observable indices or observable objects. parameter_values: concrete parameters to be bound. - run_options: runtime options used for circuit execution. + run_options: Default runtime options used for circuit execution. Returns: EstimatorResult: The result of the estimator. diff --git a/qiskit/primitives/base_sampler.py b/qiskit/primitives/base_sampler.py index 9dbae175a1e4..25664dd4eea6 100644 --- a/qiskit/primitives/base_sampler.py +++ b/qiskit/primitives/base_sampler.py @@ -126,7 +126,7 @@ def __init__( circuits: Quantum circuits to be executed. parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. - run_options: runtime options. + run_options: Default runtime options. Raises: QiskitError: For mismatch of circuits and parameters list. @@ -229,12 +229,8 @@ def set_run_options(self, **fields) -> BaseSampler: Args: **fields: The fields to update the options - - Returns: - self """ self._run_options.update_options(**fields) - return self @deprecate_function( "The BaseSampler.__call__ method is deprecated as of Qiskit Terra 0.21.0 " diff --git a/qiskit/primitives/estimator.py b/qiskit/primitives/estimator.py index 78ffbd6e078d..16b46846af4b 100644 --- a/qiskit/primitives/estimator.py +++ b/qiskit/primitives/estimator.py @@ -56,6 +56,17 @@ def __init__( parameters: Iterable[Iterable[Parameter]] | None = None, run_options: dict | None = None, ): + """ + Args: + circuits: circuits that represent quantum states. + observables: observables to be estimated. + parameters: Parameters of each of the quantum circuits. + Defaults to ``[circ.parameters for circ in circuits]``. + run_options: Default runtime options. + + Raises: + QiskitError: if some classical bits are not used for measurements. + """ if isinstance(circuits, QuantumCircuit): circuits = (circuits,) if circuits is not None: diff --git a/qiskit/primitives/sampler.py b/qiskit/primitives/sampler.py index 7d740f8a8a32..77f4c4381486 100644 --- a/qiskit/primitives/sampler.py +++ b/qiskit/primitives/sampler.py @@ -60,7 +60,7 @@ def __init__( circuits: circuits to be executed parameters: Parameters of each of the quantum circuits. Defaults to ``[circ.parameters for circ in circuits]``. - run_options: runtime options. + run_options: Default runtime options. Raises: QiskitError: if some classical bits are not used for measurements. diff --git a/test/python/primitives/test_sampler.py b/test/python/primitives/test_sampler.py index b9ee0be8aa61..461d23bbd657 100644 --- a/test/python/primitives/test_sampler.py +++ b/test/python/primitives/test_sampler.py @@ -650,7 +650,7 @@ def test_run_with_shots_option_none(self): [self._pqc], parameter_values=[[0, 1, 1, 2, 3, 5]], shots=None, seed=15 ).result() self.assertDictAlmostEqual(result_42.quasi_dists, result_15.quasi_dists) - + def test_primitive_job_status_done(self): """test primitive job's status""" bell = self._circuit[1]