diff --git a/qiskit_experiments/framework/composite/batch_experiment.py b/qiskit_experiments/framework/composite/batch_experiment.py index c7f66cdca3..265b7fbbc7 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. @@ -65,6 +66,9 @@ def __init__( provided this will be initialized automatically from the supplied experiments. """ + # Experiment identification metadata + self._type = experiment_type if experiment_type else type(self).__name__ + if flatten_results is None: # Backward compatibility for 0.6 # This if-clause will be removed in 0.7 and flatten_result=True is set in arguments. @@ -89,6 +93,19 @@ def __init__( experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results ) + @property + 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 not None: + self._type = exp_type + else: + self._type = type(self).__name__ + def circuits(self): return self._batch_circuits(to_transpile=False) diff --git a/qiskit_experiments/framework/composite/parallel_experiment.py b/qiskit_experiments/framework/composite/parallel_experiment.py index 91031051ff..38db616691 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. @@ -65,6 +66,9 @@ def __init__( provided this will be initialized automatically from the supplied experiments. """ + # Experiment identification metadata + self._type = experiment_type if experiment_type else type(self).__name__ + if flatten_results is None: # Backward compatibility for 0.6 # This if-clause will be removed in 0.7 and flatten_result=True is set in arguments. @@ -82,6 +86,19 @@ def __init__( experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results ) + @property + 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 not None: + self._type = exp_type + else: + self._type = type(self).__name__ + def circuits(self): return self._combined_circuits(device_layout=False) diff --git a/test/framework/test_composite.py b/test/framework/test_composite.py index 9c2555c0c0..8b9c9be87a 100644 --- a/test/framework/test_composite.py +++ b/test/framework/test_composite.py @@ -44,6 +44,7 @@ # pylint: disable=missing-raises-doc +@ddt class TestComposite(QiskitExperimentsTestCase): """ Test composite experiment behavior. @@ -151,6 +152,29 @@ def test_roundtrip_serializable(self): self.assertRoundTripSerializable(exp) + @data(None, "Test") + def test_experiment_type(self, exp_type): + """Test the experiment_type setter for the parallel experiment and + batch experiment. + """ + exp = FakeExperiment([0]) + + par_exp = ParallelExperiment([exp], flatten_results=False) + if exp_type is None: + exp_type = type(par_exp).__name__ + self.assertEqual(par_exp.experiment_type, exp_type) + else: + par_exp.experiment_type = exp_type + self.assertEqual(par_exp.experiment_type, exp_type) + + batch_exp = BatchExperiment([exp], flatten_results=False) + if exp_type is None: + exp_type = type(batch_exp).__name__ + self.assertEqual(batch_exp.experiment_type, exp_type) + else: + batch_exp.experiment_type = exp_type + self.assertEqual(batch_exp.experiment_type, exp_type) + @ddt class TestCompositeExperimentData(QiskitExperimentsTestCase):