Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix init of EstimatorV2 and SamplerV2 #2120

Merged
merged 9 commits into from
May 28, 2024
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
20 changes: 14 additions & 6 deletions qiskit_aer/primitives/estimator_v2.py
Original file line number Diff line number Diff line change
@@ -70,12 +70,20 @@ 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)
if "method" not in self.options.backend_options:
method = (
"density_matrix" if "noise_model" in self.options.backend_options else "automatic"
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this line necessary? Shouldn't we just let AerSimulator set the method if it's not specified? It should default to automatic, which should be smart enough to choose density_matrix if noise_model is specified.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this so now method=automatic is set by default and #2117 is fixed

ops = dict(method=method)
self.options.backend_options.update(ops)
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:
9 changes: 6 additions & 3 deletions qiskit_aer/primitives/sampler_v2.py
Original file line number Diff line number Diff line change
@@ -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:
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