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

Add scheduling to ParallelExperiment #1100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions qiskit_experiments/framework/composite/parallel_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Collaborator

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.

Copy link
Collaborator Author

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)

Copy link
Collaborator

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.


# 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))
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 Mar 23, 2023

Choose a reason for hiding this comment

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

I think we can remove this because adding delay after measurement, or adding any instruction to qubit without measurement doesn't make any sense. Then name could be something like delay_sub_circuit_execution: bool, which can avoid confusion with built-in scheduling logic in preset transpiler.

(Edit) We can add some noisy experiment to investigate spectator interaction, but I don't think adding delay impacts the result.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@nkanazawa1989 nkanazawa1989 Mar 24, 2023

Choose a reason for hiding this comment

The 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)
Expand Down
7 changes: 7 additions & 0 deletions releasenotes/notes/parallel-scheduling-03e2eca55f8d8ff2.yaml
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.
4 changes: 2 additions & 2 deletions test/library/characterization/test_t1.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,14 @@ def test_t1_parallel_exp_transpile(self):
self.assertEqual(circ.num_qubits, 2)
op_counts = circ.count_ops()
self.assertEqual(op_counts.get("rx"), 2)
self.assertEqual(op_counts.get("delay"), 2)
self.assertEqual(op_counts.get("delay"), 3)

tcircs = parexp._transpiled_circuits()
for circ in tcircs:
self.assertEqual(circ.num_qubits, num_qubits)
op_counts = circ.count_ops()
self.assertEqual(op_counts.get("rx"), 2)
self.assertEqual(op_counts.get("delay"), 2)
self.assertEqual(op_counts.get("delay"), 3)

def test_experiment_config(self):
"""Test converting to and from config works"""
Expand Down