Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting type for composite experiment #1296

Conversation

nayan2167
Copy link
Contributor

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)

  • I have read the contributing guide CONTRIBUTING.md.
  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have added a release note file using reno if this change needs to be documented in the release notes.

@yaelbh yaelbh self-requested a review October 31, 2023 18:05
Copy link
Collaborator

@yaelbh yaelbh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for working on this 😄 .

@@ -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__
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will happen in the parent's constructor, what you have to do is to pass experiment_type to the parent's constructor.

By the way, in BaseExperiment you can modify this line to self.experiment_type = experiment_type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol I should have read docs first, fixed it, removed proparty and setter from both BaseExperiment and ParallelExperiment added init arg experiment_type in both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line.
Then, in BaseExperiment, replace this line with self.experiment_type = experiment_type.

@@ -89,6 +93,19 @@ def __init__(
experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results
)

@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These property and setter already exist in BaseExperiment, so there is no reason to copy them here.

Note that all the comments for the BatchExperiment apply also to the ParallelExperiment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "fixed"? Did you commit the fix? Because the property and setter are still here.

---
features:
- |
Added ``experiment_type`` as optional ``__init__`` kwarg in BatchExperiment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two separate things in this PR: the setter and the args in init. You can separate them in the release notes.

and ParallelExperiment the 'experiment_type' can now be easily set and
retrieved from the experiment object post-construction using the 'experiment_type'
property and setter.
:math":`BatchExperiment.experiment_type` as property and setter.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change after fixing batch_experiment.py.

- |
:math:`BaseExperiment.experiment_type` as setter.
- |
Refer issue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coruscating Do we usually refer to issues in the release notes?

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once you remove the setters from these classes, you can also remove the test here. However do test the the init arg (such a test is currently missing).

test/framework/test_framework.py Show resolved Hide resolved
def circuits(self):
pass

exp1 = MyExp(physical_qubits=[0], experiment_type=exp_type1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing exp_type to the constructor, pass some arbitrary string (like blaaa) and verify that the setter overrides it.


exp1 = MyExp(physical_qubits=[0], experiment_type=exp_type1)
if exp_type1 is None:
exp_type1 = type(exp1).__name__
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what you're trying to test here. Note that you're not testing the case where the setters gets None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this?

    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])
        exp2.experiment_type = "suieee"
        self.assertEqual(exp2.experiment_type, "suieee")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and between the third and fourth line add:

self.assertEqual(exp2.experiment_type, "MyExp")

@@ -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__
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line.
Then, in BaseExperiment, replace this line with self.experiment_type = experiment_type.

@@ -89,6 +93,19 @@ def __init__(
experiments, qubits, backend=backend, analysis=analysis, flatten_results=flatten_results
)

@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by "fixed"? Did you commit the fix? Because the property and setter are still here.

@nayan2167 nayan2167 force-pushed the Setting-type-for-composite-experiment branch from ce63ed3 to ec9686f Compare November 1, 2023 18:57
@nayan2167
Copy link
Contributor Author

image

image

I have got this same output for both cases

ParallelExperiment
--------------------
suiee
--------------------
BatchExperiment
--------------------
blaa
--------------------

---
features:
- |
Added ``experiment_type`` as optional ``__init__`` kwarg in BatchExperiment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mark BatchExperiment as a class

Added ``experiment_type`` as optional ``__init__`` kwarg in BatchExperiment
and ParallelExperiment.
- |
:math:`BaseExperiment.experiment_type` as setter,'experiment_type' can now
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:math:`BaseExperiment.experiment_type` as setter,'experiment_type' can now
'experiment_type' can now

@yaelbh
Copy link
Collaborator

yaelbh commented Nov 5, 2023

The code looks fine. I don't understand why the docs don't build. I'll try to rerun.

@coruscating
Copy link
Collaborator

CI is fixed now.

@yaelbh yaelbh added this pull request to the merge queue Nov 7, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Nov 7, 2023
@coruscating coruscating added this pull request to the merge queue Nov 9, 2023
Merged via the queue into qiskit-community:main with commit d4b9d14 Nov 9, 2023
10 checks passed
@nayan2167 nayan2167 deleted the Setting-type-for-composite-experiment branch November 21, 2023 17:10
nkanazawa1989 pushed a commit to nkanazawa1989/qiskit-experiments that referenced this pull request Jan 17, 2024
### Summary

This PR solves issue qiskit-community#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants