From fee8d6c8c3c72419656a1b9d6635290ff6f8d96b Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 May 2024 14:56:40 +0900 Subject: [PATCH 1/4] fix init of EstimatorV2 and SamplerV2 to handle method --- qiskit_aer/primitives/estimator_v2.py | 9 +++++++-- qiskit_aer/primitives/sampler_v2.py | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/qiskit_aer/primitives/estimator_v2.py b/qiskit_aer/primitives/estimator_v2.py index a4bdb4da68..2532eb927e 100644 --- a/qiskit_aer/primitives/estimator_v2.py +++ b/qiskit_aer/primitives/estimator_v2.py @@ -70,8 +70,13 @@ def __init__( the runtime options (``run_options``). """ self._options = Options(**options) if options else Options() - method = "density_matrix" if "noise_model" in self.options.backend_options else "automatic" - self._backend = AerSimulator(method=method, **self.options.backend_options) + if "method" not in self.options.backend_options: + method = ( + "density_matrix" if "noise_model" in self.options.backend_options else "automatic" + ) + ops = dict(method=method) + self.options.backend_options.update(ops) + self._backend = AerSimulator(**self.options.backend_options) def from_backend(self, backend, **options): """use external backend""" diff --git a/qiskit_aer/primitives/sampler_v2.py b/qiskit_aer/primitives/sampler_v2.py index 4f8c8f52a2..80a451b15a 100644 --- a/qiskit_aer/primitives/sampler_v2.py +++ b/qiskit_aer/primitives/sampler_v2.py @@ -98,6 +98,12 @@ def __init__( self._seed = seed self._options = Options(**options) if options else Options() + if "method" not in self.options.backend_options: + method = ( + "density_matrix" if "noise_model" in self.options.backend_options else "automatic" + ) + ops = dict(method=method) + self.options.backend_options.update(ops) self._backend = AerSimulator(**self.options.backend_options) def from_backend(self, backend, **options): From a7282345d79dc060da84cb3c7099a39dfe47481b Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 2 May 2024 15:13:36 +0900 Subject: [PATCH 2/4] add release note --- .../notes/fix_primitiveV2_init-afe7b331ddbef538.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 releasenotes/notes/fix_primitiveV2_init-afe7b331ddbef538.yaml diff --git a/releasenotes/notes/fix_primitiveV2_init-afe7b331ddbef538.yaml b/releasenotes/notes/fix_primitiveV2_init-afe7b331ddbef538.yaml new file mode 100644 index 0000000000..ba319729a9 --- /dev/null +++ b/releasenotes/notes/fix_primitiveV2_init-afe7b331ddbef538.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Fixed init function of EstimatorV2 and SamplerV2 to set `method` in + its option property if there is no `method` in input parameter From aef108a92adbe46a21407eaddfe3b8451d77c5b1 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 13 May 2024 18:56:02 +0900 Subject: [PATCH 3/4] fix from_backend and example in README --- README.md | 8 ++++---- qiskit_aer/primitives/estimator_v2.py | 9 ++++++--- qiskit_aer/primitives/sampler_v2.py | 15 ++++++--------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index cfd4cd36f7..770a172177 100755 --- a/README.md +++ b/README.md @@ -134,13 +134,13 @@ provider = QiskitRuntimeService(channel='ibm_quantum', token="set your own token backend = provider.get_backend("ibm_kyoto") # create sampler from the actual backend -sampler.from_backend(backend) +sampler = SamplerV2.from_backend(backend) # run a sampler job on the parameterized circuits with noise model of the actual hardware -job3 = sampler.run([(pqc, theta1), (pqc2, theta2)]) +bell_t = transpile(bell, AerSimulator(basis_gates=["ecr", "id", "rz", "sx"]), optimization_level=0) +job3 = sampler.run([bell_t], shots=128) job_result = job3.result() -print(f"Parameterized for Bell circuit w/noise: {job_result[0].data.meas.get_counts()}") - +print(f"counts for Bell circuit w/noise: {job_result[0].data.meas.get_counts()}") ``` ## Contribution Guidelines diff --git a/qiskit_aer/primitives/estimator_v2.py b/qiskit_aer/primitives/estimator_v2.py index 2532eb927e..3176858936 100644 --- a/qiskit_aer/primitives/estimator_v2.py +++ b/qiskit_aer/primitives/estimator_v2.py @@ -78,9 +78,12 @@ def __init__( self.options.backend_options.update(ops) self._backend = AerSimulator(**self.options.backend_options) - def from_backend(self, backend, **options): - """use external backend""" - self._backend.from_backend(backend, **options) + @classmethod + def from_backend(cls, backend, **options): + """make new sampler that uses external backend""" + estimator = cls(**options) + estimator._backend = AerSimulator.from_backend(backend) + return estimator @property def options(self) -> Options: diff --git a/qiskit_aer/primitives/sampler_v2.py b/qiskit_aer/primitives/sampler_v2.py index 80a451b15a..f2c79b1476 100644 --- a/qiskit_aer/primitives/sampler_v2.py +++ b/qiskit_aer/primitives/sampler_v2.py @@ -98,17 +98,14 @@ def __init__( self._seed = seed self._options = Options(**options) if options else Options() - if "method" not in self.options.backend_options: - method = ( - "density_matrix" if "noise_model" in self.options.backend_options else "automatic" - ) - ops = dict(method=method) - self.options.backend_options.update(ops) self._backend = AerSimulator(**self.options.backend_options) - def from_backend(self, backend, **options): - """use external backend""" - self._backend.from_backend(backend, **options) + @classmethod + def from_backend(cls, backend, **options): + """make new sampler that uses external backend""" + sampler = cls(**options) + sampler._backend = AerSimulator.from_backend(backend) + return sampler @property def default_shots(self) -> int: From 6eb0655d9e7223064d9599b821ef233172d68184 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 20 May 2024 17:35:11 +0900 Subject: [PATCH 4/4] remove setting density_matrix --- qiskit_aer/primitives/estimator_v2.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/qiskit_aer/primitives/estimator_v2.py b/qiskit_aer/primitives/estimator_v2.py index 3176858936..bfeeca73c5 100644 --- a/qiskit_aer/primitives/estimator_v2.py +++ b/qiskit_aer/primitives/estimator_v2.py @@ -70,12 +70,6 @@ def __init__( the runtime options (``run_options``). """ self._options = Options(**options) if options else Options() - if "method" not in self.options.backend_options: - method = ( - "density_matrix" if "noise_model" in self.options.backend_options else "automatic" - ) - ops = dict(method=method) - self.options.backend_options.update(ops) self._backend = AerSimulator(**self.options.backend_options) @classmethod