Skip to content

Commit

Permalink
Add untested linspace implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
terrorfisch committed Jul 1, 2024
1 parent 2fd223e commit cd67b0d
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions qupulse/program/linspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class LinSpaceNode:
def dependencies(self) -> Mapping[int, set]:
raise NotImplementedError

def reversed(self, level: int):
raise NotImplementedError


@dataclass
class LinSpaceHold(LinSpaceNode):
Expand All @@ -60,13 +63,34 @@ def dependencies(self) -> Mapping[int, set]:
for idx, factors in enumerate(self.factors)
if factors}

def reversed(self, level: int):
factors = []
for ch_factors in self.factors:
if ch_factors is None or len(ch_factors) <= level:
factors.append(ch_factors)
else:
reversed_factors = ch_factors[:level] + tuple(-f for f in ch_factors[level:])
factors.append(reversed_factors)

if self.duration_factors is not None and len(self.duration_factors) <= level:
duration_factors = self.duration_factors
else:
duration_factors = self.duration_factors[:level] + tuple(-f for f in self.duration_factors[level:])
return LinSpaceHold(self.bases, factors, duration_base=self.duration_base, duration_factors=duration_factors)


@dataclass
class LinSpaceArbitraryWaveform(LinSpaceNode):
"""This is just a wrapper to pipe arbitrary waveforms through the system."""
waveform: Waveform
channels: Tuple[ChannelID, ...]

def reversed(self, level: int):
return LinSpaceArbitraryWaveform(
waveform=self.waveform.reversed(),
channels=self.channels,
)


@dataclass
class LinSpaceRepeat(LinSpaceNode):
Expand All @@ -81,6 +105,9 @@ def dependencies(self):
dependencies.setdefault(idx, set()).update(deps)
return dependencies

def reversed(self, level: int):
return LinSpaceRepeat(tuple(node.reversed(level) for node in reversed(self.body)), self.count)


@dataclass
class LinSpaceIter(LinSpaceNode):
Expand All @@ -100,6 +127,9 @@ def dependencies(self):
dependencies.setdefault(idx, set()).update(shortened)
return dependencies

def reversed(self, level: int):
return LinSpaceIter(tuple(node.reversed() for node in reversed(self.body)), self.length)


class LinSpaceBuilder(ProgramBuilder):
"""This program builder supports efficient translation of pulse templates that use symbolic linearly
Expand Down Expand Up @@ -214,6 +244,13 @@ def with_iteration(self, index_name: str, rng: range,
if cmds:
self._stack[-1].append(LinSpaceIter(body=tuple(cmds), length=len(rng)))

def time_reversed(self) -> ContextManager['LinSpaceBuilder']:
self._stack.append([])
yield self
inner = self._stack.pop()
level = len(self._ranges)
self._stack[-1].extend(node.reversed(level) for node in reversed(inner))

def to_program(self) -> Optional[Sequence[LinSpaceNode]]:
if self._root():
return self._root()
Expand Down

0 comments on commit cd67b0d

Please sign in to comment.