Skip to content

Commit

Permalink
Fix init of EstimatorV2 and SamplerV2 (Qiskit#2120)
Browse files Browse the repository at this point in the history
* fix init of EstimatorV2 and SamplerV2 to handle method

* add release note

* fix from_backend and example in README

* remove setting density_matrix
  • Loading branch information
doichanj committed May 28, 2024
1 parent 42c82c1 commit 989a76e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions qiskit_aer/primitives/estimator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ 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)

def from_backend(self, backend, **options):
"""use external backend"""
self._backend.from_backend(backend, **options)
self._backend = AerSimulator(**self.options.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:
Expand Down
9 changes: 6 additions & 3 deletions qiskit_aer/primitives/sampler_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ def __init__(
self._options = Options(**options) if options else Options()
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:
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix_primitiveV2_init-afe7b331ddbef538.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 989a76e

Please sign in to comment.