-
Notifications
You must be signed in to change notification settings - Fork 127
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
Add scheduling to ParallelExperiment #1100
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
import numpy as np | ||
|
||
from qiskit import QuantumCircuit, ClassicalRegister | ||
from qiskit.circuit import Clbit | ||
from qiskit.circuit import Clbit, Delay | ||
from qiskit.providers.backend import Backend | ||
from qiskit_experiments.exceptions import QiskitError | ||
from .composite_experiment import CompositeExperiment, BaseExperiment | ||
|
@@ -92,12 +92,39 @@ def _combined_circuits(self, device_layout: bool) -> List[QuantumCircuit]: | |
else: | ||
num_qubits = 1 + max(self.physical_qubits) | ||
|
||
joint_circuits = [] | ||
sub_qubits = 0 | ||
# Transpile component circuits | ||
transpiled_circuits = [] | ||
|
||
# Max duration for all components that will be combined into a single circuit | ||
max_durations = {} | ||
duration_unit = None | ||
scheduling_method = getattr(self.transpile_options, "scheduling_method", None) | ||
|
||
# Find max durations of subcircuits | ||
for exp_idx, sub_exp in enumerate(self._experiments): | ||
# Generate transpiled subcircuits | ||
sub_circuits = sub_exp._transpiled_circuits() | ||
|
||
transpiled_circuits.append(sub_circuits) | ||
if scheduling_method is not None: | ||
for circ_idx, sub_circ in enumerate(sub_circuits): | ||
if sub_circ.duration is not None: | ||
if duration_unit is None: | ||
duration_unit = sub_circ.unit | ||
if circ_idx not in max_durations: | ||
max_durations[circ_idx] = 0 | ||
max_durations[circ_idx] = max(sub_circ.duration, max_durations[circ_idx]) | ||
nkanazawa1989 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if duration_unit != sub_circ.unit: | ||
raise QiskitError( | ||
"Scheduled component experiments are scheduled with" | ||
" different time units." | ||
) | ||
|
||
# Combine circuits | ||
joint_circuits = [] | ||
sub_qubits = 0 | ||
for exp_idx, (sub_circuits, sub_exp) in enumerate( | ||
zip(transpiled_circuits, self._experiments) | ||
): | ||
# Qubit remapping for non-transpiled circuits | ||
if not device_layout: | ||
qubits = list(range(sub_qubits, sub_qubits + sub_exp.num_qubits)) | ||
|
@@ -136,6 +163,18 @@ def _combined_circuits(self, device_layout: bool) -> List[QuantumCircuit]: | |
# Apply transpiled subcircuit | ||
# Note that this assumes the circuit was not expanded to use | ||
# any qubits outside the specified physical qubits | ||
circ_duration = max_durations.get(circ_idx) | ||
pad_time = None | ||
if scheduling_method and sub_circ.duration and circ_duration: | ||
pad_time = abs(circ_duration - sub_circ.duration) | ||
nkanazawa1989 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# If scheduling method is alap prepend shorter sub-circuits with delays | ||
if scheduling_method == "alap" and pad_time: | ||
for i in sub_exp.physical_qubits: | ||
circuit._append( | ||
Delay(pad_time, unit=duration_unit), [circuit.qubits[i]], [] | ||
) | ||
|
||
for inst, qargs, cargs in sub_circ.data: | ||
mapped_cargs = [sub_cargs[sub_circ.find_bit(i).index] for i in cargs] | ||
try: | ||
|
@@ -158,6 +197,18 @@ def _combined_circuits(self, device_layout: bool) -> List[QuantumCircuit]: | |
) from ex | ||
circuit._append(inst, mapped_qargs, mapped_cargs) | ||
|
||
# If scheduling method is alap append shorter sub-circuits with delays | ||
if scheduling_method == "asap" and pad_time: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can remove this because adding delay after measurement, or (Edit) We can add some noisy experiment to investigate spectator interaction, but I don't think adding delay impacts the result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was exactly @ajavadia issue though, since backends defaults to alap scheduling if you want to do asap scheduling you need to do this padding or it will change the alignment when it reschedulings things to alap during execution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC this is not the case. Backend has meas_map constraints and they align measurements according to the map. I'm not sure if they can trigger measurement at different time. |
||
for i in sub_exp.physical_qubits: | ||
circuit._append( | ||
Delay(pad_time, unit=duration_unit), [circuit.qubits[i]], [] | ||
) | ||
|
||
# Update duration of circuit | ||
if scheduling_method and circ_duration: | ||
circuit.duration = circ_duration | ||
circuit.unit = duration_unit | ||
|
||
# Add subcircuit metadata | ||
circuit.metadata["composite_index"].append(exp_idx) | ||
circuit.metadata["composite_metadata"].append(sub_circ.metadata) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
fixes: | ||
- | | ||
Fixes :class:`.ParallelExperiment` handling of `scheduling_method` | ||
transpile option for `"asap"` and `"alap"` scheduling to correctly | ||
pad the component experiment subcircuits so that the combined circuit | ||
has a fixed duration on the experiments physical qubits. |
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.
Will it be transpile option? I think we should keep current behavior of delegating this scheduling to sub experiments, and turn this into experiment option of parallel experiment. We should give it another name because the logic you implemented is too much simplified.
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.
If you set it as a transpile option it will also be set as transpile option for component experiments. In the case where its individual set for component experiments, but not the main parallel one, they could be different values (eg some asap some alap) so there is no way to know how to align the parallel blocks unless there is a method in the main experiment (or you check they are all the same or some other heuristic)
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.
We can still schedule individual experiment with different policy. Parallel one can prepend or append delay without modulating the individual schedule (the parallel scheduler considers the individual circuit as a block). I think this is another layer of scheduling, and thus we should avoid having the same option. Otherwise user may easily misunderstand the behavior of parallel block scheduling.