From f21245f834d80fc986ea4f611de70f4513255124 Mon Sep 17 00:00:00 2001 From: lcauser-oqc Date: Tue, 26 Nov 2024 13:42:28 +0000 Subject: [PATCH] removed redundancy --- src/qat/ir/instructions.py | 46 +++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/src/qat/ir/instructions.py b/src/qat/ir/instructions.py index bcadb88b9..3ab487526 100644 --- a/src/qat/ir/instructions.py +++ b/src/qat/ir/instructions.py @@ -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 @@ -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 @@ -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__(