Skip to content

Commit

Permalink
Add proparty and setter functions for experiment_type
Browse files Browse the repository at this point in the history
  • Loading branch information
nayan2167 committed Oct 28, 2023
1 parent 5ffef95 commit 1eadffd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
17 changes: 17 additions & 0 deletions qiskit_experiments/framework/composite/batch_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)

Expand Down
17 changes: 17 additions & 0 deletions qiskit_experiments/framework/composite/parallel_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions test/framework/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# pylint: disable=missing-raises-doc


@ddt
class TestComposite(QiskitExperimentsTestCase):
"""
Test composite experiment behavior.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 1eadffd

Please sign in to comment.