Skip to content

Commit d4b9d14

Browse files
Setting type for composite experiment (#1296)
### Summary This PR solves issue #1289 Added `experiment_type` property and setter in `BatchExperiment` and `ParallelExperiment` also added `experiment_type` setter in `BaseExperiment`, this changes will provide user flexibility and ease of use within their applications. ### PR checklist (delete when all criteria are met) - [X] I have read the contributing guide `CONTRIBUTING.md`. - [X] I have added the tests to cover my changes. - [X] I have updated the documentation accordingly. - [X] I have added a release note file using `reno` if this change needs to be documented in the release notes. --------- Co-authored-by: Helena Zhang <Helena.Zhang@ibm.com>
1 parent 8bda25d commit d4b9d14

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-3
lines changed

qiskit_experiments/framework/base_experiment.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(
5353
QiskitError: If qubits contains duplicates.
5454
"""
5555
# Experiment identification metadata
56-
self._type = experiment_type if experiment_type else type(self).__name__
56+
self.experiment_type = experiment_type
5757

5858
# Circuit parameters
5959
self._num_qubits = len(physical_qubits)
@@ -90,6 +90,14 @@ def experiment_type(self) -> str:
9090
"""Return experiment type."""
9191
return self._type
9292

93+
@experiment_type.setter
94+
def experiment_type(self, exp_type: str) -> None:
95+
"""Set the type for the experiment."""
96+
if exp_type is None:
97+
self._type = type(self).__name__
98+
else:
99+
self._type = exp_type
100+
93101
@property
94102
def physical_qubits(self) -> Tuple[int, ...]:
95103
"""Return the device qubits for the experiment."""

qiskit_experiments/framework/composite/batch_experiment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(
4949
backend: Optional[Backend] = None,
5050
flatten_results: bool = None,
5151
analysis: Optional[CompositeAnalysis] = None,
52+
experiment_type: Optional[str] = None,
5253
):
5354
"""Initialize a batch experiment.
5455
@@ -86,7 +87,12 @@ def __init__(
8687
logical_qubit += 1
8788
qubits = tuple(self._qubit_map.keys())
8889
super().__init__(
89-
experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results
90+
experiments,
91+
qubits,
92+
backend=backend,
93+
analysis=analysis,
94+
flatten_results=flatten_results,
95+
experiment_type=experiment_type,
9096
)
9197

9298
def circuits(self):

qiskit_experiments/framework/composite/parallel_experiment.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(
4949
backend: Optional[Backend] = None,
5050
flatten_results: bool = None,
5151
analysis: Optional[CompositeAnalysis] = None,
52+
experiment_type: Optional[str] = None,
5253
):
5354
"""Initialize the analysis object.
5455
@@ -79,7 +80,12 @@ def __init__(
7980
for exp in experiments:
8081
qubits += exp.physical_qubits
8182
super().__init__(
82-
experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results
83+
experiments,
84+
qubits,
85+
backend=backend,
86+
analysis=analysis,
87+
flatten_results=flatten_results,
88+
experiment_type=experiment_type,
8389
)
8490

8591
def circuits(self):
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
features:
3+
- |
4+
Added ``experiment_type`` as optional ``__init__`` kwarg in :class:`.BatchExperiment`
5+
and :class:`.ParallelExperiment`.
6+
- |
7+
:math:'experiment_type' can now be easily set and retrieved from the experiment
8+
object post-construction using the 'experiment_type' property and setter.

test/framework/test_composite.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,21 @@ def test_roundtrip_serializable(self):
151151

152152
self.assertRoundTripSerializable(exp)
153153

154+
def test_experiment_type(self):
155+
"""Test experiment_type setter."""
156+
157+
exp1 = FakeExperiment([0])
158+
159+
par_exp1 = ParallelExperiment([exp1], flatten_results=False)
160+
batch_exp1 = BatchExperiment([exp1], flatten_results=False)
161+
self.assertEqual(par_exp1.experiment_type, "ParallelExperiment")
162+
self.assertEqual(batch_exp1.experiment_type, "BatchExperiment")
163+
164+
par_exp2 = ParallelExperiment([exp1], flatten_results=False, experiment_type="yooo")
165+
batch_exp2 = BatchExperiment([exp1], flatten_results=False, experiment_type="blaaa")
166+
self.assertEqual(par_exp2.experiment_type, "yooo")
167+
self.assertEqual(batch_exp2.experiment_type, "blaaa")
168+
154169

155170
@ddt
156171
class TestCompositeExperimentData(QiskitExperimentsTestCase):

test/framework/test_framework.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,22 @@ def circuits(self):
340340
}
341341

342342
self.assertEqual(exp.job_info(backend=backend), job_info)
343+
344+
def test_experiment_type(self):
345+
"""Test the experiment_type setter for the experiment."""
346+
347+
class MyExp(BaseExperiment):
348+
"""Some arbitrary experiment"""
349+
350+
def __init__(self, *args, **kwargs):
351+
super().__init__(*args, **kwargs)
352+
353+
def circuits(self):
354+
pass
355+
356+
exp1 = MyExp(physical_qubits=[0], experiment_type="blaaa")
357+
self.assertEqual(exp1.experiment_type, "blaaa")
358+
exp2 = MyExp(physical_qubits=[0])
359+
self.assertEqual(exp2.experiment_type, "MyExp")
360+
exp2.experiment_type = "suieee"
361+
self.assertEqual(exp2.experiment_type, "suieee")

0 commit comments

Comments
 (0)