Skip to content

Commit

Permalink
removed redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
lcauser-oqc committed Nov 26, 2024
1 parent 28dbab7 commit f21245f
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/qat/ir/instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,6 @@ def __repr__(self):
return f"delay {str(self.time)}"


def pulse_channel_to_strs(targets):
targets = [targets] if not isinstance(targets, list) else targets
unique_targets = set()
for target in (
chan
for val in targets
for chan in (val.pulse_channels.values() if isinstance(val, Qubit) else [val])
):
if isinstance(target, str):
unique_targets.add(target)
elif isinstance(target, PulseChannel):
unique_targets.add(target.full_id())
else:
raise ValueError(
f"Attempted to add a non PulseChannel ({target}) to the instruction."
)
return set(sorted(unique_targets))


class GroupInstruction(QuantumInstruction):
"""
Some instructions are just a collection of multiple targets with a label
Expand All @@ -466,14 +447,29 @@ def __init__(

@field_validator("quantum_targets", mode="before")
@classmethod
def _pulse_channel_to_strs(cls, targets):
return pulse_channel_to_strs(targets)
def _pulse_channels_to_strs(cls, targets):
targets = [targets] if not isinstance(targets, list) else targets
unique_targets = set()
for target in (
chan
for val in targets
for chan in (val.pulse_channels.values() if isinstance(val, Qubit) else [val])
):
if isinstance(target, str):
unique_targets.add(target)
elif isinstance(target, PulseChannel):
unique_targets.add(target.full_id())
else:
raise ValueError(
f"Attempted to add a non PulseChannel ({target}) to the instruction."
)
return set(sorted(unique_targets))

def add_channels(
self,
sync_channels: Union[Qubit, PulseChannel, List[Union[Qubit, PulseChannel]]],
):
new_targets = pulse_channel_to_strs(sync_channels)
new_targets = self._pulse_channels_to_strs(sync_channels)
self.quantum_targets.update(new_targets)
return self

Expand Down Expand Up @@ -554,13 +550,13 @@ def __init__(
# TODO: should we be storing properties of the pulse channel in the instruction?
# It is only used for calcualting the duration (which was previously done externally).
if not sample_time:
chan = quantum_targets
if isinstance(chan, list):
chan = chan[0]
if not isinstance(chan, PulseChannel):
raise (
ValueError("Sample time cannot be determined without a pulse channel.")
)
chan = quantum_targets
if isinstance(chan, list):
chan = chan[0]
sample_time = chan.sample_time

super().__init__(
Expand Down

0 comments on commit f21245f

Please # to comment.