forked from qiskit-community/quantum-prototype-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from QuantumApplicationLab/migrate_to_estimato…
…r_v2 Add compatibility with EstimatorV2 (`qiskit-aer` and `qiskit-ibm-runtime`)
- Loading branch information
Showing
8 changed files
with
381 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Primitives builder package.""" | ||
|
||
from .estimator_run_builder import EstimatorRunBuilder | ||
from .sampler_run_builder import SamplerRunBuilder | ||
|
||
|
||
__all__ = [ | ||
"EstimatorRunBuilder", | ||
"SamplerRunBuilder", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""This module defines a base class for primitive run builders.""" | ||
|
||
from typing import Union, List, Tuple, Dict, Any | ||
from qiskit import QuantumCircuit | ||
from qiskit.primitives import PrimitiveJob | ||
from qiskit_ibm_runtime import RuntimeJobV2 | ||
|
||
|
||
class BasePrimitiveRunBuilder: | ||
""" | ||
Base class for building and configuring primitive runs based on their provenance and options. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
primitive, | ||
circuits: List[QuantumCircuit], | ||
parameter_sets: List[List[float]], | ||
options: Dict[str, Any], | ||
): | ||
""" | ||
Initializes BasePrimitiveRunBuilder for given primitive, circuits, parameters, and options. | ||
Args: | ||
primitive (Union[SamplerValidType, EstimatorValidType]): The primitive to use for runs. | ||
circuits (List[QuantumCircuit]): The quantum circuits to run. | ||
parameter_sets (List[List[float]]): The parameters to vary in the circuits. | ||
options (Dict[str, Any]): Configuration options such as number of shots. | ||
""" | ||
self.primitive = primitive | ||
self.circuits = circuits | ||
self.parameter_sets = parameter_sets | ||
self.shots = options.pop("shots", None) | ||
self.seed = options.pop("seed", None) | ||
self.provenance = self.find_provenance() | ||
|
||
def find_provenance(self) -> Tuple[str, str]: | ||
"""Determines the provenance of the primitive based on its class and module.""" | ||
return ( | ||
self.primitive.__class__.__module__.split(".")[0], | ||
self.primitive.__class__.__name__, | ||
) | ||
|
||
def build_run(self) -> Union[PrimitiveJob, RuntimeJobV2]: | ||
""" | ||
Configures and returns primitive runs based on its provenance. | ||
Raises: | ||
NotImplementedError: If the primitive's provenance is not supported. | ||
Returns: | ||
Union[PrimitiveJob, RuntimeJobV2]: A primitive job. | ||
""" | ||
primitive_job = self._select_run_builder() | ||
return primitive_job() | ||
|
||
def _select_run_builder(self) -> Union[PrimitiveJob, RuntimeJobV2]: | ||
"""Selects the appropriate builder function based on the primitive's provenance.""" | ||
raise NotImplementedError("This method should be implemented by subclasses.") | ||
|
||
def _build_native_qiskit_run(self) -> PrimitiveJob: | ||
"""Builds a run function for a standard qiskit primitive.""" | ||
raise NotImplementedError("This method should be implemented by subclasses.") | ||
|
||
def _build_v2_run(self) -> Union[PrimitiveJob, RuntimeJobV2]: | ||
"""Builds a run function for qiskit-aer and qiskit-ibm-runtime V2 primitives.""" | ||
raise NotImplementedError("This method should be implemented by subclasses.") | ||
|
||
def _build_v1_run(self): | ||
""" | ||
Attempts to build a run function for primitives V1, which will be soon deprecated. | ||
Raises: | ||
NotImplementedError: Indicates that V1 will be soon deprecated. | ||
""" | ||
raise NotImplementedError( | ||
"Primitives V1 will be soon deprecated. Please, use V2 implementation." | ||
) |
Oops, something went wrong.