diff --git a/qiskit_experiments/framework/base_experiment.py b/qiskit_experiments/framework/base_experiment.py index 2bbb12cf7e..41240df41c 100644 --- a/qiskit_experiments/framework/base_experiment.py +++ b/qiskit_experiments/framework/base_experiment.py @@ -53,7 +53,7 @@ def __init__( QiskitError: If qubits contains duplicates. """ # Experiment identification metadata - self._type = experiment_type if experiment_type else type(self).__name__ + self.experiment_type = experiment_type # Circuit parameters self._num_qubits = len(physical_qubits) @@ -90,6 +90,14 @@ def experiment_type(self) -> str: """Return experiment type.""" return self._type + @experiment_type.setter + def experiment_type(self, exp_type: str) -> None: + """Set the type for the experiment.""" + if exp_type is None: + self._type = type(self).__name__ + else: + self._type = exp_type + @property def physical_qubits(self) -> Tuple[int, ...]: """Return the device qubits for the experiment.""" diff --git a/qiskit_experiments/framework/composite/batch_experiment.py b/qiskit_experiments/framework/composite/batch_experiment.py index c7f66cdca3..7ef50ac2a0 100644 --- a/qiskit_experiments/framework/composite/batch_experiment.py +++ b/qiskit_experiments/framework/composite/batch_experiment.py @@ -49,6 +49,7 @@ def __init__( backend: Optional[Backend] = None, flatten_results: bool = None, analysis: Optional[CompositeAnalysis] = None, + experiment_type: Optional[str] = None, ): """Initialize a batch experiment. @@ -86,7 +87,12 @@ def __init__( logical_qubit += 1 qubits = tuple(self._qubit_map.keys()) super().__init__( - experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results + experiments, + qubits, + backend=backend, + analysis=analysis, + flatten_results=flatten_results, + experiment_type=experiment_type, ) def circuits(self): diff --git a/qiskit_experiments/framework/composite/parallel_experiment.py b/qiskit_experiments/framework/composite/parallel_experiment.py index 91031051ff..24f901a292 100644 --- a/qiskit_experiments/framework/composite/parallel_experiment.py +++ b/qiskit_experiments/framework/composite/parallel_experiment.py @@ -49,6 +49,7 @@ def __init__( backend: Optional[Backend] = None, flatten_results: bool = None, analysis: Optional[CompositeAnalysis] = None, + experiment_type: Optional[str] = None, ): """Initialize the analysis object. @@ -79,7 +80,12 @@ def __init__( for exp in experiments: qubits += exp.physical_qubits super().__init__( - experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results + experiments, + qubits, + backend=backend, + analysis=analysis, + flatten_results=flatten_results, + experiment_type=experiment_type, ) def circuits(self): diff --git a/releasenotes/notes/setter-methods-for-experiment-099074e59faffb49.yaml b/releasenotes/notes/setter-methods-for-experiment-099074e59faffb49.yaml new file mode 100644 index 0000000000..157002c964 --- /dev/null +++ b/releasenotes/notes/setter-methods-for-experiment-099074e59faffb49.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Added ``experiment_type`` as optional ``__init__`` kwarg in :class:`.BatchExperiment` + and :class:`.ParallelExperiment`. + - | + :math:'experiment_type' can now be easily set and retrieved from the experiment + object post-construction using the 'experiment_type' property and setter. diff --git a/test/framework/test_composite.py b/test/framework/test_composite.py index 9c2555c0c0..b006d9f7c0 100644 --- a/test/framework/test_composite.py +++ b/test/framework/test_composite.py @@ -151,6 +151,21 @@ def test_roundtrip_serializable(self): self.assertRoundTripSerializable(exp) + def test_experiment_type(self): + """Test experiment_type setter.""" + + exp1 = FakeExperiment([0]) + + par_exp1 = ParallelExperiment([exp1], flatten_results=False) + batch_exp1 = BatchExperiment([exp1], flatten_results=False) + self.assertEqual(par_exp1.experiment_type, "ParallelExperiment") + self.assertEqual(batch_exp1.experiment_type, "BatchExperiment") + + par_exp2 = ParallelExperiment([exp1], flatten_results=False, experiment_type="yooo") + batch_exp2 = BatchExperiment([exp1], flatten_results=False, experiment_type="blaaa") + self.assertEqual(par_exp2.experiment_type, "yooo") + self.assertEqual(batch_exp2.experiment_type, "blaaa") + @ddt class TestCompositeExperimentData(QiskitExperimentsTestCase): diff --git a/test/framework/test_framework.py b/test/framework/test_framework.py index 674b54f850..16488a38f7 100644 --- a/test/framework/test_framework.py +++ b/test/framework/test_framework.py @@ -340,3 +340,22 @@ def circuits(self): } self.assertEqual(exp.job_info(backend=backend), job_info) + + def test_experiment_type(self): + """Test the experiment_type setter for the experiment.""" + + class MyExp(BaseExperiment): + """Some arbitrary experiment""" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def circuits(self): + pass + + exp1 = MyExp(physical_qubits=[0], experiment_type="blaaa") + self.assertEqual(exp1.experiment_type, "blaaa") + exp2 = MyExp(physical_qubits=[0]) + self.assertEqual(exp2.experiment_type, "MyExp") + exp2.experiment_type = "suieee" + self.assertEqual(exp2.experiment_type, "suieee")