Skip to content

Commit

Permalink
Setting type for composite experiment (#1296)
Browse files Browse the repository at this point in the history
### 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>
  • Loading branch information
nayan2167 and coruscating authored Nov 9, 2023
1 parent 8bda25d commit d4b9d14
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
10 changes: 9 additions & 1 deletion qiskit_experiments/framework/base_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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."""
Expand Down
8 changes: 7 additions & 1 deletion 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 Down Expand Up @@ -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):
Expand Down
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 Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions test/framework/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
19 changes: 19 additions & 0 deletions test/framework/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit d4b9d14

Please sign in to comment.