Skip to content

Conversation

kevinsung
Copy link
Contributor

@kevinsung kevinsung commented Feb 17, 2022

Fixes #669.

Summary

Adds BaseExperiment._transpiled_circuits to enable customization of transpilation. This makes BaseExperiment._postprocess_transpiled_circuits obsolete so that is deprecated with a version 0.4.0 deadline.

Details and comments

@chriseclectic I didn't find any issues with composite experiments, but let me know if there are any specific tests I should add.

@kevinsung kevinsung force-pushed the transpile branch 3 times, most recently from bda4650 to 07e0246 Compare February 17, 2022 16:40
BaseExperiment._postprocess_transpiled_circuits
@kevinsung
Copy link
Contributor Author

CI failure appears to be unrelated:

 Warning, treated as error:
Cell printed to stderr:
/home/runner/work/qiskit-experiments/qiskit-experiments/qiskit_experiments/curve_analysis/guess.py:188: RuntimeWarning: invalid value encountered in log
  coeffs = np.polyfit(x, np.log(y), deg=1)
/home/runner/work/qiskit-experiments/qiskit-experiments/qiskit_experiments/curve_analysis/curve_analysis.py:848: UserWarning: All initial guesses and parameter boundaries failed to fit the data. Please provide better initial guesses or fit parameter boundaries.
  warnings.warn(

Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 left a comment

Choose a reason for hiding this comment

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

Code looks good to me but this is indeed conflicting with #667 @yaelbh could you please decide if merge this first or close this and make @kevinsung co-author of your PR.

"""Additional post-processing of transpiled circuits before running on backend"""
pass
def _transpile(
self, circuits: List[QuantumCircuit], backend: Backend, **transpile_options
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably backend is not necessary since you can access to it via self.backend (also this will bypass _set_backend hook). Transpile options and circuit too. Likely you want to make it static method -- is there any plan to hack this method? Something like injecting circuit which is not generated by this experiment instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is necessary because self.backend could be None, in which case the backend is passed to BaseExperiment.run and forwarded to this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should not be a static method because transpilation can depend on the experiment instance.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not correct because existence of backend is guaranteed by these lines. Seems like this allows a hack that injecting backend which is not associated with the instance. Currently you are using self to only access deprecated method.
https://github.com/Qiskit/qiskit-experiments/blob/317b964b8cbde0febcda8696fd4bd5cce3490e0a/qiskit_experiments/framework/base_experiment.py#L251-L259

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nothing needs to be passed as kwargs to this function except the circuits (and that one is debatable too, the only reason to pass them is to allow overloading with some pre-transpilation logic). Options and backend should all be taken from the self instance. As Naoki points out backend can never be None when this function is called inside run.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps you are assuming the situation the user directly call this method of the instance initialized without backend to check the physical circuit? Then I agree it's worth having backend here. But I think circuit and transpile options can come from self.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still, the backend needs to be forwarded because it uses experiment.backend which could be different from self.backend.

Copy link
Contributor Author

@kevinsung kevinsung Feb 17, 2022

Choose a reason for hiding this comment

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

Perhaps you are assuming the situation the user directly call this method of the instance initialized without backend to check the physical circuit? Then I agree it's worth having backend here. But I think circuit and transpile options can come from self.

Yes, exactly. If the user calls run with a backend specified, then using self.backend would be incorrect. I actually this this a bit confusing, and that only one way of passing the backend should be supported. That would also eliminate the need for the complicated backend selection code. But that is a separate issue.

@yaelbh
Copy link
Collaborator

yaelbh commented Feb 17, 2022

I suggest that the PR that's ready first will be merged first, and the other one will resolve the conflicts

"""Additional post-processing of transpiled circuits before running on backend"""
pass
def _transpile(
self, circuits: List[QuantumCircuit], backend: Backend, **transpile_options
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nothing needs to be passed as kwargs to this function except the circuits (and that one is debatable too, the only reason to pass them is to allow overloading with some pre-transpilation logic). Options and backend should all be taken from the self instance. As Naoki points out backend can never be None when this function is called inside run.

@kevinsung
Copy link
Contributor Author

kevinsung commented Feb 17, 2022

@chriseclectic I have eliminated the transpilation options keyword arguments. As I explained above, backend is needed for the case the user called run with a backend specified. I think circuits should remain as a keyword argument in order to have a clean separation of functionality with BaseExperiment.circuits.

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.

Thanks @kevinsung 😃.
I intentionally mark "Comment" and not "Request changes", to make the approvals of Chris and Naoki sufficient for merging the PR.

@yaelbh
Copy link
Collaborator

yaelbh commented Feb 20, 2022

Please don't proceed with approving and merging this PR before we resolve #667 (comment).

@kevinsung
Copy link
Contributor Author

I've updated the code to align with #667 by changing _transpile to _transpiled_circuits, which does not take the circuits as input.

@kevinsung kevinsung changed the title Define BaseExperiment._transpile and deprecate BaseExperiment._postprocess_transpiled_circuits Define BaseExperiment._transpiled_circuits and deprecate BaseExperiment._postprocess_transpiled_circuits Feb 21, 2022
@kevinsung
Copy link
Contributor Author

CI failure appears unrelated again, see #682

Copy link
Collaborator

@chriseclectic chriseclectic left a comment

Choose a reason for hiding this comment

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

This looks very close. I would suggest merging #667 first, then rebasing this again after.

@kevinsung
Copy link
Contributor Author

@chriseclectic I've addressed your comments. It might make sense to merge this before #667 because #667 actually includes this change and does more on top.

Copy link
Collaborator

@chriseclectic chriseclectic left a comment

Choose a reason for hiding this comment

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

Minor formatting

Copy link
Collaborator

@chriseclectic chriseclectic left a comment

Choose a reason for hiding this comment

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

@kevinsung sounds good, we can merge this one first

@chriseclectic chriseclectic merged commit d788c2a into qiskit-community:main Feb 23, 2022
@kevinsung kevinsung deleted the transpile branch February 23, 2022 18:31
paco-ri pushed a commit to paco-ri/qiskit-experiments that referenced this pull request Jul 11, 2022
Add BaseExperiment._transpile_circuits method and deprecate
BaseExperiment._postprocess_transpiled_circuits

Co-authored-by: Christopher J. Wood <cjwood@us.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.

BaseExperiment._postprocess_transpiled_circuits should be functional, not in-place
4 participants