Skip to content

Commit

Permalink
fix: Move sequence compilation to its subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
alecandido committed Dec 19, 2024
1 parent ec560fe commit 7eaf2f0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 55 deletions.
52 changes: 2 additions & 50 deletions src/qibolab/_core/instruments/qblox/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
from qibolab._core.execution_parameters import ExecutionParameters
from qibolab._core.identifier import ChannelId, Result
from qibolab._core.instruments.abstract import Controller
from qibolab._core.pulses import PulseId, PulseLike, Readout
from qibolab._core.pulses import PulseId
from qibolab._core.sequence import PulseSequence
from qibolab._core.sweeper import ParallelSweepers

from . import config
from .config import PortAddress, SeqeuencerMap, SlotId
from .log import Logger
from .sequence import Sequence, compile

# from qcodes.instrument import find_or_create_instrument

Expand Down Expand Up @@ -122,52 +123,3 @@ def _execute(self, sequencers: SeqeuencerMap) -> dict[PulseId, Result]:
module.start_sequencer()

return {}


def _split_channels(sequence: PulseSequence) -> dict[ChannelId, PulseSequence]:
def unwrap(pulse: PulseLike, output: bool) -> PulseLike:
return (
pulse
if not isinstance(pulse, Readout)
else pulse.probe if output else pulse.acquisition
)

def unwrap_seq(seq: PulseSequence, output: bool) -> PulseSequence:
return PulseSequence((ch, unwrap(p, output)) for ch, p in seq)

def ch_pulses(channel: ChannelId) -> PulseSequence:
return PulseSequence((ch, pulse) for ch, pulse in sequence if ch == channel)

def probe(channel: ChannelId) -> ChannelId:
return channel.split("/")[0] + "/probe"

def split(channel: ChannelId) -> dict[ChannelId, PulseSequence]:
seq = ch_pulses(channel)
readouts = any(isinstance(p, Readout) for _, p in seq)
assert not readouts or probe(channel) not in sequence.channels
return (
{channel: seq}
if not readouts
else {
channel: unwrap_seq(seq, output=False),
probe(channel): unwrap_seq(seq, output=True),
}
)

return {
ch: seq for channel in sequence.channels for ch, seq in split(channel).items()
}


def _prepare(
sequence: PulseSequence,
sweepers: list[ParallelSweepers],
options: ExecutionParameters,
sampling_rate: float,
) -> dict[ChannelId, Sequence]:
return {
ch: Sequence.from_pulses(seq, sweepers, options, sampling_rate)
for ch, seq in _split_channels(sequence).items()
}


4 changes: 2 additions & 2 deletions src/qibolab/_core/instruments/qblox/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Optional

from qblox_instruments.qcodes_drivers.sequencer import Sequencer
from qblox_instruments.qcodes_drivers.module import Module
from qblox_instruments.qcodes_drivers.sequencer import Sequencer

from qibolab._core.serialize import Model
from qibolab._core.identifier import ChannelId
from qibolab._core.serialize import Model

from .sequence import Sequence

Expand Down
2 changes: 1 addition & 1 deletion src/qibolab/_core/instruments/qblox/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from qibolab._core.components.configs import Configs, LogConfig
from qibolab._core.identifier import ChannelId

from .sequence import Sequence
from .config import SeqeuencerMap
from .sequence import Sequence

__all__ = []

Expand Down
51 changes: 50 additions & 1 deletion src/qibolab/_core/instruments/qblox/sequence/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pydantic import PlainSerializer, PlainValidator

from qibolab._core.execution_parameters import ExecutionParameters
from qibolab._core.identifier import ChannelId
from qibolab._core.pulses import PulseLike, Readout
from qibolab._core.sequence import PulseSequence
from qibolab._core.serialize import Model
from qibolab._core.sweeper import ParallelSweepers
Expand All @@ -14,7 +16,7 @@
from .program import program
from .waveforms import Waveform, Waveforms, waveform_indices, waveforms

__all__ = ["Sequence"]
__all__ = ["Sequence", "compile"]


Weight = Waveform
Expand Down Expand Up @@ -55,3 +57,50 @@ def from_pulses(
sampling_rate,
),
)


def _split_channels(sequence: PulseSequence) -> dict[ChannelId, PulseSequence]:
def unwrap(pulse: PulseLike, output: bool) -> PulseLike:
return (
pulse
if not isinstance(pulse, Readout)
else pulse.probe if output else pulse.acquisition
)

def unwrap_seq(seq: PulseSequence, output: bool) -> PulseSequence:
return PulseSequence((ch, unwrap(p, output)) for ch, p in seq)

def ch_pulses(channel: ChannelId) -> PulseSequence:
return PulseSequence((ch, pulse) for ch, pulse in sequence if ch == channel)

def probe(channel: ChannelId) -> ChannelId:
return channel.split("/")[0] + "/probe"

def split(channel: ChannelId) -> dict[ChannelId, PulseSequence]:
seq = ch_pulses(channel)
readouts = any(isinstance(p, Readout) for _, p in seq)
assert not readouts or probe(channel) not in sequence.channels
return (
{channel: seq}
if not readouts
else {
channel: unwrap_seq(seq, output=False),
probe(channel): unwrap_seq(seq, output=True),
}
)

return {
ch: seq for channel in sequence.channels for ch, seq in split(channel).items()
}


def compile(
sequence: PulseSequence,
sweepers: list[ParallelSweepers],
options: ExecutionParameters,
sampling_rate: float,
) -> dict[ChannelId, Sequence]:
return {
ch: Sequence.from_pulses(seq, sweepers, options, sampling_rate)
for ch, seq in _split_channels(sequence).items()
}
1 change: 0 additions & 1 deletion src/qibolab/_core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
Field,
PlainSerializer,
TypeAdapter,
ValidationError,
)
from pydantic_core import core_schema
from typing_extensions import NotRequired, TypedDict
Expand Down

0 comments on commit 7eaf2f0

Please sign in to comment.