Skip to content

Commit

Permalink
Speed-up length property lookup for Pulse and PulseBlock.
Browse files Browse the repository at this point in the history
For Pulse, we make it one of the named properties in the namedtuple. For
PulseBlock, we compute it on construction, and update it whenever we tensor on
another Pulse.
  • Loading branch information
blakejohnson committed Jul 19, 2016
1 parent 6e99be0 commit cd4f4bd
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions QGL/PulseSequencer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from collections import namedtuple

class Pulse(namedtuple("Pulse", ["label", "channel", "amp", "phase", "frequency",
class Pulse(namedtuple("Pulse", ["label", "channel", "length", "amp", "phase", "frequency",
"frameChange", "shapeParams", "isTimeAmp",
"isZero", "ignoredStrParams"])):
__slots__ = ()
Expand All @@ -46,7 +46,7 @@ def __new__(cls, label, channel, shapeParams, amp=1.0, phase=0, frameChange=0, i
raise NameError("shapeParams must include {0}".format(param))
isTimeAmp = (shapeParams['shapeFun'] == PulseShapes.constant)
isZero = (amp == 0)
return super(cls, Pulse).__new__(cls, label, channel, amp, phase, frequency, frameChange, shapeParams, isTimeAmp, isZero, ignoredStrParams)
return super(cls, Pulse).__new__(cls, label, channel, shapeParams['length'], amp, phase, frequency, frameChange, shapeParams, isTimeAmp, isZero, ignoredStrParams)

def __str__(self):
kwvals = []
Expand All @@ -69,18 +69,6 @@ def __str__(self):
def _repr_pretty_(self, p, cycle):
p.text(str(self))

@property
def length(self):
"""Alias the shape parameter "length" """
return self.shapeParams["length"]

def __mul__(self, other):
""" Overload multiplication of Pulses as a "tensor" operator"""
if isinstance(other, Pulse):
return PulseBlock(self) * PulseBlock(other)
else:
return PulseBlock(self) * other

def hashshape(self):
return hash(frozenset(self.shapeParams.items()))

Expand All @@ -96,6 +84,7 @@ def __neg__(self):
-self.amp, self.phase, -self.frameChange)

def __mul__(self, other):
""" Overload multiplication of Pulses as a "tensor" operator"""
return self.promote() * other.promote()

def promote(self):
Expand Down Expand Up @@ -182,10 +171,10 @@ class PulseBlock(object):
'''

def __init__(self, *pulses):
#Set some default values
#How multiple channels are aligned.
self.alignment = 'left'
self.pulses = OrderedDict([(pulse.channel, pulse) for pulse in pulses])
# The maximum length for any channel on this block
self.length = max(p.length for p in self.pulses.values())
self.label = None

def __repr__(self):
Expand All @@ -203,7 +192,7 @@ def _repr_pretty_(self, p, cycle):
p.text(labelPart + "⊗ ".join([str(pulse)
for pulse in self.pulses.values()]))

#Overload the multiplication operator to combine pulse blocks
# Overload the multiplication operator to combine pulse blocks
def __mul__(self, rhs):
# make sure RHS is a PulseBlock
rhs = rhs.promote()
Expand All @@ -217,6 +206,7 @@ def __mul__(self, rhs):
"Attempted to multiply pulses acting on the same space")
else:
result.pulses[k] = v
result.length = max(self.length, rhs.length)
return result

def __eq__(self, other):
Expand All @@ -232,20 +222,14 @@ def __eq__(self, other):
def __ne__(self, other):
return not self == other

#PulseBlocks don't need to be promoted, so just return self
# PulseBlocks don't need to be promoted, so just return self
def promote(self):
return self

@property
def channel(self):
return self.pulses.keys()

#The maximum length for any channel on this block
@property
def length(self):
return max(p.length for p in self.pulses.values())


def align(pulseBlock, mode="center"):
# make sure we have a PulseBlock
pulseBlock = pulseBlock.promote()
Expand Down

0 comments on commit cd4f4bd

Please sign in to comment.