-
Notifications
You must be signed in to change notification settings - Fork 131
More flexible execution #358
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
Closed
nkanazawa1989
wants to merge
1
commit into
qiskit-community:main
from
nkanazawa1989:feature/experiment_run_hooks
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Event hooks to run experiment. | ||
| """ | ||
|
|
||
| from .runner import ExperimentRunner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Execute events. | ||
| """ | ||
|
|
||
| from qiskit import assemble | ||
| from qiskit.providers.basebackend import BaseBackend as LegacyBackend | ||
|
|
||
|
|
||
| def backend_run(experiment, backend, circuits, run_options, **kwargs): | ||
|
|
||
| if isinstance(backend, LegacyBackend): | ||
| qobj = assemble(circuits, backend=backend, **run_options) | ||
| job = backend.run(qobj) | ||
| else: | ||
| job = backend.run(circuits, **run_options) | ||
|
|
||
| return {"job": job} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Post processing events. | ||
| """ | ||
|
|
||
| import copy | ||
|
|
||
|
|
||
| def set_analysis(experiment, analysis, job, experiment_data, **kwargs): | ||
|
|
||
| if analysis and experiment.__analysis_class__ is not None: | ||
| run_analysis_callback = experiment.run_analysis | ||
| else: | ||
| run_analysis_callback = None | ||
|
|
||
| experiment_data.add_data(job, post_processing_callback=run_analysis_callback) | ||
|
|
||
|
|
||
| def add_job_metadata(experiment, experiment_data, job, run_options, **kwargs): | ||
| """Add runtime job metadata to ExperimentData. | ||
|
|
||
| Args: | ||
| experiment: Experiment object. | ||
| experiment_data: The experiment data container. | ||
| job: The job object. | ||
| run_options: Backend run options for the job. | ||
| """ | ||
| metadata = { | ||
| "job_id": job.job_id(), | ||
| "experiment_options": copy.copy(experiment.experiment_options.__dict__), | ||
| "transpile_options": copy.copy(experiment.transpile_options.__dict__), | ||
| "analysis_options": copy.copy(experiment.analysis_options.__dict__), | ||
| "run_options": copy.copy(run_options) | ||
| } | ||
|
|
||
| experiment_data._metadata["job_metadata"].append(metadata) |
44 changes: 44 additions & 0 deletions
44
qiskit_experiments/framework/events/events_preprocessing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Pre processing events. | ||
| """ | ||
| import copy | ||
|
|
||
| from qiskit_experiments.framework import ExperimentData | ||
| from qiskit.exceptions import QiskitError | ||
|
|
||
|
|
||
| def initialize_experiment_data(experiment, backend, expeirment_data, **kwargs): | ||
| if expeirment_data is not None: | ||
| init_data = experiment.__experiment_data__(experiment=experiment, backend=backend) | ||
| else: | ||
| # Validate experiment is compatible with existing data | ||
| if not isinstance(expeirment_data, ExperimentData): | ||
| raise QiskitError("Input `experiment_data` is not a valid ExperimentData.") | ||
| if expeirment_data.experiment_type != experiment.experiment_type: | ||
| raise QiskitError("Existing ExperimentData contains data from a different experiment.") | ||
| if expeirment_data.metadata.get("physical_qubits") != list(experiment.physical_qubits): | ||
| raise QiskitError( | ||
| "Existing ExperimentData contains data for a different set of physical qubits." | ||
| ) | ||
| init_data = expeirment_data._copy_metadata() | ||
|
|
||
| return {"expeirment_data": init_data} | ||
|
|
||
|
|
||
| def update_run_options(experiment, run_options, **kwargs): | ||
| options = copy.copy(experiment.run_options).__dict__ | ||
| options.update(**run_options) | ||
|
|
||
| return {"run_options": options} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2021. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """ | ||
| Transpile events. | ||
| """ | ||
|
|
||
| import copy | ||
|
|
||
| from qiskit import transpile | ||
| from qiskit.test.mock import FakeBackend | ||
|
|
||
| from qiskit_experiments.framework import ParallelExperiment | ||
|
|
||
|
|
||
| def count_transpiled_ops(experiment, circuits, **kwargs): | ||
|
|
||
| def get_metadata(circuit): | ||
| if circuit.metadata["experiment_type"] == experiment.experiment_type: | ||
| return circuit.metadata | ||
| if circuit.metadata["experiment_type"] == ParallelExperiment.__name__: | ||
| for meta in circuit.metadata["composite_metadata"]: | ||
| if meta["physical_qubits"] == experiment.physical_qubits: | ||
| return meta | ||
| return dict() | ||
|
|
||
| for circ in circuits: | ||
| meta = get_metadata(circ) | ||
| if meta: | ||
| qubits = range(len(circ.qubits)) | ||
| c_count_ops = {} | ||
| for instr, qargs, _ in circ: | ||
| instr_qubits = [] | ||
| skip_instr = False | ||
| for qubit in qargs: | ||
| qubit_index = circ.qubits.index(qubit) | ||
| if qubit_index not in qubits: | ||
| skip_instr = True | ||
| instr_qubits.append(qubit_index) | ||
| if not skip_instr: | ||
| instr_qubits = tuple(instr_qubits) | ||
| c_count_ops[(instr_qubits, instr.name)] = ( | ||
| c_count_ops.get((instr_qubits, instr.name), 0) + 1 | ||
| ) | ||
| circuit_length = meta["xval"] | ||
| count_ops = [(key, (value, circuit_length)) for key, value in c_count_ops.items()] | ||
| meta.update({"count_ops": count_ops}) | ||
|
|
||
|
|
||
| def set_scheduling_contraints(expeirment, backend, **kwargs): | ||
| if not backend.configuration().simulator and not isinstance(backend, FakeBackend): | ||
| timing_constraints = getattr( | ||
| expeirment.transpile_options.__dict__, | ||
| "timing_constraints", | ||
| {} | ||
| ) | ||
| timing_constraints["acquire_alignment"] = getattr( | ||
| timing_constraints, "acquire_alignment", 16 | ||
| ) | ||
| scheduling_method = getattr( | ||
| expeirment.transpile_options.__dict__, "scheduling_method", "alap" | ||
| ) | ||
| expeirment.set_transpile_options( | ||
| timing_constraints=timing_constraints, scheduling_method=scheduling_method | ||
| ) | ||
|
|
||
|
|
||
| def transpile_circuits(experiment, backend, **kwargs): | ||
| options = copy.copy(experiment.transpile_options.__dict__) | ||
| options["initial_layout"] = list(experiment.physical_qubits) | ||
| circuits = transpile(experiment.circuits(backend), backend, **options) | ||
|
|
||
| return {"circuits": circuits} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this 16 hard-coded here? This should be configurable, e.g. in an option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agreed. I just copied it from the original code.